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
-
#access_token ⇒ String?
Returns the current access token or nil.
-
#access_token_available? ⇒ Boolean
Returns true if an access token is set.
-
#account ⇒ Fog::Compute::Brightbox::Account
Returns the scoped account being used for requests.
-
#authenticating_as_user? ⇒ Boolean
Returns true if authentication is being performed as a user.
-
#default_image ⇒ String, NilClass
Returns an identifier for the default image for use.
-
#expires_in ⇒ Number?
Returns the current token expiry time in seconds or nil.
-
#get_access_token ⇒ String
Requests a new access token.
-
#get_access_token! ⇒ String
Requests a new access token and raises if there is a problem.
-
#initialize(options) ⇒ Object
Creates a new instance of the Brightbox Compute service.
-
#refresh_token ⇒ String?
Returns the current refresh token or nil.
-
#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.
-
#scoped_account=(scoped_account) ⇒ Object
Sets the scoped account for future requests.
-
#scoped_account_reset ⇒ Object
Resets the scoped account back to intially configured one.
Methods included from OAuth2
Instance Method Details
#access_token ⇒ String?
Returns the current access token or nil
123 124 125 |
# File 'lib/fog/brightbox/compute/shared.rb', line 123 def access_token @credentials.access_token end |
#access_token_available? ⇒ Boolean
Returns true if an access token is set
117 118 119 |
# File 'lib/fog/brightbox/compute/shared.rb', line 117 def access_token_available? !! @credentials.access_token end |
#account ⇒ Fog::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 thebrightbox_account
setting in your configuration
104 105 106 107 |
# File 'lib/fog/brightbox/compute/shared.rb', line 104 def account account_data = get_scoped_account.merge(:service => self) Fog::Compute::Brightbox::Account.new(account_data) end |
#authenticating_as_user? ⇒ Boolean
Returns true if authentication is being performed as a user
111 112 113 |
# File 'lib/fog/brightbox/compute/shared.rb', line 111 def authenticating_as_user? @credentials.user_details? end |
#default_image ⇒ String, 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!
173 174 175 176 |
# File 'lib/fog/brightbox/compute/shared.rb', line 173 def default_image return @default_image_id unless @default_image_id.nil? @default_image_id = Fog.credentials[:brightbox_default_image] || select_default_image end |
#expires_in ⇒ Number?
Returns the current token expiry time in seconds or nil
135 136 137 |
# File 'lib/fog/brightbox/compute/shared.rb', line 135 def expires_in @credentials.expires_in end |
#get_access_token ⇒ String
Requests a new access token
142 143 144 145 146 147 148 149 |
# File 'lib/fog/brightbox/compute/shared.rb', line 142 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
156 157 158 159 160 |
# File 'lib/fog/brightbox/compute/shared.rb', line 156 def get_access_token! response = request_access_token(@auth_connection, @credentials) update_credentials_from_response(@credentials, response) @credentials.access_token end |
#initialize(options) ⇒ Object
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
Overide the existing behaviour to request access tokens if expired
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fog/brightbox/compute/shared.rb', line 46 def initialize() # Currently authentication and api endpoints are the same but may change @auth_url = [:brightbox_auth_url] || Fog.credentials[:brightbox_auth_url] || API_URL @auth_connection = Fog::Connection.new(@auth_url) @api_url = [:brightbox_api_url] || Fog.credentials[:brightbox_api_url] || API_URL @connection_options = [:connection_options] || {} @persistent = [:persistent] || false @connection = Fog::Connection.new(@api_url, @persistent, @connection_options) # Authentication options client_id = [:brightbox_client_id] || Fog.credentials[:brightbox_client_id] client_secret = [:brightbox_secret] || Fog.credentials[:brightbox_secret] username = [:brightbox_username] || Fog.credentials[:brightbox_username] password = [:brightbox_password] || Fog.credentials[:brightbox_password] @configured_account = [:brightbox_account] || Fog.credentials[:brightbox_account] # Request account can be changed at anytime and changes behaviour of future requests @scoped_account = @configured_account = { :username => username, :password => password } @credentials = CredentialSet.new(client_id, client_secret, ) # If existing tokens have been cached, allow continued use of them in the service @credentials.update_tokens([:brightbox_access_token], [:brightbox_refresh_token]) @token_management = .fetch(:brightbox_token_management, true) end |
#refresh_token ⇒ String?
Returns the current refresh token or nil
129 130 131 |
# File 'lib/fog/brightbox/compute/shared.rb', line 129 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
87 88 89 |
# File 'lib/fog/brightbox/compute/shared.rb', line 87 def scoped_account( = nil) [, @scoped_account].compact.first end |
#scoped_account=(scoped_account) ⇒ Object
Sets the scoped account for future requests
77 78 79 |
# File 'lib/fog/brightbox/compute/shared.rb', line 77 def scoped_account=(scoped_account) @scoped_account = scoped_account end |
#scoped_account_reset ⇒ Object
Resets the scoped account back to intially configured one
92 93 94 |
# File 'lib/fog/brightbox/compute/shared.rb', line 92 def scoped_account_reset @scoped_account = @configured_account end |