From 3e338a1cca7aa7291f042fb404a324138c57d096 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Mon, 24 Apr 2023 00:01:53 +1200 Subject: [PATCH] udm_dns_record: minor refactor (#6382) * udm_dns_record: minor refactor * remove unused import * improve ptr_record zone validation * add changelog frag * undo zone validation change as it breaks for IPv6 addresses --- .../6382-udm-dns-record-refactor.yml | 2 + plugins/modules/udm_dns_record.py | 50 +++++++------------ 2 files changed, 21 insertions(+), 31 deletions(-) create mode 100644 changelogs/fragments/6382-udm-dns-record-refactor.yml diff --git a/changelogs/fragments/6382-udm-dns-record-refactor.yml b/changelogs/fragments/6382-udm-dns-record-refactor.yml new file mode 100644 index 0000000000..78a18e0714 --- /dev/null +++ b/changelogs/fragments/6382-udm-dns-record-refactor.yml @@ -0,0 +1,2 @@ +minor_changes: + - udm_dns_record - minor refactor to the code (https://github.com/ansible-collections/community.general/pull/6382). diff --git a/plugins/modules/udm_dns_record.py b/plugins/modules/udm_dns_record.py index 458a8e4ba5..849c84a2d2 100644 --- a/plugins/modules/udm_dns_record.py +++ b/plugins/modules/udm_dns_record.py @@ -97,19 +97,9 @@ EXAMPLES = ''' RETURN = '''#''' -HAVE_UNIVENTION = False -HAVE_IPADDRESS = False -try: - from univention.admin.handlers.dns import ( - forward_zone, - reverse_zone, - ) - HAVE_UNIVENTION = True -except ImportError: - pass from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.basic import missing_required_lib +from ansible_collections.community.general.plugins.module_utils import deps from ansible_collections.community.general.plugins.module_utils.univention_umc import ( umc_module_for_add, umc_module_for_edit, @@ -118,27 +108,26 @@ from ansible_collections.community.general.plugins.module_utils.univention_umc i config, uldap, ) -try: + + +with deps.declare("univention", msg="This module requires univention python bindings"): + from univention.admin.handlers.dns import ( + forward_zone, + reverse_zone, + ) + +with deps.declare("ipaddress"): import ipaddress - HAVE_IPADDRESS = True -except ImportError: - pass def main(): module = AnsibleModule( argument_spec=dict( - type=dict(required=True, - type='str'), - zone=dict(required=True, - type='str'), - name=dict(required=True, - type='str'), - data=dict(default={}, - type='dict'), - state=dict(default='present', - choices=['present', 'absent'], - type='str') + type=dict(required=True, type='str'), + zone=dict(required=True, type='str'), + name=dict(required=True, type='str'), + data=dict(default={}, type='dict'), + state=dict(default='present', choices=['present', 'absent'], type='str') ), supports_check_mode=True, required_if=([ @@ -146,8 +135,7 @@ def main(): ]) ) - if not HAVE_UNIVENTION: - module.fail_json(msg="This module requires univention python bindings") + deps.validate(module, "univention") type = module.params['type'] zone = module.params['zone'] @@ -159,8 +147,8 @@ def main(): workname = name if type == 'ptr_record': - if not HAVE_IPADDRESS: - module.fail_json(msg=missing_required_lib('ipaddress')) + deps.validate(module, "ipaddress") + try: if 'arpa' not in zone: raise Exception("Zone must be reversed zone for ptr_record. (e.g. 1.1.192.in-addr.arpa)") @@ -196,7 +184,7 @@ def main(): '(zoneName={0})'.format(zone), scope='domain', ) - if len(so) == 0: + if not so == 0: raise Exception("Did not find zone '{0}' in Univention".format(zone)) obj = umc_module_for_add('dns/{0}'.format(type), container, superordinate=so[0]) else: