brightbox-cli 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/Gemfile.lock +5 -2
  4. data/brightbox-cli.gemspec +1 -1
  5. data/lib/brightbox/cli/config.rb +19 -0
  6. data/lib/brightbox-cli/accounts.rb +2 -2
  7. data/lib/brightbox-cli/api.rb +4 -4
  8. data/lib/brightbox-cli/commands/accounts/default.rb +1 -1
  9. data/lib/brightbox-cli/commands/accounts/list.rb +1 -1
  10. data/lib/brightbox-cli/commands/config/client-add.rb +2 -2
  11. data/lib/brightbox-cli/commands/config/client-default.rb +3 -3
  12. data/lib/brightbox-cli/commands/config/client-list.rb +6 -6
  13. data/lib/brightbox-cli/commands/config/client-remove.rb +4 -4
  14. data/lib/brightbox-cli/commands/config/user-add.rb +2 -2
  15. data/lib/brightbox-cli/commands/login.rb +53 -0
  16. data/lib/brightbox-cli/config/accounts.rb +5 -0
  17. data/lib/brightbox-cli/config/authentication_tokens.rb +10 -3
  18. data/lib/brightbox-cli/config/cache.rb +8 -12
  19. data/lib/brightbox-cli/config/clients.rb +14 -38
  20. data/lib/brightbox-cli/config/sections.rb +70 -13
  21. data/lib/brightbox-cli/config/user_application.rb +13 -5
  22. data/lib/brightbox-cli/config.rb +22 -14
  23. data/lib/brightbox-cli/connection_manager.rb +2 -2
  24. data/lib/brightbox-cli/gli_global_hooks.rb +25 -28
  25. data/lib/brightbox-cli/version.rb +1 -1
  26. data/lib/brightbox_cli.rb +4 -0
  27. data/spec/cassettes/Brightbox_BBConfig/_add_section/when_config_exists_and_overwrite_duplicates_is_false/does_not_update_the_config_file.yml +199 -0
  28. data/spec/cassettes/Brightbox_BBConfig/_add_section/when_config_exists_and_overwrite_duplicates_is_true/does_not_update_the_config_file.yml +199 -0
  29. data/spec/cassettes/Brightbox_BBConfig_add_login/when_altering_a_custom_option/does_not_alter_the_configuration.yml +99 -0
  30. data/spec/cassettes/Brightbox_BBConfig_add_login/when_altering_a_custom_option/updates_access_token.yml +99 -0
  31. data/spec/cassettes/Brightbox_BBConfig_add_login/when_altering_a_custom_option/updates_refresh_token.yml +99 -0
  32. data/spec/cassettes/Brightbox_BBConfig_add_login/when_configured_with_custom_options/does_not_alter_the_configuration.yml +99 -0
  33. data/spec/cassettes/Brightbox_BBConfig_add_login/when_configured_with_custom_options/updates_access_token.yml +99 -0
  34. data/spec/cassettes/Brightbox_BBConfig_add_login/when_configured_with_custom_options/updates_refresh_token.yml +99 -0
  35. data/spec/cassettes/Brightbox_BBConfig_add_login/when_logged_in_previously/does_not_alter_the_configuration.yml +149 -0
  36. data/spec/cassettes/Brightbox_BBConfig_add_login/when_logged_in_previously/updates_access_token.yml +149 -0
  37. data/spec/cassettes/Brightbox_BBConfig_add_login/when_logged_in_previously/updates_refresh_token.yml +149 -0
  38. data/spec/cassettes/Brightbox_BBConfig_add_login/when_no_config_exists/creates_the_configuration.yml +101 -0
  39. data/spec/cassettes/Brightbox_BBConfig_add_login/when_no_config_exists/refreshed_tokens.yml +101 -0
  40. data/spec/cassettes/Brightbox_BBConfig_add_login/when_using_an_email_and_suffix/creates_the_configuration.yml +101 -0
  41. data/spec/cassettes/Brightbox_BBConfig_add_login/when_using_an_email_and_suffix/refreshed_tokens.yml +101 -0
  42. data/spec/cassettes/brightbox_accounts/list/_when_access_token_expired_/does_not_report_invalid_token_errors.yml +64 -18
  43. data/spec/cassettes/brightbox_accounts/list/_when_no_tokens_/does_not_report_invalid_token_errors.yml +62 -16
  44. data/spec/cassettes/brightbox_login/when_alternative_client_credentials_are_given/does_not_error.yml +101 -0
  45. data/spec/cassettes/brightbox_login/when_alternative_client_credentials_are_given/does_not_prompt_to_rerun_the_command.yml +101 -0
  46. data/spec/cassettes/brightbox_login/when_alternative_client_credentials_are_given/prompts_for_the_password.yml +101 -0
  47. data/spec/cassettes/brightbox_login/when_alternative_client_credentials_are_given/requests_access_tokens.yml +101 -0
  48. data/spec/cassettes/brightbox_login/when_alternative_client_credentials_are_given/sets_up_the_config.yml +101 -0
  49. data/spec/cassettes/brightbox_login/when_custom_api/auth_URLs_are_given/does_not_error.yml +101 -0
  50. data/spec/cassettes/brightbox_login/when_custom_api/auth_URLs_are_given/does_not_prompt_to_rerun_the_command.yml +101 -0
  51. data/spec/cassettes/brightbox_login/when_custom_api/auth_URLs_are_given/prompts_for_the_password.yml +101 -0
  52. data/spec/cassettes/brightbox_login/when_custom_api/auth_URLs_are_given/requests_access_tokens.yml +101 -0
  53. data/spec/cassettes/brightbox_login/when_custom_api/auth_URLs_are_given/sets_up_the_config.yml +101 -0
  54. data/spec/cassettes/brightbox_login/when_default_account_is_passed/does_not_error.yml +51 -0
  55. data/spec/cassettes/brightbox_login/when_default_account_is_passed/does_not_prompt_to_rerun_the_command.yml +51 -0
  56. data/spec/cassettes/brightbox_login/when_default_account_is_passed/prompts_for_the_password.yml +51 -0
  57. data/spec/cassettes/brightbox_login/when_default_account_is_passed/requests_access_tokens.yml +51 -0
  58. data/spec/cassettes/brightbox_login/when_default_account_is_passed/sets_up_the_config.yml +51 -0
  59. data/spec/cassettes/brightbox_login/when_no_config_is_present/does_not_error.yml +101 -0
  60. data/spec/cassettes/brightbox_login/when_no_config_is_present/does_not_prompt_to_rerun_the_command.yml +101 -0
  61. data/spec/cassettes/brightbox_login/when_no_config_is_present/requests_access_tokens.yml +101 -0
  62. data/spec/cassettes/brightbox_login/when_no_config_is_present/sets_up_the_config.yml +101 -0
  63. data/spec/cassettes/brightbox_login/when_no_password_is_given/does_not_error.yml +101 -0
  64. data/spec/cassettes/brightbox_login/when_no_password_is_given/does_not_prompt_to_rerun_the_command.yml +101 -0
  65. data/spec/cassettes/brightbox_login/when_no_password_is_given/prompts_for_the_password.yml +101 -0
  66. data/spec/cassettes/brightbox_login/when_no_password_is_given/requests_access_tokens.yml +101 -0
  67. data/spec/cassettes/brightbox_login/when_no_password_is_given/sets_up_the_config.yml +101 -0
  68. data/spec/commands/accounts/list_spec.rb +1 -0
  69. data/spec/commands/login_spec.rb +239 -0
  70. data/spec/configs/user_app.ini +0 -2
  71. data/spec/spec_helper.rb +1 -0
  72. data/spec/support/config_helpers.rb +5 -0
  73. data/spec/unit/brightbox/account/all_spec.rb +1 -3
  74. data/spec/unit/brightbox/api/conn_spec.rb +3 -0
  75. data/spec/unit/brightbox/api/find_spec.rb +3 -0
  76. data/spec/unit/brightbox/bb_config/account_spec.rb +2 -0
  77. data/spec/unit/brightbox/bb_config/add_login_spec.rb +222 -0
  78. data/spec/unit/brightbox/bb_config/client_name_spec.rb +4 -12
  79. data/spec/unit/brightbox/bb_config/client_named_spec.rb +1 -1
  80. data/spec/unit/brightbox/bb_config/config_directory_exists_spec.rb +5 -2
  81. data/spec/unit/brightbox/bb_config/config_directory_spec.rb +4 -2
  82. data/spec/unit/brightbox/bb_config/config_spec.rb +4 -8
  83. data/spec/unit/brightbox/bb_config/default_account_spec.rb +10 -6
  84. data/spec/unit/brightbox/bb_config/find_or_set_default_account_spec.rb +2 -0
  85. data/spec/unit/brightbox/bb_config/renew_tokens_spec.rb +3 -0
  86. data/spec/unit/brightbox/bb_config/section_names_spec.rb +46 -0
  87. data/spec/unit/brightbox/bb_config/using_client_spec.rb +12 -12
  88. data/spec/unit/brightbox/bb_config_spec.rb +0 -1
  89. data/spec/unit/brightbox/cloud_ip/find_spec.rb +3 -0
  90. data/spec/unit/brightbox/config/user_application/to_fog_spec.rb +33 -29
  91. data/spec/unit/brightbox/config/user_application/valid_spec.rb +0 -32
  92. data/spec/unit/brightbox/connection_manager/fetch_connection_spec.rb +7 -3
  93. data/spec/unit/brightbox/firewall_policy/apply_to_spec.rb +3 -0
  94. data/spec/unit/brightbox/firewall_policy/create_spec.rb +3 -0
  95. data/spec/unit/brightbox/firewall_policy/destroy_spec.rb +3 -0
  96. data/spec/unit/brightbox/firewall_policy/find_or_call_spec.rb +3 -0
  97. data/spec/unit/brightbox/firewall_policy/find_spec.rb +3 -0
  98. data/spec/unit/brightbox/firewall_rule/create_spec.rb +3 -0
  99. data/spec/unit/brightbox/firewall_rule/destroy_spec.rb +3 -0
  100. data/spec/unit/brightbox/firewall_rule/find_spec.rb +3 -0
  101. data/spec/unit/brightbox/firewall_rule/from_policy_spec.rb +3 -0
  102. data/spec/unit/brightbox/server/destroy_spec.rb +4 -0
  103. data/spec/unit/brightbox/server/find_or_call_spec.rb +3 -0
  104. data/spec/unit/brightbox/server/find_spec.rb +4 -0
  105. data/spec/unit/brightbox/server/shutdown_spec.rb +4 -0
  106. data/spec/unit/brightbox/server/start_spec.rb +4 -0
  107. data/spec/unit/brightbox/server/stop_spec.rb +4 -0
  108. data/spec/unit/brightbox/server/update_spec.rb +4 -0
  109. data/spec/unit/brightbox/server_group/find_spec.rb +3 -0
  110. data/spec/unit/brightbox/user_collaboration/get_for_account_spec.rb +3 -0
  111. metadata +90 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34feccb6a89c40ab981c31b408d896606d548abd
