On 01/24/2017 10:13 AM, Nicolas Ecarnot wrote:
Hello,
When trying to create a VM by cloning a template, I found out I couldn't
choose the target storage domain :
[...]
vm_storage_domain = api.storagedomains.get(name=storage_domain)
vm_params = params.VM(name=vm_name,
memory=vm_memory,
cluster=vm_cluster,
template=vm_template,
os=vm_os,
storage_domain=vm_storage_domain,
)
try:
api.vms.add(vm=vm_params)
[...]
I'm getting :
Failed to create VM from Template:
status: 400
reason: Bad Request
detail: Cannot add VM. The selected Storage Domain does not contain the
VM Template.
... which I know, but I thought that, as with the GUI, I could specify
the target storage domain.
I made my homework, and I found a nice answer from Juan :
http://lists.ovirt.org/pipermail/users/2016-January/037321.html
but this relates to snapshots, and not to template usage, so I'm still
stuck.
May I ask an advice?
In order to do that you need to specify that you want to clone the disks
of the template, and for each disk you need to specify the storage
domain where you want to create it. Should be something like this:
---8<---
# Find the identifiers of the disks of the template:
template = api.templates.get(name='mytemplate')
disk_ids = [disk.get_id() for disk in template.disks.list()]
# Find the target storage domain:
sd = api.storagedomains.get(name='yourdata')
# Prepare a list of disks that specifies the target storage domain
# for each disk of the template:
disks = []
for disk_id in disk_ids:
disk = params.Disk(
id=disk_id,
storage_domains=params.StorageDomains(
storage_domain=[
params.StorageDomain(
id=sd.get_id()
)
]
)
)
disks.append(disk)
# Create the virtual machine, specifying that the list should
# be cloned, and to which storage domain they should be cloned:
api.vms.add(
vm=params.VM(
name='myvm',
cluster=params.Cluster(
name='mycluster'
),
template=params.Template(
name='mytemplate',
),
disks=params.Disks(
clone=True,
disk=disks
)
)
)
--->8---
Also, please be careful when specifying the cluster and the template.
You are currently doing this:
cluster=vm_cluster,
template=vm_template,
Not sure how you are assigning the values to those 'vm_cluster' and
'vm_template' variables, but you are probably doing this:
vm_cluster = api.vms.get(name='mycluster')
vm_template = api.vms.get(name='mytemplate')
That combination isn't ideal, because you will be sending with the 'add'
request the complete representation of the cluster and the template,
when the server only needs the id or the name. Consider doing this instead:
cluster=params.Cluster(
id=vm_cluster.get_id()
),
template=params.Cluster(
id=vm_template.get_id()
),
Or just the names, as in the example above.