Class: OvirtSDK4::VmsService

Inherits:
Service
  • Object
show all
Defined in:
lib/ovirtsdk4/services.rb,
lib/ovirtsdk4/services.rb

Instance Method Summary (collapse)

Instance Method Details

- (Vm) add(vm, opts = {})

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:

#!/bin/sh -ex

url="https://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:

#!/bin/sh -ex

url="https://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:

<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.

<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:

# 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,
    ),
  )
)

Parameters:

Returns:



26838
26839
26840
26841
26842
26843
26844
26845
26846
26847
26848
26849
26850
26851
26852
26853
26854
26855
26856
26857
26858
26859
26860
26861
26862
# File 'lib/ovirtsdk4/services.rb', line 26838

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

- (Array<Vm>) list(opts = {})

Returns the representation of the object managed by this service.

Parameters:

  • opts (Hash) (defaults to: {})

    Additional options.

Options Hash (opts):

  • :case_sensitive (Boolean)

    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.

  • :filter (Boolean)

    Indicates if the results should be filtered according to the permissions of the user.

  • :max (Integer)

    The maximum number of results to return.

  • :search (String)

    A query string used to restrict the returned virtual machines.

Returns:

  • (Array<Vm>)


26881
26882
26883
26884
26885
26886
26887
26888
26889
26890
26891
26892
26893
26894
26895
26896
26897
26898
26899
26900
26901
26902
26903
26904
26905
26906
26907
26908
26909
26910
26911
26912
26913
26914
26915
# File 'lib/ovirtsdk4/services.rb', line 26881

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

- (Service) service(path)

Locates the service corresponding to the given path.

Parameters:

  • path (String)

    The path of the service.

Returns:

  • (Service)

    A reference to the service.



26935
26936
26937
26938
26939
26940
26941
26942
26943
26944
# File 'lib/ovirtsdk4/services.rb', line 26935

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

- (String) to_s

Returns an string representation of this service.

Returns:

  • (String)


26951
26952
26953
# File 'lib/ovirtsdk4/services.rb', line 26951

def to_s
  return "#<#{VmsService}:#{@path}>"
end

- (VmService) vm_service(id)

Locates the vm service.

Parameters:

  • id (String)

    The identifier of the vm.

Returns:

  • (VmService)

    A reference to the vm service.



26924
26925
26926
# File 'lib/ovirtsdk4/services.rb', line 26924

def vm_service(id)
  return VmService.new(@connection, "#{@path}/#{id}")
end