def get_historical_accounts(): """Fetches valid accounts from SWAG if enabled or a list accounts.""" if os.environ.get('SWAG_BUCKET', False): swag_opts = { 'swag.type': 's3', 'swag.bucket_name': os.environ['SWAG_BUCKET'], 'swag.data_file': os.environ.get('SWAG_DATA_FILE', 'accounts.json'), 'swag.region': os.environ.get('SWAG_REGION', 'us-east-1') } swag = SWAGManager(**parse_swag_config_options(swag_opts)) search_filter = f"[?provider=='aws' && owner=='{os.environ['SWAG_OWNER']}' && account_status!='deleted'" if parse_boolean(os.environ.get('TEST_ACCOUNTS_ONLY')): search_filter += " && environment=='test'" search_filter += ']' accounts = swag.get_service_enabled('historical', search_filter=search_filter) else: accounts = [{ 'id': account_id } for account_id in os.environ['ENABLED_ACCOUNTS'].split(',')] return accounts
def _prep_accounts(account_names): """ Convert CLI provided account names into list of accounts from SWAG. Considers account aliases as well as account names. Returns a list of account numbers """ matching_accounts = list() account_names = account_names.split(',') account_names = {name.lower().strip() for name in account_names} # create a new copy of the account_names list so we can remove accounts as needed for account in list(account_names): if re.match('\d{12}', account): account_names.remove(account) matching_accounts.append(account) if not account_names: return matching_accounts try: current_app.logger.info('getting bucket {}'.format( current_app.config.get('SWAG_BUCKET'))) swag = SWAGManager(**parse_swag_config_options(current_app.config.get('SWAG_OPTS'))) all_accounts = swag.get_all(current_app.config.get('SWAG_FILTER')) service_enabled_requirement = current_app.config.get('SWAG_SERVICE_ENABLED_REQUIREMENT', None) if service_enabled_requirement: all_accounts = swag.get_service_enabled(service_enabled_requirement, accounts_list=all_accounts) except (KeyError, InvalidSWAGDataException, Exception) as e: current_app.logger.error('Account names passed but SWAG not configured or unavailable: {}'.format(e)) if 'all' in account_names: return [account['id'] for account in all_accounts] lookup = {account['name']: Bunch(account) for account in all_accounts} for account in all_accounts: # get the right key, depending on whether we're using swag v1 or v2 alias_key = 'aliases' if account['schemaVersion'] == '2' else 'alias' for alias in account[alias_key]: lookup[alias] = Bunch(account) for name in account_names: if name not in lookup: current_app.logger.warn('Could not find an account named %s' % name) continue account_number = lookup[name].get('id', None) if account_number: matching_accounts.append(account_number) return matching_accounts
def test_file_backend_get_service_enabled_v1(vector_path): from swag_client.backend import SWAGManager from swag_client.util import parse_swag_config_options swag_opts = { 'swag.data_dir': vector_path, 'swag.namespace': 'valid_accounts_v1', 'swag.cache_expires': 0, 'swag.schema_version': 1 } swag = SWAGManager(**parse_swag_config_options(swag_opts)) enabled = swag.get_service_enabled('myService') assert len(enabled) == 1 enabled = swag.get_service_enabled('myService', region='us-east-1') assert len(enabled) == 1 enabled = swag.get_service_enabled('myService1') assert len(enabled) == 1 enabled = swag.get_service_enabled('myService1', region='us-east-1') assert len(enabled) == 1 enabled = swag.get_service_enabled('myService2', region='us-east-1') assert len(enabled) == 0 enabled = swag.get_service_enabled('myService2') assert len(enabled) == 0
def get_historical_accounts(): """Fetches valid accounts from SWAG if enabled or a list accounts.""" if os.environ.get('SWAG_BUCKET', False): swag_opts = { 'swag.type': 's3', 'swag.bucket_name': os.environ['SWAG_BUCKET'], 'swag.data_file': os.environ.get('SWAG_DATA_FILE', 'accounts.json'), 'swag.region': os.environ.get('SWAG_REGION', 'us-east-1') } swag = SWAGManager(**parse_swag_config_options(swag_opts)) accounts = swag.get_service_enabled( 'historical', search_filter="[?provider=='aws'] && [?owner=='{}']".format( os.environ['SWAG_OWNER'])) else: accounts = os.environ['ENABLED_ACCOUNTS'] return accounts