From 7ac061139b3f924015a4e3b2a174dc7002d067fe Mon Sep 17 00:00:00 2001 From: Andreas Olsson Date: Thu, 11 Jan 2018 18:03:54 +0100 Subject: [PATCH] Sort out parameter requirements for cloudflare_dns (#31800) This module is mostly written to make room for the option of not setting the `value` parameter while `state=absent`. That choice being a feature, since it allows both for the removal of individual records as well as the removal of full record sets. The opposite goes for the `record` parameter, which needs to be defined at least by its default value for the module to be able to produce any meaningful result. Hence making it explicit as part of required_if. The MX record type is already plenty covered. In addition to the priority parameter having a default value, the `ensure_dns_record` method does its own parameter checking. SRV records on the other hand do need additional parameter checking. Primarily with the `delete_dns_records` method in mind. Fixes #23957 --- .../modules/net_tools/cloudflare_dns.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/ansible/modules/net_tools/cloudflare_dns.py b/lib/ansible/modules/net_tools/cloudflare_dns.py index 41c094c4de3..04d4ca46c65 100644 --- a/lib/ansible/modules/net_tools/cloudflare_dns.py +++ b/lib/ansible/modules/net_tools/cloudflare_dns.py @@ -473,7 +473,8 @@ class CloudflareAPI(object): content = params['value'] search_record = params['record'] if params['type'] == 'SRV': - content = str(params['weight']) + '\t' + str(params['port']) + '\t' + params['value'] + if not (params['value'] is None or params['value'] == ''): + content = str(params['weight']) + '\t' + str(params['port']) + '\t' + params['value'] search_record = params['service'] + '.' + params['proto'] + '.' + params['record'] if params['solo']: search_value = None @@ -616,22 +617,20 @@ def main(): ), supports_check_mode=True, required_if=([ - ('state', 'present', ['record', 'type']), - ('type', 'MX', ['priority', 'value']), - ('type', 'SRV', ['port', 'priority', 'proto', 'service', 'value', 'weight']), - ('type', 'A', ['value']), - ('type', 'AAAA', ['value']), - ('type', 'CNAME', ['value']), - ('type', 'TXT', ['value']), - ('type', 'NS', ['value']), - ('type', 'SPF', ['value']) + ('state', 'present', ['record', 'type', 'value']), + ('state', 'absent', ['record']), + ('type', 'SRV', ['proto', 'service']), ] ), - required_one_of=( - [['record', 'value', 'type']] - ) ) + if module.params['type'] == 'SRV': + if not ((module.params['weight'] is not None and module.params['port'] is not None + and not (module.params['value'] is None or module.params['value'] == '')) + or (module.params['weight'] is None and module.params['port'] is None + and (module.params['value'] is None or module.params['value'] == ''))): + module.fail_json(msg="For SRV records the params weight, port and value all need to be defined, or not at all.") + changed = False cf_api = CloudflareAPI(module)