2011-08-24 21:29:14 -04:00
|
|
|
require 'fog/ecloud/models/compute/server'
|
2011-02-17 13:44:46 -05:00
|
|
|
|
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Ecloud
|
2011-02-17 13:44:46 -05:00
|
|
|
class Servers < Fog::Ecloud::Collection
|
2012-11-27 19:57:16 -05:00
|
|
|
|
2011-06-16 19:28:54 -04:00
|
|
|
model Fog::Compute::Ecloud::Server
|
2011-02-17 13:44:46 -05:00
|
|
|
|
2012-06-07 12:50:11 -04:00
|
|
|
identity :href
|
2011-02-17 13:44:46 -05:00
|
|
|
|
|
|
|
def all
|
2012-12-22 18:27:38 -05:00
|
|
|
data = service.get_servers(href).body
|
2012-06-07 12:50:11 -04:00
|
|
|
if data.keys.include?(:VirtualMachines)
|
|
|
|
data = data[:VirtualMachines][:VirtualMachine]
|
2012-09-27 11:37:21 -04:00
|
|
|
elsif data[:VirtualMachine]
|
2012-06-07 12:50:11 -04:00
|
|
|
data = data[:VirtualMachine]
|
2012-09-27 11:37:21 -04:00
|
|
|
else
|
|
|
|
data = []
|
2012-06-07 12:50:11 -04:00
|
|
|
end
|
|
|
|
load(data)
|
2011-02-17 13:44:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get(uri)
|
2012-12-22 18:27:38 -05:00
|
|
|
data = service.get_server(uri).body
|
2013-02-04 13:25:34 -05:00
|
|
|
new(data)
|
|
|
|
rescue Fog::Errors::NotFound
|
2011-02-17 13:44:46 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2012-06-07 12:50:11 -04:00
|
|
|
def from_data(data)
|
|
|
|
new(data)
|
2011-02-17 13:44:46 -05:00
|
|
|
end
|
|
|
|
|
2012-06-07 12:50:11 -04:00
|
|
|
def create( template_uri, options )
|
2012-11-27 19:57:16 -05:00
|
|
|
options[:cpus] ||= 1
|
|
|
|
options[:memory] ||= 512
|
2012-06-07 12:50:11 -04:00
|
|
|
options[:description] ||= ""
|
2012-11-27 19:57:16 -05:00
|
|
|
options[:tags] ||= []
|
|
|
|
|
2012-06-07 12:50:11 -04:00
|
|
|
if template_uri =~ /\/templates\/\d+/
|
|
|
|
options[:uri] = href + "/action/createVirtualMachine"
|
|
|
|
options[:customization] ||= :linux
|
|
|
|
options[:powered_on] ||= false
|
|
|
|
if options[:ips]
|
2012-11-27 19:57:16 -05:00
|
|
|
options[:ips] = [*options[:ips]]
|
2012-06-07 12:50:11 -04:00
|
|
|
else
|
2012-11-27 19:57:16 -05:00
|
|
|
[*options[:network_uri]].each do |uri|
|
2012-06-07 12:50:11 -04:00
|
|
|
index = options[:network_uri].index(uri)
|
2012-12-22 18:27:38 -05:00
|
|
|
ip = self.service.ip_addresses(:href => uri).detect { |i| i.host == nil && i.detected_on.nil? }.name
|
2012-06-07 12:50:11 -04:00
|
|
|
options[:ips] ||= []
|
|
|
|
options[:ips][index] = ip
|
|
|
|
end
|
|
|
|
end
|
2012-12-22 18:27:38 -05:00
|
|
|
data = service.virtual_machine_create_from_template( template_uri, options ).body
|
2011-02-17 13:44:46 -05:00
|
|
|
else
|
2012-06-07 12:50:11 -04:00
|
|
|
options[:uri] = href + "/action/importVirtualMachine"
|
2012-12-22 18:27:38 -05:00
|
|
|
data = service.virtual_machine_import( template_uri, options ).body
|
2011-02-17 13:44:46 -05:00
|
|
|
end
|
2012-12-22 18:27:38 -05:00
|
|
|
object = self.service.servers.new(data)
|
2012-06-07 12:50:11 -04:00
|
|
|
object
|
2011-02-17 13:44:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-07 12:50:11 -04:00
|
|
|
|