4
- data.tar.gz: a58a457c767e64ade86c3283e7313f8a76ba1445
3
+ metadata.gz: 878ff7e5b319cc7c15095db32d0091a6e1d2988b
4
+ data.tar.gz: d5809b9c66921ea3820855723d871fb1eeeb99ec
5
5
  SHA512:
6
- metadata.gz: dd53e4d8ec2d68ad698290ba23a4ca89605a4e17b5af26db4a6f9208362304f68977e144f647ab824b28484b69caea3d88e5fd73da28656054e4a8d232f0859f
7
- data.tar.gz: a50b5c409d4b938708b3c2ad29219675f054396cbfaf4175f5019523668cf0ef7bc5175eaafffabb21508c291273990c6cdd6850cd8c89abbcc5e3cd77a17d0a
6
+ metadata.gz: 95b4dce981e19ad00c6f79636cc4bd29c620eee0ca43eb5256be40c2a5c60be9de634348c861177a21f4d4d72720ed1b26b8c8efbb787c8948011bddc55d148b
7
+ data.tar.gz: e5f9235468da53a0f4635665376dae4f9f35d4d43612c144ef36bc343342f03496cf1eb415fa682217fd101b09bd1ac06222f6f58b28d5987fb3ca12cf53765e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ### v2.3.0 / 2015-10-23
2
+
3
+ [Full Changelog](https://github.com/brightbox/brightbox-cli/compare/v2.2.0...v2.3.0)
4
+
5
+ Enhancements:
6
+
7
+ * Adds embedded client that is used when a user has not setup their own
8
+ application.
9
+ * Adds `brightbox login` command which allows a simplified login for users with
10
+ just their email address and password (prompted).
11
+
12
+ brightbox login user@example.com
13
+
14
+ This uses the embedded application to save extra steps setting up.
15
+
16
+ Bug fixes:
17
+
18
+ * Guard against errors when using `DEBUG=true` whilst configuration is not
19
+ selected triggering a `NoMethodError`
20
+ * Use `pry-remote` for debugging. `pry` runs in process and the IO is trapped
21
+ when testing and in some error cases making it unusable.
22
+ * Fix configuration code so it can initialise without network side effects when
23
+ reading client_name and other values.
24
+
1
25
  ### v2.2.0 / 2015-09-28
2
26
 
3
27
  [Full Changelog](https://github.com/brightbox/brightbox-cli/compare/v2.1.2...v2.2.0)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- brightbox-cli (2.2.0)
4
+ brightbox-cli (2.3.0)
5
5
  fog-brightbox (>= 0.9.0)
6
6
  gli (~> 2.12.0)
7
7
  highline (~> 1.6.0)
@@ -50,6 +50,9 @@ GEM
50
50
  coderay (~> 1.0.5)
51
51
  method_source (~> 0.8)
52
52
  slop (~> 3.4)
53
+ pry-remote (0.1.8)
54
+ pry (~> 0.9)
55
+ slop (~> 3.0)
53
56
  rake (10.1.0)
54
57
  rspec (2.99.0)
55
58
  rspec-core (~> 2.99.0)
@@ -68,7 +71,7 @@ PLATFORMS
68
71
  DEPENDENCIES
69
72
  brightbox-cli!
70
73
  mocha
71
- pry
74
+ pry-remote
72
75
  rake
73
76
  rspec (~> 2.99)
74
77
  vcr (~> 2.5)
@@ -30,7 +30,7 @@ Gem::Specification.new do |s|
30
30
  s.add_dependency "hirb", "~> 0.6"
31
31
 
32
32
  s.add_development_dependency "mocha"
33
- s.add_development_dependency "pry"
33
+ s.add_development_dependency "pry-remote"
34
34
  s.add_development_dependency "rake"
35
35
  s.add_development_dependency "rspec", "~> 2.99"
36
36
  s.add_development_dependency "vcr", "~> 2.5"
@@ -0,0 +1,19 @@
1
+ require "singleton"
2
+
3
+ module Brightbox
4
+ module CLI
5
+ class Config
6
+ include Singleton
7
+
8
+ attr_accessor :config
9
+ end
10
+ end
11
+
12
+ def self.config
13
+ CLI::Config.instance.config
14
+ end
15
+
16
+ def self.config=(replacement)
17
+ CLI::Config.instance.config = replacement
18
+ end
19
+ end
@@ -17,7 +17,7 @@ module Brightbox
17
17
  end
18
18
 
19
19
  def self.all
20
- if $config.using_application?
20
+ if Brightbox.config.using_application?
21
21
  conn.accounts.all
22
22
  else
23
23
  [conn.account]
@@ -25,7 +25,7 @@ module Brightbox
25
25
  end
26
26
 
27
27
  def self.get(id)
28
- if $config.using_application?
28
+ if Brightbox.config.using_application?
29
29
  a = conn.accounts.get(id)
30
30
  else
31
31
  a = conn.account
@@ -22,7 +22,7 @@ module Brightbox
22
22
  if @@connection_manager
23
23
  @@connection_manager.fetch_connection(require_account?)
24
24
  else
25
- @@connection_manager = Brightbox::ConnectionManager.new($config.to_fog)
25
+ @@connection_manager = Brightbox::ConnectionManager.new(Brightbox.config.to_fog)
26
26
  @@connection_manager.fetch_connection(require_account?)
27
27
  end
28
28
  end
@@ -52,7 +52,7 @@ module Brightbox
52
52
  else
53
53
  raise InvalidArguments, "Can't initialize #{self.class} with #{m.inspect}"
54
54
  end
55
- $config.cache_id(@id) if $config.respond_to?(:cache_id)
55
+ Brightbox.config.cache_id(@id) if Brightbox.config.respond_to?(:cache_id)
56
56
  end
57
57
 
58
58
  def fog_model
@@ -164,7 +164,7 @@ module Brightbox
164
164
  if value
165
165
  value
166
166
  else
167
- $config.cache_id id
167
+ Brightbox.config.cache_id id
168
168
  @cache[id] = get(id)
169
169
  end
170
170
  end
@@ -179,7 +179,7 @@ module Brightbox
179
179
  @cache = {}
180
180
  all.each do |f|
181
181
  @cache[f.id] = f
182
- $config.cache_id f.id
182
+ Brightbox.config.cache_id f.id
183
183
  end
184
184
  end
185
185
 
@@ -16,7 +16,7 @@ module Brightbox
16
16
  raise "Invalid account-id"
17
17
  end
18
18
 
19
- $config.save_default_account(account_id)
19
+ Brightbox.config.save_default_account(account_id)
20
20
  end
21
21
  end
22
22
  end
@@ -8,7 +8,7 @@ module Brightbox
8
8
  cmd.command [:list] do |c|
9
9
 
10
10
  c.action do |global_options, _options, _args|
11
- if $config.using_application?
11
+ if Brightbox.config.using_application?
12
12
  # Collaborating Accounts are combined from owned and collaborations
13
13
  accounts = CollaboratingAccount.all
14
14
  else
@@ -14,7 +14,7 @@ module Brightbox
14
14
 
15
15
  c.action do |_global_options, options, args|
16
16
 
17
- info "Using config file #{$config.config_filename}"
17
+ info "Using config file #{Brightbox.config.config_filename}"
18
18
 
19
19
  client_id = args.shift
20
20
  secret = args.shift
@@ -39,7 +39,7 @@ module Brightbox
39
39
  :api_url => api_url,
40
40
  :auth_url => auth_url
41
41
  }
42
- $config.add_section(calias, client_id, secret, options)
42
+ Brightbox.config.add_section(calias, client_id, secret, options)
43
43
  end
44
44
  end
45
45
  end
@@ -7,16 +7,16 @@ module Brightbox
7
7
 
8
8
  c.action do |_global_options, _options, args|
9
9
 
10
- info "Using config file #{$config.config_filename}"
10
+ info "Using config file #{Brightbox.config.config_filename}"
11
11
  calias = args.shift
12
12
 
13
13
  if calias.nil?
14
14
  raise "You must specify the api alias you want to set as the default"
15
15
  end
16
16
 
17
- if $config.client_named?(calias)
17
+ if Brightbox.config.client_named?(calias)
18
18
  info "Setting #{calias} as default api client"
19
- $config.set_default_client(calias)
19
+ Brightbox.config.set_default_client(calias)
20
20
  else
21
21
  raise "An api client with the alias #{calias} does not exist in the config"
22
22
  end
@@ -8,21 +8,21 @@ module Brightbox
8
8
 
9
9
  c.action do |global_options, _options, _args|
10
10
 
11
- info "Using config file #{$config.config_filename}"
11
+ info "Using config file #{Brightbox.config.config_filename}"
12
12
 
13
- clients = $config.clients.map do |cid|
14
- c = $config[cid]
13
+ clients = Brightbox.config.section_names.map do |cid|
14
+ c = Brightbox.config[cid]
15
15
  calias = c["alias"] || cid
16
16
 
17
17
  # Append a star for the configured default client
18
- if $config.default_client == cid && $config.has_multiple_clients?
18
+ if Brightbox.config.default_client == cid && Brightbox.config.has_multiple_clients?
19
19
  calias = "*#{calias}"
20
20
  end
21
21
 
22
22
  {
23
23
  :alias => calias,
24
- :client_id => c["client_id"],
25
- :secret => c["secret"],
24
+ :client_id => c["client_id"] || Brightbox::EMBEDDED_APP_ID,
25
+ :secret => c["secret"] || Brightbox::EMBEDDED_APP_SECRET,
26
26
  :api_url => c["api_url"],
27
27
  :auth_url => c["auth_url"] || c["api_url"]
28
28
  }
@@ -7,7 +7,7 @@ module Brightbox
7
7
 
8
8
  c.action do |_global_options, _options, args|
9
9
 
10
- info "Using config file #{$config.config_filename}"
10
+ info "Using config file #{Brightbox.config.config_filename}"
11
11
 
12
12
  calias = args.shift
13
13
 
@@ -15,8 +15,8 @@ module Brightbox
15
15
  raise "You must specify the api alias you want to remove"
16
16
  end
17
17
 
18
- client_config = $config.clients.find do |config|
19
- $config[config]["alias"] == calias
18
+ client_config = Brightbox.config.section_names.find do |config|
19
+ Brightbox.config[config]["alias"] == calias
20
20
  end
21
21
 
22
22
  if client_config.nil?
@@ -24,7 +24,7 @@ module Brightbox
24
24
  end
25
25
 
26
26
  info "Removing api client #{calias}"
27
- $config.delete_section client_config
27
+ Brightbox.config.delete_section client_config
28
28
  end
29
29
  end
30
30
  end
@@ -15,7 +15,7 @@ module Brightbox
15
15
 
16
16
  require 'highline'
17
17
 
18
- info "Using config file #{$config.config_filename}"
18
+ info "Using config file #{Brightbox.config.config_filename}"
19
19
 
20
20
  email = args.shift
21
21
  client_id = args.shift
@@ -57,7 +57,7 @@ module Brightbox
57
57
  :api_url => api_url,
58
58
  :auth_url => auth_url
59
59
  }
