1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/vcloud_director/models/compute/vm.rb

143 lines
3.8 KiB
Ruby
Raw Normal View History

2013-06-24 07:46:30 -04:00
require 'fog/core/model'
2013-08-27 05:19:54 -04:00
require 'fog/vcloud_director/models/compute/vm_customization'
2013-06-24 07:46:30 -04:00
module Fog
module Compute
2013-08-27 05:19:54 -04:00
class VcloudDirector
2013-06-24 07:46:30 -04:00
2013-07-09 09:48:44 -04:00
class Vm < Model
2013-06-24 07:46:30 -04:00
identity :id
2013-06-26 10:19:39 -04:00
2013-08-27 08:33:36 -04:00
attribute :vapp_id
attribute :vapp_name
2013-06-24 07:46:30 -04:00
attribute :name
attribute :type
attribute :href
2013-09-16 13:13:09 -04:00
attribute :status
2013-06-24 07:46:30 -04:00
attribute :operating_system
attribute :ip_address
2013-06-26 08:23:54 -04:00
attribute :cpu, :type => :integer
attribute :memory, :type => :integer
2013-07-09 09:48:44 -04:00
attribute :hard_disks, :aliases => :disks
2013-09-16 13:13:09 -04:00
2013-09-02 05:58:05 -04:00
def reload
2013-09-16 13:13:09 -04:00
# when collection.vapp is nil, it means it's fatherless,
2013-09-02 05:58:05 -04: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 13:13:09 -04:00
2013-09-02 05:58:05 -04: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 13:13:09 -04:00
# Power off the VM.
def power_off
requires :id
response = service.post_power_off_vapp(id)
service.process_task(response.body)
end
2013-09-16 13:13:09 -04:00
# Power on the VM.
2013-07-05 08:35:23 -04:00
def power_on
requires :id
response = service.post_power_on_vapp(id)
service.process_task(response.body)
2013-07-05 08:35:23 -04:00
end
2013-09-16 13:13:09 -04:00
# Reboot the VM.
def reboot
requires :id
response = service.post_reboot_vapp(id)
service.process_task(response.body)
end
# Reset the VM.
def reset
requires :id
response = service.post_reset_vapp(id)
service.process_task(response.body)
end
# Shut down the VM.
def shutdown
requires :id
response = service.post_shutdown_vapp(id)
service.process_task(response.body)
end
# Suspend the VM.
def suspend
requires :id
response = service.post_suspend_vapp(id)
2013-09-17 07:51:19 -04:00
service.process_task(response.body)
end
2013-07-03 07:10:31 -04:00
def tags
requires :id
service.tags(:vm => self)
2013-07-03 07:10:31 -04:00
end
2013-09-16 13:13:09 -04:00
2013-06-24 07:46:30 -04:00
def customization
requires :id
2013-06-24 07:46:30 -04:00
data = service.get_vm_customization(id).body
service.vm_customizations.new(data)
end
2013-09-16 13:13:09 -04:00
2013-07-01 11:27:52 -04:00
def network
requires :id
2013-07-01 11:27:52 -04:00
data = service.get_vm_network(id).body
service.vm_networks.new(data)
end
2013-09-16 13:13:09 -04:00
2013-06-28 06:49:11 -04:00
def disks
requires :id
service.disks(:vm => self)
2013-06-28 06:49:11 -04:00
end
2013-09-16 13:13:09 -04:00
2013-06-26 10:19:39 -04:00
def memory=(new_memory)
has_changed = ( memory != new_memory.to_i )
not_first_set = !memory.nil?
2013-06-26 10:19:39 -04:00
attributes[:memory] = new_memory.to_i
if not_first_set && has_changed
response = service.put_memory(id, memory)
service.process_task(response.body)
end
2013-06-26 08:23:54 -04:00
end
2013-09-16 13:13:09 -04:00
2013-06-26 08:23:54 -04:00
def cpu=(new_cpu)
has_changed = ( cpu != new_cpu.to_i )
not_first_set = !cpu.nil?
2013-06-26 08:23:54 -04:00
attributes[:cpu] = new_cpu.to_i
if not_first_set && has_changed
response = service.put_cpu(id, cpu)
service.process_task(response.body)
end
2013-06-26 08:23:54 -04:00
end
def ready?
reload
status == 'on'
end
def vapp
# get_by_metadata returns a vm collection where every vapp parent is orpahn
collection.vapp ||= service.vapps.get(vapp_id)
end
2013-06-24 07:46:30 -04:00
end
end
end
2013-09-16 13:13:09 -04:00
end