Permalink
Browse files

Added in a ResourceRecordSet.hosted_zone property.

  • Loading branch information...
1 parent dee90ff commit fdd7512e298ebbf078dd16dce796f9d31654c23c @gtaylor committed Nov 2, 2012
View
@@ -43,7 +43,7 @@ def _send_request(self, path, data, method):
def _do_autopaginating_api_call(self, path, params, method, parser_func,
next_marker_xpath, next_marker_param_name,
- next_type_xpath=None):
+ next_type_xpath=None, parser_kwargs=None):
"""
Given an API method, the arguments passed to it, and a function to
hand parsing off to, loop through the record sets in the API call
@@ -62,19 +62,24 @@ def _do_autopaginating_api_call(self, path, params, method, parser_func,
:keyword str next_type_xpath: For the
py:meth:`list_resource_record_sets_by_zone_id` method, there's
an additional paginator token. Specifying this XPath looks for it.
+ :keyword dict parser_kwargs: Optional dict of additional kwargs to pass
+ on to the parser function.
:rtype: generator
:returns: Returns a generator that may be returned by the top-level
API method.
"""
+ if not parser_kwargs:
+ parser_kwargs = {}
+
# We loop indefinitely since we have no idea how many "pages" of
# results we're going to have to go through.
while True:
# An lxml Element node.
root = self._send_request(path, params, method)
# Individually yield HostedZone instances after parsing/instantiating.
- for record in parser_func(root, connection=self):
+ for record in parser_func(root, connection=self, **parser_kwargs):
yield record
# This will determine at what offset we start the next query.
@@ -230,6 +235,7 @@ def list_resource_record_sets_by_zone_id(self, id, rrset_type=None,
params=params,
method='GET',
parser_func=xml_parsers.list_resource_record_sets_by_zone_id_parser,
+ parser_kwargs={'zone_id': id},
next_marker_xpath="./{*}NextRecordName",
next_marker_param_name="name",
next_type_xpath="./{*}NextRecordType"
@@ -8,11 +8,13 @@ class ResourceRecordSet(object):
one of the methods on:py:class:``route53.connection.Route53Connection`.
"""
- def __init__(self, connection, name, rrset_type, ttl, records):
+ def __init__(self, connection, zone_id, name, rrset_type, ttl, records):
"""
:param Route53Connection connection: The connection instance that
was used to query the Route53 API, leading to this object's
creation.
+ :param str zone_id: The zone ID of the HostedZone that this
+ resource record set belongs to.
:param str name: The fully qualified name of the resource record set.
:param int ttl: The time-to-live. A Aliases have no TTL, so this can
be None in that case.
@@ -21,13 +23,25 @@ def __init__(self, connection, name, rrset_type, ttl, records):
"""
self.connection = connection
+ self.zone_id = zone_id
self.name = name
self.rrset_type = rrset_type
self.ttl = int(ttl) if ttl else None
self.records = records
def __str__(self):
- return '<ResourceRecordSet: %s -- %s>' % (self.name, self.rrset_type)
+ return '<%s: %s>' % (self.__class__.__name__, self.name)
+
+ @property
+ def hosted_zone(self):
+ """
+ Queries for this record set's HostedZone.
+
+ :rtype: HostedZone
+ :returns: The matching HostedZone for this record set.
+ """
+
+ return self.connection.get_hosted_zone_by_id(self.zone_id)
class AResourceRecordSet(ResourceRecordSet):
@@ -55,7 +55,7 @@ def parse_rrset_record_values(e_resource_records):
return records
-def parse_rrset(e_rrset, connection):
+def parse_rrset(e_rrset, connection, zone_id):
"""
This a parser that allows the passing of any valid ResourceRecordSet
tag. It will spit out the appropriate ResourceRecordSet object for the tag.
@@ -64,12 +64,16 @@ def parse_rrset(e_rrset, connection):
response from the API.
:param Route53Connection connection: The connection instance used to
query the API.
+ :param str zone_id: The zone ID of the HostedZone these rrsets belong to.
:rtype: ResourceRecordSet
:returns: An instantiated ResourceRecordSet object.
"""
# This dict will be used to instantiate a ResourceRecordSet instance to yield.
- kwargs = {'connection': connection}
+ kwargs = {
+ 'connection': connection,
+ 'zone_id': zone_id,
+ }
rrset_type = None
for e_field in e_rrset:
@@ -109,7 +113,7 @@ def parse_rrset(e_rrset, connection):
RRSetSubclass = RRSET_TYPE_TO_RSET_SUBCLASS_MAP[rrset_type]
return RRSetSubclass(**kwargs)
-def list_resource_record_sets_by_zone_id_parser(e_root, connection):
+def list_resource_record_sets_by_zone_id_parser(e_root, connection, zone_id):
"""
Parses the API responses for the
:py:meth:`route53.connection.Route53Connection.list_resource_record_sets_by_zone_id`
@@ -119,6 +123,7 @@ def list_resource_record_sets_by_zone_id_parser(e_root, connection):
response from the API.
:param Route53Connection connection: The connection instance used to
query the API.
+ :param str zone_id: The zone ID of the HostedZone these rrsets belong to.
:rtype: ResourceRecordSet
:returns: A generator of fully formed ResourceRecordSet instances.
"""
@@ -129,4 +134,4 @@ def list_resource_record_sets_by_zone_id_parser(e_root, connection):
e_rrsets = e_root.find('./{*}ResourceRecordSets')
for e_rrset in e_rrsets:
- yield parse_rrset(e_rrset, connection)
+ yield parse_rrset(e_rrset, connection, zone_id)

0 comments on commit fdd7512

Please sign in to comment.