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

Attributes

client[R]

Public Class Methods

new(options = {}) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/fog/ovirt/compute/v4.rb, line 137
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

rubocop:disable Metrics/AbcSize

# File lib/fog/ovirt/requests/compute/v4/add_interface.rb, line 7
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

  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/AbcSize

# File lib/fog/ovirt/requests/compute/v4/add_volume.rb, line 17
def add_options_defaults(options)
  options = options.dup
  search = options[:search] || format("datacenter=%<datacenter>s", :datacenter => datacenter)
  options[:bootable] = options.delete(:bootable) == "true"
  options[:interface] ||= OvirtSDK4::DiskInterface::VIRTIO
  options[:provisioned_size] = options[:size_gb].to_i * Fog::Compute::Ovirt::DISK_SIZE_TO_GB if options[:size_gb]

  options[:storage_domain_id] = options[:storage_domain] || storagedomains(:role => "data", :search => search).first.id
  # If no size is given, default to a volume size of 8GB
  options[:provisioned_size] ||= 8 * Fog::Compute::Ovirt::DISK_SIZE_TO_GB
  options[:type] ||= OvirtSDK4::DiskType::DATA
  options[:format] ||= OvirtSDK4::DiskFormat::COW
  options[:sparse] = true unless options[:sparse].present?
  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][:storage_domains] ||= [client.system_service.storage_domains_service.storage_domain_service(options[:storage_domain_id]).get]
  options[:disk][:provisioned_size] ||= options.delete(:provisioned_size)
  options[:disk][:format] ||= options.delete(:format)
  options[:disk][:sparse] ||= options.delete(:sparse) == "true"
  options[:disk][:wipe_after_delete] ||= options.delete(:wipe_after_delete) == "true"
  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

rubocop:enable Metrics/AbcSize

# File lib/fog/ovirt/compute/v4.rb, line 159
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 173
def blank_template
  @blank_template ||= client.system_service.get.special_objects.blank_template
end
check_for_option(opts, name) click to toggle source
# File lib/fog/ovirt/requests/compute/v4/create_vm.rb, line 6
def check_for_option(opts, name)
  opts[name.to_sym] || opts[(name + "_name").to_sym]
end
create_search_by_datacenter(search:, datacenter:, page: nil) click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 177
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/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength

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

  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[: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::Compute::Ovirt::DISK_SIZE_TO_GB
  attrs[:high_availability] = OvirtSDK4::HighAvailability.new(:enabled => attrs[:ha] == "1") if attrs[:ha].present?

  # TODO: handle cloning from template
  process_vm_opts(attrs)
  new_vm = OvirtSDK4::Vm.new(attrs)
  vms_service.add(new_vm)
end
datacenter() click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 164
def datacenter
  @datacenter ||= datacenter_hash[:id]
end
datacenter_hash() click to toggle source
# File lib/fog/ovirt/compute/v4.rb, line 168
def datacenter_hash
  @datacenter_hash ||= datacenters.find { |x| x[:id] == @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
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|
    ovirt_attrs client.follow_link(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

rubocop:disable Metrics/AbcSize

# File lib/fog/ovirt/requests/compute/v4/list_vm_volumes.rb, line 7
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
process_vm_opts(opts) click to toggle source

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

# File lib/fog/ovirt/requests/compute/v4/create_vm.rb, line 11
def process_vm_opts(opts)
  return unless check_for_option(opts, "template") && check_for_option(opts, "storagedomain")

  template_id = opts[:template] || client.system_service.templates_service.search(:name => opts[:template_name]).first.id
  template_disks = client.system_service.templates_service.template_service(template_id).get.disk_attachments
  storagedomain_id = opts[:storagedomain] || storagedomains.select { |s| s.name == opts[:storagedomain_name] }.first.id

  # Make sure the 'clone' option is set if any of the disks defined by
  # the template is stored on a different storage domain than requested
  opts[:clone] = true unless opts[:clone] == true || template_disks.empty? || template_disks.all? { |d| d.storage_domain == storagedomain_id }

  # Create disks map
  opts[:disks] = template_disks.collect { |d| { :id => d.id, :storagedomain => storagedomain_id } }
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)
  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)
  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:disable Metrics/AbcSize

# File lib/fog/ovirt/requests/compute/v4/create_vm.rb, line 71
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

rubocop:disable Metrics/AbcSize

# File lib/fog/ovirt/requests/compute/v4/update_vm.rb, line 7
def update_vm(attrs)
  attrs = attrs.dup
  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)

  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 = {})
  client.system_service.vms_service.vm_service(id).ticket(options)
end
wrap_attribute(attrs, attribute, klass) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/fog/ovirt/requests/compute/v4/update_vm.rb, line 38
def wrap_attribute(attrs, attribute, klass)
  if attrs[attribute].present?
    attrs[attribute] = klass.new(:id => attrs[attribute])
  else
    attrs.delete(attribute)
  end
end