Class: VCloudSdk::VM

Inherits:
Object
  • Object
show all
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

Methods included from Powerable

#power_off, #power_on, #status

Constructor Details

#initialize(session, link) ⇒ VM

Returns a new instance of VM.



15
16
17
18
# File 'lib/ruby_vcloud_sdk/vm.rb', line 15

def initialize(session, link)
  @session = session
  @link = link
end

Instance Method Details

#add_nic(network_name, ip_addressing_mode = , ip = nil) ⇒ Object



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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# 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) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# 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") ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# 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) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
# 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) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
# 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) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# 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) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# 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

#hrefObject



20
21
22
# File 'lib/ruby_vcloud_sdk/vm.rb', line 20

def href
  @link
end

#independent_disksObject



109
110
111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# 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_disksObject



282
283
284
285
286
287
288
289
290
291
292
# 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_disksObject



121
122
123
124
125
126
127
128
129
130
# 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_networksObject



89
90
91
92
93
94
# 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

#memoryObject

returns size of memory in megabytes



25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object

sets size of memory in megabytes



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

#nicsObject



96
97
98
99
100
101
102
103
104
105
106
107
# 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_propertiesObject



265
266
267
268
269
270
# 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) ⇒ Object



272
273
274
275
276
277
278
279
280
# 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

#vcpuObject

returns number of virtual cpus of VM



59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

sets number of virtual cpus of VM



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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