Merge pull request #2792 from bblommers/bugfix/718

EC2 - Allow filtering by multiple tags
This commit is contained in:
Steve Pulec 2020-03-15 16:40:33 -05:00 committed by GitHub
commit dfe0047077
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -252,7 +252,8 @@ def dhcp_configuration_from_querystring(querystring, option="DhcpConfiguration")
def filters_from_querystring(querystring_dict): def filters_from_querystring(querystring_dict):
response_values = {} response_values = {}
for key, value in querystring_dict.items(): last_tag_key = None
for key, value in sorted(querystring_dict.items()):
match = re.search(r"Filter.(\d).Name", key) match = re.search(r"Filter.(\d).Name", key)
if match: if match:
filter_index = match.groups()[0] filter_index = match.groups()[0]
@ -262,6 +263,10 @@ def filters_from_querystring(querystring_dict):
for filter_key, filter_value in querystring_dict.items() for filter_key, filter_value in querystring_dict.items()
if filter_key.startswith(value_prefix) if filter_key.startswith(value_prefix)
] ]
if value[0] == "tag-key":
last_tag_key = "tag:" + filter_values[0]
elif last_tag_key and value[0] == "tag-value":
response_values[last_tag_key] = filter_values
response_values[value[0]] = filter_values response_values[value[0]] = filter_values
return response_values return response_values
@ -329,6 +334,8 @@ def tag_filter_matches(obj, filter_name, filter_values):
tag_values = get_obj_tag_names(obj) tag_values = get_obj_tag_names(obj)
elif filter_name == "tag-value": elif filter_name == "tag-value":
tag_values = get_obj_tag_values(obj) tag_values = get_obj_tag_values(obj)
elif filter_name.startswith("tag:"):
tag_values = get_obj_tag_values(obj)
else: else:
tag_values = [get_obj_tag(obj, filter_name) or ""] tag_values = [get_obj_tag(obj, filter_name) or ""]

View File

@ -468,3 +468,36 @@ def test_delete_tag_empty_resource():
ex.exception.response["Error"]["Message"].should.equal( ex.exception.response["Error"]["Message"].should.equal(
"The request must contain the parameter resourceIdSet" "The request must contain the parameter resourceIdSet"
) )
@mock_ec2
def test_retrieve_resource_with_multiple_tags():
ec2 = boto3.resource("ec2", region_name="us-west-1")
blue, green = ec2.create_instances(ImageId="ANY_ID", MinCount=2, MaxCount=2)
ec2.create_tags(
Resources=[blue.instance_id],
Tags=[
{"Key": "environment", "Value": "blue"},
{"Key": "application", "Value": "api"},
],
)
ec2.create_tags(
Resources=[green.instance_id],
Tags=[
{"Key": "environment", "Value": "green"},
{"Key": "application", "Value": "api"},
],
)
green_instances = list(ec2.instances.filter(Filters=(get_filter("green"))))
green_instances.should.equal([green])
blue_instances = list(ec2.instances.filter(Filters=(get_filter("blue"))))
blue_instances.should.equal([blue])
def get_filter(color):
return [
{"Name": "tag-key", "Values": ["application"]},
{"Name": "tag-value", "Values": ["api"]},
{"Name": "tag-key", "Values": ["environment"]},
{"Name": "tag-value", "Values": [color]},
]