Module: Fog::Brightbox::Compute::Shared
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/".freeze
Constants included from OAuth2
Instance Attribute Summary collapse
-
#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.
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::Brightbox::Compute::Account
Returns the scoped account being used for requests.
-
#authenticating_as_user? ⇒ Boolean
Returns true if authentication is being performed as a user.
- #credentials ⇒ Object
-
#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(config) ⇒ Object
Creates a new instance of the Brightbox Compute service.
-
#refresh_token ⇒ String?
Returns the current refresh token or nil.
-
#scoped_account_reset ⇒ Object
Resets the scoped account back to intially configured one.
- #two_factor? ⇒ Boolean
Methods included from OAuth2
Instance Attribute Details
#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
61 62 63 |
# File 'lib/fog/brightbox/compute/shared.rb', line 61 def scoped_account( = nil) [, @scoped_account].compact.first end |
Instance Method Details
#access_token ⇒ String?
Returns the current access token or nil
103 104 105 |
# File 'lib/fog/brightbox/compute/shared.rb', line 103 def access_token @credentials.access_token end |
#access_token_available? ⇒ Boolean
Returns true if an access token is set
97 98 99 |
# File 'lib/fog/brightbox/compute/shared.rb', line 97 def access_token_available? !!@credentials.access_token end |
#account ⇒ Fog::Brightbox::Compute::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
84 85 86 87 |
# 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? ⇒ Boolean
Returns true if authentication is being performed as a user
91 92 93 |
# File 'lib/fog/brightbox/compute/shared.rb', line 91 def authenticating_as_user? @credentials.user_details? end |
#credentials ⇒ Object
70 71 72 73 74 |
# File 'lib/fog/brightbox/compute/shared.rb', line 70 def credentials client_id = @config.client_id client_secret = @config.client_secret @credentials ||= CredentialSet.new(client_id, client_secret, ) 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!
153 154 155 |
# 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 ⇒ Number?
Returns the current token expiry time in seconds or nil
115 116 117 |
# File 'lib/fog/brightbox/compute/shared.rb', line 115 def expires_in @credentials.expires_in end |
#get_access_token ⇒ String
Requests a new access token
122 123 124 125 126 127 128 129 |
# File 'lib/fog/brightbox/compute/shared.rb', line 122 def get_access_token begin get_access_token! rescue Fog::Brightbox::OAuth2::TwoFactorMissingError, 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
136 137 138 139 140 |
# 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 |
#initialize(config) ⇒ 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
25 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 |
# File 'lib/fog/brightbox/compute/shared.rb', line 25 def initialize(config) @config = if config.respond_to?(:config_service?) && config.config_service? config else 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. @persistent = @config.connection_persistent? @connection = Fog::Core::Connection.new(@api_url, @persistent, @connection_options) @configured_account = @config.account # Request account can be changed at anytime and changes behaviour of future requests @scoped_account = @configured_account @two_factor = @config.two_factor? @one_time_password = @config.one_time_password # 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_token ⇒ String?
Returns the current refresh token or nil
109 110 111 |
# File 'lib/fog/brightbox/compute/shared.rb', line 109 def refresh_token @credentials.refresh_token end |
#scoped_account_reset ⇒ Object
Resets the scoped account back to intially configured one
66 67 68 |
# File 'lib/fog/brightbox/compute/shared.rb', line 66 def scoped_account_reset @scoped_account = @configured_account end |
#two_factor? ⇒ Boolean
157 158 159 |
# File 'lib/fog/brightbox/compute/shared.rb', line 157 def two_factor? @two_factor || false end |