class Fog::Ovirt::Compute::V4::Real

Attributes

client[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 165
def initialize(options = {})
  require "ovirtsdk4"
  username   = options[:ovirt_username]
  password   = options[:ovirt_password]
  server     = options[:ovirt_server]
  port       = options[:ovirt_port]       || 8080
  api_path   = options[:ovirt_api_path]   || "/api"
  url        = options[:ovirt_url]        || "https://#{server}:#{port}#{api_path}"

  connection_opts = {
    :url => url,
    :username => username,
    :password => password
  }
  @datacenter = options[:ovirt_datacenter]
  connection_opts[:ca_file]  = options[:ca_file]
  connection_opts[:ca_certs] = [OpenSSL::X509::Certificate.new(options[:public_key])] if options[:public_key].present?

  @client = ExceptionWrapper.new(OvirtSDK4::Connection.new(connection_opts))
end

Public Instance Methods

add_interface(id, options = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/add_interface.rb, line 6
def add_interface(id, options = {})
  raise ArgumentError, "instance id is a required parameter" unless id
  vm = client.system_service.vms_service.vm_service(id)
  nics_service = vm.nics_service
  options = options.dup
  options = convert_string_to_bool(options)
  if options[:network].present?
    network = client.system_service.networks_service.network_service(options[:network]).get

    profiles = client.follow_link(network.vnic_profiles)

    profile = profiles.detect { |x| x.name == network.name }

    profile ||= profiles.min_by(&:name)

    options.delete(:network)
    options[:vnic_profile] = { :id => profile.id }
  end

  interface = OvirtSDK4::Nic.new(options)
  nics_service.add(interface)
end
add_options_defaults(options) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/fog/ovirt/requests/compute/v4/add_volume.rb, line 17
def add_options_defaults(options)
  options = options.dup
  options = convert_string_to_bool(options)
  options[:bootable] = options.delete(:bootable)
  options[:interface] ||= OvirtSDK4::DiskInterface::VIRTIO
  options[:provisioned_size] = options[:size_gb].to_i * Fog::Ovirt::Compute::DISK_SIZE_TO_GB if options[:size_gb]
  options[:sparse] = true if options[:sparse].nil?
  options[:storage_domain_id] = options[:storage_domain] if options[:storage_domain]
  # If no size is given, default to a volume size of 8GB
  options[:provisioned_size] ||= 8 * Fog::Ovirt::Compute::DISK_SIZE_TO_GB
  options[:type] ||= OvirtSDK4::DiskType::DATA
  options[:format] ||= OvirtSDK4::DiskFormat::COW
  options[:quota] = options[:quota].present? ? client.system_service.data_centers_service.data_center_service(datacenter).quotas_service.quota_service(options[:quota]).get : nil
  options[:disk] ||= {}
  options[:disk][:sparse] = options.delete(:sparse) if options[:disk][:sparse].nil?
  options[:disk][:storage_domains] ||= [client.system_service.storage_domains_service.storage_domain_service(options[:storage_domain_id]).get] if options[:storage_domain_id]
  options[:disk][:provisioned_size] ||= options.delete(:provisioned_size)
  options[:disk][:format] ||= options.delete(:format)
  options[:disk][:wipe_after_delete] = options.delete(:wipe_after_delete) if options[:disk][:wipe_after_delete].nil?
  options
end
add_volume(id, options = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/add_volume.rb, line 6
def add_volume(id, options = {})
  raise ArgumentError, "instance id is a required parameter" unless id

  options = add_options_defaults(options)

  disk_attachments_service = client.system_service.vms_service.vm_service(id).disk_attachments_service
  disk = OvirtSDK4::DiskAttachment.new(options)
  disk_attachments_service.add(disk)
end
api_version() click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 192
def api_version
  api = client.system_service.get
  api.product_info.version.full_version
end
blank_template() click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 206
def blank_template
  @blank_template ||= client.system_service.get.special_objects.blank_template
end
create_disk_attachment_from_disk(disk_to_attachment) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/create_vm.rb, line 6
def create_disk_attachment_from_disk(disk_to_attachment)
  storage_domain = client.system_service.storage_domains_service.storage_domain_service(disk_to_attachment["storage_domain"]).get

  disk = {
    :id => disk_to_attachment["id"],
    :format => disk_to_attachment.fetch("format", OvirtSDK4::DiskFormat::COW),
    :sparse => disk_to_attachment.fetch("sparse", true),
    :storage_domains => [storage_domain]
  }

  OvirtSDK4::DiskAttachment.new(:disk => disk)
end
create_search_by_datacenter(search:, datacenter:, page: nil) click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 210
def create_search_by_datacenter(search:, datacenter:, page: nil)
  search ||= ""
  search += " datacenter=#{datacenter}" if datacenter
  search += " page #{page}" if page
  search
end
create_vm(attrs) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength

# File lib/fog/ovirt/requests/compute/v4/create_vm.rb, line 25
def create_vm(attrs)
  attrs = attrs.dup
  attrs = convert_string_to_bool(attrs)

  if attrs[:cluster].present?
    attrs[:cluster] = client.system_service.clusters_service.cluster_service(attrs[:cluster]).get
  else
    if attrs[:cluster_name].present?
      cluster = client.system_service.clusters_service.list(:search => "name=#{attrs[:cluster_name]}").first
    else
      cluster = client.system_service.clusters_service.list(:search => "datacenter=#{datacenter_hash[:name]}").first
      attrs[:cluster_name] = cluster.name
    end

    attrs[:cluster] = cluster
  end

  vms_service = client.system_service.vms_service

  attrs[:type] = attrs.fetch(:type, OvirtSDK4::VmType::SERVER)
  attrs[:instance_type] = attrs[:instance_type].present? ? client.system_service.instance_types_service.instance_type_service(attrs[:instance_type]).get : nil

  if attrs[:template].present?
    attrs[:template] = client.system_service.templates_service.template_service(attrs[:template]).get
  else
    attrs[:template] = client.system_service.get.special_objects.blank_template
    update_os_attrs(attrs)
  end

  attrs[:comment] ||= ""
  attrs[:quota] = attrs[:quota].present? ? client.system_service.data_centers_service.data_center_service(datacenter).quotas_service.quota_service(attrs[:quota]).get : nil
  if attrs[:cores].present? || attrs[:sockets].present?
    cpu_topology = OvirtSDK4::CpuTopology.new(:cores => attrs.fetch(:cores, "1"), :sockets => attrs.fetch(:sockets, "1"))
    attrs[:cpu] = OvirtSDK4::Cpu.new(:topology => cpu_topology)
  end
  attrs[:memory_policy] = OvirtSDK4::MemoryPolicy.new(:guaranteed => attrs[:memory]) if attrs[:memory].to_i < Fog::Ovirt::Compute::DISK_SIZE_TO_GB
  attrs[:high_availability] = OvirtSDK4::HighAvailability.new(:enabled => attrs[:ha] == "1") if attrs[:ha].present?

  process_vm_disks(attrs) if attrs[:clone] == true && attrs[:disks].present?

  new_vm = OvirtSDK4::Vm.new(attrs)
  vms_service.add(new_vm, :clone => attrs[:clone])
end
datacenter() click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 197
def datacenter
  datacenter_hash[:id]
end
datacenter_hash() click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 201
def datacenter_hash
  @datacenter_hash ||= datacenters.find { |x| x[:id] == @datacenter } || datacenters.find { |x| x[:name] == @datacenter } if @datacenter
  @datacenter_hash ||= datacenters.first
end
datacenters(filter = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/datacenters.rb, line 6
def datacenters(filter = {})
  client.system_service.data_centers_service.list(filter).map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
destroy_interface(id, options) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/destroy_interface.rb, line 6
def destroy_interface(id, options)
  raise ArgumentError, "instance id is a required parameter" unless id
  raise ArgumentError, "interface id is a required parameter for destroy-interface" unless options.key? :id
  vm = client.system_service.vms_service.vm_service(id)
  vm.nics_service.nic_service(options[:id]).remove
end
destroy_vm(options = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/destroy_vm.rb, line 6
def destroy_vm(options = {})
  raise ArgumentError, "instance id is a required parameter" unless options.key? :id

  client.system_service.vms_service.vm_service(options[:id]).remove
  true
end
destroy_volume(id, options) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/destroy_volume.rb, line 6
def destroy_volume(id, options)
  raise ArgumentError, "instance id is a required parameter" unless id
  raise ArgumentError, "volume id is a required parameter for destroy-volume" unless options.key? :id

  disks_service = client.system_service.disks_service
  disks_service.disk_service(options[:id]).remove
end
get_cluster(id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/get_cluster.rb, line 6
def get_cluster(id)
  ovirt_attrs client.system_service.clusters_service.cluster_service(id).get
end
get_instance_type(id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/get_instance_type.rb, line 6
def get_instance_type(id)
  ovirt_attrs client.system_service.instance_types_service.instance_type_service(id).get
end
get_quota(id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/get_quota.rb, line 6
def get_quota(id)
  quota = client.system_service.data_centers_service.data_center_service(datacenter).quotas_service.quota_service(id).get
  ovirt_attrs quota
end
get_template(id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/get_template.rb, line 6
def get_template(id)
  ovirt_attrs client.system_service.templates_service.template_service(id).get
end
get_virtual_machine(id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/get_virtual_machine.rb, line 6
def get_virtual_machine(id)
  ovirt_attrs client.system_service.vms_service.vm_service(id).get(:all_content => true)
end
get_volume(id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/get_volume.rb, line 6
def get_volume(id)
  ovirt_attrs client.system_service.disks_service.disk_service(id).get
end
list_clusters(opts = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_clusters.rb, line 6
def list_clusters(opts = {})
  opts[:search] = create_search_by_datacenter(:search => opts[:search], :datacenter => datacenter_hash[:name])
  client.system_service.clusters_service.list(opts).map { |ovirt_obj| ovirt_attrs(ovirt_obj) }
end
list_instance_types(filters = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_instance_types.rb, line 6
def list_instance_types(filters = {})
  client.system_service.instance_types_service.list(filters).map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
list_networks(cluster_id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_networks.rb, line 6
def list_networks(cluster_id)
  client.system_service.clusters_service.cluster_service(cluster_id).networks_service.list
end
list_operating_systems() click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_operating_systems.rb, line 6
def list_operating_systems
  client.system_service.operating_systems_service.list.map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
list_quotas(filters = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_quotas.rb, line 6
def list_quotas(filters = {})
  data_center = client.system_service.data_centers_service.data_center_service(datacenter)
  data_center.quotas_service.list(filters).map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
list_template_interfaces(vm_id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_template_interfaces.rb, line 6
def list_template_interfaces(vm_id)
  client.system_service.templates_service.template_service(vm_id).nics_service.list.map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
list_template_volumes(template_id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_template_volumes.rb, line 6
def list_template_volumes(template_id)
  template = client.system_service.templates_service.template_service(template_id).get
  attachments = client.follow_link(template.disk_attachments)
  attachments.map do |attachment|
    attachment_disk = client.follow_link(attachment.disk)
    attachment = client.follow_link(attachment)
    bootable = attachment.bootable
    interface = attachment.interface
    attachment_disk.bootable = bootable if attachment_disk.bootable.nil?
    attachment_disk.interface = interface if attachment_disk.interface.nil?
    attachment_disk.storage_domain = attachment_disk.storage_domains[0].id if attachment_disk.storage_domains[0].present?
    ovirt_attrs attachment_disk
  end
end
list_templates(filters = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_templates.rb, line 6
def list_templates(filters = {})
  client.system_service.templates_service.list(filters).map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
list_virtual_machines(filters = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_virtual_machines.rb, line 6
def list_virtual_machines(filters = {})
  filters = filters.dup
  page = filters.delete(:page)
  without_details = filters.delete(:without_details)
  filters[:all_content] = true unless without_details
  filters[:search] = create_search_by_datacenter(:search => filters[:search], :datacenter => datacenter_hash[:name], :page => page)
  client.system_service.vms_service.list(filters).map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
list_vm_interfaces(vm_id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_vm_interfaces.rb, line 6
def list_vm_interfaces(vm_id)
  client.system_service.vms_service.vm_service(vm_id).nics_service.list.map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
list_vm_volumes(vm_id) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_vm_volumes.rb, line 6
def list_vm_volumes(vm_id)
  vm = client.system_service.vms_service.vm_service(vm_id).get
  attachments = client.follow_link(vm.disk_attachments)
  attachments.map do |attachment|
    attachment_disk = client.follow_link(attachment.disk)
    attachment = client.follow_link(attachment)
    bootable = attachment.bootable
    interface = attachment.interface
    attachment_disk.bootable = bootable if attachment_disk.bootable.nil?
    attachment_disk.interface = interface if attachment_disk.interface.nil?
    attachment_disk.storage_domain = attachment_disk.storage_domains[0].id
    ovirt_attrs attachment_disk
  end
end
list_volumes() click to toggle source
# File lib/fog/ovirt/requests/compute/v4/list_volumes.rb, line 6
def list_volumes
  client.system_service.disks_service.list.map { |ovirt_obj| ovirt_attrs ovirt_obj }
end
ovirt_attrs(obj) click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 186
def ovirt_attrs(obj)
  shared_ovirt_attrs(obj) do
    client.follow_link(obj.vnic_profile).network.id
  end
end
process_vm_disks(opts) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/create_vm.rb, line 19
def process_vm_disks(opts)
  opts[:disk_attachments] = opts[:disks].map { |disk| create_disk_attachment_from_disk(disk) }
  opts.delete(:disks)
end
storage_domains(filter = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/storage_domains.rb, line 6
def storage_domains(filter = {})
  filter = filter.dup
  role_filter = filter.delete(:role)
  filter[:search] = create_search_by_datacenter(:search => filter[:search], :datacenter => datacenter_hash[:name])
  client.system_service.storage_domains_service.list(filter).collect do |sd|
    # Filter by role is not supported by the search language. The work around is to list all, then filter.
    role_filter.nil? || sd.type == role_filter ? sd : nil
  end.compact
end
update_interface(id, options) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/update_interface.rb, line 15
def update_interface(id, options)
  options = convert_string_to_bool(options)
  check_arguments(id, options)

  interface_id = options[:id]
  nic = client.system_service.vms_service.vm_service(id).nics_service.nic_service(interface_id)
  nic.update(nic, options)
end
update_os_attrs(attrs) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength

# File lib/fog/ovirt/requests/compute/v4/create_vm.rb, line 70
def update_os_attrs(attrs)
  attrs[:os] ||= {}
  attrs[:os][:type] ||= "Other OS"
  attrs[:os][:boot] ||= [attrs.fetch(:boot_dev1, OvirtSDK4::BootDevice::NETWORK), attrs.fetch(:boot_dev2, OvirtSDK4::BootDevice::HD)]
  attrs[:os][:boot] = attrs[:os][:boot].without(attrs["first_boot_dev"]).prepend(attrs["first_boot_dev"]) if attrs["first_boot_dev"]

  attrs[:os] = OvirtSDK4::OperatingSystem.new(:type => attrs[:os][:type], :boot => OvirtSDK4::Boot.new(:devices => attrs[:os][:boot]))
end
update_vm(attrs) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/update_vm.rb, line 6
def update_vm(attrs)
  attrs = attrs.dup
  attrs = convert_string_to_bool(attrs)
  vm_service = client.system_service.vms_service.vm_service(attrs[:id])

  if attrs[:cores].present?
    cpu_topology = OvirtSDK4::CpuTopology.new(:cores => attrs[:cores], :sockets => "1")
    attrs[:cpu] = OvirtSDK4::Cpu.new(:topology => cpu_topology)
  end
  wrap_attribute(attrs, :cluster, OvirtSDK4::Cluster)
  attrs[:template] = if attrs[:template].present?
                       OvirtSDK4::Template.new(:id => attrs[:template])
                     else
                       blank_template
                     end

  wrap_attribute(attrs, :large_icon, OvirtSDK4::Icon)
  wrap_attribute(attrs, :small_icon, OvirtSDK4::Icon)
  wrap_attribute(attrs, :cpu_profile, OvirtSDK4::CpuProfile)
  wrap_attribute(attrs, :host, OvirtSDK4::Host)
  wrap_attribute(attrs, :quota, OvirtSDK4::Quota)
  wrap_attribute(attrs, :instance_type, OvirtSDK4::InstanceType)
  attrs[:high_availability] = OvirtSDK4::HighAvailability.new(:enabled => attrs[:ha] == "1") if attrs[:ha].present?
  attrs[:original_template] = if attrs[:original_template].present?
                                OvirtSDK4::Template.new(:id => attrs[:original_template])
                              else
                                blank_template
                              end
  vm_service.update(attrs)
end
update_volume(id, options) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/update_volume.rb, line 15
def update_volume(id, options)
  check_arguments(id, options)
  options = convert_string_to_bool(options)
  disk_id = options[:id]
  disk_attachment = client.system_service.vms_service.vm_service(id).disk_attachments_service.attachment_service(disk_id)
  disk_attachment.update(disk_attachment, options)
  true # If we come here, expect success and return true
end
vm_action(options = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/vm_action.rb, line 6
def vm_action(options = {})
  raise ArgumentError, "instance id is a required parameter" unless options.key? :id
  raise ArgumentError, "action is a required parameter" unless options.key? :action

  vm_service = client.system_service.vms_service.vm_service(options[:id])
  vm_service.public_send(options[:action])
  vm_service.get.status
end
vm_start_with_cloudinit(options = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/vm_start_with_cloudinit.rb, line 6
def vm_start_with_cloudinit(options = {})
  raise ArgumentError, "instance id is a required parameter" unless options.key? :id

  vm_service = client.system_service.vms_service.vm_service(options[:id])
  vm_service.start(:use_cloud_init => true, :vm => { :initialization => options[:user_data] })
end
vm_ticket(id, options = {}) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/vm_ticket.rb, line 6
def vm_ticket(id, options = {})
  options = convert_string_to_bool(options)
  client.system_service.vms_service.vm_service(id).ticket(options).value
end
wrap_attribute(attrs, attribute, klass) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/update_vm.rb, line 37
def wrap_attribute(attrs, attribute, klass)
  if attrs[attribute].present?
    attrs[attribute] = klass.new(:id => attrs[attribute])
  else
    attrs.delete(attribute)
  end
end