Class: Fog::Compute::VcloudDirector::Vm

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/vcloud_director/models/compute/vm.rb

Instance Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #lazy_load_attrs, #make_attr_loaded_method, #make_lazy_load_method

Constructor Details

This class inherits a constructor from Fog::Compute::VcloudDirector::Model

Instance Method Details

#cpu=(new_cpu) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 162

def cpu=(new_cpu)
  has_changed = ( cpu != new_cpu.to_i )
  not_first_set = !cpu.nil?
  attributes[:cpu] = new_cpu.to_i
  if not_first_set && has_changed
    response = service.put_cpu(id, cpu)
    service.process_task(response.body)
  end
end

#customizationObject



135
136
137
138
139
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 135

def customization
  requires :id
  data = service.get_vm_customization(id).body
  service.vm_customizations.new(data)
end

#disksObject



147
148
149
150
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 147

def disks
  requires :id
  service.disks(:vm => self)
end

#memory=(new_memory) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 152

def memory=(new_memory)
  has_changed = ( memory != new_memory.to_i )
  not_first_set = !memory.nil?
  attributes[:memory] = new_memory.to_i
  if not_first_set && has_changed
    response = service.put_memory(id, memory)
    service.process_task(response.body)
  end
end

#networkObject



141
142
143
144
145
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 141

def network
  requires :id
  data = service.get_vm_network(id).body
  service.vm_networks.new(data)
end

#power_offObject

Power off the VM.



48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 48

def power_off
  requires :id
  begin
    response = service.post_power_off_vapp(id)
  rescue Fog::Compute::VcloudDirector::BadRequest => ex
    Fog::Logger.debug(ex.message)
    return false
  end
  service.process_task(response.body)
end

#power_onObject

Power on the VM.



60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 60

def power_on
  requires :id
  begin
    response = service.post_power_on_vapp(id)
  rescue Fog::Compute::VcloudDirector::BadRequest => ex
    Fog::Logger.debug(ex.message)
    return false
  end
  service.process_task(response.body)
end

#ready?Boolean

Returns:

  • (Boolean)


185
186
187
188
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 185

def ready?
  reload
  status == 'on'
end

#rebootObject

Reboot the VM.



72
73
74
75
76
77
78
79
80
81
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 72

def reboot
  requires :id
  begin
    response = service.post_reboot_vapp(id)
  rescue Fog::Compute::VcloudDirector::BadRequest => ex
    Fog::Logger.debug(ex.message)
    return false
  end
  service.process_task(response.body)
end

#reconfigure(options) ⇒ Object

Reconfigure a VM using any of the options documented in post_reconfigure_vm



174
175
176
177
178
179
180
181
182
183
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 174

def reconfigure(options)
  options[:name] ||= name # name has to be sent
  # Delete those things that are not changing for performance
  [:cpu, :memory, :description].each do |k|
    options.delete(k) if options.key? k and options[k] == attributes[k]
  end
  response = service.post_reconfigure_vm(id, options)
  service.process_task(response.body)
  options.each {|k,v| attributes[k] = v}
end

#reloadObject



25
26
27
28
29
30
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 25

def reload
  # when collection.vapp is nil, it means it's fatherless,
  # vms from different vapps are loaded in the same collection.
  # This situation comes from a "query" result
  collection.vapp.nil? ? reload_single_vm : super
end

#reload_single_vmObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 32

def reload_single_vm
  return unless data = begin
    collection.get_single_vm(identity)
  rescue Excon::Errors::SocketError
    nil
  end
  # these two attributes don't exists in the get_single_vm response
  # that's why i use the old attributes
  data.attributes[:vapp_id] = attributes[:vapp_id]
  data.attributes[:vapp_name] = attributes[:vapp_name]
  new_attributes = data.attributes
  merge_attributes(new_attributes)
  self
end

#resetObject

Reset the VM.



84
85
86
87
88
89
90
91
92
93
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 84

def reset
  requires :id
  begin
    response = service.post_reset_vapp(id)
  rescue Fog::Compute::VcloudDirector::BadRequest => ex
    Fog::Logger.debug(ex.message)
    return false
  end
  service.process_task(response.body)
end

#shutdownObject

Shut down the VM.



107
108
109
110
111
112
113
114
115
116
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 107

def shutdown
  requires :id
  begin
    response = service.post_shutdown_vapp(id)
  rescue Fog::Compute::VcloudDirector::BadRequest => ex
    Fog::Logger.debug(ex.message)
    return false
  end
  service.process_task(response.body)
end

#suspendObject

Suspend the VM.



119
120
121
122
123
124
125
126
127
128
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 119

def suspend
  requires :id
  begin
    response = service.post_suspend_vapp(id)
  rescue Fog::Compute::VcloudDirector::BadRequest => ex
    Fog::Logger.debug(ex.message)
    return false
  end
  service.process_task(response.body)
end

#tagsObject



130
131
132
133
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 130

def tags
  requires :id
  service.tags(:vm => self)
end

#undeployObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 95

def undeploy
  requires :id
  begin
    response = service.post_undeploy_vapp(id)
  rescue Fog::Compute::VcloudDirector::BadRequest => ex
    Fog::Logger.debug(ex.message)
    return false
  end
  service.process_task(response.body)
end

#vappObject



190
191
192
193
# File 'lib/fog/vcloud_director/models/compute/vm.rb', line 190

def vapp
  # get_by_metadata returns a vm collection where every vapp parent is orpahn
  collection.vapp ||= service.vapps.get(vapp_id)
end