Adding support for UPSERT'ing route53 records

This commit is contained in:
Min-Young Wu 2016-04-12 10:37:01 -07:00
parent 100ec4e7c8
commit 826ba82f6e
3 changed files with 37 additions and 2 deletions

View File

@ -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]

View File

@ -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"])

View File

@ -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")