pyvcloud icon indicating copy to clipboard operation
pyvcloud copied to clipboard

Ability to create a copy of a virtual machine (in online state)

Open pilot513 opened this issue 4 years ago • 0 comments

In the file "pyvcloud / vcd / vm.py" in the "copy_to" method of the "VM" class, the ability to copy a virtual machine to another Vapp is implemented. This is done by calling the private method "_clone". In this method, the copying process is started only if the machine is "off" state, or when the original machine after copying must be deleted. Below is the code for this method.

def _clone(self, source_vapp_name, target_vapp_name, target_vm_name,
           source_delete):
    """Clone VM from one vApp to another.
    :param: str source vApp name
    :param: str target vApp name
    :param: str target VM name
    :param: bool source delete option
    :return: an object containing EntityType.TASK XML data which represents
             the asynchronous task that is copying VM
    :rtype: lxml.objectify.ObjectifiedElement
    """
    from pyvcloud.vcd.vapp import VApp
    vm_resource = self.get_resource()
    resource_type = ResourceType.VAPP.value
    if self.is_powered_off(vm_resource) or source_delete:
        records1 = self.___validate_vapp_records(
            vapp_name=source_vapp_name, resource_type=resource_type)
        source_vapp_href = records1[0].get('href')
        records2 = self.___validate_vapp_records(
            vapp_name=target_vapp_name, resource_type=resource_type)
        target_vapp_href = records2[0].get('href')
        source_vapp = VApp(self.client, href=source_vapp_href)
        target_vapp = VApp(self.client, href=target_vapp_href)
        target_vapp.reload()
        spec = {
            'vapp': source_vapp.get_resource(),
            'source_vm_name': self.get_resource().get('name'),
            'target_vm_name': target_vm_name
        }
        return target_vapp.add_vms([spec],
                                   deploy=False,
                                   power_on=False,
                                   all_eulas_accepted=True,
                                   source_delete=source_delete
                                   )
    else:
        raise InvalidStateException("VM Must be powered off.")

In this line : if self.is_powered_off(vm_resource) or source_delete:

Question: why can't you copy the included vm? For example, the VCloud admin web interface allows this.

pilot513 avatar Nov 24 '20 14:11 pilot513