Class: Vcloud::Core::Vm

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComputeMetadata

get_metadata

Constructor Details

#initialize(id, vapp) ⇒ Vm

Returns a new instance of Vm.



8
9
10
11
12
13
14
# File 'lib/vcloud/core/vm.rb', line 8

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

Instance Method Details

#add_extra_disks(extra_disks) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/vcloud/core/vm.rb', line 86

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) ⇒ Object



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

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) ⇒ Object



119
120
121
# File 'lib/vcloud/core/vm.rb', line 119

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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vcloud/core/vm.rb', line 96

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]

    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
  end
  Vcloud::Core::Fog::ServiceInterface.new.put_network_connection_system_section_vapp(id, section)
end

#cpuObject



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

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

#detach_independent_disks(disk_list) ⇒ Object



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

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

#hrefObject



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

def href
  vcloud_attributes[:href]
end

#memoryObject



28
29
30
31
# File 'lib/vcloud/core/vm.rb', line 28

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

#nameObject



38
39
40
# File 'lib/vcloud/core/vm.rb', line 38

def name
  vcloud_attributes[:name]
end

#update_cpu_count(new_cpu_count) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/vcloud/core/vm.rb', line 55

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) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/vcloud/core/vm.rb', line 20

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) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/vcloud/core/vm.rb', line 63

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) ⇒ Object



46
47
48
49
# File 'lib/vcloud/core/vm.rb', line 46

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) ⇒ Object



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

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_nameObject



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

def vapp_name
  @vapp.name
end

#vcloud_attributesObject



16
17
18
# File 'lib/vcloud/core/vm.rb', line 16

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