Permalink
Browse files

Changed some return values. Added parsing of ISO 8601 time strings.

  • Loading branch information...
1 parent 04d1b82 commit 13cacd2b58e73d9e9b38a3a368d6225e7a899550 @gtaylor committed Oct 30, 2012
View
@@ -110,8 +110,11 @@ def create_hosted_zone(self, name, caller_reference=None, comment=None):
retried without the risk of executing the operation twice. If no
value is given, we'll generate a Type 4 UUID for you.
:keyword str comment: An optional comment to attach to the zone.
- :rtype: HostedZone
- :returns: The newly created HostedZone instance.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(hosted_zone, change_info)``.
+ The ``hosted_zone`` variable contains a HostedZone instance matching
+ the newly created zone, and ``change_info`` is a dict with some
+ details about the API request.
"""
body = xml_generators.create_hosted_zone_writer(
@@ -158,7 +161,8 @@ def delete_hosted_zone_by_id(self, id):
:param str id: The hosted zone's ID (a short hash string).
:rtype: dict
- :returns: A dict with some info regarding the deletion process.
+ :returns: A dict of change info, which contains some details about
+ the request.
"""
root = self._send_request(
View
@@ -1,8 +1,33 @@
"""
-Various utility functions that are useful across the codebase.
+Various utility stuff that is useful across the codebase.
"""
+import datetime
from lxml import etree
-def prettyprint_xml(node):
- return etree.tostring(node, pretty_print=True).decode('utf-8')
+# This can be used as a tzinfo arg to the datetime functions/methods.
+UTC_TIMEZONE = datetime.timezone(datetime.timedelta(hours=0), 'UTC')
+
+def parse_iso_8601_time_str(time_str):
+ """
+ Parses a standard ISO 8601 time string. The Route53 API uses these here
+ and there.
+
+ :param str time_str: An ISO 8601 time string.
+ :rtype: datetime.datetime
+ :returns: A timezone aware (UTC) datetime.datetime instance.
+ """
+ submitted_at = datetime.datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S.%fZ')
+ # Parse the string, and make it explicitly UTC.
+ return submitted_at.replace(tzinfo=UTC_TIMEZONE)
+
+def prettyprint_xml(element):
+ """
+ A rough and dirty way to prettyprint an Element with indention.
+
+ :param lxml.etree._Element element: The Element or ElementTree to format.
+ :rtype: str
+ :returns: A prettyprinted representation of the element.
+ """
+
+ return etree.tostring(element, pretty_print=True).decode('utf-8')
@@ -7,6 +7,8 @@
# This dict maps tag names in the API response to a kwarg key used to
# instantiate HostedZone instances.
+from route53.util import parse_iso_8601_time_str
+
HOSTED_ZONE_TAG_TO_KWARG_MAP = {
'Id': 'id',
'Name': 'name',
@@ -70,3 +72,23 @@ def parse_delegation_set(zone, e_delegation_set):
nameservers.append(e_nameserver.text)
zone._nameservers = nameservers
+
+def parse_change_info(e_change_info):
+ """
+ Parses a ChangeInfo tag. Seen in CreateHostedZone and DeleteHostedZone.
+
+ :param lxml.etree._Element e_change_info: A ChangeInfo element.
+ :rtype: dict
+ :returns: A dict representation of the change info.
+ """
+
+ id = e_change_info.find('./{*}Id').text
+ status = e_change_info.find('./{*}Status').text
+ submitted_at = e_change_info.find('./{*}SubmittedAt').text
+ submitted_at = parse_iso_8601_time_str(submitted_at)
+
+ return {
+ 'request_id': id,
+ 'request_status': status,
+ 'request_submitted_at': submitted_at
+ }
@@ -1,4 +1,4 @@
-from route53.xml_parsers.common_hosted_zone import parse_hosted_zone
+from route53.xml_parsers.common_hosted_zone import parse_hosted_zone, parse_delegation_set, parse_change_info
def created_hosted_zone_parser(root, connection):
"""
@@ -14,4 +14,19 @@ def created_hosted_zone_parser(root, connection):
"""
zone = root.find('./{*}HostedZone')
- return parse_hosted_zone(zone, connection)
+ # This pops out a HostedZone instance.
+ hosted_zone = parse_hosted_zone(zone, connection)
+
+ # Now we'll fill in the nameservers.
+ e_delegation_set = root.find('./{*}DelegationSet')
+ # Modifies the HostedZone in place.
+ parse_delegation_set(hosted_zone, e_delegation_set)
+
+ # With each CreateHostedZone request, there's some details about the
+ # request's ID, status, and submission time. We'll return this in a tuple
+ # just for the sake of completeness.
+ e_change_info = root.find('./{*}ChangeInfo')
+ # Translate the ChangeInfo values to a dict.
+ change_info = parse_change_info(e_change_info)
+
+ return hosted_zone, change_info
@@ -1,3 +1,5 @@
+from route53.xml_parsers.common_hosted_zone import parse_change_info
+
#noinspection PyUnusedLocal
def delete_hosted_zone_by_id_parser(root, connection):
"""
@@ -14,12 +16,4 @@ def delete_hosted_zone_by_id_parser(root, connection):
e_change_info = root.find('./{*}ChangeInfo')
- id = e_change_info.find('./{*}Id').text
- status = e_change_info.find('./{*}Status').text
- submitted_at = e_change_info.find('./{*}SubmittedAt').text
-
- return {
- 'request_id': id,
- 'request_status': status,
- 'request_submitted_at': submitted_at
- }
+ return parse_change_info(e_change_info)

0 comments on commit 13cacd2

Please sign in to comment.