implement get_hosted_zone_count() api (#4814)

This commit is contained in:
Paul 2022-01-30 08:25:36 -05:00 committed by GitHub
parent 4f1dcc5529
commit f554ad3997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 1 deletions

View File

@ -64,7 +64,7 @@ route53
- [ ] get_health_check_last_failure_reason
- [ ] get_health_check_status
- [X] get_hosted_zone
- [ ] get_hosted_zone_count
- [X] get_hosted_zone_count
- [ ] get_hosted_zone_limit
- [X] get_query_logging_config
Return query logging config, if it exists.

View File

@ -498,6 +498,9 @@ class Route53Backend(BaseBackend):
raise NoSuchHostedZone(id_)
return the_zone
def get_hosted_zone_count(self):
return len(self.list_hosted_zones())
def get_hosted_zone_by_name(self, name):
for zone in self.list_hosted_zones():
if zone.name == name:

View File

@ -93,6 +93,12 @@ class Route53(BaseResponse):
template = Template(LIST_HOSTED_ZONES_BY_VPC_RESPONSE)
return 200, headers, template.render(zones=zones, xmlns=XMLNS)
def get_hosted_zone_count_response(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
num_zones = route53_backend.get_hosted_zone_count()
template = Template(GET_HOSTED_ZONE_COUNT_RESPONSE)
return 200, headers, template.render(zone_count=num_zones, xmlns=XMLNS)
@error_handler
def get_or_delete_hostzone_response(self, request, full_url, headers):
self.setup_class(request, full_url, headers)
@ -414,6 +420,11 @@ DELETE_HOSTED_ZONE_RESPONSE = """<DeleteHostedZoneResponse xmlns="https://route5
</ChangeInfo>
</DeleteHostedZoneResponse>"""
GET_HOSTED_ZONE_COUNT_RESPONSE = """<GetHostedZoneCountResponse> xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
<HostedZoneCount>{{ zone_count }}</HostedZoneCount>
</GetHostedZoneCountResponse>"""
GET_HOSTED_ZONE_RESPONSE = """<GetHostedZoneResponse xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
<HostedZone>
<Id>/hostedzone/{{ zone.id }}</Id>

View File

@ -18,6 +18,7 @@ url_paths = {
r"{0}/(?P<api_version>[\d_-]+)/hostedzone/(?P<zone_id>[^/]+)/rrset/?$": Route53().rrset_response,
r"{0}/(?P<api_version>[\d_-]+)/hostedzonesbyname": Route53().list_hosted_zones_by_name_response,
r"{0}/(?P<api_version>[\d_-]+)/hostedzonesbyvpc": Route53().list_hosted_zones_by_vpc_response,
r"{0}/(?P<api_version>[\d_-]+)/hostedzonecount": Route53().get_hosted_zone_count_response,
r"{0}/(?P<api_version>[\d_-]+)/healthcheck": Route53().health_check_response,
r"{0}/(?P<api_version>[\d_-]+)/healthcheck/(?P<health_check_id>[^/]+)$": Route53().health_check_response,
r"{0}/(?P<api_version>[\d_-]+)/tags/healthcheck/(?P<zone_id>[^/]+)$": tag_response1,

View File

@ -61,6 +61,57 @@ def test_delete_hosted_zone():
res.should.have.length_of(1)
@mock_route53
def test_get_hosted_zone_count_no_zones():
conn = boto3.client("route53", region_name="us-east-1")
zone_count = conn.get_hosted_zone_count()
zone_count.should.have.key("HostedZoneCount")
isinstance(zone_count["HostedZoneCount"], int).should.be.true
zone_count["HostedZoneCount"].should.be.equal(0)
@mock_route53
def test_get_hosted_zone_count_one_zone():
conn = boto3.client("route53", region_name="us-east-1")
zone = "a"
zone_name = f"test.{zone}.com."
conn.create_hosted_zone(
Name=zone_name,
CallerReference=str(hash("foo")),
HostedZoneConfig=dict(PrivateZone=False, Comment=f"test {zone} com"),
)
zone_count = conn.get_hosted_zone_count()
zone_count.should.have.key("HostedZoneCount")
isinstance(zone_count["HostedZoneCount"], int).should.be.true
zone_count["HostedZoneCount"].should.be.equal(1)
@mock_route53
def test_get_hosted_zone_count_many_zones():
conn = boto3.client("route53", region_name="us-east-1")
zones = {}
zone_indexes = []
for char in range(ord("a"), ord("d") + 1):
for char2 in range(ord("a"), ord("z") + 1):
zone_indexes.append(f"{chr(char)}{chr(char2)}")
# Create 100-ish zones and make sure we get 100 back. This works
# for 702 zones {a..zz}, but seemed a needless waste of
# time/resources.
for zone in zone_indexes:
zone_name = f"test.{zone}.com."
zones[zone] = conn.create_hosted_zone(
Name=zone_name,
CallerReference=str(hash("foo")),
HostedZoneConfig=dict(PrivateZone=False, Comment=f"test {zone} com"),
)
zone_count = conn.get_hosted_zone_count()
zone_count.should.have.key("HostedZoneCount")
isinstance(zone_count["HostedZoneCount"], int).should.be.true
zone_count["HostedZoneCount"].shouldnt.be.equal(0)
zone_count["HostedZoneCount"].should.be.equal(len(zone_indexes))
@mock_route53
def test_get_unknown_hosted_zone():
conn = boto3.client("route53", region_name="us-east-1")