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/compute/new_servers.rb

104 lines
3 KiB
Ruby
Raw Normal View History

2010-10-04 17:02:08 -04:00
require 'fog/core/parser'
module Fog
module NewServers
class Compute < Fog::Service
requires :new_servers_password, :new_servers_username
recognizes :host, :port, :scheme, :persistent
2011-01-07 21:14:39 -05:00
recognizes :provider # remove post deprecation
2011-01-07 19:52:09 -05:00
model_path 'fog/compute/models/new_servers'
2011-01-07 19:52:09 -05:00
request_path 'fog/compute/requests/new_servers'
request :add_server
request :cancel_server
request :get_server
request :list_images
request :list_plans
request :list_servers
request :reboot_server
class Mock
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
2011-01-07 21:14:39 -05:00
unless options.delete(:provider)
location = caller.first
warning = "[yellow][WARN] Fog::NewServers::Compute.new is deprecated, use Fog::Compute.new(:provider => 'NewServers') instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
end
@new_server_username = options[:new_servers_username]
@data = self.class.data[@new_server_username]
end
end
class Real
def initialize(options={})
2011-01-07 21:14:39 -05:00
unless options.delete(:provider)
location = caller.first
warning = "[yellow][WARN] Fog::NewServers::Compute.new is deprecated, use Fog::Compute.new(:provider => 'NewServers') instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
end
@new_servers_password = options[:new_servers_password]
@new_servers_username = options[:new_servers_username]
@host = options[:host] || "noc.newservers.com"
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
def reload
@connection.reset
end
def request(params)
params[:query] ||= {}
params[:query].merge!({
:password => @new_servers_password,
:username => @new_servers_username
})
params[:headers] ||= {}
case params[:method]
when 'DELETE', 'GET', 'HEAD'
params[:headers]['Accept'] = 'application/xml'
when 'POST', 'PUT'
params[:headers]['Content-Type'] = 'application/xml'
end
begin
response = @connection.request(params.merge!({:host => @host}))
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
Fog::NewServers::Compute::NotFound.slurp(error)
else
error
end
end
response
end
end
end
end
end