Merge pull request #860 from 2rs2ts/add-list-hosted-zones-by-name
Add ListHostedZonesByName
This commit is contained in:
		
						commit
						7eb5b60620
					
				@ -7,7 +7,7 @@ from .models import route53_backend
 | 
			
		||||
import xmltodict
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Route53 (BaseResponse):
 | 
			
		||||
class Route53(BaseResponse):
 | 
			
		||||
 | 
			
		||||
    def list_or_create_hostzone_response(self, request, full_url, headers):
 | 
			
		||||
        self.setup_class(request, full_url, headers)
 | 
			
		||||
@ -47,6 +47,32 @@ class Route53 (BaseResponse):
 | 
			
		||||
            template = Template(LIST_HOSTED_ZONES_RESPONSE)
 | 
			
		||||
            return 200, headers, template.render(zones=all_zones)
 | 
			
		||||
 | 
			
		||||
    def list_hosted_zones_by_name_response(self, request, full_url, headers):
 | 
			
		||||
        self.setup_class(request, full_url, headers)
 | 
			
		||||
        parsed_url = urlparse(full_url)
 | 
			
		||||
        query_params = parse_qs(parsed_url.query)
 | 
			
		||||
        dnsname = query_params.get("dnsname")
 | 
			
		||||
 | 
			
		||||
        if dnsname:
 | 
			
		||||
            dnsname = dnsname[0]    # parse_qs gives us a list, but this parameter doesn't repeat
 | 
			
		||||
            # return all zones with that name (there can be more than one)
 | 
			
		||||
            zones = [zone for zone in route53_backend.get_all_hosted_zones() if zone.name == dnsname]
 | 
			
		||||
        else:
 | 
			
		||||
            # sort by names, but with domain components reversed
 | 
			
		||||
            # see http://boto3.readthedocs.io/en/latest/reference/services/route53.html#Route53.Client.list_hosted_zones_by_name
 | 
			
		||||
 | 
			
		||||
            def sort_key(zone):
 | 
			
		||||
                domains = zone.name.split(".")
 | 
			
		||||
                if domains[-1] == "":
 | 
			
		||||
                    domains = domains[-1:] + domains[:-1]
 | 
			
		||||
                return ".".join(reversed(domains))
 | 
			
		||||
 | 
			
		||||
            zones = route53_backend.get_all_hosted_zones()
 | 
			
		||||
            zones = sorted(zones, key=sort_key)
 | 
			
		||||
 | 
			
		||||
        template = Template(LIST_HOSTED_ZONES_BY_NAME_RESPONSE)
 | 
			
		||||
        return 200, headers, template.render(zones=zones)
 | 
			
		||||
 | 
			
		||||
    def get_or_delete_hostzone_response(self, request, full_url, headers):
 | 
			
		||||
        self.setup_class(request, full_url, headers)
 | 
			
		||||
        parsed_url = urlparse(full_url)
 | 
			
		||||
@ -289,6 +315,25 @@ LIST_HOSTED_ZONES_RESPONSE = """<ListHostedZonesResponse xmlns="https://route53.
 | 
			
		||||
   <IsTruncated>false</IsTruncated>
 | 
			
		||||
</ListHostedZonesResponse>"""
 | 
			
		||||
 | 
			
		||||
LIST_HOSTED_ZONES_BY_NAME_RESPONSE = """<ListHostedZonesByNameResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
 | 
			
		||||
  <HostedZones>
 | 
			
		||||
      {% for zone in zones %}
 | 
			
		||||
      <HostedZone>
 | 
			
		||||
         <Id>/hostedzone/{{ zone.id }}</Id>
 | 
			
		||||
         <Name>{{ zone.name }}</Name>
 | 
			
		||||
         <Config>
 | 
			
		||||
            {% if zone.comment %}
 | 
			
		||||
                <Comment>{{ zone.comment }}</Comment>
 | 
			
		||||
            {% endif %}
 | 
			
		||||
           <PrivateZone>{{ zone.private_zone }}</PrivateZone>
 | 
			
		||||
         </Config>
 | 
			
		||||
         <ResourceRecordSetCount>{{ zone.rrsets|count  }}</ResourceRecordSetCount>
 | 
			
		||||
      </HostedZone>
 | 
			
		||||
      {% endfor %}
 | 
			
		||||
   </HostedZones>
 | 
			
		||||
   <IsTruncated>false</IsTruncated>
 | 
			
		||||
</ListHostedZonesByNameResponse>"""
 | 
			
		||||
 | 
			
		||||
CREATE_HEALTH_CHECK_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<CreateHealthCheckResponse xmlns="https://route53.amazonaws.com/doc/2013-04-01/">
 | 
			
		||||
  {{ health_check.to_xml() }}
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,7 @@ url_paths = {
 | 
			
		||||
    '{0}/(?P<api_version>[\d_-]+)/hostedzone$': Route53().list_or_create_hostzone_response,
 | 
			
		||||
    '{0}/(?P<api_version>[\d_-]+)/hostedzone/(?P<zone_id>[^/]+)$': Route53().get_or_delete_hostzone_response,
 | 
			
		||||
    '{0}/(?P<api_version>[\d_-]+)/hostedzone/(?P<zone_id>[^/]+)/rrset/?$': Route53().rrset_response,
 | 
			
		||||
    '{0}/(?P<api_version>[\d_-]+)/hostedzonesbyname': Route53().list_hosted_zones_by_name_response,
 | 
			
		||||
    '{0}/(?P<api_version>[\d_-]+)/healthcheck': Route53().health_check_response,
 | 
			
		||||
    '{0}/(?P<api_version>[\d_-]+)/tags/healthcheck/(?P<zone_id>[^/]+)$': tag_response1,
 | 
			
		||||
    '{0}/(?P<api_version>[\d_-]+)/tags/hostedzone/(?P<zone_id>[^/]+)$': tag_response2,
 | 
			
		||||
 | 
			
		||||
@ -361,8 +361,9 @@ def test_hosted_zone_private_zone_preserved_boto3():
 | 
			
		||||
    hosted_zones = conn.list_hosted_zones()
 | 
			
		||||
    hosted_zones["HostedZones"][0]["Config"]["PrivateZone"].should.equal(True)
 | 
			
		||||
 | 
			
		||||
    # zone = conn.list_hosted_zones_by_name(DNSName="testdns.aws.com.")
 | 
			
		||||
    # zone.config["PrivateZone"].should.equal(True)
 | 
			
		||||
    hosted_zones = conn.list_hosted_zones_by_name(DNSName="testdns.aws.com.")
 | 
			
		||||
    len(hosted_zones["HostedZones"]).should.equal(1)
 | 
			
		||||
    hosted_zones["HostedZones"][0]["Config"]["PrivateZone"].should.equal(True)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@mock_route53
 | 
			
		||||
@ -445,3 +446,48 @@ def test_list_or_change_tags_for_resource_request():
 | 
			
		||||
    response = conn.list_tags_for_resource(
 | 
			
		||||
        ResourceType='healthcheck', ResourceId=healthcheck_id)
 | 
			
		||||
    response['ResourceTagSet']['Tags'].should.be.empty
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@mock_route53
 | 
			
		||||
def test_list_hosted_zones_by_name():
 | 
			
		||||
    conn = boto3.client('route53', region_name='us-east-1')
 | 
			
		||||
    conn.create_hosted_zone(
 | 
			
		||||
        Name="test.b.com.",
 | 
			
		||||
        CallerReference=str(hash('foo')),
 | 
			
		||||
        HostedZoneConfig=dict(
 | 
			
		||||
            PrivateZone=True,
 | 
			
		||||
            Comment="test com",
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
    conn.create_hosted_zone(
 | 
			
		||||
        Name="test.a.org.",
 | 
			
		||||
        CallerReference=str(hash('bar')),
 | 
			
		||||
        HostedZoneConfig=dict(
 | 
			
		||||
            PrivateZone=True,
 | 
			
		||||
            Comment="test org",
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
    conn.create_hosted_zone(
 | 
			
		||||
        Name="test.a.org.",
 | 
			
		||||
        CallerReference=str(hash('bar')),
 | 
			
		||||
        HostedZoneConfig=dict(
 | 
			
		||||
            PrivateZone=True,
 | 
			
		||||
            Comment="test org 2",
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # test lookup
 | 
			
		||||
    zones = conn.list_hosted_zones_by_name(DNSName="test.b.com.")
 | 
			
		||||
    len(zones["HostedZones"]).should.equal(1)
 | 
			
		||||
    zones["HostedZones"][0]["Name"].should.equal("test.b.com.")
 | 
			
		||||
    zones = conn.list_hosted_zones_by_name(DNSName="test.a.org.")
 | 
			
		||||
    len(zones["HostedZones"]).should.equal(2)
 | 
			
		||||
    zones["HostedZones"][0]["Name"].should.equal("test.a.org.")
 | 
			
		||||
    zones["HostedZones"][1]["Name"].should.equal("test.a.org.")
 | 
			
		||||
 | 
			
		||||
    # test sort order
 | 
			
		||||
    zones = conn.list_hosted_zones_by_name()
 | 
			
		||||
    len(zones["HostedZones"]).should.equal(3)
 | 
			
		||||
    zones["HostedZones"][0]["Name"].should.equal("test.b.com.")
 | 
			
		||||
    zones["HostedZones"][1]["Name"].should.equal("test.a.org.")
 | 
			
		||||
    zones["HostedZones"][2]["Name"].should.equal("test.a.org.")
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user