60
- $config.add_section(calias, client_id, secret, options)
60
+ Brightbox.config.add_section(calias, client_id, secret, options)
61
61
  end
62
62
  end
63
63
  end
@@ -0,0 +1,53 @@
1
+ module Brightbox
2
+ command [:login] do |cmd|
3
+ cmd.desc I18n.t("login.desc")
4
+ cmd.arg_name "email"
5
+
6
+ cmd.desc "password, if not specified you will be prompted"
7
+ cmd.flag [:p, "password"]
8
+
9
+ cmd.desc "default account"
10
+ cmd.flag [:"default-account"]
11
+
12
+ cmd.flag [:"application-id"]
13
+ cmd.flag [:"application-secret"]
14
+
15
+ cmd.flag [:"api-url"]
16
+ cmd.flag [:"auth-url"]
17
+
18
+ cmd.action do |_global_options, options, args|
19
+ config_name = args.shift
20
+ email = config_name
21
+
22
+ raise "You must specify your email address" if email.nil?
23
+
24
+ password = options[:p]
25
+ password = Brightbox.config.gpg_password unless password
26
+
27
+ if !password || password.empty?
28
+ require "highline"
29
+ highline = HighLine.new
30
+ password = highline.ask("Enter your password : ") { |q| q.echo = false }
31
+ end
32
+ raise "You must specify your Brightbox password." if password.empty?
33
+
34
+ api_url = options[:"api-url"] || DEFAULT_API_ENDPOINT
35
+ auth_url = options[:"auth-url"] || api_url
36
+
37
+ section_options = {
38
+ :client_name => config_name,
39
+ :alias => config_name,
40
+ :username => email,
41
+ :password => password,
42
+ :api_url => api_url,
43
+ :auth_url => auth_url
44
+ }
45
+ section_options[:default_account] = options[:"default-account"] if options[:"default-account"]
46
+
47
+ section_options[:client_id] = options[:"application-id"] if options[:"application-id"]
48
+ section_options[:secret] = options[:"application-secret"] if options[:"application-secret"]
49
+
50
+ Brightbox.config.add_login(email, password, section_options)
51
+ end
52
+ end
53
+ end
@@ -22,6 +22,11 @@ module Brightbox
22
22
  end
