Skip to content

Commit

Permalink
darker fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Mar 6, 2023
1 parent 4c14565 commit 80c3ba4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
41 changes: 28 additions & 13 deletions plugins/inventory/aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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'])

Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand Down
68 changes: 26 additions & 42 deletions tests/unit/plugins/inventory/test_aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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(
Expand Down

0 comments on commit 80c3ba4

Please sign in to comment.