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/ecloud/models/compute/servers.rb

73 lines
2 KiB
Ruby
Raw Normal View History

require 'fog/ecloud/models/compute/server'
module Fog
module Compute
class Ecloud
class Servers < Fog::Ecloud::Collection
2012-11-27 19:57:16 -05:00
model Fog::Compute::Ecloud::Server
2012-06-07 12:50:11 -04:00
identity :href
def all
data = service.get_servers(href).body
2012-06-07 12:50:11 -04:00
if data.keys.include?(:VirtualMachines)
data = data[:VirtualMachines][:VirtualMachine]
elsif data[:VirtualMachine]
2012-06-07 12:50:11 -04:00
data = data[:VirtualMachine]
else
data = []
2012-06-07 12:50:11 -04:00
end
load(data)
end
def get(uri)
data = service.get_server(uri).body
2012-11-27 19:57:16 -05:00
if data == ""
new({})
else
new(data)
end
2012-11-27 19:57:16 -05:00
rescue Excon::Errors::NotFound
nil
end
2012-06-07 12:50:11 -04:00
def from_data(data)
new(data)
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)
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
data = service.virtual_machine_create_from_template( template_uri, options ).body
else
2012-06-07 12:50:11 -04:00
options[:uri] = href + "/action/importVirtualMachine"
data = service.virtual_machine_import( template_uri, options ).body
end
object = self.service.servers.new(data)
2012-06-07 12:50:11 -04:00
object
end
end
end
end
end
2012-06-07 12:50:11 -04:00