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/"
|
|
|
|
|
2012-07-27 06:29:43 -04:00
|
|
|
# Client credentials
|
2010-12-16 18:31:24 -05:00
|
|
|
requires :brightbox_client_id, :brightbox_secret
|
2012-07-27 06:29:43 -04:00
|
|
|
|
|
|
|
# API endpoint settings
|
|
|
|
recognizes :brightbox_auth_url, :brightbox_api_url
|
|
|
|
|
|
|
|
# User credentials (still requires client details)
|
|
|
|
recognizes :brightbox_username, :brightbox_password, :brightbox_account
|
|
|
|
|
|
|
|
# Excon connection settings
|
|
|
|
recognizes :persistent
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-08-24 21:31:12 -04:00
|
|
|
model_path 'fog/brightbox/models/compute'
|
2012-04-25 07:57:15 -04:00
|
|
|
collection :accounts
|
|
|
|
model :account
|
2012-07-27 07:06:28 -04:00
|
|
|
collection :applications
|
|
|
|
model :application
|
2012-09-21 08:42:40 -04:00
|
|
|
collection :api_clients
|
|
|
|
model :api_client
|
2011-01-07 19:52:09 -05:00
|
|
|
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
|
2012-07-27 07:06:28 -04:00
|
|
|
request :create_application
|
2010-11-08 07:21:31 -05:00
|
|
|
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
|
2012-07-27 07:06:28 -04:00
|
|
|
request :destroy_application
|
2010-11-08 07:21:31 -05:00
|
|
|
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
|
2012-07-27 07:06:28 -04:00
|
|
|
request :get_application
|
2012-11-02 06:31:27 -04:00
|
|
|
request :get_authenticated_user
|
2010-11-08 07:21:31 -05:00
|
|
|
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
|
2012-11-02 06:31:27 -04:00
|
|
|
request :get_scoped_account
|
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
|
2012-04-25 07:57:15 -04:00
|
|
|
request :list_accounts
|
2010-11-08 07:21:31 -05:00
|
|
|
request :list_api_clients
|
2012-07-27 07:06:28 -04:00
|
|
|
request :list_applications
|
2010-11-08 07:21:31 -05:00
|
|
|
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
|
2012-07-27 07:06:28 -04:00
|
|
|
request :reset_secret_application
|
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
|
2012-07-27 07:06:28 -04:00
|
|
|
request :update_application
|
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
|
2012-11-02 14:52:07 -04:00
|
|
|
request :update_scoped_account
|
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
|
|
|
|
|
2012-08-15 07:27:09 -04:00
|
|
|
module Shared
|
|
|
|
# Returns an identifier for the default image for use
|
|
|
|
#
|
|
|
|
# Currently tries to find the latest version Ubuntu LTS (i686) widening
|
|
|
|
# up to the latest, official version of Ubuntu available.
|
|
|
|
#
|
|
|
|
# Highly recommended that you actually select the image you want to run
|
|
|
|
# on your servers yourself!
|
|
|
|
#
|
|
|
|
# @return [String, nil]
|
|
|
|
def default_image
|
|
|
|
return @default_image_id unless @default_image_id.nil?
|
|
|
|
@default_image_id = Fog.credentials[:brightbox_default_image] || select_default_image
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
class Mock
|
2012-08-15 07:27:09 -04:00
|
|
|
include Shared
|
2010-11-08 07:21:31 -05:00
|
|
|
|
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
|
2012-08-15 07:27:09 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def select_default_image
|
|
|
|
"img-mockd"
|
|
|
|
end
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2012-08-15 07:27:09 -04:00
|
|
|
include Shared
|
2010-11-08 07:21:31 -05:00
|
|
|
|
|
|
|
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]
|
2012-07-27 06:29:43 -04:00
|
|
|
@brightbox_username = options[:brightbox_username] || Fog.credentials[:brightbox_username]
|
|
|
|
@brightbox_password = options[:brightbox_password] || Fog.credentials[:brightbox_password]
|
|
|
|
@brightbox_account = options[:brightbox_account] || Fog.credentials[:brightbox_account]
|
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
|
|
|
|
|
2012-07-27 06:29:43 -04:00
|
|
|
def request(method, url, expected_responses, options = {})
|
2011-08-16 12:12:39 -04:00
|
|
|
request_options = {
|
|
|
|
:method => method.to_s.upcase,
|
|
|
|
:path => url,
|
|
|
|
:expects => expected_responses
|
|
|
|
}
|
2012-07-27 06:29:43 -04:00
|
|
|
options[:account_id] = @brightbox_account if options[:account_id].nil? && @brightbox_account
|
|
|
|
request_options[:body] = Fog::JSON.encode(options) unless options.empty?
|
2011-08-16 12:12:39 -04:00
|
|
|
make_request(request_options)
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
|
2012-11-02 06:31:27 -04:00
|
|
|
# Returns the scoped account being used for requests
|
|
|
|
#
|
2012-11-15 08:48:32 -05:00
|
|
|
# * For API clients this is the owning account
|
|
|
|
# * For User applications this is the account specified by either +account_id+
|
|
|
|
# option on a connection or the +brightbox_account+ setting in your configuration
|
2012-11-02 06:31:27 -04:00
|
|
|
#
|
2012-11-15 08:48:32 -05:00
|
|
|
# @return [Fog::Compute::Brightbox::Account]
|
2012-11-02 06:31:27 -04:00
|
|
|
#
|
2010-11-08 07:21:31 -05:00
|
|
|
def account
|
2012-11-02 06:31:27 -04:00
|
|
|
Fog::Compute::Brightbox::Account.new(get_scoped_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-07-27 06:29:43 -04:00
|
|
|
authentication_body_hash = if @brightbox_username && @brightbox_password
|
|
|
|
{
|
|
|
|
'client_id' => @brightbox_client_id,
|
|
|
|
'grant_type' => 'password',
|
|
|
|
'username' => @brightbox_username,
|
|
|
|
'password' => @brightbox_password
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{'client_id' => @brightbox_client_id, 'grant_type' => 'none'}
|
|
|
|
end
|
|
|
|
@authentication_body = Fog::JSON.encode(authentication_body_hash)
|
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
|
2012-08-15 07:27:09 -04:00
|
|
|
|
|
|
|
# Queries the API and tries to select the most suitable official Image
|
|
|
|
# to use if the user chooses not to select their own.
|
|
|
|
def select_default_image
|
|
|
|
return @default_image_id unless @default_image_id.nil?
|
|
|
|
|
2012-10-19 04:26:21 -04:00
|
|
|
all_images = list_images
|
2012-08-15 07:27:09 -04:00
|
|
|
official_images = all_images.select {|img| img["official"] == true}
|
|
|
|
ubuntu_lts_images = official_images.select {|img| img["name"] =~ /Ubuntu.*LTS/}
|
|
|
|
ubuntu_lts_i686_images = ubuntu_lts_images.select {|img| img["arch"] == "i686"}
|
|
|
|
|
|
|
|
if ubuntu_lts_i686_images.empty?
|
|
|
|
# Accept other architectures
|
|
|
|
if ubuntu_lts_images.empty?
|
|
|
|
# Accept non-LTS versions of Ubuntu
|
|
|
|
unsorted_images = official_images.select {|img| img["name"] =~ /Ubuntu/}
|
|
|
|
else
|
|
|
|
unsorted_images = ubuntu_lts_images
|
|
|
|
end
|
|
|
|
else
|
|
|
|
unsorted_images = ubuntu_lts_i686_images
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the latest and use it's ID for the default image
|
|
|
|
@default_image_id = unsorted_images.sort {|a,b| a["created_at"] <=> b["created_at"]}.first["id"]
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
2012-04-12 08:02:59 -04:00
|
|
|
|
2010-11-08 07:21:31 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-07 19:52:09 -05:00
|
|
|
end
|