Class: VCloudSdk::VM
- Inherits:
-
Object
- Object
- VCloudSdk::VM
- Extended by:
- Forwardable
- Includes:
- Infrastructure, Powerable
- Defined in:
- lib/ruby_vcloud_sdk/vm.rb
Constant Summary
Constants included from Infrastructure
Infrastructure::ERROR_STATUSES, Infrastructure::SUCCESS_STATUS
Instance Method Summary collapse
- #add_nic(network_name, ip_addressing_mode = , ip = nil) ⇒ Object
- #attach_disk(disk) ⇒ Object
- #create_internal_disk(capacity, bus_type = "scsi", bus_sub_type = "lsilogic") ⇒ Object
- #detach_disk(disk) ⇒ Object
- #eject_media(catalog_name, media_file_name) ⇒ Object
- #href ⇒ Object
- #independent_disks ⇒ Object
-
#initialize(session, link) ⇒ VM
constructor
A new instance of VM.
- #insert_media(catalog_name, media_file_name) ⇒ Object
- #internal_disks ⇒ Object
- #list_disks ⇒ Object
- #list_networks ⇒ Object
-
#memory ⇒ Object
returns size of memory in megabytes.
-
#memory=(size) ⇒ Object
sets size of memory in megabytes.
- #product_section_properties ⇒ Object
- #product_section_properties=(properties) ⇒ Object
-
#vcpu ⇒ Object
returns number of virtual cpus of VM.
-
#vcpu=(count) ⇒ Object
sets number of virtual cpus of VM.
Methods included from Powerable
#power_off, #power_on, #status
Constructor Details
#initialize(session, link) ⇒ VM
Returns a new instance of VM.
14 15 16 17 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 14 def initialize(session, link) @session = session @link = link end |
Instance Method Details
#add_nic(network_name, ip_addressing_mode = , ip = nil) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 184 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 = list_networks.size # nic index begins with 0 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) end |
#attach_disk(disk) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 118 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]) task = monitor_task(task) Config.logger.info "Disk '#{disk.name}' is attached to VM '#{name}'" task end |
#create_internal_disk(capacity, bus_type = "scsi", bus_sub_type = "lsilogic") ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 264 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) end |
#detach_disk(disk) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 131 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]) task = monitor_task(task) Config.logger.info "Disk '#{disk.name}' is detached from VM '#{name}'" task end |
#eject_media(catalog_name, media_file_name) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 168 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) end |
#href ⇒ Object
19 20 21 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 19 def href @link end |
#independent_disks ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 95 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) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 152 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) end |
#internal_disks ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 252 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 ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 107 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 ⇒ Object
88 89 90 91 92 93 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 88 def list_networks entity_xml .network_connection_section .network_connections .map { |network_connection| network_connection.network } end |
#memory ⇒ Object
returns size of memory in megabytes
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 24 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) ⇒ Object
sets size of memory in megabytes
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 39 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 |
#product_section_properties ⇒ Object
235 236 237 238 239 240 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 235 def product_section_properties product_section = entity_xml.product_section return [] if product_section.nil? product_section.properties end |
#product_section_properties=(properties) ⇒ Object
242 243 244 245 246 247 248 249 250 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 242 def product_section_properties=(properties) Config.logger .info("Updating VM #{name} production sections with properties: #{properties.pretty_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 ⇒ Object
returns number of virtual cpus of VM
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 58 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) ⇒ Object
sets number of virtual cpus of VM
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ruby_vcloud_sdk/vm.rb', line 70 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 |