From 826ba82f6ed956c64440539594436dac96cb077a Mon Sep 17 00:00:00 2001 From: Min-Young Wu Date: Tue, 12 Apr 2016 10:37:01 -0700 Subject: [PATCH] Adding support for UPSERT'ing route53 records --- moto/route53/models.py | 10 ++++++++++ moto/route53/responses.py | 7 +++++-- tests/test_route53/test_route53.py | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/moto/route53/models.py b/moto/route53/models.py index 3a51b284f..7f8873803 100644 --- a/moto/route53/models.py +++ b/moto/route53/models.py @@ -135,6 +135,16 @@ class FakeZone(object): self.rrsets.append(record_set) return record_set + def upsert_rrset(self, record_set): + new_rrset = RecordSet(record_set) + for i, rrset in enumerate(self.rrsets): + if rrset.name == new_rrset.name: + self.rrsets[i] = new_rrset + break + else: + self.rrsets.append(new_rrset) + return new_rrset + def delete_rrset_by_name(self, name): self.rrsets = [record_set for record_set in self.rrsets if record_set.name != name] diff --git a/moto/route53/responses.py b/moto/route53/responses.py index edd766b01..25c5d05cc 100644 --- a/moto/route53/responses.py +++ b/moto/route53/responses.py @@ -57,7 +57,7 @@ def rrset_response(request, full_url, headers): for value in change_list: action = value['Action'] record_set = value['ResourceRecordSet'] - if action == 'CREATE': + if action in ('CREATE', 'UPSERT'): if 'ResourceRecords' in record_set: resource_records = list(record_set['ResourceRecords'].values())[0] if not isinstance(resource_records, list): @@ -67,7 +67,10 @@ def rrset_response(request, full_url, headers): elif 'AliasTarget' in record_set: record_values = [record_set['AliasTarget']['DNSName']] record_set['ResourceRecords'] = record_values - the_zone.add_rrset(record_set) + if action == 'CREATE': + the_zone.add_rrset(record_set) + else: + the_zone.upsert_rrset(record_set) elif action == "DELETE": if 'SetIdentifier' in record_set: the_zone.delete_rrset_by_id(record_set["SetIdentifier"]) diff --git a/tests/test_route53/test_route53.py b/tests/test_route53/test_route53.py index 5556bfc7d..08c2382b2 100644 --- a/tests/test_route53/test_route53.py +++ b/tests/test_route53/test_route53.py @@ -70,6 +70,28 @@ def test_rrset(): rrsets = conn.get_all_rrsets(zoneid) rrsets.should.have.length_of(0) + changes = ResourceRecordSets(conn, zoneid) + change = changes.add_change("UPSERT", "foo.bar.testdns.aws.com", "A") + change.add_value("1.2.3.4") + changes.commit() + + rrsets = conn.get_all_rrsets(zoneid, type="A") + rrsets.should.have.length_of(1) + rrsets[0].resource_records[0].should.equal('1.2.3.4') + + changes = ResourceRecordSets(conn, zoneid) + change = changes.add_change("UPSERT", "foo.bar.testdns.aws.com", "A") + change.add_value("5.6.7.8") + changes.commit() + + rrsets = conn.get_all_rrsets(zoneid, type="A") + rrsets.should.have.length_of(1) + rrsets[0].resource_records[0].should.equal('5.6.7.8') + + changes = ResourceRecordSets(conn, zoneid) + changes.add_change("DELETE", "foo.bar.testdns.aws.com", "A") + changes.commit() + changes = ResourceRecordSets(conn, zoneid) change = changes.add_change("CREATE", "foo.bar.testdns.aws.com", "A") change.add_value("1.2.3.4")