23
23
  end
24
24
 
25
+ def determine_account(preferred_account)
26
+ return preferred_account if preferred_account
27
+ return config[client_name]["default_account"] unless client_name.nil?
28
+ end
29
+
25
30
  def account
26
31
  if defined?(@account) && @account
27
32
  @account
@@ -4,7 +4,7 @@ module Brightbox
4
4
  attr_writer :access_token, :refresh_token
5
5
 
6
6
  def access_token_filename
7
- file_name = "#{client_name}.oauth_token"
7
+ file_name = "#{base_token_name}.oauth_token"
8
8
  @access_token_filename ||= File.join(config_directory, file_name)
9
9
  end
10
10
 
@@ -20,7 +20,7 @@ module Brightbox
20
20
  end
21
21
 
22
22
  def refresh_token_filename
23
- file_name = "#{client_name}.refresh_token"
23
+ file_name = "#{base_token_name}.refresh_token"
24
24
  @refresh_token_filename ||= File.join(config_directory, file_name)
25
25
  end
26
26
 
@@ -55,7 +55,7 @@ module Brightbox
55
55
  # This attempts to renew access (and refresh) tokens for the current
56
56
  # configuration based on the current connection.
57
57
  #
58
- # @note $config and Api.conn are actually two different worlds and should
58
+ # @note Brightbox.config and Api.conn are actually two different worlds and should
59
59
  # be merged (so a configuration holds the current connection)
