From c3d5371510ad2e27e71800e73287ffc496b6df5b Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 5 Dec 2019 06:22:11 +0100 Subject: [PATCH] route53: diff support (#64867) * Refactoring. * Add diff support. * Add changelog. --- changelogs/fragments/64867-route53-diff.yml | 2 + lib/ansible/modules/cloud/amazon/route53.py | 69 +++++++++++++-------- 2 files changed, 45 insertions(+), 26 deletions(-) create mode 100644 changelogs/fragments/64867-route53-diff.yml diff --git a/changelogs/fragments/64867-route53-diff.yml b/changelogs/fragments/64867-route53-diff.yml new file mode 100644 index 00000000000..a27be1b102f --- /dev/null +++ b/changelogs/fragments/64867-route53-diff.yml @@ -0,0 +1,2 @@ +minor_changes: +- "route53 - the module now has diff support." diff --git a/lib/ansible/modules/cloud/amazon/route53.py b/lib/ansible/modules/cloud/amazon/route53.py index 34ed02c4586..9a49229e6f1 100644 --- a/lib/ansible/modules/cloud/amazon/route53.py +++ b/lib/ansible/modules/cloud/amazon/route53.py @@ -472,6 +472,39 @@ def invoke_with_throttling_retries(function_ref, *argv, **kwargs): retries += 1 +def decode_name(name): + # Due to a bug in either AWS or Boto, "special" characters are returned as octals, preventing round + # tripping of things like * and @. + decoded_name = name.replace(r'\052', '*') + decoded_name = decoded_name.replace(r'\100', '@') + return decoded_name + + +def to_dict(rset, zone_in, zone_id): + record = dict() + record['zone'] = zone_in + record['type'] = rset.type + record['record'] = decode_name(rset.name) + record['ttl'] = str(rset.ttl) + record['identifier'] = rset.identifier + record['weight'] = rset.weight + record['region'] = rset.region + record['failover'] = rset.failover + record['health_check'] = rset.health_check + record['hosted_zone_id'] = zone_id + if rset.alias_dns_name: + record['alias'] = True + record['value'] = rset.alias_dns_name + record['values'] = [rset.alias_dns_name] + record['alias_hosted_zone_id'] = rset.alias_hosted_zone_id + record['alias_evaluate_target_health'] = rset.alias_evaluate_target_health + else: + record['alias'] = False + record['value'] = ','.join(sorted(rset.resource_records)) + record['values'] = sorted(rset.resource_records) + return record + + def main(): argument_spec = ec2_argument_spec() argument_spec.update(dict( @@ -615,11 +648,8 @@ def main(): rset = invoke_with_throttling_retries(next, sets_iter) except StopIteration: break - # Due to a bug in either AWS or Boto, "special" characters are returned as octals, preventing round - # tripping of things like * and @. - decoded_name = rset.name.replace(r'\052', '*') - decoded_name = decoded_name.replace(r'\100', '@') - # Need to save this changes in rset, because of comparing rset.to_xml() == wanted_rset.to_xml() in next block + # Need to save decoded name in rset, because of comparing rset.to_xml() == wanted_rset.to_xml() in next block + decoded_name = decode_name(rset.name) rset.name = decoded_name if identifier_in is not None: @@ -630,26 +660,7 @@ def main(): # Sort records rset.resource_records = sorted(rset.resource_records) found_record = True - record['zone'] = zone_in - record['type'] = rset.type - record['record'] = decoded_name - record['ttl'] = rset.ttl - record['identifier'] = rset.identifier - record['weight'] = rset.weight - record['region'] = rset.region - record['failover'] = rset.failover - record['health_check'] = rset.health_check - record['hosted_zone_id'] = zone_id - if rset.alias_dns_name: - record['alias'] = True - record['value'] = rset.alias_dns_name - record['values'] = [rset.alias_dns_name] - record['alias_hosted_zone_id'] = rset.alias_hosted_zone_id - record['alias_evaluate_target_health'] = rset.alias_evaluate_target_health - else: - record['alias'] = False - record['value'] = ','.join(sorted(rset.resource_records)) - record['values'] = sorted(rset.resource_records) + record = to_dict(rset, zone_in, zone_id) if command_in == 'create' and rset.to_xml() == wanted_rset.to_xml(): module.exit_json(changed=False) @@ -700,7 +711,13 @@ def main(): except TimeoutError: module.fail_json(msg='Timeout waiting for changes to replicate') - module.exit_json(changed=True) + module.exit_json( + changed=True, + diff=dict( + before=record, + after=to_dict(wanted_rset, zone_in, zone_id) if command != 'delete' else {}, + ), + ) if __name__ == '__main__':