EC2: describe_availability_zones() now supports additional parameters (#7119)

This commit is contained in:
Bert Blommers 2023-12-12 09:32:39 -01:00 committed by GitHub
parent a26072e6d8
commit cb3696a809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 2 deletions

View File

@ -24,6 +24,7 @@ class Zone:
self.region_name = region_name
self.zone_id = zone_id
self.zone_type = zone_type
self.state = "available"
class RegionsAndZonesBackend:
@ -318,8 +319,15 @@ class RegionsAndZonesBackend:
return ret
def describe_availability_zones(
self, filters: Optional[List[Dict[str, Any]]] = None
self,
filters: Optional[List[Dict[str, Any]]] = None,
zone_names: Optional[List[str]] = None,
zone_ids: Optional[List[str]] = None,
) -> List[Zone]:
"""
The following parameters are supported: ZoneIds, ZoneNames, Filters
The following filters are supported: zone-id, zone-type, zone-name, region-name, state
"""
# We might not have any zones for the current region, if it was introduced recently
zones = self.zones.get(self.region_name, []) # type: ignore[attr-defined]
attr_pairs = (
@ -327,10 +335,13 @@ class RegionsAndZonesBackend:
("zone-type", "zone_type"),
("zone-name", "name"),
("region-name", "region_name"),
("state", "state"),
)
result = zones
if filters:
result = filter_resources(zones, filters, attr_pairs)
result = [r for r in result if not zone_ids or r.zone_id in zone_ids]
result = [r for r in result if not zone_names or r.name in zone_names]
return result
def get_zone_by_name(self, name: str) -> Optional[Zone]:

View File

@ -5,7 +5,11 @@ class AvailabilityZonesAndRegions(EC2BaseResponse):
def describe_availability_zones(self) -> str:
self.error_on_dryrun()
filters = self._filters_from_querystring()
zones = self.ec2_backend.describe_availability_zones(filters)
zone_names = self._get_multi_param("ZoneName")
zone_ids = self._get_multi_param("ZoneId")
zones = self.ec2_backend.describe_availability_zones(
filters, zone_names=zone_names, zone_ids=zone_ids
)
template = self.response_template(DESCRIBE_ZONES_RESPONSE)
return template.render(zones=zones)

View File

@ -38,6 +38,45 @@ def test_boto3_availability_zones():
assert region in rec["ZoneName"]
@mock_ec2
def test_availability_zones__parameters():
us_east = boto3.client("ec2", "us-east-1")
zones = us_east.describe_availability_zones(ZoneNames=["us-east-1b"])[
"AvailabilityZones"
]
assert len(zones) == 1
assert zones[0]["ZoneId"] == "use1-az1"
zones = us_east.describe_availability_zones(ZoneNames=["us-east-1a", "us-east-1b"])[
"AvailabilityZones"
]
assert len(zones) == 2
assert set([zone["ZoneId"] for zone in zones]) == {"use1-az1", "use1-az6"}
zones = us_east.describe_availability_zones(ZoneIds=["use1-az1"])[
"AvailabilityZones"
]
assert len(zones) == 1
assert zones[0]["ZoneId"] == "use1-az1"
zones = us_east.describe_availability_zones(
Filters=[{"Name": "state", "Values": ["unavailable"]}]
)["AvailabilityZones"]
assert zones == []
zones = us_east.describe_availability_zones(
Filters=[{"Name": "zone-id", "Values": ["use1-az2"]}]
)["AvailabilityZones"]
assert len(zones) == 1
assert zones[0]["ZoneId"] == "use1-az2"
zones = us_east.describe_availability_zones(
Filters=[{"Name": "zone-name", "Values": ["us-east-1b"]}]
)["AvailabilityZones"]
assert len(zones) == 1
assert zones[0]["ZoneId"] == "use1-az1"
@mock_ec2
def test_describe_availability_zones_dryrun():
client = boto3.client("ec2", region_name="us-east-1")