mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[Brightbox] Adds means to update scoped account
A Compute instance encapsulates a connection for a client to the Brightbox API. Users can have multiple accounts but there was no easy way to switch between them when account had to be passed in via the initializer. Now the scoped account can be set on an existing instance which overrides any configured setting but it can be reset if needed. The #request method still can accept `account_id` as an option which again overrides the previous settings. Finally the parameter is now correctly sent as a query string parameter not part of the API request JSON.
This commit is contained in:
parent
dbc53d0f48
commit
31c5895119
2 changed files with 61 additions and 2 deletions
|
@ -206,7 +206,9 @@ module Fog
|
|||
|
||||
username = options[:brightbox_username] || Fog.credentials[:brightbox_username]
|
||||
password = options[:brightbox_password] || Fog.credentials[:brightbox_password]
|
||||
@scoped_account = options[:brightbox_account] || Fog.credentials[:brightbox_account]
|
||||
@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)
|
||||
|
@ -238,7 +240,13 @@ module Fog
|
|||
:path => path,
|
||||
:expects => expected_responses
|
||||
}
|
||||
parameters[:account_id] = @scoped_account if parameters[:account_id].nil? && @scoped_account
|
||||
|
||||
# Select the account to scope for this request
|
||||
account = scoped_account(parameters.fetch(:account_id, nil))
|
||||
if account
|
||||
request_options[:query] = { :account_id => account }
|
||||
end
|
||||
|
||||
request_options[:body] = Fog::JSON.encode(parameters) unless parameters.empty?
|
||||
|
||||
response = make_request(request_options)
|
||||
|
@ -252,6 +260,27 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
# Sets the scoped account for future requests
|
||||
# @param [String] scoped_account Identifier of the account to scope request to
|
||||
def scoped_account=(scoped_account)
|
||||
@scoped_account = scoped_account
|
||||
end
|
||||
|
||||
# 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
|
||||
def scoped_account(options_account = nil)
|
||||
[options_account, @scoped_account].compact.first
|
||||
end
|
||||
|
||||
# Resets the scoped account back to intially configured one
|
||||
def scoped_account_reset
|
||||
@scoped_account = @configured_account
|
||||
end
|
||||
|
||||
# Returns the scoped account being used for requests
|
||||
#
|
||||
# * For API clients this is the owning account
|
||||
|
|
|
@ -66,4 +66,34 @@ Shindo.tests('Fog::Compute.new', ['brightbox']) do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
tests("account scoping") do
|
||||
service = Fog::Compute.new(:provider => "Brightbox")
|
||||
configured_account = Fog.credentials[:brightbox_account]
|
||||
tests("when Fog.credentials are #{configured_account}") do
|
||||
test("#scoped_account == #{configured_account}") { service.scoped_account == configured_account }
|
||||
end
|
||||
|
||||
set_account = "acc-35791"
|
||||
tests("when Compute instance is updated to #{set_account}") do
|
||||
service.scoped_account = set_account
|
||||
test("#scoped_account == #{set_account}") { service.scoped_account == set_account }
|
||||
end
|
||||
|
||||
tests("when Compute instance is reset") do
|
||||
service.scoped_account_reset
|
||||
test("#scoped_account == #{configured_account}") { service.scoped_account == configured_account }
|
||||
end
|
||||
|
||||
optioned_account = "acc-56789"
|
||||
tests("when Compute instance created with :brightbox_account => #{optioned_account}") do
|
||||
service = Fog::Compute.new(:provider => "Brightbox", :brightbox_account => optioned_account )
|
||||
test("#scoped_account == #{optioned_account}") { service.scoped_account == optioned_account }
|
||||
end
|
||||
|
||||
request_account = "acc-24680"
|
||||
tests("when requested with #{request_account}") do
|
||||
test("#scoped_account(#{request_account}) == #{request_account}") { service.scoped_account(request_account) == request_account }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue