EC2: describe_instances() should be able to filter by exact tag-values (#5980)

This commit is contained in:
Bert Blommers 2023-02-25 10:19:28 -01:00 committed by GitHub
parent 354c81b7e1
commit 5d7d096bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View File

@ -395,6 +395,8 @@ def tag_filter_matches(obj: Any, filter_name: str, filter_values: List[str]) ->
for tag_value in tag_values: for tag_value in tag_values:
if any(regex.match(tag_value) for regex in regex_filters): if any(regex.match(tag_value) for regex in regex_filters):
return True return True
if tag_value in filter_values:
return True
return False return False

View File

@ -1,5 +1,6 @@
import base64 import base64
import ipaddress import ipaddress
import json
import warnings import warnings
from unittest import SkipTest, mock from unittest import SkipTest, mock
from uuid import uuid4 from uuid import uuid4
@ -769,9 +770,15 @@ def test_get_instances_filtering_by_tag():
tag1_val = str(uuid4()) tag1_val = str(uuid4())
tag2_name = str(uuid4())[0:6] tag2_name = str(uuid4())[0:6]
tag2_val = str(uuid4()) tag2_val = str(uuid4())
tag3_name = str(uuid4())[0:6]
instance1.create_tags(Tags=[{"Key": tag1_name, "Value": tag1_val}]) instance1.create_tags(
instance1.create_tags(Tags=[{"Key": tag2_name, "Value": tag2_val}]) Tags=[
{"Key": tag1_name, "Value": tag1_val},
{"Key": tag2_name, "Value": tag2_val},
{"Key": tag3_name, "Value": json.dumps(["entry1", "entry2"])},
]
)
instance2.create_tags(Tags=[{"Key": tag1_name, "Value": tag1_val}]) instance2.create_tags(Tags=[{"Key": tag1_name, "Value": tag1_val}])
instance2.create_tags(Tags=[{"Key": tag2_name, "Value": "wrong value"}]) instance2.create_tags(Tags=[{"Key": tag2_name, "Value": "wrong value"}])
instance3.create_tags(Tags=[{"Key": tag2_name, "Value": tag2_val}]) instance3.create_tags(Tags=[{"Key": tag2_name, "Value": tag2_val}])
@ -812,6 +819,16 @@ def test_get_instances_filtering_by_tag():
res["Reservations"][0]["Instances"][0]["InstanceId"].should.equal(instance1.id) res["Reservations"][0]["Instances"][0]["InstanceId"].should.equal(instance1.id)
res["Reservations"][0]["Instances"][1]["InstanceId"].should.equal(instance3.id) res["Reservations"][0]["Instances"][1]["InstanceId"].should.equal(instance3.id)
# We should be able to use tags containing special characters
res = client.describe_instances(
Filters=[
{"Name": f"tag:{tag3_name}", "Values": [json.dumps(["entry1", "entry2"])]}
]
)
res["Reservations"].should.have.length_of(1)
res["Reservations"][0]["Instances"].should.have.length_of(1)
res["Reservations"][0]["Instances"][0]["InstanceId"].should.equal(instance1.id)
@mock_ec2 @mock_ec2
def test_get_instances_filtering_by_tag_value(): def test_get_instances_filtering_by_tag_value():