60
60
  #
61
61
  def renew_tokens(options = {})
@@ -234,6 +234,13 @@ module Brightbox
234
234
  def cached_refresh_token
235
235
  File.open(refresh_token_filename, "r") { |fl| fl.read.chomp }
236
236
  end
237
+
238
+ private
239
+
240
+ def base_token_name
241
+ return nil if client_name.nil?
242
+ client_name.gsub("/", "_")
243
+ end
237
244
  end
238
245
  end
239
246
  end
@@ -2,21 +2,17 @@ module Brightbox
2
2
  module Config
3
3
  module Cache
4
4
  def cache_path
5
- if @cache_path
6
- @cache_path
7
- else
8
- @cache_path = File.join(config_directory, 'cache')
9
- unless File.exist? @cache_path
10
- begin
11
- FileUtils.mkpath @cache_path
12
- rescue Errno::EEXIST
13
- end
14
- end
15
- @cache_path
16
- end
5
+ File.join(config_directory, "cache")
17
6
  end
18
7
 
19
8
  def cache_id(cid)
9
+ return if cid.nil?
10
+ unless File.exist?(cache_path)
11
+ begin
12
+ FileUtils.mkpath(cache_path)
13
+ rescue Errno::EEXIST
14
+ end
15
+ end
20
16
  FileUtils.touch(File.join(cache_path, cid)) unless cid.nil?
