diff --git a/plugins/inventory/aws_ec2.py b/plugins/inventory/aws_ec2.py index e4531d9a597..246bd606380 100644 --- a/plugins/inventory/aws_ec2.py +++ b/plugins/inventory/aws_ec2.py @@ -608,14 +608,22 @@ def _get_all_hostnames(self, instance, hostnames): return hostname_list - def _query(self, regions, include_filters, exclude_filters, strict_permissions, include_filter_and_logic, exclude_filter_and_logic): - ''' - :param regions: a list of regions to query - :param include_filters: a list of boto3 filter dictionaries - :param exclude_filters: a list of boto3 filter dictionaries - :param strict_permissions: a boolean determining whether to fail or ignore 403 error codes - - ''' + def _query( + self, + regions, + include_filters, + exclude_filters, + strict_permissions, + include_filter_and_logic, + exclude_filter_and_logic, + ): + """ + :param regions: a list of regions to query + :param include_filters: a list of boto3 filter dictionaries + :param exclude_filters: a list of boto3 filter dictionaries + :param strict_permissions: a boolean determining whether to fail or ignore 403 error codes + + """ instances = [] ids_to_ignore = [] @@ -631,11 +639,11 @@ def _build_aws_filters(filters_opts, apply_and_logic): for filter in _build_aws_filters(exclude_filters, exclude_filter_and_logic): for i in self._get_instances_by_region(regions, filter, strict_permissions): - ids_to_ignore.append(i['InstanceId']) + ids_to_ignore.append(i["InstanceId"]) for filter in _build_aws_filters(include_filters, include_filter_and_logic): for i in self._get_instances_by_region(regions, filter, strict_permissions): - if i['InstanceId'] not in ids_to_ignore: + if i["InstanceId"] not in ids_to_ignore: instances.append(i) ids_to_ignore.append(i['InstanceId']) @@ -731,8 +739,8 @@ def parse(self, inventory, loader, path, cache=True): strict_permissions = self.get_option('strict_permissions') allow_duplicated_hosts = self.get_option('allow_duplicated_hosts') - include_filter_and_logic = self.get_option('apply_and_logic_on_include_filters') - exclude_filter_and_logic = self.get_option('apply_and_logic_on_exclude_filters') + include_filter_and_logic = self.get_option("apply_and_logic_on_include_filters") + exclude_filter_and_logic = self.get_option("apply_and_logic_on_exclude_filters") hostvars_prefix = self.get_option("hostvars_prefix") hostvars_suffix = self.get_option("hostvars_suffix") @@ -747,7 +755,14 @@ def parse(self, inventory, loader, path, cache=True): result_was_cached, results = self.get_cached_result(path, cache) if not result_was_cached: - results = self._query(regions, include_filters, exclude_filters, strict_permissions, include_filter_and_logic, exclude_filter_and_logic) + results = self._query( + regions, + include_filters, + exclude_filters, + strict_permissions, + include_filter_and_logic, + exclude_filter_and_logic, + ) self._populate( results, diff --git a/tests/unit/plugins/inventory/test_aws_ec2.py b/tests/unit/plugins/inventory/test_aws_ec2.py index 28582e636d1..58a9842fdfd 100644 --- a/tests/unit/plugins/inventory/test_aws_ec2.py +++ b/tests/unit/plugins/inventory/test_aws_ec2.py @@ -436,12 +436,12 @@ def test_inventory_query(inventory, include_filters, exclude_filters, instances_ strict = False params = { - 'regions': regions, - 'strict_permissions': strict, - 'include_filters': [], - 'exclude_filters': [], - 'include_filter_and_logic': False, - 'exclude_filter_and_logic': False, + "regions": regions, + "strict_permissions": strict, + "include_filters": [], + "exclude_filters": [], + "include_filter_and_logic": False, + "exclude_filter_and_logic": False, } for u in include_filters: @@ -456,10 +456,7 @@ def test_inventory_query(inventory, include_filters, exclude_filters, instances_ def test_inventory_query_include_filters_with_and_logic(inventory): - - instances = [ - {'InstanceId': 2, 'name': 'instance-2'} - ] + instances = [{"InstanceId": 2, "name": "instance-2"}] inventory._get_instances_by_region = MagicMock() inventory._get_instances_by_region.side_effect = [instances] @@ -473,30 +470,22 @@ def test_inventory_query_include_filters_with_and_logic(inventory): ] params = { - 'regions': regions, - 'strict_permissions': strict, - 'include_filters': include_filters, - 'exclude_filters': [], - 'include_filter_and_logic': True, - 'exclude_filter_and_logic': False, + "regions": regions, + "strict_permissions": strict, + "include_filters": include_filters, + "exclude_filters": [], + "include_filter_and_logic": True, + "exclude_filter_and_logic": False, } - assert inventory._query(**params) == {'aws_ec2': instances} + assert inventory._query(**params) == {"aws_ec2": instances} - aws_filters = [ - {'Name': 'tag-key', 'Values': ['environment']}, - {'Name': 'tag:Project', 'Values': ['Ansible']} - ] - inventory._get_instances_by_region.assert_called_once_with( - regions, aws_filters, strict - ) + aws_filters = [{"Name": "tag-key", "Values": ["environment"]}, {"Name": "tag:Project", "Values": ["Ansible"]}] + inventory._get_instances_by_region.assert_called_once_with(regions, aws_filters, strict) def test_inventory_query_exclude_filters_with_and_logic(inventory): - - instances = [ - {'InstanceId': 2, 'name': 'instance-2'} - ] + instances = [{"InstanceId": 2, "name": "instance-2"}] inventory._get_instances_by_region = MagicMock() inventory._get_instances_by_region.side_effect = [instances] @@ -510,23 +499,18 @@ def test_inventory_query_exclude_filters_with_and_logic(inventory): ] params = { - 'regions': regions, - 'strict_permissions': strict, - 'include_filters': [], - 'exclude_filters': exclude_filters, - 'include_filter_and_logic': False, - 'exclude_filter_and_logic': True, + "regions": regions, + "strict_permissions": strict, + "include_filters": [], + "exclude_filters": exclude_filters, + "include_filter_and_logic": False, + "exclude_filter_and_logic": True, } - assert inventory._query(**params) == {'aws_ec2': []} + assert inventory._query(**params) == {"aws_ec2": []} - aws_filters = [ - {'Name': 'tag-key', 'Values': ['environment']}, - {'Name': 'tag:Project', 'Values': ['Ansible']} - ] - inventory._get_instances_by_region.assert_called_once_with( - regions, aws_filters, strict - ) + aws_filters = [{"Name": "tag-key", "Values": ["environment"]}, {"Name": "tag:Project", "Values": ["Ansible"]}] + inventory._get_instances_by_region.assert_called_once_with(regions, aws_filters, strict) @pytest.mark.parametrize(