From 620997c5a571ad77bea86ae2c5b301602f99c20e Mon Sep 17 00:00:00 2001 From: Mukul Date: Fri, 11 Feb 2022 02:12:02 +0530 Subject: [PATCH] Copy/Move VM from source vApp to target vApp This commit address issue to copy/move vms from source vApp to target vApp Work towards: #200 --- modules/vcd_vapp_vm.py | 45 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/modules/vcd_vapp_vm.py b/modules/vcd_vapp_vm.py index b98d6c2..30b0445 100644 --- a/modules/vcd_vapp_vm.py +++ b/modules/vcd_vapp_vm.py @@ -217,7 +217,8 @@ VAPP_VM_SET_METADATA_VALUE_TYPE = ['String', 'Number', 'Boolean', 'DateTime'] VAPP_VM_OPERATIONS = ['poweron', 'poweroff', 'reloadvm', 'deploy', 'undeploy', 'list_disks', 'list_nics', - 'set_meta', 'get_meta', 'remove_meta'] + 'set_meta', 'get_meta', 'remove_meta', 'copy', + 'move'] def vapp_vm_argument_spec(): @@ -310,6 +311,12 @@ def manage_operations(self): if operation == "remove_meta": return self.remove_meta() + if operation == "move": + return self.move_vm() + + if operation == "copy": + return self.copy_vm() + def get_org(self): org_name = self.params.get('org_name') org_resource = self.client.get_org() @@ -635,6 +642,42 @@ def remove_meta(self): return response + def copy_vm(self): + response = dict() + response['changed'] = False + target_vm_name = self.params.get('target_vm_name') + source_vapp_name = self.params.get('source_vapp') + target_vapp_name = self.params.get('target_vapp') + vm = self.get_vm() + + try: + vm.copy_to(source_vapp_name, target_vapp_name, target_vm_name) + msg = "VM has been copied to {0} with name {1}" + response["msg"] = msg.format(target_vapp_name, target_vm_name) + response['changed'] = True + except Exception as ex: + response['msg'] = ex + + return response + + def move_vm(self): + response = dict() + response['changed'] = False + target_vm_name = self.params.get('target_vm_name') + source_vapp_name = self.params.get('source_vapp') + target_vapp_name = self.params.get('target_vapp') + vm = self.get_vm() + + try: + vm.move_to(source_vapp_name, target_vapp_name, target_vm_name) + msg = "VM has been moved to {0} with name {1}" + response["msg"] = msg.format(target_vapp_name, target_vm_name) + response['changed'] = True + except Exception as ex: + response['msg'] = ex + + return response + def main(): argument_spec = vapp_vm_argument_spec()