2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/brightbox'
|
2011-08-31 16:52:53 -04:00
|
|
|
require 'fog/compute'
|
2013-06-24 08:48:06 -04:00
|
|
|
require 'fog/brightbox/compute/shared'
|
2013-05-02 04:30:48 -04:00
|
|
|
require 'fog/brightbox/compute/image_selector'
|
2011-08-31 16:52:53 -04:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2012-11-21 09:31:04 -05:00
|
|
|
# Cached tokens
|
|
|
|
recognizes :brightbox_access_token, :brightbox_refresh_token
|
|
|
|
|
2012-11-27 05:11:03 -05:00
|
|
|
# Automatic token management
|
|
|
|
recognizes :brightbox_token_management
|
|
|
|
|
2012-07-27 06:29:43 -04:00
|
|
|
# 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
|
2012-10-19 11:38:44 -04:00
|
|
|
collection :collaborations
|
|
|
|
model :collaboration
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
collection :user_collaborations
|
|
|
|
model :user_collaboration
|
2010-11-08 07:21:31 -05:00
|
|
|
|
2011-08-24 21:31:12 -04:00
|
|
|
request_path 'fog/brightbox/requests/compute'
|
2012-10-19 11:38:44 -04:00
|
|
|
request :accept_user_collaboration
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :accept_user_collaboration
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :create_collaboration
|
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
|
2013-05-14 05:10:17 -04:00
|
|
|
request :delete_api_client
|
|
|
|
request :delete_application
|
|
|
|
request :delete_cloud_ip
|
2012-10-19 11:38:44 -04:00
|
|
|
request :delete_collaboration
|
2013-05-14 05:10:17 -04:00
|
|
|
request :delete_firewall_policy
|
|
|
|
request :delete_firewall_rule
|
|
|
|
request :delete_image
|
|
|
|
request :delete_load_balancer
|
|
|
|
request :delete_server
|
|
|
|
request :delete_server_group
|
2012-10-19 11:38:44 -04:00
|
|
|
request :delete_user_collaboration
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :get_collaboration
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :get_user_collaboration
|
2010-11-08 07:21:31 -05:00
|
|
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :list_collaborations
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :list_user_collaborations
|
2010-11-08 07:21:31 -05:00
|
|
|
request :list_zones
|
|
|
|
request :map_cloud_ip
|
2011-08-17 12:03:45 -04:00
|
|
|
request :move_servers_server_group
|
2012-10-19 11:38:44 -04:00
|
|
|
request :reject_user_collaboration
|
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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :resend_collaboration
|
2010-11-08 07:21:31 -05:00
|
|
|
request :reset_ftp_password_account
|
2012-11-20 04:59:49 -05:00
|
|
|
request :reset_ftp_password_scoped_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
|
2012-10-19 11:38:44 -04:00
|
|
|
request :resend_collaboration
|
|
|
|
request :reject_user_collaboration
|
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
|
2012-12-17 05:16:59 -05:00
|
|
|
request :update_firewall_policy
|
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-11-29 10:47:32 -05:00
|
|
|
# The Mock Service allows you to run a fake instance of the Service
|
|
|
|
# which makes no real connections.
|
|
|
|
#
|
|
|
|
# @todo Implement
|
|
|
|
#
|
|
|
|
class Mock
|
2013-06-24 08:48:06 -04:00
|
|
|
include Fog::Brightbox::Compute::Shared
|
2012-11-29 10:47:32 -05:00
|
|
|
|
|
|
|
def request(method, path, expected_responses, parameters = {})
|
|
|
|
_request
|
|
|
|
end
|
|
|
|
|
|
|
|
def request_access_token(connection, credentials)
|
|
|
|
_request
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def _request
|
|
|
|
raise Fog::Errors::MockNotImplemented
|
|
|
|
end
|
|
|
|
|
|
|
|
def select_default_image
|
|
|
|
"img-mockd"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# The Real Service actually makes real connections to the Brightbox
|
|
|
|
# service.
|
|
|
|
#
|
|
|
|
class Real
|
2013-06-24 08:48:06 -04:00
|
|
|
include Fog::Brightbox::Compute::Shared
|
2012-11-29 10:47:32 -05:00
|
|
|
|
|
|
|
# Makes an API request to the given path using passed options or those
|
|
|
|
# set with the service setup
|
|
|
|
#
|
|
|
|
# @todo Standard Fog behaviour is to return the Excon::Response but
|
|
|
|
# this was unintentionally changed to be the Hash version of the
|
|
|
|
# data in the body. This loses access to some details and should
|
|
|
|
# be corrected in a backwards compatible manner
|
|
|
|
#
|
2012-12-07 12:21:26 -05:00
|
|
|
# @overload request(params)
|
|
|
|
# @param [Hash] params Excon compatible options
|
|
|
|
# @option params [String] :body text to be sent over a socket
|
|
|
|
# @option params [Hash<Symbol, String>] :headers The default headers to supply in a request
|
|
|
|
# @option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
|
|
|
|
# @option params [String] :path appears after 'scheme://host:port/'
|
|
|
|
# @option params [Fixnum] :port The port on which to connect, to the destination host
|
|
|
|
# @option params [Hash] :query appended to the 'scheme://host:port/path/' in the form of '?key=value'
|
|
|
|
# @option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
|
|
|
|
# @return [Excon::Response]
|
|
|
|
# @see https://github.com/geemus/excon/blob/master/lib/excon/connection.rb
|
|
|
|
#
|
|
|
|
# @overload request(method, path, expected_responses, params = {})
|
|
|
|
# @param [String] method HTTP method to use for the request
|
|
|
|
# @param [String] path The absolute path for the request
|
|
|
|
# @param [Array<Fixnum>] expected_responses HTTP response codes that have been successful
|
|
|
|
# @param [Hash] params Keys and values for JSON
|
|
|
|
# @option params [String] :account_id The scoping account if required
|
|
|
|
# @deprecated #request with multiple arguments is deprecated
|
|
|
|
# since it is inconsistent with original fog version.
|
|
|
|
# @return [Hash]
|
|
|
|
def request(*args)
|
|
|
|
if args.size == 1
|
|
|
|
authenticated_request(*args)
|
|
|
|
else
|
|
|
|
Fog::Logger.deprecation("#request with multiple parameters is deprecated, use #wrapped_request instead [light_black](#{caller.first})[/]")
|
|
|
|
wrapped_request(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Makes a request but with seperated arguments and parses the response to a hash
|
|
|
|
#
|
|
|
|
# @note #wrapped_request is the non-standard form of request introduced by mistake
|
|
|
|
#
|
2012-11-29 10:47:32 -05:00
|
|
|
# @param [String] method HTTP method to use for the request
|
|
|
|
# @param [String] path The absolute path for the request
|
|
|
|
# @param [Array<Fixnum>] expected_responses HTTP response codes that have been successful
|
|
|
|
# @param [Hash] parameters Keys and values for JSON
|
|
|
|
# @option parameters [String] :account_id The scoping account if required
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
2012-12-07 12:21:26 -05:00
|
|
|
def wrapped_request(method, path, expected_responses, parameters = {})
|
|
|
|
_wrapped_request(method, path, expected_responses, parameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Wrapped request is the non-standard form of request introduced by mistake
|
|
|
|
#
|
|
|
|
# @param [String] method HTTP method to use for the request
|
|
|
|
# @param [String] path The absolute path for the request
|
|
|
|
# @param [Array<Fixnum>] expected_responses HTTP response codes that have been successful
|
|
|
|
# @param [Hash] parameters Keys and values for JSON
|
|
|
|
# @option parameters [String] :account_id The scoping account if required
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
def _wrapped_request(method, path, expected_responses, parameters = {})
|
2012-11-29 10:47:32 -05:00
|
|
|
request_options = {
|
|
|
|
:method => method.to_s.upcase,
|
|
|
|
:path => path,
|
|
|
|
:expects => expected_responses
|
|
|
|
}
|
|
|
|
|
|
|
|
# Select the account to scope for this request
|
|
|
|
account = scoped_account(parameters.fetch(:account_id, nil))
|
|
|
|
if account
|
|
|
|
request_options[:query] = { :account_id => account }
|
|
|
|
end
|
|
|
|
|
|
|
|
request_options[:body] = Fog::JSON.encode(parameters) unless parameters.empty?
|
|
|
|
|
|
|
|
response = make_request(request_options)
|
|
|
|
|
|
|
|
# FIXME We should revert to returning the Excon::Request after a suitable
|
|
|
|
# configuration option is in place to switch back to this incorrect behaviour
|
|
|
|
unless response.body.empty?
|
|
|
|
Fog::JSON.decode(response.body)
|
|
|
|
else
|
|
|
|
response
|
|
|
|
end
|
|
|
|
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.
|
2013-05-02 04:30:48 -04:00
|
|
|
#
|
|
|
|
# @return [String] if image is found, the image's identifier
|
|
|
|
# @return [NilClass] if no image found or an error occured
|
|
|
|
#
|
2012-08-15 07:27:09 -04:00
|
|
|
def select_default_image
|
2013-05-02 04:30:48 -04:00
|
|
|
Fog::Brightbox::Compute::ImageSelector.new(list_images).latest_ubuntu
|
2012-08-15 07:27:09 -04:00
|
|
|
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
|