After a long time trying to figure this out I was finally able to capture an image from a vm.
firstly, the vm needs to be dealocated and generalized:
async_vm_deallocate = self.compute_client.virtual_machines.deallocate(resource_group.name, names.vm)
async_vm_deallocate.wait()
self.compute_client.virtual_machines.generalize(resource_group.name, names.vm)
I found that there are 2 options to creating the image:
compute_client.virtual_machines.capture(resource_group_name=resource_group.name, vm_name=vm.name, parameters=parameters)
this way requires creating a ComputeManagmentClient and the following import:
from azure.mgmt.compute.v2015_06_15.models import VirtualMachineCaptureParameters
the parameters have to be object type: ~azure.mgmt.compute.v2015_06_15.models.VirtualMachineCaptureParameters.
The object VirtualMachineCaptureParameters has 3 required params:
vhd_name_prefix (str), destination container name (str), overwrite vhds (bool)
what these are, I have no idea and there is no explanation as to what they are. so I didnt use this way
- (the way I choose to use)
compute_client.images.create_or_update(resource_group_name=resource_group, image_name=unique_name, parameters=params)
this way requires creating a ComputeManagmentClient and the following import:
from azure.mgmt.compute.v2020_06_01.models import Image, SubResource
and it is pretty straight forward
sub_resource = SubResource(id=vm.id)
params = Image(location=LOCATION, source_virtual_machine=sub_resource)
i = compute_client.images.create_or_update(resource_group_name=resource_group, image_name=image_name, parameters=params)
i.wait()
creating the SubResource() and the Image() objects is mandatory as that is the object type expected