class OvirtSDK4::VmsService
Public Class Methods
Creates a new implementation of the service.
@param connection [Connection] The connection to be used by this service.
@param path [String] The relative path of this service, for example `vms/123/disks`.
@api private
# File lib/ovirtsdk4/services.rb, line 26410 def initialize(connection, path) @connection = connection @path = path end
Public Instance Methods
Creates a new virtual machine.
The virtual machine can be created in different ways:
-
From a template. In this case the identifier or name of the template must be provided. For example, using a plain shell script and XML:
- source,bash
#!/bin/sh -ex
url=“engine.example.com/ovirt-engine/api” user=“admin@internal” password=“…” curl \ –verbose \ –cacert /etc/pki/ovirt-engine/ca.pem \ –user “${user}:${password}” \ –request POST \ –header “Content-Type: application/xml” \ –header “Accept: application/xml” \ –data ' <vm>
<name>myvm</name> <template> <name>Blank</name> </template> <cluster> <name>mycluster</name> </cluster>
</vm> ' \ “${url}/vms”
-
From a snapshot. In this case the identifier of the snapshot has to be provided. For example, using a plain shel script and XML:
- source,bash
#!/bin/sh -ex
url=“engine.example.com/ovirt-engine/api” user=“admin@internal” password=“…” curl \ –verbose \ –cacert /etc/pki/ovirt-engine/ca.pem \ –user “${user}:${password}” \ –request POST \ –header “Content-Type: application/xml” \ –header “Accept: application/xml” \ –data ' <vm>
<name>myvm</name> <snapshots> <snapshot id="266742a5-6a65-483c-816d-d2ce49746680"/> </snapshots> <cluster> <name>mycluster</name> </cluster>
</vm> ' \ “${url}/vms”
When creating a virtual machine from a template or from a snapshot it is usually useful to explicitly indicate in what storage domain to create the disks for the virtual machine. If the virtual machine is created from a template then this is achieved passing a set of `disk` elements that indicate the mapping:
- source,xml
<vm>
... <disks> <disk id="8d4bd566-6c86-4592-a4a7-912dbf93c298"> <storage_domains> <storage_domain id="9cb6cb0a-cf1d-41c2-92ca-5a6d665649c9"/> </storage_domains> </disk> </disks>
</vm>
When the virtual machine is created from a snapshot this set of disks is sligthly different, it uses the `imageId` attribute instead of `id`.
- source,xml
<vm>
... <disks> <disk> <image_id>8d4bd566-6c86-4592-a4a7-912dbf93c298</image_id> <storage_domains> <storage_domain id="9cb6cb0a-cf1d-41c2-92ca-5a6d665649c9"/> </storage_domains> </disk> </disks>
</vm>
In all cases the name or identifier of the cluster where the virtual machine will be created is mandatory.
This is an example of how creating a virtual machine from a snapshot with the disks in a different storage domain can be done with the Python SDK:
- source,python
# Find the VM: vm = api.vms.get(name=“myvm”)
# Find the snapshot: snapshot = None for current in vm.snapshots.list():
if current.get_description() == 'mysnap': snapshot = current break
# Find the identifiers of the disks of the snapshot, as we need them in # order to explicitly indicate that we want them created in a different storage # domain: disk_ids = [] for current in snapshot.disks.list():
disk_ids.append(current.get_id())
# Find the storage domain where the disks should be created: sd = api.storagedomains.get(name=“yourdata”)
# Prepare the list of disks for the operation to create the snapshot, # explicitly indicating for each of them the storage domain where it should be # created: disk_list = [] for disk_id in disk_ids:
disk = params.Disk( image_id=disk_id, storage_domains=params.StorageDomains( storage_domain=[ params.StorageDomain( id=sd.get_id(), ), ], ), ) disk_list.append(disk)
# Create the VM from the snapshot: api.vms.add(
params.VM( name="myclone", cluster=params.Cluster(name="mycluster"), snapshots=params.Snapshots( snapshot=[ params.Snapshot( id=snapshot.get_id(), ), ], ), disks=params.Disks( disk=disk_list, ), )
)
@param vm [Vm]
@return [Vm]
-
# File lib/ovirtsdk4/services.rb, line 26585 def add(vm, opts = {}) if vm.is_a?(Hash) vm = OvirtSDK4::Vm.new(vm) end request = Request.new(:method => :POST, :path => @path) begin writer = XmlWriter.new(nil, true) VmWriter.write_one(vm, writer, 'vm') request.body = writer.string ensure writer.close end response = @connection.send(request) case response.code when 201, 202 begin reader = XmlReader.new(response.body) return VmReader.read_one(reader) ensure reader.close end else check_fault(response) end end
Returns the representation of the object managed by this service.
@param opts [Hash] Additional options.
@option opts [Boolean] :case_sensitive Indicates if the search performed using the `search` parameter should be performed taking case into
account. The default value is `true`, which means that case is taken into account. If you want to search ignoring case set it to `false`.
@option opts [Boolean] :filter Indicates if the results should be filtered according to the permissions of the user.
@option opts [Integer] :max The maximum number of results to return.
@option opts [String] :search A query string used to restrict the returned virtual machines.
@return [Array<Vm>]
# File lib/ovirtsdk4/services.rb, line 26628 def list(opts = {}) query = {} value = opts[:case_sensitive] unless value.nil? value = Writer.render_boolean(value) query['case_sensitive'] = value end value = opts[:filter] unless value.nil? value = Writer.render_boolean(value) query['filter'] = value end value = opts[:max] unless value.nil? value = Writer.render_integer(value) query['max'] = value end value = opts[:search] unless value.nil? query['search'] = value end request = Request.new(:method => :GET, :path => @path, :query => query) response = @connection.send(request) case response.code when 200 begin reader = XmlReader.new(response.body) return VmReader.read_many(reader) ensure reader.close end else check_fault(response) end end
Locates the service corresponding to the given path.
@param path [String] The path of the service.
@return [Service] A reference to the service.
# File lib/ovirtsdk4/services.rb, line 26682 def service(path) if path.nil? || path == '' return self end index = path.index('/') if index.nil? return vm_service(path) end return vm_service(path[0..(index - 1)]).service(path[(index +1)..-1]) end
Returns an string representation of this service.
@return [String]
# File lib/ovirtsdk4/services.rb, line 26698 def to_s return "#<#{VmsService}:#{@path}>" end
Locates the `vm` service.
@param id [String] The identifier of the `vm`.
@return [VmService] A reference to the `vm` service.
# File lib/ovirtsdk4/services.rb, line 26671 def vm_service(id) return VmService.new(@connection, "#{@path}/#{id}") end