21
17
  end
22
18
  end
@@ -8,12 +8,16 @@ module Brightbox
8
8
 
9
9
  # Is the currently selected config using user application details?
10
10
  def using_application?
11
- config_identifier_match_prefix?("app")
11
+ if client_name
12
+ !config[client_name]["username"].nil?
13
+ else
14
+ raise NoSelectedClientError, NO_CLIENT_MESSAGE
15
+ end
12
16
  end
13
17
 
14
18
  # Does this config have multiple clients defined within?
15
19
  def has_multiple_clients?
16
- clients.size > 1
20
+ section_names.size > 1
17
21
  end
18
22
 
19
23
  # Does the currently selected client have an alias and a config section
@@ -32,44 +36,9 @@ module Brightbox
32
36
  selected_config["alias"] || client_name
33
37
  end
34
38
 
35
- # Returns a client name or raises depending on a number of factors.
36
- #
37
- # FIXME: This combines too much decision making into what appears to be a
38
- # getter.
39
- #
40
- # If a default_client is not set and there are more than one client it
41
- # raises an error because it is ambiguous which to use.
42
- #
43
- # If +force_default_config+ is passed in as false (for +config+ commands)
44
- # then it attempts to use the first config
45
- #
46
- # Calling +client_name+ within the +config+ command will break the config
47
- # command because you can no longer manage your config (and set a default)
48
- #
49
- # This has been fixed by short circuiting to return +nil+ in that case which
50
- # should never exist now.
51
- #
52
- def client_name
53
- if @client_name
54
- @client_name
55
- else
56
- # If we do not require a default client do not error if one is not set
57
- # TODO: Remove when certain unused
58
- if @options[:force_default_config] == false
59
- @client_name = default_client
60
- else
61
- # Is client ambigious?
62
- if default_client.nil? && clients.length > 1
63
- raise AmbiguousClientError, AMBIGUOUS_CLIENT_ERROR
64
- end
65
- @client_name = default_client || clients.first
66
- end
67
- end
68
- end
69
-
70
39
  # Returns the actual client ID with no risk of an alias
