Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding various create_*_record methods to HostedZone.

  • Loading branch information...
commit 871f37488a0dd9438d0b87675b9c9102d68c71c2 1 parent 8667c41
@gtaylor authored
View
4 route53/connection.py
@@ -39,7 +39,7 @@ def _send_request(self, path, data, method):
response_body = self.transport.send_request(path, data, method)
root = etree.fromstring(response_body)
- print(prettyprint_xml(root))
+ #print(prettyprint_xml(root))
return root
def _do_autopaginating_api_call(self, path, params, method, parser_func,
@@ -270,7 +270,7 @@ def _change_resource_record_sets(self, change_set, comment=None):
method='POST',
)
- print(prettyprint_xml(root))
+ #print(prettyprint_xml(root))
e_change_info = root.find('./{*}ChangeInfo')
return parse_change_info(e_change_info)
View
353 route53/hosted_zone.py
@@ -1,6 +1,6 @@
from route53.change_set import ChangeSet
from route53.exceptions import AlreadyDeletedError
-from route53.resource_record_set import AResourceRecordSet
+from route53.resource_record_set import AResourceRecordSet, AAAAResourceRecordSet, CNAMEResourceRecordSet, MXResourceRecordSet, NSResourceRecordSet, PTRResourceRecordSet, SOAResourceRecordSet, SPFResourceRecordSet, SRVResourceRecordSet, TXTResourceRecordSet
class HostedZone(object):
"""
@@ -127,11 +127,52 @@ def _halt_if_already_deleted(self):
if self._is_deleted:
raise AlreadyDeletedError("Can't manipulate a deleted zone.")
- def add_a_record(self, name, values, ttl=60, weight=None, region=None,
+ def _add_record(self, record_set_class, name, values, ttl=60, weight=None,
+ region=None,set_identifier=None, alias_hosted_zone_id=None,
+ alias_dns_name=None):
+ """
+ Convenience method for creating ResourceRecordSets. Most of the calls
+ are basically the same, this saves on repetition.
+
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created ResourceRecordSet sub-class
+ instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ rrset_kwargs = dict(
+ connection=self.connection,
+ zone_id=self.id,
+ name=name,
+ ttl=ttl,
+ records=values,
+ weight=weight,
+ region=region,
+ set_identifier=set_identifier,
+ )
+
+ if alias_hosted_zone_id or alias_dns_name:
+ rrset_kwargs.update(dict(
+ alias_hosted_zone_id=alias_hosted_zone_id,
+ alias_dns_name=alias_dns_name
+ ))
+
+ rrset = record_set_class(**rrset_kwargs)
+
+ cset = ChangeSet(connection=self.connection, hosted_zone_id=self.id)
+ cset.add_change('CREATE', rrset)
+
+ change_info = self.connection._change_resource_record_sets(cset)
+
+ return rrset, change_info
+
+ def create_a_record(self, name, values, ttl=60, weight=None, region=None,
set_identifier=None, alias_hosted_zone_id=None,
alias_dns_name=None):
"""
- Adds an A record to the hosted zone.
+ Creates an A record attached to this hosted zone.
:param str name: The fully qualified name of the record to add.
:param list values: A list of value strings for the record.
@@ -158,22 +199,296 @@ def add_a_record(self, name, values, ttl=60, weight=None, region=None,
self._halt_if_already_deleted()
- rrset = AResourceRecordSet(
- alias_hosted_zone_id=alias_hosted_zone_id,
- alias_dns_name=alias_dns_name,
- connection=self.connection,
- zone_id=self.id,
- name=name,
- ttl=ttl,
- records=values,
- weight=weight,
- region=region,
- set_identifier=set_identifier,
- )
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
- cset = ChangeSet(connection=self.connection, hosted_zone_id=self.id)
- cset.add_change('CREATE', rrset)
+ return self._add_record(AResourceRecordSet, **values)
- change_info = self.connection._change_resource_record_sets(cset)
+ def create_aaaa_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates an AAAA record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created AAAAResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(AAAAResourceRecordSet, **values)
+
+ def create_cname_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a CNAME record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created CNAMEResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(CNAMEResourceRecordSet, **values)
+
+ def create_mx_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a MX record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created MXResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(MXResourceRecordSet, **values)
+
+ def create_ns_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a NS record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created NSResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(NSResourceRecordSet, **values)
+
+ def create_ptr_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a PTR record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created PTRResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(PTRResourceRecordSet, **values)
+
+ def create_soa_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a SOA record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created SOAResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(SOAResourceRecordSet, **values)
+
+ def create_spf_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a SPF record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created SPFResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(SPFResourceRecordSet, **values)
+
+ def create_srv_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a SRV record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created SRVResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
+
+ return self._add_record(SRVResourceRecordSet, **values)
+
+ def create_txt_record(self, name, values, ttl=60, weight=None, region=None,
+ set_identifier=None):
+ """
+ Creates a TXT record attached to this hosted zone.
+
+ :param str name: The fully qualified name of the record to add.
+ :param list values: A list of value strings for the record.
+ :keyword int ttl: The time-to-live of the record (in seconds).
+ :keyword int weight: For weighted record sets only. Among resource record
+ sets that have the same combination of DNS name and type, a value
+ that determines what portion of traffic for the current resource
+ record set is routed to the associated location. Ranges from 0-255.
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
+ where the resource that is specified in this resource record set
+ resides.
+ :keyword str set_identifier: For weighted and latency resource record
+ sets only. An identifier that differentiates among multiple
+ resource record sets that have the same combination of DNS name
+ and type. 1-128 chars.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created TXTResourceRecordSet instance.
+ """
+
+ self._halt_if_already_deleted()
+
+ # Grab the params/kwargs here for brevity's sake.
+ values = locals()
+ del values['self']
- return rrset, change_info
+ return self._add_record(TXTResourceRecordSet, **values)
View
16 route53/resource_record_set.py
@@ -12,8 +12,8 @@ class ResourceRecordSet(object):
# Override this in your sub-class.
rrset_type = None
- def __init__(self, connection, zone_id, name, ttl, records, weight, region,
- set_identifier):
+ def __init__(self, connection, zone_id, name, ttl, records, weight=None,
+ region=None, set_identifier=None):
"""
:param Route53Connection connection: The connection instance that
was used to query the Route53 API, leading to this object's
@@ -25,14 +25,14 @@ def __init__(self, connection, zone_id, name, ttl, records, weight, region,
be None in that case.
:param list records: A list of resource record strings. For some
types (A entries that are Aliases), this is an empty list.
- :param int weight: For weighted record sets only. Among resource record
+ :keyword int weight: For weighted record sets only. Among resource record
sets that have the same combination of DNS name and type, a value
that determines what portion of traffic for the current resource
record set is routed to the associated location. Ranges from 0-255.
- :param str region: For latency-based record sets. The Amazon EC2 region
+ :keyword str region: For latency-based record sets. The Amazon EC2 region
where the resource that is specified in this resource record set
resides.
- :param str set_identifier: For weighted and latency resource record
+ :keyword str set_identifier: For weighted and latency resource record
sets only. An identifier that differentiates among multiple
resource record sets that have the same combination of DNS name
and type. 1-128 chars.
@@ -146,11 +146,11 @@ class AResourceRecordSet(ResourceRecordSet):
rrset_type = 'A'
- def __init__(self, alias_hosted_zone_id, alias_dns_name, *args, **kwargs):
+ def __init__(self, alias_hosted_zone_id=None, alias_dns_name=None, *args, **kwargs):
"""
- :param str alias_hosted_zone_id: Alias A records have this specified.
+ :keyword str alias_hosted_zone_id: Alias A records have this specified.
It appears to be the hosted zone ID for the ELB the Alias points at.
- :param str alias_dns_name: Alias A records have this specified. It is
+ :keyword str alias_dns_name: Alias A records have this specified. It is
the DNS name for the ELB that the Alias points to.
"""
View
18 route53/xml_generators/change_resource_record_set.py
@@ -59,15 +59,23 @@ def write_change(change):
e_type = etree.SubElement(e_rrset, "Type")
e_type.text = rrset.rrset_type
- if 'set_identifier' in change_vals:
+ if change_vals.get('set_identifier'):
e_set_id = etree.SubElement(e_rrset, "SetIdentifier")
- e_set_id.text = change_vals['set_identifer']
+ e_set_id.text = change_vals['set_identifier']
- if 'weight' in change_vals:
+ if change_vals.get('weight'):
e_weight = etree.SubElement(e_rrset, "Weight")
e_weight.text = change_vals['weight']
- if 'region' in change_vals:
+ if change_vals.get('alias_hosted_zone_id') or change_vals.get('alias_dns_name'):
+ e_alias_target = etree.SubElement(e_rrset, "AliasTarget")
+
+ e_hosted_zone_id = etree.SubElement(e_alias_target, "HostedZoneId")
+ e_hosted_zone_id.text = change_vals['alias_hosted_zone_id']
+ e_dns_name = etree.SubElement(e_alias_target, "DNSName")
+ e_dns_name.text = change_vals['alias_dns_name']
+
+ if change_vals.get('region'):
e_weight = etree.SubElement(e_rrset, "Region")
e_weight.text = change_vals['region']
@@ -118,7 +126,7 @@ def change_resource_record_set_writer(connection, change_set, comment=None):
e_tree = etree.ElementTree(element=e_root)
- print(prettyprint_xml(e_root))
+ #print(prettyprint_xml(e_root))
fobj = BytesIO()
# This writes bytes.
View
3  route53/xml_parsers/list_resource_record_sets_by_zone_id.py
@@ -5,6 +5,9 @@
RRSET_TAG_TO_KWARG_MAP = {
'Name': 'name',
'TTL': 'ttl',
+ 'Weight': 'weight',
+ 'Region': 'region',
+ 'SetIdentifier': 'set_identifier',
}
# Maps the various ResourceRecordSet Types to various RRSet subclasses.
Please sign in to comment.
Something went wrong with that request. Please try again.