mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
72 lines
2 KiB
Ruby
72 lines
2 KiB
Ruby
require 'fog/ecloud/models/compute/server'
|
|
|
|
module Fog
|
|
module Compute
|
|
class Ecloud
|
|
class Servers < Fog::Ecloud::Collection
|
|
|
|
model Fog::Compute::Ecloud::Server
|
|
|
|
identity :href
|
|
|
|
def all
|
|
data = connection.get_servers(href).body
|
|
if data.keys.include?(:VirtualMachines)
|
|
data = data[:VirtualMachines][:VirtualMachine]
|
|
elsif data[:VirtualMachine]
|
|
data = data[:VirtualMachine]
|
|
else
|
|
data = []
|
|
end
|
|
load(data)
|
|
end
|
|
|
|
def get(uri)
|
|
data = connection.get_server(uri).body
|
|
if data == ""
|
|
new({})
|
|
else
|
|
new(data)
|
|
end
|
|
rescue Excon::Errors::NotFound
|
|
nil
|
|
end
|
|
|
|
def from_data(data)
|
|
new(data)
|
|
end
|
|
|
|
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]]
|
|
else
|
|
[*options[:network_uri]].each do |uri|
|
|
index = options[:network_uri].index(uri)
|
|
ip = self.connection.ip_addresses(:href => uri).detect { |i| i.host == nil && i.detected_on.nil? }.name
|
|
options[:ips] ||= []
|
|
options[:ips][index] = ip
|
|
end
|
|
end
|
|
data = connection.virtual_machine_create_from_template( template_uri, options ).body
|
|
else
|
|
options[:uri] = href + "/action/importVirtualMachine"
|
|
data = connection.virtual_machine_import( template_uri, options ).body
|
|
end
|
|
object = self.connection.servers.new(data)
|
|
object
|
|
end
|
|
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|