71
40
  def client_id
72
- selected_config["client_id"]
41
+ selected_config["client_id"] || Brightbox::EMBEDDED_APP_ID
73
42
  end
74
43
 
75
44
  # @todo Account for "core" section
@@ -104,6 +73,13 @@ module Brightbox
104
73
  @default_client = nil
105
74
  end
106
75
 
76
+ def determine_client(preferred_client = nil)
77
+ return preferred_client unless preferred_client.nil?
78
+ return default_client unless default_client.nil?
79
+ section_names.first unless section_names.empty?
80
+ nil
81
+ end
82
+
107
83
  private
108
84
 
109
85
  # If the prefix is in the client ID (identifier not alias) be +true+
@@ -1,6 +1,63 @@
1
1
  module Brightbox
2
2
  module Config
3
3
  module Sections
4
+ #
5
+ # @param [String] config_alias The section name usually `email` but `email/suffix` allowed.
6
+ # @param [String] password
7
+ # @param [Hash] options
8
+ # @option options [String] :api_url
9
+ # @option options [String] :auth_url
10
+ # @option options [String] :default_account
11
+ # @option options [String] :client_id
12
+ # @option options [String] :secret
13
+ #
14
+ def add_login(config_alias, password, options = {})
15
+ # If a custom alias is passed, used that for the config header, otherwise use email
16
+ email = config_alias.split("/").first
17
+ config_section = config[config_alias]
18
+
19
+ info "Creating new client config #{config_alias}" if config_section.empty?
20
+
21
+ config_section["username"] = email unless config_section["username"]
22
+
23
+ config_section["api_url"] = options[:api_url] if options.key?(:api_url)
24
+ config_section["api_url"] = DEFAULT_API_ENDPOINT unless config_section["api_url"]
25
+
26
+ config_section["auth_url"] = options[:auth_url] if options.key?(:auth_url)
27
+ config_section["auth_url"] = config_section["api_url"]
28
+
29
+ config_section["default_account"] = options[:default_account] if options.key?(:default_account)
30
+
31
+ config_section["client_id"] = options[:client_id] if options.key?(:client_id)
32
+ config_section["secret"] = options[:secret] if options.key?(:secret)
33
+
34
+ dirty!
35
+
36
+ self.client_name = client_alias
37
+
38
+ # Renew tokens via config...
39
+ #
40
+ # Part of the "login" behaviour is to always refresh them
41
+ #
42
+ begin
43
+ flush_access_token!
44
+ renew_tokens(:client_name => config_alias, :password => password)
45
+ rescue => e
46
+ error "Something went wrong trying to refresh new tokens #{e.message}"
47
+ end
48
+
49
+ # Try to determine a default account
50
+ unless default_account == find_or_set_default_account
51
+ info "The default account of #{default_account} has been selected"
52
+ end
53
+
54
+ # If only client then set it as the default
55
+ set_default_client(client_alias) unless default_client
56
+
57
+ # Ensure all our config changes are now saved
58
+ save
59
+ end
60
+
4
61
  #
