Show Table of Contents
3.6. Adding a Resource to a Collection
The
add method of a collection adds a resource. The resource to be added is created based on the parameters provided. Parameters are provided to the add method using an instance of an object from the ovirtsdk.xml.params module. Which specific class from the module needs to be used varies based on the type of resource being created.
Example 3.5. Adding a Resource to a Collection
In this example a virtual machine resource is created.
vm_params = params.VM(name="DemoVM",
cluster=api.clusters.get("Default"),
template=api.templates.get("Blank"),
memory=536870912)
vm = api.vms.add(vm_params)
While the virtual machine created by this example is not yet ready to run it illustrates the process for creating any Red Hat Virtualization resource:
- Create an instance of the parameter object for the type of resource being created.
- Identify the collection to which the resource will be added.
- Call the
addmethod of the collection passing the parameter object as a parameter.
Some parameter objects also have complex parameters of their own.
Example 3.6. Complex Parameters
In this example an NFS data center running in full version 4.0 compatibility mode is being created. To do this it is necessary to first construct a
ovirtsdk.xml.params.Version object. Then this is used as a parameter when creating an instance of a ovirtsdk.xml.params.DataCenter object containing parameters of the data center to be created. The resource is then created using the add method of the datacenters collection.
v_params = params.Version(major=4, minor=0) dc_params = params.DataCenter(name="DemoDataCenter", storage_type="NFS", version=v_params) dc = api.datacenters.add(dc_params)

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.