def get_aws_inventory(name): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') aws_obj = AwsInventory(name=name, cache_dir=cache_dir, access_key_id='access_key_id', secret_access_key='secret_access_key', session_token='session_token', region='region') return aws_obj
def inventory(config, filter_sources=tuple()): inventorySet = InventorySet() for source, srcCfg in config.sources(): if source == 'aws': # the cache directory for the original v1 config did not separate # out multiple aws profiles into subdirectories if config.version == 1: cache_dir = config.inventoryDir(AwsInventory.name) else: cache_dir = config.inventoryDir(AwsInventory.name, srcCfg['name']) if not os.path.exists(cache_dir): os.mkdir(cache_dir) from botocore.exceptions import ProfileNotFound try: inv = AwsInventory(cache_dir, **srcCfg) inventorySet.add(inv) except ProfileNotFound: logger.error("Unconfigured AWS profile configured.") sys.exit(1) elif source == 'csv': inv = CsvInventory(path=config.inventoryDir( source, srcCfg['file']), **srcCfg) inventorySet.add(inv) elif source == 'newrelic': proxies = {} if config.dig('inventory', 'http_proxy'): proxies['http'] = config.dig('inventory', 'http_proxy') elif 'HTTP_PROXY' in os.environ: proxies['http'] = os.environ['HTTP_PROXY'] elif 'http_proxy' in os.environ: proxies['http'] = os.environ['http_proxy'] if config.dig('inventory', 'https_proxy'): proxies['https'] = config.dig('inventory', 'https_proxy') elif 'HTTPS_PROXY' in os.environ: proxies['https'] = os.environ['HTTPS_PROXY'] elif 'https_proxy' in os.environ: proxies['https'] = os.environ['https_proxy'] inv = NewRelicInventory(data_path=config.inventoryDir( NewRelicInventory.name), proxies=proxies, **srcCfg) inventorySet.add(inv) return inventorySet
def test_inclusion_filtering(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') config = Config({'inventory': {'include_pattern': 'test.*'}}) aws_obj = AwsInventory(cache_dir=cache_dir, access_key_id='access_key_id', secret_access_key='secret_access_key', session_token='session_token', region='region') mock_inventory = mocker.patch.object(bridgy.inventory, 'inventory') mock_inventory.return_value = aws_obj all_instances = instances(config) expected_instances = [ Instance(name='test-forms', address='devbox', aliases=('devbox', 'ip-172-31-8-185.us-west-2.compute.internal', 'i-e54cbaeb'), source='aws'), Instance(name='test-account-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-139.us-west-2.compute.internal', 'i-f4d726fa'), source='aws'), Instance(name='test-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-141.us-west-2.compute.internal', 'i-f3d726fd'), source='aws'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-38.us-west-2.compute.internal', 'i-0f500447384e95942'), source='aws'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-39.us-west-2.compute.internal', 'i-0f500447384e95943'), source='aws') ] assert set(all_instances) == set(expected_instances)
def test_exclusion_filtering(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') config = Config({'inventory': {'exclude_pattern': 'test.*'}}) aws_obj = AwsInventory(cache_dir=cache_dir, access_key_id='access_key_id', secret_access_key='secret_access_key', session_token='session_token', region='region') inventorySet = InventorySet() inventorySet.add(aws_obj) mock_inventory = mocker.patch.object(bridgy.inventory, 'inventory') mock_inventory.return_value = inventorySet all_instances = instances(config) expected_instances = [ Instance(name='devlab-forms', address='devbox', aliases=('devbox', 'ip-172-31-0-138.us-west-2.compute.internal', 'i-f7d726f9'), source='aws', container_id=None, type='VM'), Instance(name='devlab-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-0-142.us-west-2.compute.internal', 'i-f5d726fb'), source='aws', container_id=None, type='VM'), Instance(name='devlab-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-140.us-west-2.compute.internal', 'i-f2d726fc'), source='aws', container_id=None, type='VM') ] assert set(all_instances) == set(expected_instances)
def test_set_inventory_bastion(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') aws_obj = AwsInventory(cache_dir=cache_dir, access_key_id='access_key_id', secret_access_key='secret_access_key', session_token='session_token', region='region', bastion={ 'address': 'someaddr', 'user': '******', 'options': 'someoptions' }) assert aws_obj.bastion.destination == 'someuser@someaddr' assert aws_obj.bastion.options == 'someoptions'
def test_aws_instances_profile(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') config_dir = os.path.join(test_dir, 'aws_configs') aws_obj = AwsInventory(cache_dir=cache_dir, profile='somewhere', region='region', config_path=config_dir) instances = aws_obj.instances() expected_instances = [ Instance(name='test-forms', address='devbox', aliases=('devbox', 'ip-172-31-8-185.us-west-2.compute.internal', 'i-e54cbaeb'), source='aws', container_id=None, type='VM'), Instance(name='devlab-forms', address='devbox', aliases=('devbox', 'ip-172-31-0-138.us-west-2.compute.internal', 'i-f7d726f9'), source='aws', container_id=None, type='VM'), Instance(name='test-account-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-139.us-west-2.compute.internal', 'i-f4d726fa'), source='aws', container_id=None, type='VM'), Instance(name='devlab-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-0-142.us-west-2.compute.internal', 'i-f5d726fb'), source='aws', container_id=None, type='VM'), Instance(name='devlab-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-140.us-west-2.compute.internal', 'i-f2d726fc'), source='aws', container_id=None, type='VM'), Instance(name='test-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-141.us-west-2.compute.internal', 'i-f3d726fd'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-38.us-west-2.compute.internal', 'i-0f500447384e95942'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-39.us-west-2.compute.internal', 'i-0f500447384e95943'), source='aws', container_id=None, type='VM') ] assert set(instances) == set(expected_instances) from botocore.exceptions import ProfileNotFound with pytest.raises(ProfileNotFound): aws_obj = AwsInventory(cache_dir=cache_dir, profile='some-unconfigured-profile', region='region', config_path=config_dir)
def test_aws_instances(mocker): test_dir = os.path.dirname(os.path.abspath(__file__)) cache_dir = os.path.join(test_dir, 'aws_stubs') aws_obj = AwsInventory(cache_dir=cache_dir, access_key_id='access_key_id', secret_access_key='secret_access_key', session_token='session_token', region='region') instances = aws_obj.instances() expected_instances = [ Instance(name='test-forms', address='devbox', aliases=('devbox', 'ip-172-31-8-185.us-west-2.compute.internal', 'i-e54cbaeb'), source='aws', container_id=None, type='VM'), Instance(name='devlab-forms', address='devbox', aliases=('devbox', 'ip-172-31-0-138.us-west-2.compute.internal', 'i-f7d726f9'), source='aws', container_id=None, type='VM'), Instance(name='test-account-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-139.us-west-2.compute.internal', 'i-f4d726fa'), source='aws', container_id=None, type='VM'), Instance(name='devlab-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-0-142.us-west-2.compute.internal', 'i-f5d726fb'), source='aws', container_id=None, type='VM'), Instance(name='devlab-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-140.us-west-2.compute.internal', 'i-f2d726fc'), source='aws', container_id=None, type='VM'), Instance(name='test-game-svc', address='devbox', aliases=('devbox', 'ip-172-31-0-141.us-west-2.compute.internal', 'i-f3d726fd'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-38.us-west-2.compute.internal', 'i-0f500447384e95942'), source='aws', container_id=None, type='VM'), Instance(name='test-pubsrv', address='devbox', aliases=('devbox', 'ip-172-31-2-39.us-west-2.compute.internal', 'i-0f500447384e95943'), source='aws', container_id=None, type='VM') ] assert set(instances) == set(expected_instances)