5
62
  # @param [String] client_alias
6
63
  # @param [String] client_id
@@ -12,26 +69,26 @@ module Brightbox
12
69
  # @option options [String] :auth_url
13
70
  #
14
71
  def add_section(client_alias, client_id, secret, options)
15
- client_config = config[client_alias]
16
- if client_config.empty?
72
+ config_section = config[client_alias]
73
+ if config_section.empty?
17
74
  info "Creating new client config #{client_alias}"
18
75
  else
19
76
  old_calias = client_alias
20
77
 
21
- deduplicator = Brightbox::Config::SectionNameDeduplicator.new(client_alias, clients)
78
+ deduplicator = Brightbox::Config::SectionNameDeduplicator.new(client_alias, section_names)
22
79
  client_alias = deduplicator.next
23
80
  # Need to open the new config again
24
- client_config = config[client_alias]
81
+ config_section = config[client_alias]
25
82
 
26
83
  info "A client config named #{old_calias} already exists using #{client_alias} instead"
27
84
  end
28
85
 
29
- client_config["alias"] = client_alias
30
- client_config["client_id"] = client_id
31
- client_config["username"] = options[:username]
32
- client_config["secret"] = secret
33
- client_config["api_url"] = options[:api_url] || DEFAULT_API_ENDPOINT
34
- client_config["auth_url"] = options[:auth_url] || client_config["api_url"]
86
+ config_section["alias"] = client_alias
87
+ config_section["client_id"] = client_id
88
+ config_section["username"] = options[:username]
89
+ config_section["secret"] = secret
90
+ config_section["api_url"] = options[:api_url] || DEFAULT_API_ENDPOINT
91
+ config_section["auth_url"] = options[:auth_url] || config_section["api_url"]
35
92
 
36
93
  dirty!
37
94
 
@@ -85,13 +142,13 @@ module Brightbox
85
142
  config[section_name]
86
143
  end
87
144
 
88
- # Returns the clients in the config
145
+ # Returns the section names in the config
89
146
  #
90
147
  # @return [Array<String>]
91
148
  #
92
- def clients
149
+ def section_names
93
150
  # Exclude the global "core" section
94
- config.sections.select { |s| s != 'core' }
151
+ config.sections.reject { |s| %w(core alias).include?(s) }
95
152
  end
96
153
 
97
154
  private
@@ -3,7 +3,7 @@ module Brightbox
3
3
  class UserApplication
4
4
  # FIXME: api_url should use fog's underlying default
5
5
  #
6
- NON_BLANK_KEYS = %w(api_url client_id secret username)
6
+ NON_BLANK_KEYS = %w(api_url username)
7
7
 
8
8
  attr_accessor :selected_config, :client_name
9
9
 
@@ -19,8 +19,8 @@ module Brightbox
19
19
  :provider => 'Brightbox',
20
20
  :brightbox_api_url => selected_config['api_url'],
21
21
  :brightbox_auth_url => selected_config['auth_url'] || selected_config['api_url'],
22
- :brightbox_client_id => selected_config['client_id'],
23
- :brightbox_secret => selected_config['secret'],
22
+ :brightbox_client_id => client_id,
23
+ :brightbox_secret => client_secret,
24
24
  :persistent => persistent?
25
25
  }
26
26
  end
@@ -55,6 +55,14 @@ module Brightbox
55
55
 
56
56
  private
57
57
 
58
+ def client_id
59
+ selected_config["client_id"] || Brightbox::EMBEDDED_APP_ID
60
+ end
61
+
62
+ def client_secret
63
+ selected_config["secret"] || Brightbox::EMBEDDED_APP_SECRET
64
+ end
65
+
58
66
  def persistent?
59
67
  if selected_config["persistent"] == "false"
60
68
  false
@@ -68,8 +76,8 @@ module Brightbox
68
76
  :provider => 'Brightbox',
69
77
  :brightbox_api_url => selected_config['api_url'],
70
78
  :brightbox_auth_url => selected_config['auth_url'] || selected_config['api_url'],
71
- :brightbox_client_id => selected_config['client_id'],
72
- :brightbox_secret => selected_config['secret'],
79
+ :brightbox_client_id => client_id,
80
+ :brightbox_secret => client_secret,
73
81
  :brightbox_username => selected_config["username"],
74
82
  :persistent => persistent?
75
83
  }