diff --git a/include/History.h b/include/History.h index e1fbc81a47..efe575072f 100644 --- a/include/History.h +++ b/include/History.h @@ -76,7 +76,8 @@ public: DISK_SNAPSHOT_REVERT_ACTION = 41, // "one.vm.disksnapshotrevert" RECOVER_ACTION = 42, // "one.vm.recover" RETRY_ACTION = 43, // "one.vm.recover" - MONITOR_ACTION = 44 // internal, monitoring process + MONITOR_ACTION = 44, // internal, monitoring process + DISK_SNAPSHOT_RENAME_ACTION = 45 // "one.vm.disksnapshotrename" }; static string action_to_str(VMAction action); diff --git a/include/RequestManagerVirtualMachine.h b/include/RequestManagerVirtualMachine.h index 48f17183fc..aa9dff5557 100644 --- a/include/RequestManagerVirtualMachine.h +++ b/include/RequestManagerVirtualMachine.h @@ -521,6 +521,28 @@ private: /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +class VirtualMachineDiskSnapshotRename: public RequestManagerVirtualMachine +{ +public: + VirtualMachineDiskSnapshotRename(): + RequestManagerVirtualMachine("one.vm.disksnapshotrename", + "Rename a disk snapshot", + "A:siiis"){ + Nebula& nd = Nebula::instance(); + + //All VM disk snapshot operations are set to the same auth value + auth_op = nd.get_vm_auth_op(History::DISK_SNAPSHOT_RENAME_ACTION); + }; + + ~VirtualMachineDiskSnapshotRename(){}; + + void request_execute(xmlrpc_c::paramList const& _paramList, + RequestAttributes& att); +}; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + class VirtualMachineUpdateConf: public RequestManagerVirtualMachine { public: diff --git a/include/Snapshots.h b/include/Snapshots.h index a17339bbc2..6166a57470 100644 --- a/include/Snapshots.h +++ b/include/Snapshots.h @@ -101,6 +101,12 @@ public: */ int active_snapshot(int id); + /** + * Rename the given snapshot by the given name + */ + + int rename_snapshot(int id, const string& name, string& str_error); + /** * Clear all the snapshots in the list */ diff --git a/include/VirtualMachine.h b/include/VirtualMachine.h index c99f0e3679..7385fac328 100644 --- a/include/VirtualMachine.h +++ b/include/VirtualMachine.h @@ -1481,6 +1481,19 @@ public: disks.delete_snapshot(disk_id, snap_id, ds_quotas, vm_quotas, io, vo); } + /** + * Renames the snap_id from the list + * @param disk_id of the disk + * @param snap_id of the snapshot + * @param new_name of the snapshot + * @return 0 on success + */ + int rename_disk_snapshot(int disk_id, int snap_id, const string& new_name, + string& error_str) + { + return disks.rename_snapshot(disk_id, snap_id, new_name, error_str); + } + /** * Deletes all the disk snapshots for non-persistent disks and for persistent * disks in no shared system ds. diff --git a/include/VirtualMachineDisk.h b/include/VirtualMachineDisk.h index 8af6489ae5..dfb36d2ffc 100644 --- a/include/VirtualMachineDisk.h +++ b/include/VirtualMachineDisk.h @@ -256,6 +256,24 @@ public: return (snapshots != 0); } + /** + * Renames a snapshot + * + * @param id_snap of the snapshot + * @param new_name of the snapshot + * @return 0 on success + */ + int rename_snapshot(int snap_id, const string& new_name, string& str_error) + { + if (!snapshots) + { + str_error = "The VM does not have any snapshot"; + return -1; + } + + return snapshots->rename_snapshot(snap_id, new_name, str_error); + } + /** * Creates a new snapshot of the disk * @param name a description for this snapshot @@ -734,6 +752,15 @@ public: void delete_snapshot(int disk_id, int snap_id, Template **ds_quota, Template **vm_quota, bool& io, bool& vo); + /** + * Renames a given snapshot + * @param disk_id of the disk + * @param snap_id of the snapshot + * @param new_name of the snapshot + * @return 0 on success + */ + int rename_snapshot(int disk_id, int snap_id, const string& new_name, string& str_error); + /** * Deletes all the disk snapshots for non-persistent disks and for persistent * disks in no shared system ds. diff --git a/share/doc/xsd/acct.xsd b/share/doc/xsd/acct.xsd index 8fee77f6e1..8f45cf13e5 100644 --- a/share/doc/xsd/acct.xsd +++ b/share/doc/xsd/acct.xsd @@ -76,6 +76,7 @@ RECOVER_ACTION = 42 RETRY_ACTION = 43 MONITOR_ACTION = 44 + DISK_SNAPSHOT_RENAME_ACTION = 45 --> diff --git a/share/doc/xsd/vm.xsd b/share/doc/xsd/vm.xsd index a2d980da68..9c9f5979e0 100644 --- a/share/doc/xsd/vm.xsd +++ b/share/doc/xsd/vm.xsd @@ -181,6 +181,7 @@ RECOVER_ACTION = 42 RETRY_ACTION = 43 MONITOR_ACTION = 44 + DISK_SNAPSHOT_RENAME_ACTION = 45 --> diff --git a/src/cli/onevm b/src/cli/onevm index 4971565f00..b363561bd6 100755 --- a/src/cli/onevm +++ b/src/cli/onevm @@ -870,6 +870,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do States: RUNNING, POWEROFF, SUSPENDED EOT + command :"disk-snapshot-delete", disk_snapshot_delete_desc, :vmid, :diskid, :disk_snapshot_id do helper.perform_action(args[0],options,"disk snapshot deleted") do |o| @@ -877,6 +878,23 @@ cmd=CommandParser::CmdParser.new(ARGV) do end end + disk_snapshot_rename_desc = <<-EOT.unindent + Renames a disk snapshot. + EOT + + command :"disk-snapshot-rename", disk_snapshot_rename_desc, + :vmid, :diskid, :disk_snapshot_id, :new_snapshot_name do + helper.perform_action(args[0],options,"disk snapshot rename") do |o| + is_num = true if Integer(args[2]) rescue false + + if !is_num + OpenNebula::Error.new("The disk snapshot ID must be an integer") + else + o.disk_snapshot_rename(args[1].to_i, args[2].to_i, args[3].to_s) + end + end + end + disk_resize_desc = <<-EOT.unindent Resizes a VM disk. The new size should be larger than the old one. The valid units are: diff --git a/src/nebula/NebulaTemplate.cc b/src/nebula/NebulaTemplate.cc index 86aea17c2a..a1828a4430 100644 --- a/src/nebula/NebulaTemplate.cc +++ b/src/nebula/NebulaTemplate.cc @@ -749,6 +749,7 @@ static int _set_vm_auth_ops(const std::string& ops_str, ops_set.set(History::DISK_SNAPSHOT_CREATE_ACTION); ops_set.set(History::DISK_SNAPSHOT_DELETE_ACTION); ops_set.set(History::DISK_SNAPSHOT_REVERT_ACTION); + ops_set.set(History::DISK_SNAPSHOT_RENAME_ACTION); } else if ( the_op == "terminate" ) { diff --git a/src/oca/go/src/goca/vm.go b/src/oca/go/src/goca/vm.go index 174d0b369b..81ba09770d 100644 --- a/src/oca/go/src/goca/vm.go +++ b/src/oca/go/src/goca/vm.go @@ -691,6 +691,12 @@ func (vm *VM) SnapshotRevert(snapID int) error { return err } +// DiskSnapshotRename renames a snapshot +func (vm *VM) DiskSnapshotRename(diskID, snapID int, newName string) error { + _, err := client.Call("one.vm.disksnapshotrename", vm.ID, diskID, snapID, newName) + return err +} + // Attach a new disk to the virtual machine. diskTemplate is a string containing // a single DISK vector attribute. Syntax can be the usual attribute=value or // XML diff --git a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java index cb94dc2203..1e7eadf957 100644 --- a/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java +++ b/src/oca/java/src/org/opennebula/client/vm/VirtualMachine.java @@ -51,6 +51,7 @@ public class VirtualMachine extends PoolElement{ private static final String DISKSNAPSHOTCREATE = METHOD_PREFIX + "disksnapshotcreate"; private static final String DISKSNAPSHOTREVERT = METHOD_PREFIX + "disksnapshotrevert"; private static final String DISKSNAPSHOTDELETE = METHOD_PREFIX + "disksnapshotdelete"; + private static final String DISKSNAPSHOTRENAME = METHOD_PREFIX + "disksnapshotrename"; private static final String DISKRESIZE = METHOD_PREFIX + "diskresize"; private static final String UPDATECONF = METHOD_PREFIX + "updateconf"; @@ -575,6 +576,23 @@ public class VirtualMachine extends PoolElement{ return client.call(DISKSNAPSHOTDELETE, id, diskId, snapId); } + /** + * Deletes a disk snapshot + * + * @param client XML-RPC Client. + * @param id The VM id of the target VM. + * @param diskId Id of the disk + * @param snapId Id of the snapshot + * @param new_name New name of the snapshot + * @return If an error occurs the error message contains the reason. + */ + public static OneResponse diskSnapshotRename(Client client, int id, + int diskId, int snapId, String new_name) + { + return client.call(DISKSNAPSHOTRENAME, id, diskId, snapId, new_name); + } + + /** * Resize VM disk * diff --git a/src/oca/ruby/opennebula/virtual_machine.rb b/src/oca/ruby/opennebula/virtual_machine.rb index a1749d753b..3b00a61e23 100644 --- a/src/oca/ruby/opennebula/virtual_machine.rb +++ b/src/oca/ruby/opennebula/virtual_machine.rb @@ -46,6 +46,7 @@ module OpenNebula :disksnapshotcreate => "vm.disksnapshotcreate", :disksnapshotrevert => "vm.disksnapshotrevert", :disksnapshotdelete => "vm.disksnapshotdelete", + :disksnapshotrename => "vm.disksnapshotrename", :diskresize => "vm.diskresize", :updateconf => "vm.updateconf", :lock => "vm.lock", @@ -211,7 +212,7 @@ module OpenNebula disk-snapshot-create disk-snapshot-delete terminate terminate-hard disk-resize deploy chown chmod updateconf rename resize update snapshot-resize snapshot-delete snapshot-revert disk-saveas - disk-snapshot-revert recover retry monitor} + disk-snapshot-revert recover retry monitor disk-snapshot-rename} EXTERNAL_IP_ATTRS = [ 'GUEST_IP', @@ -649,6 +650,18 @@ module OpenNebula return call(VM_METHODS[:disksnapshotdelete], @pe_id, disk_id, snap_id) end + # Renames a disk snapshot + # + # @param disk_id [Integer] Id of the disk + # @param snap_id [Integer] Id of the snapshot + # @param new_name [String] New name for the snapshot + # + # @return [nil, OpenNebula::Error] nil in case of success, Error + # otherwise + def disk_snapshot_rename(disk_id, snap_id, new_name) + return call(VM_METHODS[:disksnapshotrename], @pe_id, disk_id, snap_id, new_name) + end + # Changes the size of a disk # # @param disk_id [Integer] Id of the disk diff --git a/src/rm/RequestManager.cc b/src/rm/RequestManager.cc index 18ea497088..9c3cb738d9 100644 --- a/src/rm/RequestManager.cc +++ b/src/rm/RequestManager.cc @@ -304,6 +304,7 @@ void RequestManager::register_xml_methods() xmlrpc_c::methodPtr vm_dsnap_create(new VirtualMachineDiskSnapshotCreate()); xmlrpc_c::methodPtr vm_dsnap_revert(new VirtualMachineDiskSnapshotRevert()); xmlrpc_c::methodPtr vm_dsnap_delete(new VirtualMachineDiskSnapshotDelete()); + xmlrpc_c::methodPtr vm_dsnap_rename(new VirtualMachineDiskSnapshotRename()); xmlrpc_c::methodPtr vm_recover(new VirtualMachineRecover()); xmlrpc_c::methodPtr vm_updateconf(new VirtualMachineUpdateConf()); xmlrpc_c::methodPtr vm_disk_resize(new VirtualMachineDiskResize()); @@ -505,6 +506,7 @@ void RequestManager::register_xml_methods() RequestManagerRegistry.addMethod("one.vm.disksnapshotcreate", vm_dsnap_create); RequestManagerRegistry.addMethod("one.vm.disksnapshotrevert", vm_dsnap_revert); RequestManagerRegistry.addMethod("one.vm.disksnapshotdelete", vm_dsnap_delete); + RequestManagerRegistry.addMethod("one.vm.disksnapshotrename", vm_dsnap_rename); RequestManagerRegistry.addMethod("one.vm.recover", vm_recover); RequestManagerRegistry.addMethod("one.vm.updateconf", vm_updateconf); RequestManagerRegistry.addMethod("one.vm.lock", vm_lock); diff --git a/src/rm/RequestManagerVirtualMachine.cc b/src/rm/RequestManagerVirtualMachine.cc index afa1095304..a8cd348d3f 100644 --- a/src/rm/RequestManagerVirtualMachine.cc +++ b/src/rm/RequestManagerVirtualMachine.cc @@ -2899,6 +2899,72 @@ void VirtualMachineDiskSnapshotDelete::request_execute( /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +void VirtualMachineDiskSnapshotRename::request_execute(xmlrpc_c::paramList const& paramList, + RequestAttributes& att) +{ + VirtualMachine * vm; + + ostringstream oss; + + const VirtualMachineDisk * disk; + + int rc; + + int id = xmlrpc_c::value_int(paramList.getInt(1)); + int did = xmlrpc_c::value_int(paramList.getInt(2)); + int snap_id = xmlrpc_c::value_int(paramList.getInt(3)); + string new_snap_name = xmlrpc_c::value_string(paramList.getString(4)); + + if ( vm_authorization(id, 0, 0, att, 0, 0, 0, auth_op) == false ) + { + return; + } + + if ((vm = get_vm(id, att)) == 0) + { + oss << "Could not rename the snapshot for VM " << id + << ", VM does not exist"; + + att.resp_msg = oss.str(); + + return; + } + + disk = (const_cast(vm))->get_disk(did); + + if (disk == 0) + { + att.resp_msg = "VM disk does not exist"; + failure_response(ACTION, att); + + vm->unlock(); + + return; + } + + rc = vm->rename_disk_snapshot(did, snap_id, new_snap_name, att.resp_msg); + + if ( rc != 0 ) + { + failure_response(ACTION, att); + + vm->unlock(); + + return; + } + + success_response(id, att); + + pool->update(vm); + + vm->unlock(); + + return; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void VirtualMachineUpdateConf::request_execute( xmlrpc_c::paramList const& paramList, RequestAttributes& att) { diff --git a/src/vm/History.cc b/src/vm/History.cc index 1a1ce57228..bfc85399c0 100644 --- a/src/vm/History.cc +++ b/src/vm/History.cc @@ -489,6 +489,9 @@ string History::action_to_str(VMAction action) case DISK_SNAPSHOT_DELETE_ACTION: st = "disk-snapshot-delete"; break; + case DISK_SNAPSHOT_RENAME_ACTION: + st = "disk-snapshot-rename"; + break; case DISK_RESIZE_ACTION: st = "disk-resize"; break; @@ -647,6 +650,10 @@ int History::action_from_str(const string& st, VMAction& action) { action = DISK_SNAPSHOT_DELETE_ACTION; } + else if (st == "disk-snapshot-rename") + { + action = DISK_SNAPSHOT_RENAME_ACTION; + } else if (st == "disk-resize") { action = DISK_RESIZE_ACTION; diff --git a/src/vm/Snapshots.cc b/src/vm/Snapshots.cc index c23cc65129..5697a00809 100644 --- a/src/vm/Snapshots.cc +++ b/src/vm/Snapshots.cc @@ -274,6 +274,24 @@ int Snapshots::active_snapshot(int id) /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int Snapshots::rename_snapshot(int id, const string& name, string& str_error) +{ + VectorAttribute * snapshot = get_snapshot(id); + + if (snapshot == 0) + { + str_error = "Snapshot does not exist"; + return -1; + } + + snapshot->replace("NAME", name); + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + const VectorAttribute * Snapshots::get_snapshot(int id) const { map::const_iterator it; diff --git a/src/vm/VirtualMachineDisk.cc b/src/vm/VirtualMachineDisk.cc index 9c9f327496..d991c98793 100644 --- a/src/vm/VirtualMachineDisk.cc +++ b/src/vm/VirtualMachineDisk.cc @@ -1381,6 +1381,23 @@ void VirtualMachineDisks::delete_snapshot(int disk_id, int snap_id, /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ +int VirtualMachineDisks::rename_snapshot(int disk_id, int snap_id, const string& new_name, + string& error_str) +{ + VirtualMachineDisk * disk = get_disk(disk_id); + + if (disk == 0) + { + error_str = "VM disk does not exist"; + return -1; + } + + return disk->rename_snapshot(snap_id, new_name, error_str); +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + void VirtualMachineDisks::delete_non_persistent_snapshots(Template **vm_quotas, vector