Skip to content

Commit

Permalink
Merge pull request #643 from reef-technologies/add-environment-arg
Browse files Browse the repository at this point in the history
Add --environment for providing custom realm #642
  • Loading branch information
ppolewicz authored Jul 14, 2020
2 parents cd56184 + 6f1317f commit 9df1ae9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ For advanced users, a hidden option `--logConfig <filename.ini>` can be used to

# Release History

## Not released yet

* Add `--environment` internal parameter for `authorize-account`

## 2.0.0 (2020-06-25)

Changes:
Expand Down
30 changes: 20 additions & 10 deletions b2/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,20 @@ class AuthorizeAccount(Command):

@classmethod
def _setup_parser(cls, parser):
parser.add_argument('--dev', action='store_true', help=argparse.SUPPRESS)
parser.add_argument('--staging', action='store_true', help=argparse.SUPPRESS)
realm_group = parser.add_mutually_exclusive_group()
realm_group.add_argument('--dev', action='store_true', help=argparse.SUPPRESS)
realm_group.add_argument('--staging', action='store_true', help=argparse.SUPPRESS)
realm_group.add_argument('--environment', help=argparse.SUPPRESS)

parser.add_argument('applicationKeyId', nargs='?')
parser.add_argument('applicationKey', nargs='?')

def run(self, args):
# Handle internal options for testing inside Backblaze. These
# are not documented in the usage string.
realm = 'production'
if args.staging:
realm = 'staging'
if args.dev:
realm = 'dev'
# Handle internal options for testing inside Backblaze.
# These are not documented in the usage string.
realm = self._get_realm(args)

url = self.api.account_info.REALM_URLS[realm]
url = self.api.account_info.REALM_URLS.get(realm, realm)
self._print('Using %s' % url)

if args.applicationKeyId is None:
Expand Down Expand Up @@ -339,6 +338,17 @@ def run(self, args):
self._print_stderr('ERROR: unable to authorize account: ' + str(e))
return 1

@classmethod
def _get_realm(cls, args):
if args.dev:
return 'dev'
if args.staging:
return 'staging'
if args.environment:
return args.environment

return 'production'


@B2.register_subcommand
class CancelAllUnfinishedLargeFiles(Command):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
arrow>=0.8.0,<0.13.1; python_version <= '3.4'
arrow>=0.8.0; python_version > '3.4'
b2sdk>=1.1.0,<1.2.0
b2sdk>=1.1.3,<1.2.0
class-registry==2.1.2
six>=1.13
19 changes: 19 additions & 0 deletions test/test_console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ def test_authorize_using_env_variables(self):
# Auth token should be in account info now
assert self.account_info.get_account_auth_token() is not None

def test_authorize_towards_custom_realm(self):
# Initial condition
assert self.account_info.get_account_auth_token() is None

# Authorize an account with a good api key.
expected_stdout = """
Using http://custom.example.com
"""

self._run_command(
[
'authorize-account', '--environment', 'http://custom.example.com', self.account_id,
self.master_key
], expected_stdout, '', 0
)

# Auth token should be in account info now
assert self.account_info.get_account_auth_token() is not None

def test_create_key_and_authorize_with_it(self):
# Start with authorizing with the master key
self._authorize_account()
Expand Down

0 comments on commit 9df1ae9

Please sign in to comment.