mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-02-04 07:51:50 +00:00
Support diff mode for netcup-dns module (#11376)
* support diff mode for netcup-dns module * Fix issue with yaml encoding after testing * Add changelog fragment * Fixed: proper and robust yaml import * Remove need for yaml import * Show whole zone in diff for context * Update changelogs/fragments/11376-netcup-dns-diff-mode.yml Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/modules/netcup_dns.py Co-authored-by: Felix Fontein <felix@fontein.de> --------- Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
13035e2a2c
commit
75234597bc
2 changed files with 23 additions and 2 deletions
|
|
@ -19,7 +19,8 @@ attributes:
|
|||
check_mode:
|
||||
support: full
|
||||
diff_mode:
|
||||
support: none
|
||||
support: full
|
||||
version_added: 12.3.0
|
||||
options:
|
||||
api_key:
|
||||
description:
|
||||
|
|
@ -255,12 +256,18 @@ def main():
|
|||
module.fail_json(msg="record type MX required the 'priority' argument")
|
||||
|
||||
has_changed = False
|
||||
|
||||
diff_mode = module._diff
|
||||
diff = None
|
||||
|
||||
all_records = []
|
||||
try:
|
||||
with nc_dnsapi.Client(customer_id, api_key, api_password, timeout) as api:
|
||||
all_records = api.dns_records(domain)
|
||||
record = DNSRecord(record, record_type, value, priority=priority)
|
||||
|
||||
if diff_mode:
|
||||
diff_after = all_records.copy()
|
||||
# try to get existing record
|
||||
record_exists = False
|
||||
for r in all_records:
|
||||
|
|
@ -284,28 +291,40 @@ def main():
|
|||
if not module.check_mode:
|
||||
all_records = api.delete_dns_records(domain, obsolete_records)
|
||||
|
||||
if diff_mode:
|
||||
diff_after = [r for r in diff_after if r not in obsolete_records]
|
||||
has_changed = True
|
||||
|
||||
if not record_exists:
|
||||
if not module.check_mode:
|
||||
all_records = api.add_dns_record(domain, record)
|
||||
|
||||
if diff_mode:
|
||||
diff_after.append(record)
|
||||
has_changed = True
|
||||
elif state == "absent" and record_exists:
|
||||
if not module.check_mode:
|
||||
all_records = api.delete_dns_record(domain, record)
|
||||
|
||||
if diff_mode:
|
||||
diff_after.remove(record)
|
||||
has_changed = True
|
||||
if diff_mode:
|
||||
diff = dict(before=into_diffable(all_records, domain), after=into_diffable(diff_after, domain))
|
||||
|
||||
except Exception as ex:
|
||||
module.fail_json(msg=str(ex))
|
||||
|
||||
module.exit_json(changed=has_changed, result={"records": [record_data(r) for r in all_records]})
|
||||
module.exit_json(changed=has_changed, diff=diff, result={"records": [record_data(r) for r in all_records]})
|
||||
|
||||
|
||||
def record_data(r):
|
||||
return {"name": r.hostname, "type": r.type, "value": r.destination, "priority": r.priority, "id": r.id}
|
||||
|
||||
|
||||
def into_diffable(records, domain):
|
||||
return {domain: [record_data(r) for r in records]}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue