Skip to content

Commit

Permalink
Route53 tech debt (#4673)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Dec 9, 2021
1 parent 0055e12 commit 7776668
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 55 deletions.
53 changes: 2 additions & 51 deletions moto/route53/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ class RecordSet(CloudFormationModel):
def __init__(self, kwargs):
self.name = kwargs.get("Name")
self.type_ = kwargs.get("Type")
self.ttl = kwargs.get("TTL")
self.ttl = kwargs.get("TTL", 0)
self.records = kwargs.get("ResourceRecords", [])
self.set_identifier = kwargs.get("SetIdentifier")
self.weight = kwargs.get("Weight")
self.weight = kwargs.get("Weight", 0)
self.region = kwargs.get("Region")
self.health_check = kwargs.get("HealthCheckId")
self.hosted_zone_name = kwargs.get("HostedZoneName")
Expand Down Expand Up @@ -197,55 +197,6 @@ def delete_from_cloudformation_json(
def physical_resource_id(self):
return self.name

def to_xml(self):
template = Template(
"""<ResourceRecordSet>
<Name>{{ record_set.name }}</Name>
<Type>{{ record_set.type_ }}</Type>
{% if record_set.set_identifier %}
<SetIdentifier>{{ record_set.set_identifier }}</SetIdentifier>
{% endif %}
{% if record_set.weight %}
<Weight>{{ record_set.weight }}</Weight>
{% endif %}
{% if record_set.region %}
<Region>{{ record_set.region }}</Region>
{% endif %}
{% if record_set.ttl %}
<TTL>{{ record_set.ttl }}</TTL>
{% endif %}
{% if record_set.failover %}
<Failover>{{ record_set.failover }}</Failover>
{% endif %}
{% if record_set.geo_location %}
<GeoLocation>
{% for geo_key in ['ContinentCode','CountryCode','SubdivisionCode'] %}
{% if record_set.geo_location[geo_key] %}<{{ geo_key }}>{{ record_set.geo_location[geo_key] }}</{{ geo_key }}>{% endif %}
{% endfor %}
</GeoLocation>
{% endif %}
{% if record_set.alias_target %}
<AliasTarget>
<HostedZoneId>{{ record_set.alias_target['HostedZoneId'] }}</HostedZoneId>
<DNSName>{{ record_set.alias_target['DNSName'] }}</DNSName>
<EvaluateTargetHealth>{{ record_set.alias_target['EvaluateTargetHealth'] }}</EvaluateTargetHealth>
</AliasTarget>
{% else %}
<ResourceRecords>
{% for record in record_set.records %}
<ResourceRecord>
<Value>{{ record|e }}</Value>
</ResourceRecord>
{% endfor %}
</ResourceRecords>
{% endif %}
{% if record_set.health_check %}
<HealthCheckId>{{ record_set.health_check }}</HealthCheckId>
{% endif %}
</ResourceRecordSet>"""
)
return template.render(record_set=self)

def delete(self, *args, **kwargs):
"""Not exposed as part of the Route 53 API - used for CloudFormation. args are ignored"""
hosted_zone = route53_backend.get_hosted_zone_by_name(self.hosted_zone_name)
Expand Down
52 changes: 48 additions & 4 deletions moto/route53/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,55 @@ def get_or_delete_query_logging_config_response(self, request, full_url, headers
</ChangeTagsForResourceResponse>
"""

LIST_RRSET_RESPONSE = """<ListResourceRecordSetsResponse xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
LIST_RRSET_RESPONSE = """<?xml version="1.0" encoding="UTF-8"?>
<ListResourceRecordSetsResponse xmlns="https://route53.amazonaws.com/doc/2012-12-12/">
<ResourceRecordSets>
{% for record_set in record_sets %}
{{ record_set.to_xml() }}
{% endfor %}
{% for record in record_sets %}
<ResourceRecordSet>
<Name>{{ record.name }}</Name>
<Type>{{ record.type_ }}</Type>
{% if record.set_identifier %}
<SetIdentifier>{{ record.set_identifier }}</SetIdentifier>
{% endif %}
{% if record.weight %}
<Weight>{{ record.weight }}</Weight>
{% endif %}
{% if record.region %}
<Region>{{ record.region }}</Region>
{% endif %}
{% if record.ttl %}
<TTL>{{ record.ttl }}</TTL>
{% endif %}
{% if record.failover %}
<Failover>{{ record.failover }}</Failover>
{% endif %}
{% if record.geo_location %}
<GeoLocation>
{% for geo_key in ['ContinentCode','CountryCode','SubdivisionCode'] %}
{% if record.geo_location[geo_key] %}<{{ geo_key }}>{{ record.geo_location[geo_key] }}</{{ geo_key }}>{% endif %}
{% endfor %}
</GeoLocation>
{% endif %}
{% if record.alias_target %}
<AliasTarget>
<HostedZoneId>{{ record.alias_target['HostedZoneId'] }}</HostedZoneId>
<DNSName>{{ record.alias_target['DNSName'] }}</DNSName>
<EvaluateTargetHealth>{{ record.alias_target['EvaluateTargetHealth'] }}</EvaluateTargetHealth>
</AliasTarget>
{% else %}
<ResourceRecords>
{% for resource in record.records %}
<ResourceRecord>
<Value><![CDATA[{{ resource }}]]></Value>
</ResourceRecord>
{% endfor %}
</ResourceRecords>
{% endif %}
{% if record.health_check %}
<HealthCheckId>{{ record.health_check }}</HealthCheckId>
{% endif %}
</ResourceRecordSet>
{% endfor %}
</ResourceRecordSets>
<IsTruncated>false</IsTruncated>
</ListResourceRecordSetsResponse>"""
Expand Down
29 changes: 29 additions & 0 deletions tests/test_route53/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sure # noqa # pylint: disable=unused-import
import xmltodict

import moto.server as server


def test_list_recordset():
backend = server.create_backend_app("route53")
test_client = backend.test_client()

# create hosted zone
request_data = '<CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/"><Name>example.com</Name><CallerReference>2014-04-01-18:47</CallerReference></CreateHostedZoneRequest>'
res = test_client.post("2013-04-01/hostedzone", data=request_data)
body = parse_xml(res.data)
zone_id = body["CreateHostedZoneResponse"]["HostedZone"]["Id"].rsplit("/")[-1]

# change record set
# Contains a special character
request_data = '<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2013-04-01/"><ChangeBatch><Changes><Change><Action>CREATE</Action><ResourceRecordSet><Name>n.example.com</Name><Type>TXT</Type><SetIdentifier>string</SetIdentifier><Weight>1</Weight><Region>us-east-1</Region><ResourceRecords><ResourceRecord><Value>val&amp;sth</Value></ResourceRecord></ResourceRecords></ResourceRecordSet></Change></Changes></ChangeBatch></ChangeResourceRecordSetsRequest>'
test_client.post(f"2013-04-01/hostedzone/{zone_id}/rrset/", data=request_data)

# list record set
res = test_client.get(f"2013-04-01/hostedzone/{zone_id}/rrset")
# Ampersand should be properly encoded
res.data.decode("utf-8").should.contain("<Value><![CDATA[val&sth]]></Value>")


def parse_xml(body):
return xmltodict.parse(body, dict_constructor=dict)

0 comments on commit 7776668

Please sign in to comment.