Class: Vcloud::Core::Vm

Inherits:
Object
  • Object
show all
Extended by:
ComputeMetadata
Defined in:
lib/vcloud/core/vm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ComputeMetadata

get_metadata

Constructor Details

#initialize(id, vapp) ⇒ Vcloud::Core::Vm

Initialize a Vcloud::Core::Vm within a vApp

Parameters:



13
14
15
16
17
18
19
# File 'lib/vcloud/core/vm.rb', line 13

def initialize(id, vapp)
  unless id =~ /^#{self.class.id_prefix}-[-0-9a-f]+$/
    raise "#{self.class.id_prefix} id : #{id} is not in correct format"
  end
  @id = id
  @vapp = vapp
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/vcloud/core/vm.rb', line 6

def id
  @id
end

Class Method Details

.id_prefixObject



198
199
200
# File 'lib/vcloud/core/vm.rb', line 198

def self.id_prefix
  'vm'
end

Instance Method Details

#add_extra_disks(extra_disks) ⇒ Boolean

Add extra disks to VM

Parameters:

  • extra_disks (Array)

    An array of hashes like [{ size: ‘20480’ }]

Returns:

  • (Boolean)

    return true or throw an error



137
138
139
140
141
142
143
144
145
# File 'lib/vcloud/core/vm.rb', line 137

def add_extra_disks(extra_disks)
  vm = Vcloud::Core::Fog::ModelInterface.new.get_vm_by_href(href)
  if extra_disks
    extra_disks.each do |extra_disk|
      Vcloud::Core.logger.debug("adding a disk of size #{extra_disk[:size]}MB into VM #{id}")
      vm.disks.create(extra_disk[:size])
    end
  end
end

#attach_independent_disks(disk_list) ⇒ Boolean

Attach independent disk(s) to VM

Parameters:

  • disk_list (Array)

    an array of Vcloud::Core::IndependentDisk objects

Returns:

  • (Boolean)

    return true or throw an error



115
116
117
118
119
120
# File 'lib/vcloud/core/vm.rb', line 115

def attach_independent_disks(disk_list)
  disk_list = Array(disk_list) # ensure we have an array
  disk_list.each do |disk|
    Vcloud::Core::Fog::ServiceInterface.new.post_attach_disk(id, disk.id)
  end
end

#configure_guest_customization_section(preamble) ⇒ Boolean

Configure guest customisation script

Parameters:

  • preamble (String)

    A script to run when the VM is created

Returns:

  • (Boolean)

    return true or throw an error



180
181
182
# File 'lib/vcloud/core/vm.rb', line 180

def configure_guest_customization_section(preamble)
  Vcloud::Core::Fog::ServiceInterface.new.put_guest_customization_section(id, vapp_name, preamble)
end

#configure_network_interfaces(networks_config) ⇒ Boolean

Configure VM network interfaces

Parameters:

  • networks_config (Array)

    An array of hashes like [{ :name => ‘NetworkName’ }]

Returns:

  • (Boolean)

    return true or throw an error



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/vcloud/core/vm.rb', line 151

def configure_network_interfaces(networks_config)
  return unless networks_config
  section = {PrimaryNetworkConnectionIndex: 0}
  section[:NetworkConnection] = networks_config.compact.each_with_index.map do |network, i|
    connection = {
        network: network[:name],
        needsCustomization: true,
        NetworkConnectionIndex: i,
        IsConnected: true
    }
    ip_address      = network[:ip_address]
    allocation_mode = network[:allocation_mode]
    mac_address     = network[:mac_address]

    allocation_mode = 'manual' if ip_address
    allocation_mode = 'dhcp' unless %w{dhcp manual pool}.include?(allocation_mode)

    connection[:IpAddressAllocationMode] = allocation_mode.upcase
    connection[:IpAddress] = ip_address if ip_address
    connection[:MACAddress] = mac_address if mac_address
    connection
  end
  Vcloud::Core::Fog::ServiceInterface.new.put_network_connection_system_section_vapp(id, section)
end

#cpuInteger

Return the number of CPUs allocated to the VM

Returns:

  • (Integer)

    number of virtual CPUs



51
52
53
54
# File 'lib/vcloud/core/vm.rb', line 51

def cpu
  cpu_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '3' }
  cpu_item[:'rasd:VirtualQuantity']
end

#detach_independent_disks(disk_list) ⇒ Boolean

Detach independent disk(s) from VM

Parameters:

  • disk_list (Array)

    an array of Vcloud::Core::IndependentDisk objects

