2014-02-01 21:13:17 -05:00
|
|
|
require 'fog/glesys/core'
|
2014-02-01 13:07:49 -05:00
|
|
|
|
2011-08-10 09:03:51 -04:00
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Glesys < Fog::Service
|
|
|
|
requires :glesys_username, :glesys_api_key
|
|
|
|
|
|
|
|
API_URL = "https://api.glesys.com"
|
|
|
|
|
2011-09-08 17:07:04 -04:00
|
|
|
model_path 'fog/glesys/models/compute'
|
2011-08-10 09:03:51 -04:00
|
|
|
collection :servers
|
|
|
|
model :server
|
|
|
|
collection :templates
|
|
|
|
model :template
|
|
|
|
collection :ips
|
|
|
|
model :ip
|
2015-03-26 04:43:51 -04:00
|
|
|
collection :ssh_keys
|
|
|
|
model :ssh_key
|
2011-08-10 09:03:51 -04:00
|
|
|
|
2011-09-08 17:07:04 -04:00
|
|
|
request_path 'fog/glesys/requests/compute'
|
2013-02-01 14:36:44 -05:00
|
|
|
|
|
|
|
# Server
|
2011-08-10 09:03:51 -04:00
|
|
|
request :create
|
2015-03-06 04:33:36 -05:00
|
|
|
request :edit
|
2011-08-10 09:03:51 -04:00
|
|
|
request :destroy
|
|
|
|
request :list_servers
|
|
|
|
request :server_details
|
|
|
|
request :server_status
|
|
|
|
request :start
|
2012-06-12 08:54:07 -04:00
|
|
|
request :reboot
|
2011-08-10 09:03:51 -04:00
|
|
|
request :stop
|
2013-02-01 14:36:44 -05:00
|
|
|
|
2011-09-08 17:07:04 -04:00
|
|
|
# Templates
|
2011-08-10 09:03:51 -04:00
|
|
|
request :template_list
|
2013-02-01 14:36:44 -05:00
|
|
|
|
|
|
|
# IP
|
2011-08-10 09:03:51 -04:00
|
|
|
request :ip_list_free
|
|
|
|
request :ip_list_own
|
|
|
|
request :ip_details
|
|
|
|
request :ip_take
|
|
|
|
request :ip_release
|
|
|
|
request :ip_add
|
|
|
|
request :ip_remove
|
|
|
|
|
2015-03-26 04:43:51 -04:00
|
|
|
# SSH keys
|
|
|
|
request :ssh_key_list
|
|
|
|
request :ssh_key_add
|
|
|
|
request :ssh_key_remove
|
|
|
|
|
2011-08-10 09:03:51 -04:00
|
|
|
class Mock
|
2011-09-23 12:09:37 -04:00
|
|
|
def initialize(options={})
|
2014-03-25 11:09:49 -04:00
|
|
|
@api_url = options[:glesys_api_url] || API_URL
|
|
|
|
@glesys_username = options[:glesys_username]
|
|
|
|
@glesys_api_key = options[:glesys_api_key]
|
2011-09-23 12:09:37 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2011-09-08 16:33:32 -04:00
|
|
|
end
|
2011-08-10 09:03:51 -04:00
|
|
|
|
2011-09-23 12:09:37 -04:00
|
|
|
def self.data
|
|
|
|
@data ||= {
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.reset
|
2011-08-10 09:03:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
def initialize(options)
|
|
|
|
require 'base64'
|
|
|
|
|
2014-03-25 11:09:49 -04:00
|
|
|
@api_url = options[:glesys_api_url] || API_URL
|
|
|
|
@glesys_username = options[:glesys_username]
|
|
|
|
@glesys_api_key = options[:glesys_api_key]
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
@persistent = options[:persistent] || false
|
2014-02-26 19:50:35 -05:00
|
|
|
@connection = Fog::XML::Connection.new(@api_url, @persistent, @connection_options)
|
2011-08-10 09:03:51 -04:00
|
|
|
end
|
|
|
|
|
2014-02-01 13:07:49 -05:00
|
|
|
def request(method_name, options = {})
|
2011-08-10 09:03:51 -04:00
|
|
|
options.merge!( {:format => 'json'})
|
2012-02-20 14:36:39 -05:00
|
|
|
|
2011-08-10 09:03:51 -04:00
|
|
|
begin
|
|
|
|
parser = options.delete(:parser)
|
|
|
|
data = @connection.request(
|
|
|
|
:expects => 200,
|
|
|
|
:method => "POST",
|
|
|
|
:body => urlencode(options),
|
|
|
|
:parser => parser,
|
|
|
|
:path => method_name,
|
|
|
|
:headers => {
|
|
|
|
'Authorization' => "Basic #{encoded_api_auth}",
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2012-04-25 10:31:28 -04:00
|
|
|
data.body = Fog::JSON.decode(data.body)
|
2011-08-10 09:03:51 -04:00
|
|
|
|
2012-02-20 14:36:39 -05:00
|
|
|
response_code = data.body['response']['status']['code']
|
|
|
|
|
|
|
|
unless response_code.to_i == 200
|
2011-08-10 09:03:51 -04:00
|
|
|
raise Fog::Compute::Glesys::Error, "#{data.body['response']['status']['text']}"
|
|
|
|
end
|
|
|
|
data
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
|
|
|
Fog::Compute::Glesys::NotFound.slurp(error)
|
|
|
|
else
|
|
|
|
error
|
2014-02-01 13:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-08-10 09:03:51 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def encoded_api_auth
|
|
|
|
token = [@glesys_username, @glesys_api_key].join(':')
|
|
|
|
Base64.encode64(token).delete("\r\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def urlencode(hash)
|
2015-03-25 10:16:29 -04:00
|
|
|
hash.to_a.map! { |k, v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&")
|
2011-08-10 09:03:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|