Module: Fog::Compute::Brightbox::Shared

Includes:
Brightbox::OAuth2
Included in:
Mock, Real
Defined in:
lib/fog/brightbox/compute.rb

Instance Method Summary collapse

Methods included from Brightbox::OAuth2

#request_access_token

Instance Method Details

#access_tokenString?

Returns the current access token or nil

Returns:



236
237
238
# File 'lib/fog/brightbox/compute.rb', line 236

def access_token
  @credentials.access_token
end

#access_token_available?Boolean

Returns true if an access token is set

Returns:

  • (Boolean)


230
231
232
# File 'lib/fog/brightbox/compute.rb', line 230

def access_token_available?
  !! @credentials.access_token
end

#accountFog::Compute::Brightbox::Account

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



217
218
219
220
# File 'lib/fog/brightbox/compute.rb', line 217

def 
   = .merge(:service => self)
  Fog::Compute::Brightbox::Account.new()
end

#authenticating_as_user?Boolean

Returns true if authentication is being performed as a user

Returns:

  • (Boolean)


224
225
226
# File 'lib/fog/brightbox/compute.rb', line 224

def authenticating_as_user?
  @credentials.user_details?
end

#default_imageString?

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!

Returns:



278
279
280
281
# File 'lib/fog/brightbox/compute.rb', line 278

def default_image
  return @default_image_id unless @default_image_id.nil?
  @default_image_id = Fog.credentials[:brightbox_default_image] || select_default_image
end

#get_access_tokenString

Requests a new access token

Returns:

  • (String)

    New access token



249
250
251
252
253
254
255
256
# File 'lib/fog/brightbox/compute.rb', line 249

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!String

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

Returns:

  • (String)

    New access token

Raises:

  • (Excon::Errors::BadRequest)

    The credentials are expired or incorrect



263
264
265
266
267
# File 'lib/fog/brightbox/compute.rb', line 263

def get_access_token!
  response = request_access_token(@auth_connection, @credentials)
  update_credentials_from_response(@credentials, response)
  @credentials.access_token
end

#initialize(options) ⇒ Object

Note:

If you create service using just a refresh token when it expires the service will no longer be able to authenticate.

Creates a new instance of the Brightbox Compute service

Parameters:

Options Hash (options):

  • :brightbox_api_url (String)

    Override the default (or configured) API endpoint

  • :brightbox_auth_url (String)

    Override the default (or configured) API authentication endpoint

  • :brightbox_client_id (String)

    Client identifier to authenticate with (overrides configured)

  • :brightbox_secret (String)

    Client secret to authenticate with (overrides configured)

  • :brightbox_username (String)

    Email or user identifier for user based authentication

  • :brightbox_password (String)

    Password for user based authentication

  • :brightbox_account (String)

    Account identifier to scope this connection to

  • :connection_options (String)

    Settings to pass to underlying Fog::Connection

  • :persistent (Boolean)

    Sets a persistent HTTP Fog::Connection

  • :brightbox_access_token (String)

    Sets the OAuth access token to use rather than requesting a new token

  • :brightbox_refresh_token (String)

    Sets the refresh token to use when requesting a newer access token

  • :brightbox_token_management (String)

    Overide the existing behaviour to request access tokens if expired (default is ‘true`)



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/fog/brightbox/compute.rb', line 159

def initialize(options)
  # Currently authentication and api endpoints are the same but may change
  @auth_url            = options[:brightbox_auth_url]  || Fog.credentials[:brightbox_auth_url] || API_URL
  @auth_connection     = Fog::Connection.new(@auth_url)

  @api_url             = options[:brightbox_api_url]   || Fog.credentials[:brightbox_api_url]  || API_URL
  @connection_options  = options[:connection_options]  || {}
  @persistent          = options[:persistent]          || false
  @connection          = Fog::Connection.new(@api_url, @persistent, @connection_options)

  # Authentication options
  client_id            = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
  client_secret        = options[:brightbox_secret]    || Fog.credentials[:brightbox_secret]

  username             = options[:brightbox_username]  || Fog.credentials[:brightbox_username]
  password             = options[:brightbox_password]  || Fog.credentials[:brightbox_password]
  @configured_account  = options[:brightbox_account]   || Fog.credentials[:brightbox_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(options[:brightbox_access_token], options[:brightbox_refresh_token])

  @token_management    = options.fetch(:brightbox_token_management, true)
end

#refresh_tokenString?

Returns the current refresh token or nil

Returns:



242
243
244
# File 'lib/fog/brightbox/compute.rb', line 242

def refresh_token
  @credentials.refresh_token
end

#scoped_account(options_account = nil) ⇒ String?

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

Parameters:

  • options_account (String) (defaults to: nil)

    Any identifier passed into the request

Returns:

  • (String, nil)

    The account identifier to scope the request to or nil



200
201
202
# File 'lib/fog/brightbox/compute.rb', line 200

def ( = nil)
  [, @scoped_account].compact.first
end

#scoped_account=(scoped_account) ⇒ Object

Sets the scoped account for future requests

Parameters:

  • scoped_account (String)

    Identifier of the account to scope request to



190
191
192
# File 'lib/fog/brightbox/compute.rb', line 190

def scoped_account=()
  @scoped_account = 
end

#scoped_account_resetObject

Resets the scoped account back to intially configured one



205
206
207
# File 'lib/fog/brightbox/compute.rb', line 205

def 
  @scoped_account = @configured_account
end