2013-06-24 13:46:30 +02:00
|
|
|
require 'fog/core/model'
|
2013-08-27 11:19:54 +02:00
|
|
|
require 'fog/vcloud_director/models/compute/vm_customization'
|
2013-06-24 13:46:30 +02:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
2013-08-27 11:19:54 +02:00
|
|
|
class VcloudDirector
|
2013-06-24 13:46:30 +02:00
|
|
|
|
2013-07-09 15:48:44 +02:00
|
|
|
class Vm < Model
|
2013-06-24 13:46:30 +02:00
|
|
|
identity :id
|
2013-06-26 16:19:39 +02:00
|
|
|
|
2013-08-27 14:33:36 +02:00
|
|
|
attribute :vapp_id
|
|
|
|
attribute :vapp_name
|
2013-06-24 13:46:30 +02:00
|
|
|
attribute :name
|
|
|
|
attribute :type
|
|
|
|
attribute :href
|
2013-09-16 18:13:09 +01:00
|
|
|
attribute :status
|
2013-06-24 13:46:30 +02:00
|
|
|
attribute :operating_system
|
|
|
|
attribute :ip_address
|
2013-06-26 14:23:54 +02:00
|
|
|
attribute :cpu, :type => :integer
|
2013-06-24 13:46:30 +02:00
|
|
|
attribute :memory
|
2013-07-09 15:48:44 +02:00
|
|
|
attribute :hard_disks, :aliases => :disks
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-09-02 11:58:05 +02:00
|
|
|
def reload
|
2013-09-16 18:13:09 +01:00
|
|
|
# when collection.vapp is nil, it means it's fatherless,
|
2013-09-02 11:58:05 +02:00
|
|
|
# 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
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-09-02 11:58:05 +02:00
|
|
|
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
|
2013-09-16 18:13:09 +01:00
|
|
|
|
|
|
|
|
2013-07-05 14:35:23 +02:00
|
|
|
def power_on
|
|
|
|
response = service.post_vm_poweron(id)
|
2013-07-05 19:10:43 +02:00
|
|
|
service.process_task(response.body)
|
2013-07-05 14:35:23 +02:00
|
|
|
end
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-09-17 14:51:19 +03:00
|
|
|
def power_off
|
|
|
|
response = service.post_vm_poweroff(id)
|
|
|
|
service.process_task(response.body)
|
|
|
|
end
|
|
|
|
|
2013-07-03 13:10:31 +02:00
|
|
|
def tags
|
|
|
|
requires :id
|
2013-07-09 16:55:06 +02:00
|
|
|
service.tags(:vm => self)
|
2013-07-03 13:10:31 +02:00
|
|
|
end
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-06-24 13:46:30 +02:00
|
|
|
def customization
|
|
|
|
data = service.get_vm_customization(id).body
|
|
|
|
service.vm_customizations.new(data)
|
|
|
|
end
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-07-01 17:27:52 +02:00
|
|
|
def network
|
|
|
|
data = service.get_vm_network(id).body
|
|
|
|
service.vm_networks.new(data)
|
|
|
|
end
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-06-28 12:49:11 +02:00
|
|
|
def disks
|
|
|
|
requires :id
|
2013-07-09 16:55:06 +02:00
|
|
|
service.disks(:vm => self)
|
2013-06-28 12:49:11 +02:00
|
|
|
end
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-06-26 16:19:39 +02:00
|
|
|
def memory=(new_memory)
|
2013-07-05 15:52:23 +02:00
|
|
|
has_changed = ( memory != new_memory.to_i )
|
|
|
|
not_first_set = !memory.nil?
|
2013-06-26 16:19:39 +02:00
|
|
|
attributes[:memory] = new_memory.to_i
|
2013-07-05 15:52:23 +02:00
|
|
|
if not_first_set && has_changed
|
|
|
|
response = service.put_vm_memory(id, memory)
|
2013-07-05 19:10:43 +02:00
|
|
|
service.process_task(response.body)
|
2013-07-05 15:52:23 +02:00
|
|
|
end
|
2013-06-26 14:23:54 +02:00
|
|
|
end
|
2013-09-16 18:13:09 +01:00
|
|
|
|
2013-06-26 14:23:54 +02:00
|
|
|
def cpu=(new_cpu)
|
2013-07-05 15:52:23 +02:00
|
|
|
has_changed = ( cpu != new_cpu.to_i )
|
|
|
|
not_first_set = !cpu.nil?
|
2013-06-26 14:23:54 +02:00
|
|
|
attributes[:cpu] = new_cpu.to_i
|
2013-07-05 15:52:23 +02:00
|
|
|
if not_first_set && has_changed
|
|
|
|
response = service.put_vm_cpu(id, cpu)
|
2013-07-05 19:10:43 +02:00
|
|
|
service.process_task(response.body)
|
2013-07-05 15:52:23 +02:00
|
|
|
end
|
2013-06-26 14:23:54 +02:00
|
|
|
end
|
2013-07-05 15:52:23 +02:00
|
|
|
|
2013-06-24 13:46:30 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-09-16 18:13:09 +01:00
|
|
|
end
|