Class: Brightbox::Config::UserApplication

Inherits:
Object
  • Object
show all
Defined in:
lib/brightbox-cli/config/user_application.rb

Constant Summary collapse

NON_BLANK_KEYS =

FIXME: api_url should use fog’s underlying default

%w[api_url username].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(incoming_config, client_name) ⇒ UserApplication

Returns a new instance of UserApplication.



10
11
12
13
# File 'lib/brightbox-cli/config/user_application.rb', line 10

def initialize(incoming_config, client_name)
  @selected_config = incoming_config
  @client_name = client_name
end

Instance Attribute Details

#client_nameObject

Returns the value of attribute client_name.



8
9
10
# File 'lib/brightbox-cli/config/user_application.rb', line 8

def client_name
  @client_name
end

#selected_configObject

Returns the value of attribute selected_config.



8
9
10
# File 'lib/brightbox-cli/config/user_application.rb', line 8

def selected_config
  @selected_config
end

Instance Method Details

#fetch_refresh_token(options) ⇒ Object

FIXME: This calls the underlying fog methods which also resets the access token but this method does not expose that token.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/brightbox-cli/config/user_application.rb', line 42

def fetch_refresh_token(options)
  password_options = {
    :brightbox_password => options[:password],
    :brightbox_support_two_factor => true,
    :brightbox_one_time_password => options[:one_time_password]
  }

  default_fog_options = password_auth_params.merge(password_options)
  connection = Fog::Compute.new(default_fog_options)
  begin
    connection.get_access_token!
  rescue Excon::Errors::Unauthorized
    raise Brightbox::Api::ApiError, "Invalid credentials"
  end
  connection
end

#to_fogObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/brightbox-cli/config/user_application.rb', line 15

def to_fog
  check_required_params
  # Note we have to merge in refresh token at the higher level
  {
    :provider => "Brightbox",
    :brightbox_api_url => selected_config["api_url"],
    :brightbox_auth_url => selected_config["auth_url"] || selected_config["api_url"],
    :brightbox_client_id => client_id,
    :brightbox_secret => client_secret,
    :brightbox_support_two_factor => true,
    :persistent => persistent?
  }
end

#valid?Boolean

TODO:

Implementation requires keys that are really optional

Returns true if the config section passed in has expected keys

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/brightbox-cli/config/user_application.rb', line 33

def valid?
  NON_BLANK_KEYS.all? do |key|
    selected_config.key?(key) && !selected_config[key].to_s.empty?
  end
end