Returns:

  • (Boolean)

    return true or throw an error



126
127
128
129
130
131
# File 'lib/vcloud/core/vm.rb', line 126

def detach_independent_disks(disk_list)
  disk_list = Array(disk_list) # ensure we have an array
  disk_list.each do |disk|
    Vcloud::Core::Fog::ServiceInterface.new.post_detach_disk(id, disk.id)
  end
end

#hrefString

Return the href of VM

Returns:

  • (String)

    the href of instance



66
67
68
# File 'lib/vcloud/core/vm.rb', line 66

def href
  vcloud_attributes[:href]
end

#memoryInteger

Return the amount of memory allocated to VM

Returns:

  • (Integer)

    amount of memory in megabytes



43
44
45
46
# File 'lib/vcloud/core/vm.rb', line 43

def memory
  memory_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '4' }
  memory_item[:'rasd:VirtualQuantity']
end

#nameString

Return the name of VM

Returns:

  • (String)

    the name of instance



59
60
61
# File 'lib/vcloud/core/vm.rb', line 59

def name
  vcloud_attributes[:name]
end

#update_cpu_count(new_cpu_count) ⇒ Boolean

Update the number of CPUs in VM

Parameters:

  • new_cpu_count (Integer)

    The number of virtual CPUs to allocate

Returns:

  • (Boolean)

    return true or throw an error



90
91
92
93
94
95
96
# File 'lib/vcloud/core/vm.rb', line 90

def update_cpu_count(new_cpu_count)
  return if new_cpu_count.nil?
  return if new_cpu_count.to_i == 0
  unless cpu.to_i == new_cpu_count.to_i
    Vcloud::Core::Fog::ServiceInterface.new.put_cpu(id, new_cpu_count)
  end
end

#update_memory_size_in_mb(new_memory) ⇒ Boolean

Set the amount of memory in VM which can’t be nil or less than 64 (mb)

Parameters:

  • new_memory (Integer)

    amount of memory for instance

Returns:

  • (Boolean)

    return true or throws an error



32
33
34
35
36
37
38
# File 'lib/vcloud/core/vm.rb', line 32

def update_memory_size_in_mb(new_memory)
  return if new_memory.nil?
  return if new_memory.to_i < 64
  unless memory.to_i == new_memory.to_i
    Vcloud::Core::Fog::ServiceInterface.new.put_memory(id, new_memory)
  end
end

#update_metadata(metadata) ⇒ Boolean

Update the metadata for VM

Parameters:

  • metadata (Hash)

    hash of keys, values to set

Returns:

  • (Boolean)

    return true or throw an error



102
103
104
105
106
107
108
109
# File 'lib/vcloud/core/vm.rb', line 102

def ()
  return if .nil?
  fsi = Vcloud::Core::Fog::ServiceInterface.new
  .each do |k, v|
    fsi.(@vapp.id, k, v)
    fsi.(id, k, v)
  end
end

#update_name(new_name) ⇒ Boolean

Update the name of VM

Parameters:

  • new_name (String)

    The new name for the VM

Returns:

  • (Boolean)

    return true or throw an error



74
75
76
77
# File 'lib/vcloud/core/vm.rb', line 74

def update_name(new_name)
  fsi = Vcloud::Core::Fog::ServiceInterface.new
  fsi.put_vm(id, new_name) unless name == new_name
end

#update_storage_profile(storage_profile) ⇒ Boolean

Update the storage profile of a VM

Parameters:

  • storage_profile (String)

    The name of the storage profile

Returns:

  • (Boolean)

    return true or throw an error



188
189
190
191
192
193
194
195
196
# File 'lib/vcloud/core/vm.rb', line 188

def update_storage_profile storage_profile
  storage_profile_href = get_storage_profile_href_by_name(storage_profile, @vapp.name)
  Vcloud::Core::Fog::ServiceInterface.new.put_vm(id, name, {
    :StorageProfile => {
      name: storage_profile,
      href: storage_profile_href
    }
  })
end

#vapp_nameString

Return the name of the vApp containing VM

Returns:

  • (String)

    the name of the vApp



82
83
84
# File 'lib/vcloud/core/vm.rb', line 82

def vapp_name
  @vapp.name
end

#vcloud_attributesHash

Return the vCloud data associated with VM

Returns:

  • (Hash)

    the complete vCloud data for VM



24
25
26
# File 'lib/vcloud/core/vm.rb', line 24

def vcloud_attributes
  Vcloud::Core::Fog::ServiceInterface.new.get_vapp(id)
end