[ovirt-users] [PySDK v3] Choose storage domain
Juan Hernández
jhernand at redhat.com
Tue Jan 24 12:33:22 UTC 2017
On 01/24/2017 01:18 PM, Nicolas Ecarnot wrote:
> Juan,
>
> Thank you very much for your help, this is working.
>
> Some comments below.
>
> Le 24/01/2017 à 11:04, Juan Hernández a écrit :
>> 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.
>
> This is what I feared, and it was not really obvious at first sight (to
> prepare a disks list...).
> I think I saw it was less weird in V4.
>
>> [...]
>
>> 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')
>
> Here is what I was doing (please don't laugh) :
>
> c_list = api.clusters.list()
> # At present (2017), each datacenter contains only one cluster
> vm_cluster = c_list[0]
>
> vm_template = api.templates.get(name=template_name)
>
>> 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()
>> ),
>
> Is it right to say that this last method is only valid in the api
> context, and would not work outside of the api object scope?
> I mean : if I want to use your method, I can no longer separate the
> preparation of a vm_params object before calling the vms.add, right?
>
>
>
>
> OK, just one second before sending this e-mail, I made a quick test with
> the template object and it is working anyway.
> Does it mean nothing is instantiated before the api.vms.add call?
>
The concern is not about preparing the objects before calling vms.add,
that is perfectly OK. The concern is what that prepared object contains.
With the code you are using currently you will be sending to the server
the complete representation of the cluster and the template. Run debug
on, with the "debug=True" parameter of the API constructor, and you will
see it, you will be sending a request like this:
POST /ovirt-engine/api/vms
With a request body like this:
<vm>
<cluster>
<name>mycluster</name>
<description>My cluster</description>
<error_handling>...</error_handling>
<fencing_policy>...</fencing_policy>
<!-- Etc, all the attributes of the cluster object. -->
<cluster>
<template>
<!-- All the attribute of the template. -->
</template>
...
</vm>
All those cluster and template attributes aren't needed, the server will
just ignore them, because it only needs the name (or id):
<vm>
<cluster>
<name>mycluster</name>
<cluster>
<template>
<name>mytemplate</name>
</template>
...
</vm>
So it is a waste of resources, and in some cases it may trigger hidden bugs.
If you still want to prepare the variables before calling the vms.add
method, just build their values so that they contain only the values (in
this case is better to use the id instead of the name, as it is more
specific):
vm_template=api.templates.get(name=template_name)
vm_template=params.Template(
id=vm_template.get_id()
)
This isn't very convenient if you want to use other attributes of the
'vm_template' object later. That is why my recommendation is to create
this temporary 'params.Template' object within the call to the 'vms.add'
method:
vms.add(
vm=params.Vm(
template=params.Template(
id=vm_template.get_id()
),
...
)
)
Just consider this as a good practice.
More information about the Users
mailing list