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
2
changelogs/fragments/11376-netcup-dns-diff-mode.yml
Normal file
2
changelogs/fragments/11376-netcup-dns-diff-mode.yml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- netcup_dns - support diff mode (https://github.com/ansible-collections/community.general/pull/11376).
|
||||||
|
|
@ -19,7 +19,8 @@ attributes:
|
||||||
check_mode:
|
check_mode:
|
||||||
support: full
|
support: full
|
||||||
diff_mode:
|
diff_mode:
|
||||||
support: none
|
support: full
|
||||||
|
version_added: 12.3.0
|
||||||
options:
|
options:
|
||||||
api_key:
|
api_key:
|
||||||
description:
|
description:
|
||||||
|
|
@ -255,12 +256,18 @@ def main():
|
||||||
module.fail_json(msg="record type MX required the 'priority' argument")
|
module.fail_json(msg="record type MX required the 'priority' argument")
|
||||||
|
|
||||||
has_changed = False
|
has_changed = False
|
||||||
|
|
||||||
|
diff_mode = module._diff
|
||||||
|
diff = None
|
||||||
|
|
||||||
all_records = []
|
all_records = []
|
||||||
try:
|
try:
|
||||||
with nc_dnsapi.Client(customer_id, api_key, api_password, timeout) as api:
|
with nc_dnsapi.Client(customer_id, api_key, api_password, timeout) as api:
|
||||||
all_records = api.dns_records(domain)
|
all_records = api.dns_records(domain)
|
||||||
record = DNSRecord(record, record_type, value, priority=priority)
|
record = DNSRecord(record, record_type, value, priority=priority)
|
||||||
|
|
||||||
|
if diff_mode:
|
||||||
|
diff_after = all_records.copy()
|
||||||
# try to get existing record
|
# try to get existing record
|
||||||
record_exists = False
|
record_exists = False
|
||||||
for r in all_records:
|
for r in all_records:
|
||||||
|
|
@ -284,28 +291,40 @@ def main():
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
all_records = api.delete_dns_records(domain, obsolete_records)
|
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
|
has_changed = True
|
||||||
|
|
||||||
if not record_exists:
|
if not record_exists:
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
all_records = api.add_dns_record(domain, record)
|
all_records = api.add_dns_record(domain, record)
|
||||||
|
|
||||||
|
if diff_mode:
|
||||||
|
diff_after.append(record)
|
||||||
has_changed = True
|
has_changed = True
|
||||||
elif state == "absent" and record_exists:
|
elif state == "absent" and record_exists:
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
all_records = api.delete_dns_record(domain, record)
|
all_records = api.delete_dns_record(domain, record)
|
||||||
|
|
||||||
|
if diff_mode:
|
||||||
|
diff_after.remove(record)
|
||||||
has_changed = True
|
has_changed = True
|
||||||
|
if diff_mode:
|
||||||
|
diff = dict(before=into_diffable(all_records, domain), after=into_diffable(diff_after, domain))
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
module.fail_json(msg=str(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):
|
def record_data(r):
|
||||||
return {"name": r.hostname, "type": r.type, "value": r.destination, "priority": r.priority, "id": r.id}
|
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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue