2012-05-08 13:01:13 -04:00
|
|
|
require 'fog/compute/models/server'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Cloudstack
|
|
|
|
class Server < Fog::Compute::Server
|
2012-07-18 19:00:32 -04:00
|
|
|
identity :id, :aliases => 'id'
|
2012-05-08 13:01:13 -04:00
|
|
|
attribute :name
|
2012-07-18 19:00:32 -04:00
|
|
|
attribute :account_name, :aliases => 'account'
|
|
|
|
attribute :domain_name, :aliases => 'domain'
|
2012-05-08 13:01:13 -04:00
|
|
|
attribute :created
|
|
|
|
attribute :state
|
|
|
|
attribute :haenable
|
2012-05-16 14:12:08 -04:00
|
|
|
attribute :memory
|
2012-07-18 19:00:32 -04:00
|
|
|
attribute :display_name, :aliases => 'displayname'
|
|
|
|
attribute :domain_id, :aliases => 'domainid'
|
|
|
|
attribute :host_id, :aliases => 'hostid'
|
|
|
|
attribute :host_name, :aliases => 'hostname'
|
|
|
|
attribute :project_id, :aliases => 'projectid'
|
|
|
|
attribute :zone_id, :aliases => 'zoneid'
|
|
|
|
attribute :zone_name, :aliases => 'zonename'
|
|
|
|
attribute :image_id, :aliases => ['templateid', :template_id]
|
|
|
|
attribute :image_name, :aliases => ['templatename', :template_name]
|
|
|
|
attribute :templated_display_text, :aliases => 'templatedisplaytext'
|
|
|
|
attribute :password_enabled, :aliases => 'passwordenabled'
|
|
|
|
attribute :flavor_id, :aliases => ['serviceofferingid', :service_offering_id]
|
|
|
|
attribute :flavor_name, :aliases => ['serviceofferingname', :service_offering_name]
|
|
|
|
attribute :cpu_number, :aliases => 'cpunumber'
|
|
|
|
attribute :cpu_speed, :aliases => 'cpuspeed'
|
|
|
|
attribute :cpu_used, :aliases => 'cpuused'
|
|
|
|
attribute :network_kbs_read, :aliases => 'networkkbsread'
|
|
|
|
attribute :network_kbs_write, :aliases => 'networkkbswrite'
|
|
|
|
attribute :guest_os_id, :aliases => 'guestosid'
|
|
|
|
attribute :root_device_id, :aliases => 'rootdeviceid'
|
|
|
|
attribute :root_device_type, :aliases => 'rootdevicetype'
|
2012-11-20 23:49:39 -05:00
|
|
|
attribute :group
|
2012-07-18 19:00:32 -04:00
|
|
|
attribute :security_group_list, :type => :array, :aliases => 'securitygroup'
|
|
|
|
attribute :nics, :type => :array, :aliases => 'nic'
|
2012-05-08 13:01:13 -04:00
|
|
|
|
2012-05-16 14:12:08 -04:00
|
|
|
attr_accessor :network_ids, :disk_offering_id, :ip_address, :ip_to_network_list
|
2012-07-18 19:00:32 -04:00
|
|
|
attr_writer :security_group_ids
|
|
|
|
|
|
|
|
def addresses
|
|
|
|
nics.map{|nic| Address.new(nic)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
requires :id
|
2012-12-22 18:28:53 -05:00
|
|
|
data = service.destroy_virtual_machine("id" => id)
|
|
|
|
service.jobs.new(data["destroyvirtualmachineresponse"])
|
2012-07-18 19:00:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def flavor
|
2012-12-22 18:28:53 -05:00
|
|
|
service.flavors.get(self.flavor_id)
|
2012-07-18 19:00:32 -04:00
|
|
|
end
|
2012-05-08 13:01:13 -04:00
|
|
|
|
|
|
|
def ready?
|
2012-05-16 14:12:08 -04:00
|
|
|
state == 'Running'
|
2012-05-08 13:01:13 -04:00
|
|
|
end
|
|
|
|
|
2012-05-21 19:53:53 -04:00
|
|
|
def reboot
|
|
|
|
requires :id
|
2012-12-22 18:28:53 -05:00
|
|
|
data = service.reboot_virtual_machine('id' => self.id) # FIXME: does this ever fail?
|
|
|
|
service.jobs.new(data["rebootvirtualmachineresponse"])
|
2012-05-21 19:53:53 -04:00
|
|
|
end
|
|
|
|
|
2012-07-18 19:00:32 -04:00
|
|
|
def security_groups=(security_groups)
|
|
|
|
self.security_group_ids= Array(security_groups).map(&:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def security_group_ids
|
|
|
|
@security_group_ids || (self.security_group_list || []).map{|sg| sg["id"]}
|
|
|
|
end
|
|
|
|
|
|
|
|
def security_groups
|
2012-12-22 18:28:53 -05:00
|
|
|
security_group_ids.map{|id| service.security_groups.get(id)}
|
2012-07-18 19:00:32 -04:00
|
|
|
end
|
|
|
|
|
2012-05-08 13:01:13 -04:00
|
|
|
def save
|
2012-05-16 14:12:08 -04:00
|
|
|
requires :image_id, :flavor_id, :zone_id
|
|
|
|
|
|
|
|
options = {
|
|
|
|
'templateid' => image_id,
|
|
|
|
'serviceofferingid' => flavor_id,
|
|
|
|
'zoneid' => zone_id,
|
|
|
|
'networkids' => network_ids,
|
|
|
|
'diskofferingid' => disk_offering_id,
|
|
|
|
'displayname' => display_name,
|
2012-11-20 23:49:39 -05:00
|
|
|
'group' => group,
|
2012-05-16 14:12:08 -04:00
|
|
|
'domainid' => domain_id,
|
|
|
|
'hostid' => host_id,
|
|
|
|
'ipaddress' => ip_address,
|
|
|
|
'iptonetworklist' => ip_to_network_list,
|
2012-07-18 19:00:32 -04:00
|
|
|
'projectid' => project_id,
|
2012-05-16 14:12:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
options.merge!('networkids' => network_ids) if network_ids
|
2012-11-09 16:37:37 -05:00
|
|
|
options.merge!('securitygroupids' => security_group_ids) unless security_group_ids.empty?
|
2012-05-16 14:12:08 -04:00
|
|
|
|
2012-12-22 18:28:53 -05:00
|
|
|
data = service.deploy_virtual_machine(options)
|
2012-05-08 13:01:13 -04:00
|
|
|
merge_attributes(data['deployvirtualmachineresponse'])
|
|
|
|
end
|
|
|
|
|
2012-07-18 19:00:32 -04:00
|
|
|
def start
|
|
|
|
requires :id
|
2012-12-22 18:28:53 -05:00
|
|
|
data = service.start_virtual_machine("id" => self.id)
|
|
|
|
service.jobs.new(data["startvirtualmachineresponse"])
|
2012-05-16 14:12:08 -04:00
|
|
|
end
|
|
|
|
|
2012-07-18 19:00:32 -04:00
|
|
|
def stop(force=false)
|
2012-05-08 13:01:13 -04:00
|
|
|
requires :id
|
2012-12-22 18:28:53 -05:00
|
|
|
data = service.stop_virtual_machine("id" => self.id, "force" => force)
|
|
|
|
service.jobs.new(data["stopvirtualmachineresponse"])
|
2012-05-08 13:01:13 -04:00
|
|
|
end
|
|
|
|
end # Server
|
|
|
|
end # Cloudstack
|
|
|
|
end # Compute
|
|
|
|
end # Fog
|