Class: Vcloud::Core::Vm
- Inherits:
-
Object
- Object
- Vcloud::Core::Vm
- Extended by:
- ComputeMetadata
- Defined in:
- lib/vcloud/core/vm.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_extra_disks(extra_disks) ⇒ Boolean
Add extra disks to VM.
-
#attach_independent_disks(disk_list) ⇒ Boolean
Attach independent disk(s) to VM.
-
#configure_guest_customization_section(preamble) ⇒ Boolean
Configure guest customisation script.
-
#configure_network_interfaces(networks_config) ⇒ Boolean
Configure VM network interfaces.
-
#cpu ⇒ Integer
Return the number of CPUs allocated to the VM.
-
#detach_independent_disks(disk_list) ⇒ Boolean
Detach independent disk(s) from VM.
-
#href ⇒ String
Return the href of VM.
-
#initialize(id, vapp) ⇒ Vcloud::Core::Vm
constructor
Initialize a Vcloud::Core::Vm within a vApp.
-
#memory ⇒ Integer
Return the amount of memory allocated to VM.
-
#name ⇒ String
Return the name of VM.
-
#update_cpu_count(new_cpu_count) ⇒ Boolean
Update the number of CPUs in VM.
-
#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).
-
#update_metadata(metadata) ⇒ Boolean
Update the metadata for VM.
-
#update_name(new_name) ⇒ Boolean
Update the name of VM.
-
#update_storage_profile(storage_profile) ⇒ Boolean
Update the storage profile of a VM.
-
#vapp_name ⇒ String
Return the name of the vApp containing VM.
-
#vcloud_attributes ⇒ Hash
Return the vCloud data associated with VM.
Methods included from ComputeMetadata
Constructor Details
#initialize(id, vapp) ⇒ Vcloud::Core::Vm
Initialize a Vcloud::Core::Vm within a vApp
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
#id ⇒ Object (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_prefix ⇒ Object
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
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
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
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
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 |
#cpu ⇒ Integer
Return the number of CPUs allocated to the VM
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
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 |
#href ⇒ String
Return the href of VM
66 67 68 |
# File 'lib/vcloud/core/vm.rb', line 66 def href vcloud_attributes[:href] end |
#memory ⇒ Integer
Return the amount of memory allocated to VM
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 |
#name ⇒ String
Return the name of VM
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
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)
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
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
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
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_name ⇒ String
Return the name of the vApp containing VM
82 83 84 |
# File 'lib/vcloud/core/vm.rb', line 82 def vapp_name @vapp.name end |
#vcloud_attributes ⇒ Hash
Return the vCloud data associated with VM
24 25 26 |
# File 'lib/vcloud/core/vm.rb', line 24 def vcloud_attributes Vcloud::Core::Fog::ServiceInterface.new.get_vapp(id) end |