Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

feat(ec2): enhance subnet filtering with tag support in describe_subnets (#285)

Johannes Grumböckff8581a35d2c
diff --git a/ministack/services/ec2.py b/ministack/services/ec2.py
index 89115e38..94cf3676 100644
--- a/ministack/services/ec2.py
+++ b/ministack/services/ec2.py
@@ -750,6 +750,10 @@ def _matches_vpc_filters(vpc, filters):
tag_val = next((t["Value"] for t in tag_list if t["Key"] == tag_key), None)
if tag_val not in vals:
return False
+ elif name == "tag-key":
+ tag_list = _tags.get(vpc["VpcId"], [])
+ if not any(t["Key"] in vals for t in tag_list):
+ return False
return True
@@ -917,21 +921,40 @@ def _describe_subnets(p):
for subnet in _subnets.values():
if filter_ids and subnet["SubnetId"] not in filter_ids:
continue
- if filters:
- if "vpc-id" in filters and subnet["VpcId"] not in filters["vpc-id"]:
- continue
- if "availability-zone" in filters and subnet["AvailabilityZone"] not in filters["availability-zone"]:
- continue
- if "subnet-id" in filters and subnet["SubnetId"] not in filters["subnet-id"]:
- continue
- if "default-for-az" in filters:
- val = "true" if subnet.get("DefaultForAz") else "false"
- if val not in filters["default-for-az"]:
- continue
+ if not _matches_subnet_filters(subnet, filters):
+ continue
items += _subnet_xml(subnet)
return _xml(200, "DescribeSubnetsResponse", f"<subnetSet>{items}</subnetSet>")
+def _matches_subnet_filters(subnet, filters):
+ for name, vals in filters.items():
+ if name == "vpc-id":
+ if subnet["VpcId"] not in vals:
+ return False
+ elif name == "availability-zone":
+ if subnet["AvailabilityZone"] not in vals:
+ return False
+ elif name == "subnet-id":
+ if subnet["SubnetId"] not in vals:
+ return False
+ elif name == "default-for-az":
+ val = "true" if subnet.get("DefaultForAz") else "false"
+ if val not in vals:
+ return False
+ elif name.startswith("tag:"):
+ tag_key = name[4:]
+ tag_list = _tags.get(subnet["SubnetId"], [])
+ tag_val = next((t["Value"] for t in tag_list if t["Key"] == tag_key), None)
+ if tag_val not in vals:
+ return False
+ elif name == "tag-key":
+ tag_list = _tags.get(subnet["SubnetId"], [])
+ if not any(t["Key"] in vals for t in tag_list):
+ return False
+ return True
+
+
def _create_subnet(p):
vpc_id = _p(p, "VpcId") or _DEFAULT_VPC_ID
cidr = _p(p, "CidrBlock") or "10.0.1.0/24"
diff --git a/tests/test_ec2.py b/tests/test_ec2.py
index ee820819..fa9c6ef7 100644
--- a/tests/test_ec2.py
+++ b/tests/test_ec2.py
@@ -1158,7 +1158,31 @@ def test_ec2_default_subnets_three_azs(ec2):
assert s["MapPublicIpOnLaunch"] is True
-def test_describe_tags_filters(ec2):
+def test_ec2_describe_subnets_tags_filters(ec2):
+ vpc_id = ec2.create_vpc(CidrBlock="10.77.0.0/16")["Vpc"]["VpcId"]
+ subnet_id = ec2.create_subnet(VpcId=vpc_id, CidrBlock="10.77.1.0/24")["Subnet"]["SubnetId"]
+ ec2.create_tags(Resources=[subnet_id], Tags=[{"Key": "Tier", "Value": "private"}, {"Key": "Env", "Value": "dev"}])
+
+ resp = ec2.describe_subnets(Filters=[
+ {"Name": "vpc-id", "Values": [vpc_id]},
+ {"Name": "tag:Tier", "Values": ["private"]},
+ ])
+ assert any(s["SubnetId"] == subnet_id for s in resp["Subnets"])
+
+ resp = ec2.describe_subnets(Filters=[{"Name": "tag-key", "Values": ["Tier"]}])
+ assert any(s["SubnetId"] == subnet_id for s in resp["Subnets"])
+
+ resp = ec2.describe_subnets(Filters=[
+ {"Name": "vpc-id", "Values": [vpc_id]},
+ {"Name": "tag:Tier", "Values": ["public"]},
+ ])
+ assert all(s["SubnetId"] != subnet_id for s in resp["Subnets"])
+
+ ec2.delete_subnet(SubnetId=subnet_id)
+ ec2.delete_vpc(VpcId=vpc_id)
+
+
+def test_ec2_describe_tags_filters(ec2):
"""DescribeTags respects resource-id and key filters."""
# Create two instances and tag them differently
r1 = ec2.run_instances(ImageId="ami-test1", InstanceType="t2.micro", MinCount=1, MaxCount=1)