Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
ACCOUNT_NAME_PATTERN: use a regex instead of globbing
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Ermakov <alexey.ermakov@zalando.de>
  • Loading branch information
aermakov-zalando committed Aug 30, 2019
1 parent 87cfced commit dacfb39
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 38 deletions.
30 changes: 8 additions & 22 deletions sevenseconds/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fnmatch
import re
import click
import yaml
import os
Expand Down Expand Up @@ -39,7 +39,7 @@ def destroy(account_name, region):

@cli.command()
@click.argument('file', type=click.File('rb'))
@click.argument('account_name_pattern', nargs=-1)
@click.argument('account_name_pattern')
@click.option('--dry-run', is_flag=True)
@click.option('-P', '--max-procs',
help='Run up to max-procs processes at a time. Default CPU Count',
Expand All @@ -65,17 +65,11 @@ def destroy(account_name, region):
def configure(file, account_name_pattern, **options):
'''Configure one or more AWS account(s) matching the provided pattern
ACCOUNT_NAME_PATTERN are Unix shell style:
\b
* matches everything
? matches any single character
[seq] matches any character in seq
[!seq] matches any char not in seq
ACCOUNT_NAME_PATTERN is a regex that is matched against the full account name
Posible Enviroment Variables
AWS_PROFILE Connect to this Profile without SAML
SSLDIR Directory with all SSL-Files
Posible Enviroment Variables
AWS_PROFILE Connect to this Profile without SAML
SSLDIR Directory with all SSL-Files
'''
try:
config, sessions = _get_session(
Expand Down Expand Up @@ -197,19 +191,11 @@ def verify_trusted_networks(file, cidr_list):
def _get_session(msg, file, account_name_pattern, options):
config = load_config(file)
accounts = config.get('accounts', {})
account_names = []
if len(account_name_pattern) == 0:
if os.environ.get('AWS_PROFILE'):
account_name_pattern = {os.environ.get('AWS_PROFILE')}
else:
error('No AWS accounts given!')
raise

for pattern in account_name_pattern:
account_names.extend(sorted(fnmatch.filter(accounts.keys(), pattern)))
account_names = [account for account in accounts.keys() if re.fullmatch(account_name_pattern, account)]

if not account_names:
error('No configuration found for account {}'.format(', '.join(account_name_pattern)))
error('No configuration found for account {}'.format(account_name_pattern))
raise

account_name_length = max([len(x) for x in account_names])
Expand Down
17 changes: 1 addition & 16 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ def test_configure_nonexisting_account(monkeypatch):
assert 'No configuration found for account myaccount' in result.output


def test_configure_nonexisting_multi_account(monkeypatch):
runner = CliRunner()
config = {
'version': SUPPORTED_CONFIG_VERSION,
'accounts': {}
}

with runner.isolated_filesystem():
with open('config.yaml', 'w') as fd:
yaml.safe_dump(config, fd)
result = runner.invoke(cli, ['configure', 'config.yaml', 'myaccount', 'dummyaccount'], catch_exceptions=False)

assert 'No configuration found for account myaccount, dummyaccount' in result.output


def test_configure_missing_config_option(monkeypatch):

myboto3 = MagicMock(list_account_aliases=lambda *args, **vargs: {'AccountAliases': ['myaccount']},
Expand Down Expand Up @@ -92,7 +77,7 @@ def test_configure_missing_config_option(monkeypatch):
with runner.isolated_filesystem():
with open('config.yaml', 'w') as fd:
yaml.safe_dump(config, fd)
result = runner.invoke(cli, ['configure', 'config.yaml', 'my*'], catch_exceptions=False)
result = runner.invoke(cli, ['configure', 'config.yaml', 'my.*'], catch_exceptions=False)

assert 'Start configuration of: myaccount, mystaging' in result.output
assert 'Missing Option "admin_account" please set Account Name for Main-Account!' in result.output
Expand Down

0 comments on commit dacfb39

Please sign in to comment.