class VCloudSdk::VM

Constants

BYTES_PER_MEGABYTE

Public Class Methods

new(session, link) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 15
def initialize(session, link)
  @session = session
  @link = link
end

Public Instance Methods

add_nic( network_name, ip_addressing_mode = Xml::IP_ADDRESSING_MODE[:POOL], ip = nil) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 200
def add_nic(
      network_name,
      ip_addressing_mode = Xml::IP_ADDRESSING_MODE[:POOL],
      ip = nil)
  fail CloudError,
       "Invalid IP_ADDRESSING_MODE '#{ip_addressing_mode}'" unless Xml::IP_ADDRESSING_MODE
                                                                   .each_value
                                                                   .any? { |m| m == ip_addressing_mode }

  fail CloudError,
       "IP is missing for MANUAL IP_ADDRESSING_MODE" if ip_addressing_mode == Xml::IP_ADDRESSING_MODE[:MANUAL] &&
                                                          ip.nil?

  fail ObjectNotFoundError,
       "Network #{network_name} is not added to parent VApp #{vapp.name}" unless vapp
                                                                                   .list_networks
                                                                                   .any? { |n| n == network_name }

  payload = entity_xml
  fail CloudError,
       "VM #{name} is powered-on and cannot add NIC." if is_status?(payload, :POWERED_ON)

  nic_index = add_nic_index

  Config.logger
    .info("Adding NIC #{nic_index}, network #{network_name} using mode '#{ip_addressing_mode}' #{ip.nil? ? "" : "IP: #{ip}"}")

  # Add NIC
  payload
    .hardware_section
    .add_item(nic_params(payload.hardware_section,
                         nic_index,
                         network_name,
                         ip_addressing_mode,
                         ip))

  # Connect NIC
  payload
    .network_connection_section
    .add_item(network_connection_params(payload.network_connection_section,
                                        nic_index,
                                        network_name,
                                        ip_addressing_mode,
                                        ip))

  task = connection.post(payload.reconfigure_link.href,
                         payload,
                         Xml::MEDIA_TYPE[:VM])
  monitor_task(task)
  self
end
attach_disk(disk) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 132
def attach_disk(disk)
  fail CloudError,
       "Disk '#{disk.name}' of link #{disk.href} is attached to VM '#{disk.vm.name}'" if disk.attached?

  task = connection.post(entity_xml.attach_disk_link.href,
                         disk_attach_or_detach_params(disk),
                         Xml::MEDIA_TYPE[:DISK_ATTACH_DETACH_PARAMS])
  monitor_task(task)

  Config.logger.info "Disk '#{disk.name}' is attached to VM '#{name}'"
  self
end
create_internal_disk( capacity, bus_type = "scsi", bus_sub_type = "lsilogic") click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 294
def create_internal_disk(
      capacity,
      bus_type = "scsi",
      bus_sub_type = "lsilogic")

  fail(CloudError,
       "Invalid size in MB #{capacity}") if capacity <= 0

  bus_type = Xml::BUS_TYPE_NAMES[bus_type.downcase]
  fail(CloudError,
       "Invalid bus type!") unless bus_type

  bus_sub_type = Xml::BUS_SUB_TYPE_NAMES[bus_sub_type.downcase]
  fail(CloudError,
       "Invalid bus sub type!") unless bus_sub_type

  Config
    .logger
    .info "Creating internal disk #{name} of #{capacity}MB."

  payload = entity_xml
  payload.add_hard_disk(capacity, bus_type, bus_sub_type)

  task = connection.post(payload.reconfigure_link.href,
                         payload,
                         Xml::MEDIA_TYPE[:VM])
  monitor_task(task)
  self
end
delete_internal_disk_by_name(name) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 324
def delete_internal_disk_by_name(name)
  payload = entity_xml

  unless payload.delete_hard_disk?(name)
    fail ObjectNotFoundError, "Internal disk '#{name}' is not found"
  end

  task = connection.post(payload.reconfigure_link.href,
                         payload,
                         Xml::MEDIA_TYPE[:VM])
  monitor_task(task)
  self
end
delete_nics(*nics) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 252
def delete_nics(*nics)
  payload = entity_xml
  fail CloudError,
       "VM #{name} is powered-on and cannot delete NIC." if is_status?(payload, :POWERED_ON)

  payload.delete_nics(*nics)
  task = connection.post(payload.reconfigure_link.href,
                         payload,
                         Xml::MEDIA_TYPE[:VM])
  monitor_task(task)
  self
end
detach_disk(disk) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 145
def detach_disk(disk)
  parent_vapp = vapp
  if parent_vapp.status == "SUSPENDED"
    fail VmSuspendedError,
         "vApp #{parent_vapp.name} suspended, discard state before detaching disk."
  end

  unless (vm = disk.vm).href == href
    fail CloudError,
         "Disk '#{disk.name}' is attached to other VM - name: '#{vm.name}', link '#{vm.href}'"
  end

  task = connection.post(entity_xml.detach_disk_link.href,
                         disk_attach_or_detach_params(disk),
                         Xml::MEDIA_TYPE[:DISK_ATTACH_DETACH_PARAMS])
  monitor_task(task)

  Config.logger.info "Disk '#{disk.name}' is detached from VM '#{name}'"
  self
end
eject_media(catalog_name, media_file_name) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 183
def eject_media(catalog_name, media_file_name)
  catalog = find_catalog_by_name(catalog_name)
  media = catalog.find_item(media_file_name, Xml::MEDIA_TYPE[:MEDIA])

  vm = entity_xml
  media_xml = connection.get(media.href)
  Config.logger.info("Ejecting media #{media_xml.name} from VM #{vm.name}")

  wait_for_running_tasks(media_xml, "Media '#{media_xml.name}'")

  task = connection.post(vm.eject_media_link.href,
                         media_insert_or_eject_params(media),
                         Xml::MEDIA_TYPE[:MEDIA_INSERT_EJECT_PARAMS])
  monitor_task(task)
  self
end
href() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 20
def href
  @link
end
independent_disks() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 109
def independent_disks
  hardware_section = entity_xml.hardware_section
  disks = []
  hardware_section.hard_disks.each do |disk|
    disk_link = disk.host_resource.attribute("disk")
    unless disk_link.nil?
      disks << VCloudSdk::Disk.new(@session, disk_link.to_s)
    end
  end
  disks
end
insert_media(catalog_name, media_file_name) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 166
def insert_media(catalog_name, media_file_name)
  catalog = find_catalog_by_name(catalog_name)
  media = catalog.find_item(media_file_name, Xml::MEDIA_TYPE[:MEDIA])

  vm = entity_xml
  media_xml = connection.get(media.href)
  Config.logger.info("Inserting media #{media_xml.name} into VM #{vm.name}")

  wait_for_running_tasks(media_xml, "Media '#{media_xml.name}'")

  task = connection.post(vm.insert_media_link.href,
                         media_insert_or_eject_params(media),
                         Xml::MEDIA_TYPE[:MEDIA_INSERT_EJECT_PARAMS])
  monitor_task(task)
  self
end
internal_disks() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 282
def internal_disks
  hardware_section = entity_xml.hardware_section
  internal_disks = []
  hardware_section.hard_disks.each do |disk|
    disk_link = disk.host_resource.attribute("disk")
    if disk_link.nil?
      internal_disks << VCloudSdk::InternalDisk.new(disk)
    end
  end
  internal_disks
end
list_disks() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 121
def list_disks
  entity_xml.hardware_section.hard_disks.map do |disk|
    disk_link = disk.host_resource.attribute("disk")
    if disk_link.nil?
      disk.element_name
    else
      "#{disk.element_name} (#{VCloudSdk::Disk.new(@session, disk_link.to_s).name})"
    end
  end
end
list_networks() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 89
def list_networks
  entity_xml
    .network_connection_section
    .network_connections
    .map { |network_connection| network_connection.network }
end
memory() click to toggle source

returns size of memory in megabytes

# File lib/ruby_vcloud_sdk/vm.rb, line 25
def memory
  m = entity_xml
        .hardware_section
        .memory
  allocation_units = m.get_rasd_content(Xml::RASD_TYPES[:ALLOCATION_UNITS])
  bytes = eval_memory_allocation_units(allocation_units)

  virtual_quantity = m.get_rasd_content(Xml::RASD_TYPES[:VIRTUAL_QUANTITY]).to_i
  memory_mb = virtual_quantity * bytes / BYTES_PER_MEGABYTE
  fail CloudError,
       "Size of memory is less than 1 MB." if memory_mb == 0
  memory_mb
end
memory=(size) click to toggle source

sets size of memory in megabytes

# File lib/ruby_vcloud_sdk/vm.rb, line 40
def memory=(size)
  fail(CloudError,
       "Invalid vm memory size #{size}MB") if size <= 0

  Config
    .logger
    .info "Changing the vm memory to #{size}MB."

  payload = entity_xml
  payload.change_memory(size)

  task = connection.post(payload.reconfigure_link.href,
                         payload,
                         Xml::MEDIA_TYPE[:VM])
  monitor_task(task)
  self
end
nics() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 96
def nics
  primary_index = entity_xml
                    .network_connection_section
                    .primary_network_connection_index
  entity_xml
    .network_connection_section
    .network_connections
    .map do |network_connection|
      VCloudSdk::NIC.new(network_connection,
                         network_connection.network_connection_index == primary_index)
    end
end
product_section_properties() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 265
def product_section_properties
  product_section = entity_xml.product_section
  return [] if product_section.nil?

  product_section.properties
end
product_section_properties=(properties) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 272
def product_section_properties=(properties)
  Config.logger.info(
    "Updating VM #{name} production sections with properties: #{properties.inspect}")
  task = connection.put(entity_xml.product_sections_link.href,
                        product_section_list_params(properties),
                        Xml::MEDIA_TYPE[:PRODUCT_SECTIONS])
  monitor_task(task)
  self
end
vcpu() click to toggle source

returns number of virtual cpus of VM

# File lib/ruby_vcloud_sdk/vm.rb, line 59
def vcpu
  cpus = entity_xml
          .hardware_section
          .cpu
          .get_rasd_content(Xml::RASD_TYPES[:VIRTUAL_QUANTITY])

  fail CloudError,
       "Uable to retrieve number of virtual cpus of VM #{name}" if cpus.nil?
  cpus.to_i
end
vcpu=(count) click to toggle source

sets number of virtual cpus of VM

# File lib/ruby_vcloud_sdk/vm.rb, line 71
def vcpu=(count)
  fail(CloudError,
       "Invalid virtual CPU count #{count}") if count <= 0

  Config
    .logger
    .info "Changing the virtual CPU count to #{count}."

  payload = entity_xml
  payload.change_cpu_count(count)

  task = connection.post(payload.reconfigure_link.href,
                         payload,
                         Xml::MEDIA_TYPE[:VM])
  monitor_task(task)
  self
end

Private Instance Methods

add_nic_index() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 340
def add_nic_index
  # nic index begins with 0
  i = 0
  nic_indexes = nics.map { |n| n.nic_index }
  while (nic_indexes.include?(i))
    i += 1
  end
  i
end
disk_attach_or_detach_params(disk) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 350
def disk_attach_or_detach_params(disk)
  Xml::WrapperFactory
    .create_instance("DiskAttachOrDetachParams")
    .tap do |params|
    params.disk_href = disk.href
  end
end
eval_memory_allocation_units(allocation_units) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 369
def eval_memory_allocation_units(allocation_units)
  # allocation_units is in the form of "byte * modifier * base ^ exponent" such as "byte * 2^20"
  # "modifier", "base" and "exponent" are positive integers and optional.
  # "base" and "exponent" must be present together.
  # Parsing logic: remove starting "byte" and first char "*" and replace power "^" with ruby-understandable "**"
  bytes = allocation_units.sub(/byte\s*(\*)?/, "").sub(/\^/, "**")
  return 1 if bytes.empty? # allocation_units is "byte" without "modifier", "base" or "exponent"
  fail unless bytes =~ /(\d+\s*\*)?(\d+\s*\*\*\s*\d+)?/
  eval bytes
rescue
  raise ApiError,
        "Unexpected form of AllocationUnits of memory: '#{allocation_units}'"
end
media_insert_or_eject_params(media) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 363
def media_insert_or_eject_params(media)
  Xml::WrapperFactory.create_instance("MediaInsertOrEjectParams").tap do |params|
    params.media_href = media.href
  end
end
network_connection_params(section, nic_index, network_name, addressing_mode, ip) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 398
def network_connection_params(section, nic_index, network_name, addressing_mode, ip)
  Xml::WrapperFactory
    .create_instance("NetworkConnection", nil, section.doc_namespaces)
    .tap do |params|
      params.network_connection_index = nic_index
      params.network = network_name
      params.ip_address_allocation_mode = addressing_mode
      params.ip_address = ip unless ip.nil?
      params.is_connected = true
  end
end
nic_params(section, nic_index, network_name, addressing_mode, ip) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 383
def nic_params(section, nic_index, network_name, addressing_mode, ip)
  is_primary = section.nics.empty?
  item = Xml::WrapperFactory
           .create_instance("Item", nil, section.doc_namespaces)

  Xml::NicItemWrapper
    .new(item)
    .tap do |params|
      params.nic_index = nic_index
      params.network = network_name
      params.set_ip_addressing_mode(addressing_mode, ip)
      params.is_primary = is_primary
  end
end
product_section_list_params(properties) click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 410
def product_section_list_params(properties)
  Xml::WrapperFactory.create_instance("ProductSectionList").tap do |params|
    properties.each do |property|
      params.add_property(property)
    end
  end
end
vapp() click to toggle source
# File lib/ruby_vcloud_sdk/vm.rb, line 358
def vapp
  vapp_link = entity_xml.vapp_link
  VCloudSdk::VApp.new(@session, vapp_link.href)
end