[ovirt-users] ovirt-engine-sdk-python-4.1 clone a vm from snapshot
Juan Hernández
jhernand at redhat.com
Wed Mar 8 18:18:42 UTC 2017
On 03/08/2017 06:53 PM, Niyazi Elvan wrote:
> Hi Folks !
>
> I am looking for a documentation for ovirt-engine-sdk-python-4.1 but
> could not find anything yet. Looks like there are many changes compared
> to 4.0.
>
Version 4.1 of the SDK isn't very different for version 4.0. It is very
different from version 3.6.
Note that version 3 of the API, and version 3 of the SDK are deprecated
since version 4.0 of the engine, and they will be removed in version 4.2
of the engine.
There is a general description of version 4 of the SDK here:
https://github.com/oVirt/ovirt-engine-sdk/blob/master/sdk/README.adoc
A collection of examples here:
https://github.com/oVirt/ovirt-engine-sdk/tree/master/sdk/examples
And reference documentation here:
http://ovirt.github.io/ovirt-engine-sdk/4.1
You may also find useful the documentation of the API itself:
http://ovirt.github.io/ovirt-engine-api-model/4.1
Specially the section that describes how to add virtual machines, as
there is an example of how to add a virtual machine from a snapshot there:
http://ovirt.github.io/ovirt-engine-api-model/4.1/#services/vms/methods/add
> Can anyone help me about how to clone a VM from a snapshot using the
> latest sdk ?
>
To create a virtual machine from a snapshot with the latest version of
the SDK you will need something like this:
---8<---
import time
import ovirtsdk4 as sdk
import ovirtsdk4.types as types
# This example shows how to clone a virtual machine from an snapshot.
# Create the connection to the server:
connection = sdk.Connection(
url='https://engine.example.com/ovirt-engine/api',
username='admin at internal',
password='...',
ca_file='ca.pem'
)
# Get the reference to the root of the tree of services:
system_service = connection.system_service()
# Find the virtual machine:
vms_service = system_service.vms_service()
vm = vms_service.list(search='name=myvm')[0]
# Find the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Find the snapshot. Note that the snapshots collection doesn't support
# search, so we need to retrieve the complete list and the look for the
# snapshot that has the description that we are looking for.
snaps_service = vm_service.snapshots_service()
snaps = snaps_service.list()
snap = next(
(s for s in snaps if s.description == 'mysnap'),
None
)
# Create a new virtual machine, cloning it from the snapshot:
cloned_vm = vms_service.add(
vm=types.Vm(
name='myclonedvm',
snapshots=[
types.Snapshot(
id=snap.id
)
],
cluster=types.Cluster(
name='mycluster'
)
)
)
# Find the service that manages the cloned virtual machine:
cloned_vm_service = vms_service.vm_service(cloned_vm.id)
# Wait till the virtual machine is down, as that means that the creation
# of the disks of the virtual machine has been completed:
while True:
time.sleep(5)
cloned_vm = cloned_vm_service.get()
if cloned_vm.status == types.VmStatus.DOWN:
break
# Close the connection to the server:
connection.close()
--->8---
More information about the Users
mailing list