Permalink
Browse files

Improvements to the change set logic.

  • Loading branch information...
1 parent 7f6d63a commit 2034b8439c602d70fab2b31d970b192668faba8e @gtaylor committed Nov 4, 2012
Showing with 51 additions and 11 deletions.
  1. +8 −2 route53/change_set.py
  2. +3 −0 route53/connection.py
  3. +3 −2 route53/hosted_zone.py
  4. +37 −7 route53/xml_generators/change_resource_record_set.py
View
@@ -15,8 +15,9 @@ def __init__(self, connection, hosted_zone_id):
"""
self.connection = connection
- self.changes = []
self.hosted_zone_id = hosted_zone_id
+ self.creations = []
+ self.deletions = []
def add_change(self, action, record_set):
"""
@@ -32,4 +33,9 @@ def add_change(self, action, record_set):
if action not in ['CREATE', 'DELETE']:
raise Route53Error("action must be one of 'CREATE' or 'DELETE'")
- self.changes.append((action, record_set))
+ change_tuple = (action, record_set)
+
+ if action == 'CREATE':
+ self.creations.append(change_tuple)
+ else:
+ self.deletions.append(change_tuple)
View
@@ -253,6 +253,9 @@ def _change_resource_record_sets(self, change_set, comment=None):
:param change_set.ChangeSet change_set: The ChangeSet object to create
the XML doc from.
:keyword str comment: An optional comment to go along with the request.
+ :rtype: dict
+ :returns: A dict of change info, which contains some details about
+ the request.
"""
body = xml_generators.change_resource_record_set_writer(
View
@@ -92,8 +92,9 @@ def add_a_record(self, name, values, ttl=60):
: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).
- :rtype: AResourceRecordSet
- :returns: The newly created AResourceRecordSet instance.
+ :rtype: tuple
+ :returns: A tuple in the form of ``(rrset, change_info)``, where
+ ``rrset`` is the newly created AResourceRecordSet instance.
"""
rrset = AResourceRecordSet(
@@ -2,19 +2,48 @@
from lxml import etree
from route53.util import prettyprint_xml
+def get_change_values(change):
+ """
+ In the case of deletions, we pull the change values for the XML request
+ from the ResourceRecordSet._initial_vas dict, since we want the original
+ values. For creations, we pull from the attributes on ResourceRecordSet.
+
+ Since we're dealing with attributes vs. dict key/vals, we'll abstract
+ this part away here and just always pass a dict to write_change.
+
+ :rtype: dict
+ :returns: A dict of change data, used by :py:func:`write_change` to
+ write the change request XML.
+ """
+
+ action, rrset = change
+
+ if action == 'CREATE':
+ # For creations, we want the new values. They don't need to match.
+ return dict(
+ name=rrset.name,
+ ttl=rrset.ttl,
+ records=rrset.records,
+ )
+ else:
+ # We can just dump the initial values dict for deletions, since we
+ # want the original values to match in the deletion request.
+ return rrset._initial_vals
+
def write_change(change):
"""
Creates an XML element for the change.
- :param change_set.ChangeSet change_set: The ChangeSet object to create the
- XML doc from.
-
+ :param tuple change: A change tuple from a ChangeSet. Comes in the form
+ of ``(action, rrset)``.
:rtype: lxml.etree._Element
:returns: A fully baked Change tag.
"""
action, rrset = change
+ change_vals = get_change_values(change)
+
e_change = etree.Element("Change")
e_action = etree.SubElement(e_change, "Action")
@@ -23,13 +52,13 @@ def write_change(change):
e_rrset = etree.SubElement(e_change, "ResourceRecordSet")
e_name = etree.SubElement(e_rrset, "Name")
- e_name.text = rrset.name
+ e_name.text = change_vals['name']
e_type = etree.SubElement(e_rrset, "Type")
e_type.text = rrset.rrset_type
e_ttl = etree.SubElement(e_rrset, "TTL")
- e_ttl.text = str(rrset.ttl)
+ e_ttl.text = str(change_vals['ttl'])
if rrset.is_alias_record_set():
@@ -38,7 +67,7 @@ def write_change(change):
e_resource_records = etree.SubElement(e_rrset, "ResourceRecords")
- for value in rrset.records:
+ for value in change_vals['records']:
e_resource_record = etree.SubElement(e_resource_records, "ResourceRecord")
e_value = etree.SubElement(e_resource_record, "Value")
@@ -71,7 +100,8 @@ def change_resource_record_set_writer(connection, change_set, comment=None):
e_changes = etree.SubElement(e_change_batch, "Changes")
- for change in change_set.changes:
+ # Deletions need to come first in the change sets.
+ for change in change_set.deletions + change_set.creations:
e_changes.append(write_change(change))
e_tree = etree.ElementTree(element=e_root)

0 comments on commit 2034b84

Please sign in to comment.