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

70 lines
2.1 KiB
Ruby
Raw Normal View History

require 'fog/ecloud/models/compute/server'
module Fog
module Compute
class Ecloud
class Servers < Fog::Ecloud::Collection
2012-06-07 12:50:11 -04:00
model Fog::Compute::Ecloud::Server
2012-06-07 12:50:11 -04:00
identity :href
def all
2012-06-07 12:50:11 -04:00
data = connection.get_servers(href).body
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)
2012-06-07 12:50:11 -04:00
if data = connection.get_server(uri)
new(data.body)
end
rescue Fog::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 )
options[:cpus] ||= 1
options[:memory] ||= 512
options[:description] ||= ""
options[:tags] ||= []
if template_uri =~ /\/templates\/\d+/
options[:uri] = href + "/action/createVirtualMachine"
options[:customization] ||= :linux
options[:powered_on] ||= false
if options[:ips]
options[:ips] = options[:ips].is_a?(String) ? [options[:ips]] : options[:ips]
else
options[:network_uri] = options[:network_uri].is_a?(String) ? [options[:network_uri]] : options[:network_uri]
options[:network_uri].each do |uri|
index = options[:network_uri].index(uri)
ip = Fog::Compute::Ecloud::IpAddresses.new(:connection => connection, :href => uri).detect { |i| i.host == nil }.name
options[:ips] ||= []
options[:ips][index] = ip
end
end
data = connection.virtual_machine_create_from_template( template_uri, options ).body
else
2012-06-07 12:50:11 -04:00
options[:uri] = href + "/action/importVirtualMachine"
data = connection.virtual_machine_import( template_uri, options ).body
end
2012-06-07 12:50:11 -04:00
object = new(data)
object
end
end
end
end
end
2012-06-07 12:50:11 -04:00