2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/brightbox'
|
2011-08-31 16:52:53 -04:00
|
|
|
require 'fog/compute'
|
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Brightbox < Fog::Service
|
2010-11-08 07:21:31 -05:00
|
|
|
|
|
|
|
API_URL = "https://api.gb1.brightbox.com/"
|
|
|
|
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :brightbox_client_id, :brightbox_secret
|
2011-09-20 07:04:47 -04:00
|
|
|
recognizes :brightbox_auth_url, :brightbox_api_url, :persistent
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-08-24 21:31:12 -04:00
|
|
|
model_path 'fog/brightbox/models/compute'
|
2011-01-07 19:52:09 -05:00
|
|
|
model :account # Singular resource, no collection
|
|
|
|
collection :servers
|
|
|
|
model :server
|
2011-08-18 12:16:58 -04:00
|
|
|
collection :server_groups
|
|
|
|
model :server_group
|
2011-09-23 11:15:01 -04:00
|
|
|
collection :firewall_policies
|
|
|
|
model :firewall_policy
|
|
|
|
collection :firewall_rules
|
|
|
|
model :firewall_rule
|
2011-01-07 19:52:09 -05:00
|
|
|
collection :flavors
|
|
|
|
model :flavor
|
|
|
|
collection :images
|
|
|
|
model :image
|
|
|
|
collection :load_balancers
|
|
|
|
model :load_balancer
|
|
|
|
collection :zones
|
|
|
|
model :zone
|
|
|
|
collection :cloud_ips
|
|
|
|
model :cloud_ip
|
|
|
|
collection :users
|
|
|
|
model :user
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-08-24 21:31:12 -04:00
|
|
|
request_path 'fog/brightbox/requests/compute'
|
2011-03-01 11:13:11 -05:00
|
|
|
request :activate_console_server
|
2011-03-01 11:12:04 -05:00
|
|
|
request :add_listeners_load_balancer
|
2010-12-15 13:05:33 -05:00
|
|
|
request :add_nodes_load_balancer
|
2011-08-17 12:03:45 -04:00
|
|
|
request :add_servers_server_group
|
2011-08-17 12:04:52 -04:00
|
|
|
request :apply_to_firewall_policy
|
2011-10-20 08:49:40 -04:00
|
|
|
request :remove_firewall_policy
|
2010-11-08 07:21:31 -05:00
|
|
|
request :create_api_client
|
|
|
|
request :create_cloud_ip
|
2011-08-17 12:04:52 -04:00
|
|
|
request :create_firewall_policy
|
|
|
|
request :create_firewall_rule
|
2010-11-08 07:21:31 -05:00
|
|
|
request :create_image
|
2010-12-02 05:07:43 -05:00
|
|
|
request :create_load_balancer
|
2010-11-08 07:21:31 -05:00
|
|
|
request :create_server
|
2011-08-17 12:03:45 -04:00
|
|
|
request :create_server_group
|
2010-11-08 07:21:31 -05:00
|
|
|
request :destroy_api_client
|
|
|
|
request :destroy_cloud_ip
|
2011-08-17 12:04:52 -04:00
|
|
|
request :destroy_firewall_policy
|
|
|
|
request :destroy_firewall_rule
|
2010-11-08 07:21:31 -05:00
|
|
|
request :destroy_image
|
2010-12-02 05:07:43 -05:00
|
|
|
request :destroy_load_balancer
|
2010-11-08 07:21:31 -05:00
|
|
|
request :destroy_server
|
2011-08-17 12:03:45 -04:00
|
|
|
request :destroy_server_group
|
2010-11-08 07:21:31 -05:00
|
|
|
request :get_account
|
|
|
|
request :get_api_client
|
|
|
|
request :get_cloud_ip
|
2011-08-17 12:04:52 -04:00
|
|
|
request :get_firewall_policy
|
|
|
|
request :get_firewall_rule
|
2010-11-08 07:21:31 -05:00
|
|
|
request :get_image
|
|
|
|
request :get_interface
|
2010-12-02 05:07:43 -05:00
|
|
|
request :get_load_balancer
|
2010-11-08 07:21:31 -05:00
|
|
|
request :get_server
|
2011-08-17 12:03:45 -04:00
|
|
|
request :get_server_group
|
2010-11-08 07:21:31 -05:00
|
|
|
request :get_server_type
|
|
|
|
request :get_user
|
|
|
|
request :get_zone
|
|
|
|
request :list_api_clients
|
|
|
|
request :list_cloud_ips
|
2011-08-17 12:04:52 -04:00
|
|
|
request :list_firewall_policies
|
2010-11-08 07:21:31 -05:00
|
|
|
request :list_images
|
2010-12-02 05:07:43 -05:00
|
|
|
request :list_load_balancers
|
2011-08-17 12:03:45 -04:00
|
|
|
request :list_server_groups
|
2010-11-08 07:21:31 -05:00
|
|
|
request :list_server_types
|
|
|
|
request :list_servers
|
|
|
|
request :list_users
|
|
|
|
request :list_zones
|
|
|
|
request :map_cloud_ip
|
2011-08-17 12:03:45 -04:00
|
|
|
request :move_servers_server_group
|
2011-03-01 11:12:04 -05:00
|
|
|
request :remove_listeners_load_balancer
|
2010-12-15 13:05:33 -05:00
|
|
|
request :remove_nodes_load_balancer
|
2011-08-17 12:03:45 -04:00
|
|
|
request :remove_servers_server_group
|
2010-11-08 07:21:31 -05:00
|
|
|
request :reset_ftp_password_account
|
2011-09-29 05:17:00 -04:00
|
|
|
request :reset_secret_api_client
|
2010-11-08 07:21:31 -05:00
|
|
|
request :shutdown_server
|
|
|
|
request :snapshot_server
|
|
|
|
request :start_server
|
|
|
|
request :stop_server
|
|
|
|
request :unmap_cloud_ip
|
|
|
|
request :update_account
|
|
|
|
request :update_api_client
|
2011-09-26 09:00:03 -04:00
|
|
|
request :update_cloud_ip
|
2011-12-26 07:27:19 -05:00
|
|
|
request :update_firewall_rule
|
2010-11-08 07:21:31 -05:00
|
|
|
request :update_image
|
2010-12-15 13:05:33 -05:00
|
|
|
request :update_load_balancer
|
2010-11-08 07:21:31 -05:00
|
|
|
request :update_server
|
2011-08-17 12:03:45 -04:00
|
|
|
request :update_server_group
|
2010-11-08 07:21:31 -05:00
|
|
|
request :update_user
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
2010-11-16 16:24:43 -05:00
|
|
|
def initialize(options)
|
|
|
|
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
|
|
|
|
@brightbox_secret = options[:brightbox_secret] || Fog.credentials[:brightbox_secret]
|
|
|
|
end
|
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
def request(options)
|
|
|
|
raise "Not implemented"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
# Currently authentication and api endpoints are the same but may change
|
2011-09-12 11:01:48 -04:00
|
|
|
@auth_url = options[:brightbox_auth_url] || Fog.credentials[:brightbox_auth_url] || API_URL
|
|
|
|
@api_url = options[:brightbox_api_url] || Fog.credentials[:brightbox_api_url] || API_URL
|
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
|
|
|
|
@brightbox_secret = options[:brightbox_secret] || Fog.credentials[:brightbox_secret]
|
2011-09-19 12:29:17 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2011-09-12 11:01:48 -04:00
|
|
|
@connection = Fog::Connection.new(@api_url, @persistent, @connection_options)
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
2011-08-16 12:12:39 -04:00
|
|
|
def request(method, url, expected_responses, options = nil)
|
|
|
|
request_options = {
|
|
|
|
:method => method.to_s.upcase,
|
|
|
|
:path => url,
|
|
|
|
:expects => expected_responses
|
|
|
|
}
|
2012-04-25 10:31:28 -04:00
|
|
|
request_options[:body] = Fog::JSON.encode(options) unless options.nil?
|
2011-08-16 12:12:39 -04:00
|
|
|
make_request(request_options)
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def account
|
2011-06-16 19:28:54 -04:00
|
|
|
Fog::Compute::Brightbox::Account.new(get_account)
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def get_oauth_token(options = {})
|
|
|
|
auth_url = options[:brightbox_auth_url] || @auth_url
|
|
|
|
|
|
|
|
connection = Fog::Connection.new(auth_url)
|
2012-04-25 10:31:28 -04:00
|
|
|
@authentication_body = Fog::JSON.encode({'client_id' => @brightbox_client_id, 'grant_type' => 'none'})
|
2010-11-08 07:21:31 -05:00
|
|
|
|
|
|
|
response = connection.request({
|
|
|
|
:path => "/token",
|
|
|
|
:expects => 200,
|
|
|
|
:headers => {
|
|
|
|
'Authorization' => "Basic " + Base64.encode64("#{@brightbox_client_id}:#{@brightbox_secret}").chomp,
|
|
|
|
'Content-Type' => 'application/json'
|
|
|
|
},
|
|
|
|
:method => 'POST',
|
|
|
|
:body => @authentication_body
|
|
|
|
})
|
2012-04-25 10:31:28 -04:00
|
|
|
@oauth_token = Fog::JSON.decode(response.body)["access_token"]
|
2010-11-08 07:21:31 -05:00
|
|
|
return @oauth_token
|
|
|
|
end
|
|
|
|
|
2011-08-16 12:12:39 -04:00
|
|
|
def make_request(params)
|
|
|
|
begin
|
|
|
|
get_oauth_token if @oauth_token.nil?
|
|
|
|
response = authenticated_request(params)
|
2012-05-14 15:26:53 -04:00
|
|
|
rescue Excon::Errors::Unauthorized
|
2011-08-16 12:12:39 -04:00
|
|
|
get_oauth_token
|
|
|
|
response = authenticated_request(params)
|
|
|
|
end
|
|
|
|
unless response.body.empty?
|
2012-04-25 10:31:28 -04:00
|
|
|
response = Fog::JSON.decode(response.body)
|
2011-08-16 12:12:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
def authenticated_request(options)
|
|
|
|
headers = options[:headers] || {}
|
2011-08-16 12:12:39 -04:00
|
|
|
headers.merge!("Authorization" => "OAuth #{@oauth_token}", "Content-Type" => "application/json")
|
2010-11-08 07:21:31 -05:00
|
|
|
options[:headers] = headers
|
|
|
|
@connection.request(options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-07 19:52:09 -05:00
|
|
|
end
|