module Fog::Brightbox::Compute::Shared

The Shared module consists of code that was duplicated between the Real and Mock implementations.

Constants

API_URL

Attributes

scoped_account[W]

Public Class Methods

new(config) click to toggle source

Creates a new instance of the Brightbox Compute service

@note If you create service using just a refresh token when it

expires the service will no longer be able to authenticate.

@see Fog::Brightbox::Config#initialize Config object for possible configuration options

@param [Brightbox::Config, Hash] config

Any configuration to be used for this service. This ideally should be in the newer form
of a {Brightbox::Config} object but may be a Hash.
# File lib/fog/brightbox/compute/shared.rb, line 25
def initialize(config)
  if config.respond_to?(:config_service?) && config.config_service?
    @config = config
  else
    @config = Fog::Brightbox::Config.new(config)
  end
  @config = Fog::Brightbox::Compute::Config.new(@config)

  # Currently authentication and api endpoints are the same but may change
  @auth_url            = @config.auth_url.to_s
  @auth_connection     = Fog::Core::Connection.new(@auth_url)

  @api_url             = @config.compute_url.to_s
  @connection_options  = @config.connection_options
  @persistent          = @config.connection_persistent?
  @connection          = Fog::Core::Connection.new(@api_url, @persistent, @connection_options)

  # Authentication options
  client_id            = @config.client_id
  client_secret        = @config.client_secret

  username             = @config.username
  password             = @config.password
  @configured_account  = @config.account
  # Request account can be changed at anytime and changes behaviour of future requests
  @scoped_account      = @configured_account

  credential_options   = { :username => username, :password => password }
  @credentials         = CredentialSet.new(client_id, client_secret, credential_options)

  # If existing tokens have been cached, allow continued use of them in the service
  @credentials.update_tokens(@config.cached_access_token, @config.cached_refresh_token)

  @token_management    = @config.managed_tokens?
end

Public Instance Methods

access_token() click to toggle source

Returns the current access token or nil @return [String,nil]

# File lib/fog/brightbox/compute/shared.rb, line 103
def access_token
  @credentials.access_token
end
access_token_available?() click to toggle source

Returns true if an access token is set @return [Boolean]

# File lib/fog/brightbox/compute/shared.rb, line 97
def access_token_available?
  !! @credentials.access_token
end
account() click to toggle source

Returns the scoped account being used for requests

  • For API clients this is the owning account

  • For User applications this is the account specified by either account_id option on the service or the brightbox_account setting in your configuration

@return [Fog::Brightbox::Compute::Account]

# File lib/fog/brightbox/compute/shared.rb, line 84
def account
  account_data = get_scoped_account.merge(:service => self)
  Fog::Brightbox::Compute::Account.new(account_data)
end
authenticating_as_user?() click to toggle source

Returns true if authentication is being performed as a user @return [Boolean]

# File lib/fog/brightbox/compute/shared.rb, line 91
def authenticating_as_user?
  @credentials.user_details?
end
default_image() click to toggle source

Returns an identifier for the default image for use

Currently tries to find the latest version of Ubuntu (i686) from Brightbox.

Highly recommended that you actually select the image you want to run on your servers yourself!

@return [String] if image is found, returns the identifier @return [NilClass] if no image is found or an error occurs

# File lib/fog/brightbox/compute/shared.rb, line 153
def default_image
  @default_image_id ||= (@config.default_image_id || select_default_image)
end
expires_in() click to toggle source

Returns the current token expiry time in seconds or nil @return [Number,nil]

# File lib/fog/brightbox/compute/shared.rb, line 115
def expires_in
  @credentials.expires_in
end
get_access_token() click to toggle source

Requests a new access token

@return [String] New access token

# File lib/fog/brightbox/compute/shared.rb, line 122
def get_access_token
  begin
    get_access_token!
  rescue Excon::Errors::Unauthorized, Excon::Errors::BadRequest
    @credentials.update_tokens(nil, nil)
  end
  @credentials.access_token
end
get_access_token!() click to toggle source

Requests a new access token and raises if there is a problem

@return [String] New access token @raise [Excon::Errors::BadRequest] The credentials are expired or incorrect

# File lib/fog/brightbox/compute/shared.rb, line 136
def get_access_token!
  response = request_access_token(@auth_connection, @credentials)
  update_credentials_from_response(@credentials, response)
  @credentials.access_token
end
refresh_token() click to toggle source

Returns the current refresh token or nil @return [String,nil]

# File lib/fog/brightbox/compute/shared.rb, line 109
def refresh_token
  @credentials.refresh_token
end
scoped_account(options_account = nil) click to toggle source

This returns the account identifier that the request should be scoped by based on the options passed to the request and current configuration

@param [String] options_account Any identifier passed into the request

@return [String, nil] The account identifier to scope the request to or nil

# File lib/fog/brightbox/compute/shared.rb, line 67
def scoped_account(options_account = nil)
  [options_account, @scoped_account].compact.first
end
scoped_account_reset() click to toggle source

Resets the scoped account back to intially configured one

# File lib/fog/brightbox/compute/shared.rb, line 72
def scoped_account_reset
  @scoped_account = @configured_account
end

Private Instance Methods

authenticated_request(options) click to toggle source

This request makes an authenticated request of the API using currently setup credentials.

@param [Hash] options Excon compatible options

@see github.com/geemus/excon/blob/master/lib/excon/connection.rb

@return [Excon::Response]

# File lib/fog/brightbox/compute/shared.rb, line 197
def authenticated_request(options)
  headers = options[:headers] || {}
  headers.merge!("Authorization" => "Bearer #{@credentials.access_token}", "Content-Type" => "application/json")
  options[:headers] = headers
  # TODO: This is just a wrapper around a call to Excon::Connection#request
  #   so can be extracted from Compute by passing in the connection,
  #   credentials and options
  @connection.request(options)
end
make_request(options) click to toggle source

This makes a request of the API based on the configured setting for token management.

@param [Hash] options Excon compatible options @see github.com/geemus/excon/blob/master/lib/excon/connection.rb

@return [Hash] Data of response body

# File lib/fog/brightbox/compute/shared.rb, line 167
def make_request(options)
  if @token_management
    managed_token_request(options)
  else
    authenticated_request(options)
  end
end
managed_token_request(options) click to toggle source

This request checks for access tokens and will ask for a new one if it receives Unauthorized from the API before repeating the request

@param [Hash] options Excon compatible options

@return [Excon::Response]

# File lib/fog/brightbox/compute/shared.rb, line 181
def managed_token_request(options)
  get_access_token unless access_token_available?
  authenticated_request(options)
rescue Excon::Errors::Unauthorized
  get_access_token
  authenticated_request(options)
end