diff --git a/route53/__init__.py b/route53/__init__.py index 7ffa3b1..5d25c8a 100644 --- a/route53/__init__.py +++ b/route53/__init__.py @@ -21,7 +21,7 @@ def get_zones(self): zones = zones['ListHostedZonesResponse']['HostedZones'] zone_list = [] for zone in zones: - zone_list.append(Zone(zone)) + zone_list.append(Zone(zone)) return zone_list def get_zone(self, name): @@ -63,7 +63,7 @@ def repr_record(self): def repr_record_set(self): record_list = ','.join([record.__repr__() for record in self]) return '[%s]' % record_list - + ResourceRecordSets.__repr__ = repr_record_set class Zone(object): @@ -71,7 +71,7 @@ def __init__(self, zone_dict): for key in zone_dict: if key == 'Id': self.id = zone_dict['Id'].replace('/hostedzone/','') - else: + else: self.__setattr__(key.lower(), zone_dict[key]) def __repr__(self): return '' % self.name @@ -120,78 +120,104 @@ def add_cname(self, name, value, ttl=None, comment=""): ttl = ttl or default_ttl name = route53.make_qualified(name) value = route53.make_qualified(value) - return self.add_record(resource_type='CNAME', - name=name, - value=value, - ttl=ttl, + return self.add_record(resource_type='CNAME', + name=name, + value=value, + ttl=ttl, comment=comment) def add_a(self, name, value, ttl=None, comment=""): """Add an A record to the zone.""" ttl = ttl or default_ttl name = route53.make_qualified(name) - return self.add_record(resource_type='A', - name=name, - value=value, - ttl=ttl, + return self.add_record(resource_type='A', + name=name, + value=value, + ttl=ttl, + comment=comment) + + def add_aaaa(self, name, value, ttl=None, comment=""): + """Add an AAAA record to the zone.""" + ttl = ttl or default_ttl + name = route53.make_qualified(name) + return self.add_record(resource_type='AAAA', + name=name, + value=value, + ttl=ttl, comment=comment) def add_mx(self, records, ttl=None, comment=""): """Add an MX record to the zone.""" ttl = ttl or default_ttl records = route53.make_qualified(records) - return self.add_record(resource_type='MX', - name=self.name, - value=records, - ttl=ttl, + return self.add_record(resource_type='MX', + name=self.name, + value=records, + ttl=ttl, comment=comment) - - def get_cname(self, name): - """ Get the given CNAME record.""" + def get(self, resource_type, name): + """ Get the given resource_type record.""" name = route53.make_qualified(name) for record in self.get_records(): - if record.name == name and record.type == 'CNAME': + if record.name == name and record.type == resource_type: return record + return None + + def get_cname(self, name): + """ Get the given CNAME record.""" + return self.get('CNAME', name) def get_a(self, name): """ Get the given A record.""" - name = route53.make_qualified(name) - for record in self.get_records(): - if record.name == name and record.type == 'A': - return record + return self.get('A', name) + + def get_aaaa(self, name): + """ Get the given AAAA record.""" + return self.get('AAAA', name) def get_mx(self): """ Get all MX records.""" - for record in self.get_records(): - if record.type == 'MX': - return record - + return self.get('MX', name) + def update_cname(self, name, value, ttl=None, comment=""): """ Update the given CNAME record to a new value and ttl.""" name = route53.make_qualified(name) value = route53.make_qualified(value) old_record = self.get_cname(name) ttl = ttl or old_record.ttl - return self.update_record(resource_type='CNAME', - name=name, - old_value=old_record.resource_records, - new_value=value, - old_ttl=old_record.ttl, - new_ttl=ttl, + return self.update_record(resource_type='CNAME', + name=name, + old_value=old_record.resource_records, + new_value=value, + old_ttl=old_record.ttl, + new_ttl=ttl, comment=comment) - + def update_a(self, name, value, ttl=None, comment=""): """ Update the given A record to a new value and ttl.""" name = route53.make_qualified(name) old_record = self.get_a(name) ttl = ttl or old_record.ttl - return self.update_record(resource_type='A', - name=name, - old_value=old_record.resource_records, - new_value=value, - old_ttl=old_record.ttl, - new_ttl=ttl, + return self.update_record(resource_type='A', + name=name, + old_value=old_record.resource_records, + new_value=value, + old_ttl=old_record.ttl, + new_ttl=ttl, + comment=comment) + + def update_aaaa(self, name, value, ttl=None, comment=""): + """ Update the given AAAA record to a new value and ttl.""" + name = route53.make_qualified(name) + old_record = self.get_a(name) + ttl = ttl or old_record.ttl + return self.update_record(resource_type='AAAA', + name=name, + old_value=old_record.resource_records, + new_value=value, + old_ttl=old_record.ttl, + new_ttl=ttl, comment=comment) def update_mx(self, value, ttl=None, comment=""): @@ -199,37 +225,45 @@ def update_mx(self, value, ttl=None, comment=""): value = route53.make_qualified(value) old_record = self.get_mx() ttl = ttl or old_record.ttl - return self.update_record(resource_type='MX', - name=self.name, - old_value=old_record.resource_records, - new_value=value, - old_ttl=old_record.ttl, - new_ttl=ttl, + return self.update_record(resource_type='MX', + name=self.name, + old_value=old_record.resource_records, + new_value=value, + old_ttl=old_record.ttl, + new_ttl=ttl, comment=comment) - def delete_cname(self,name): + def delete_cname(self, name): """ Delete the given CNAME record for this zone.""" record = self.get_cname(route53.make_qualified(name)) - return self.delete_record(resource_type=record.type, - name=record.name, - value=record.resource_records, + return self.delete_record(resource_type=record.type, + name=record.name, + value=record.resource_records, ttl=record.ttl) - def delete_a(self,name): + def delete_a(self, name): """ Delete the given A record for this zone.""" record = self.get_a(route53.make_qualified(name)) - return self.delete_record(resource_type=record.type, - name=record.name, - value=record.resource_records, + return self.delete_record(resource_type=record.type, + name=record.name, + value=record.resource_records, + ttl=record.ttl) + + def delete_aaaa(self, name): + """ Delete the given AAAA record for this zone.""" + record = self.get_aaaa(route53.make_qualified(name)) + return self.delete_record(resource_type=record.type, + name=record.name, + value=record.resource_records, ttl=record.ttl) def delete_mx(self): """ Delete all MX records for the zone.""" record = self.get_mx() - return self.delete_record(resource_type=record.type, - name=record.name, - value=record.resource_records, + return self.delete_record(resource_type=record.type, + name=record.name, + value=record.resource_records, ttl=record.ttl) def get_records(self, type=None): diff --git a/route53/test.py b/route53/test.py index 7c5ab1c..645998c 100644 --- a/route53/test.py +++ b/route53/test.py @@ -25,6 +25,18 @@ def test_a(self): self.assertEquals(record.resource_records, [u'186.143.32.2']) self.assertEquals(record.ttl, u'800') + def test_aaaa(self): + self.zone.add_aaaa('example.com', '2002:0:0:0:0:0:660b:1701', 80) + record = self.zone.get_aaaa('example.com') + self.assertEquals(record.name, u'example.com.') + self.assertEquals(record.resource_records, [u'2002:0:0:0:0:0:660b:1701']) + self.assertEquals(record.ttl, u'80') + self.zone.update_aaaa('example.com', '2002::ba8f:2002', '800') + record = self.zone.get_aaaa('example.com') + self.assertEquals(record.name, u'example.com.') + self.assertEquals(record.resource_records, [u'2002::ba8f:2002']) + self.assertEquals(record.ttl, u'800') + def test_cname(self): self.zone.add_cname('www.example.com', 'webserver.example.com', 200) record = self.zone.get_cname('www.example.com') @@ -58,6 +70,7 @@ def test_get_zones(self): @classmethod def tearDownClass(self): self.zone.delete_a('example.com') + self.zone.delete_aaaa('example.com') self.zone.delete_cname('www.example.com') self.zone.delete_mx() self.zone.delete()