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

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

Overview

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

Constant Summary collapse

API_URL =
"https://api.gb1.brightbox.com/"

Instance Method Summary collapse

Methods included from OAuth2

#request_access_token

Instance Method Details

#access_tokenString?

Returns the current access token or nil

Returns:

  • (String, nil)


110
111
112
# File 'lib/fog/brightbox/compute/shared.rb', line 110

def access_token
  @credentials.access_token
end

#access_token_available?Boolean

Returns true if an access token is set

Returns:

  • (Boolean)


104
105
106
# File 'lib/fog/brightbox/compute/shared.rb', line 104

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



91
92
93
94
# File 'lib/fog/brightbox/compute/shared.rb', line 91

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

#authenticating_as_user?Boolean

Returns true if authentication is being performed as a user

Returns:

  • (Boolean)


98
99
100
# File 'lib/fog/brightbox/compute/shared.rb', line 98

def authenticating_as_user?
  @credentials.user_details?
end

#default_imageString, NilClass

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!

Returns:

  • (String)

    if image is found, returns the identifier

  • (NilClass)

    if no image is found or an error occurs



160
161
162
163
# File 'lib/fog/brightbox/compute/shared.rb', line 160

def default_image
  return @default_image_id unless @default_image_id.nil?
  @default_image_id = @config.default_image_id || select_default_image
end

#expires_inNumber?

Returns the current token expiry time in seconds or nil

Returns:

  • (Number, nil)


122
123
124
# File 'lib/fog/brightbox/compute/shared.rb', line 122

def expires_in
  @credentials.expires_in
end

#get_access_tokenString

Requests a new access token

Returns:

  • (String)

    New access token



129
130
131
132
133
134
135
136
# File 'lib/fog/brightbox/compute/shared.rb', line 129

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



143
144
145
146
147
# File 'lib/fog/brightbox/compute/shared.rb', line 143

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

#initialize(config) ⇒ 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:

See Also:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fog/brightbox/compute/shared.rb', line 26

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
    = @config.
  # Request account can be changed at anytime and changes behaviour of future requests
        = 

  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

#refresh_tokenString?

Returns the current refresh token or nil

Returns:

  • (String, nil)


116
117
118
# File 'lib/fog/brightbox/compute/shared.rb', line 116

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



74
75
76
# File 'lib/fog/brightbox/compute/shared.rb', line 74

def ( = nil)
  [, ].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



64
65
66
# File 'lib/fog/brightbox/compute/shared.rb', line 64

def scoped_account=()
   = 
end

#scoped_account_resetObject

Resets the scoped account back to intially configured one



79
80
81
# File 'lib/fog/brightbox/compute/shared.rb', line 79

def 
   = 
end