From 0c259286913f6dd0f3476f2ce619756f709a1519 Mon Sep 17 00:00:00 2001 From: Paul Thornthwaite Date: Thu, 29 Nov 2012 15:47:32 +0000 Subject: [PATCH] [Brightbox] Moves more of public API into Shared Moved a lot of helper methods to Shared so that the Mock version of the service is not erroring with missing methods even if requests are not implemented. --- lib/fog/brightbox/compute.rb | 167 +++++++++++++++++++---------------- 1 file changed, 91 insertions(+), 76 deletions(-) diff --git a/lib/fog/brightbox/compute.rb b/lib/fog/brightbox/compute.rb index 5cb6e2953..ef88d8b33 100644 --- a/lib/fog/brightbox/compute.rb +++ b/lib/fog/brightbox/compute.rb @@ -134,41 +134,6 @@ module Fog request :update_user 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 - - class Mock - include Shared - - 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 - - def request(options) - raise "Not implemented" - end - - private - def select_default_image - "img-mockd" - end - end - - class Real - include Shared include Fog::Brightbox::OAuth2 # Creates a new instance of the Brightbox Compute service @@ -219,47 +184,6 @@ module Fog @token_management = options.fetch(:brightbox_token_management, true) end - # 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 - # - # @param [String] method HTTP method to use for the request - # @param [String] path The absolute path for the request - # @param [Array] 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 request(method, path, expected_responses, parameters = {}) - 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 - # Sets the scoped account for future requests # @param [String] scoped_account Identifier of the account to scope request to def scoped_account=(scoped_account) @@ -343,6 +267,20 @@ module Fog @credentials.access_token end + # 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 + private # This makes a request of the API based on the configured setting for @@ -392,6 +330,83 @@ module Fog # credentials and options @connection.request(options) end + end + + # The Mock Service allows you to run a fake instance of the Service + # which makes no real connections. + # + # @todo Implement + # + class Mock + include Shared + + 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 + include Shared + + # 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 + # + # @param [String] method HTTP method to use for the request + # @param [String] path The absolute path for the request + # @param [Array] 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 request(method, path, expected_responses, parameters = {}) + 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 + + private # Queries the API and tries to select the most suitable official Image # to use if the user chooses not to select their own.