Module: Brightbox::Config::Sections

Included in:
BBConfig
Defined in:
lib/brightbox-cli/config/sections.rb

Instance Method Summary collapse

Instance Method Details

#[](section_name) ⇒ Hash

Config data for a named section

@param section_name the name of the section

Examples:

@config["dev"] # => dev client data

Returns:

  • (Hash)

    the data for the section



159
160
161
# File 'lib/brightbox-cli/config/sections.rb', line 159

def [](section_name)
  config[section_name]
end

#add_login(config_alias, password, options = {}) ⇒ Object

Parameters:

  • config_alias (String)

    The section name usually email but email/suffix allowed.

  • password (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :api_url (String)
  • :auth_url (String)
  • :default_account (String)
  • :client_id (String)
  • :secret (String)


14
15
16
17
18
19
20
21
22
23
24
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/brightbox-cli/config/sections.rb', line 14

def (config_alias, password, options = {})
  # If a custom alias is passed, used that for the config header, otherwise use email
  email = config_alias.split("/").first
  config_section = config[config_alias]

  info "Creating new client config #{config_alias}" if config_section.empty?

  config_section["username"] = email unless config_section["username"]

  config_section["api_url"] = options[:api_url] if options.key?(:api_url)
  config_section["api_url"] = DEFAULT_API_ENDPOINT unless config_section["api_url"]

  config_section["auth_url"] = options[:auth_url] if options.key?(:auth_url)
  config_section["auth_url"] = config_section["api_url"] unless config_section["auth_url"]

  config_section["default_account"] = options[:default_account] if options.key?(:default_account)

  config_section["client_id"] = options[:client_id] if options.key?(:client_id)
  config_section["secret"] = options[:secret] if options.key?(:secret)

  dirty!

  self.client_name = config_alias

  debug "Using #{client_name}"

  # Renew tokens via config...
  #
  # Part of the "login" behaviour is to always refresh them
  #
  begin
    remove_cached_tokens!
    begin
      renew_tokens(client_name: config_alias,
                   password: password)
    rescue Fog::Brightbox::OAuth2::TwoFactorMissingError
      discover_two_factor_pin

      renew_tokens(client_name: client_alias,
                   password: options[:password],
                   one_time_password: current_second_factor)
    end

    # Try to determine a default account
    unless  == 
      info "The default account of #{} has been selected"
    end

    # If only client then set it as the default
    set_default_client(client_alias) unless default_client
  rescue StandardError => e
    error "Something went wrong trying to refresh new tokens #{e.message}"
  end

  # Ensure all our config changes are now saved
  save
end

#add_section(client_alias, client_id, secret, options) ⇒ Object

Parameters:

  • client_alias (String)
  • client_id (String)
  • secret (String)
  • options (Hash)

Options Hash (options):

  • :username (String)
  • :password (String)
  • :api_url (String)
  • :auth_url (String)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/brightbox-cli/config/sections.rb', line 82

def add_section(client_alias, client_id, secret, options)
  config_section = config[client_alias]
  if config_section.empty?
    info "Creating new client config #{client_alias}"
  else
    old_calias = client_alias

    deduplicator = Brightbox::Config::SectionNameDeduplicator.new(client_alias, section_names)
    client_alias = deduplicator.next
    # Need to open the new config again
    config_section = config[client_alias]

    info "A client config named #{old_calias} already exists using #{client_alias} instead"
  end

  config_section["alias"] = client_alias
  config_section["client_id"] = client_id
  config_section["username"] = options[:username]
  config_section["secret"] = secret
  config_section["api_url"] = options[:api_url] || DEFAULT_API_ENDPOINT
  config_section["auth_url"] = options[:auth_url] || config_section["api_url"]

  dirty!

  self.client_name = client_alias

  # Renew tokens via config...
  begin
    begin
      renew_tokens(client_name: client_alias,
                   password: options[:password])
    rescue Fog::Brightbox::OAuth2::TwoFactorMissingError
      renew_tokens(client_name: client_alias,
                   password: options[:password],
                   one_time_password: discover_two_factor_pin)
    end

    # Try to determine a default account
    unless  == 
      info "The default account of #{} has been selected"
    end

    # If only client then set it as the default
    set_default_client(client_alias) unless default_client
  rescue StandardError => e
    error "Something went wrong trying to refresh new tokens #{e.message}"
  end

  # Ensure all our config changes are now saved
  save
end

#delete_section(name) ⇒ Object

Removes the config section from the configuration object. Must be persisted to disk.



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/brightbox-cli/config/sections.rb', line 137

def delete_section(name)
  return unless client_named?(name)

  if default_client == name
    clear_default_client
  end
  # remove from the Ini object
  config.delete_section(name)

  dirty! # to ensure save actually writes to disk
  save
end

#section_namesArray<String>

Returns the section names in the config

Returns:

  • (Array<String>)


167
168
169
170
# File 'lib/brightbox-cli/config/sections.rb', line 167

def section_names
  # Exclude the global "core" section
  config.sections.reject { |s| %w[core alias].include?(s) }
end