mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-23 20:29:08 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -85,59 +85,59 @@ from ansible_collections.community.general.plugins.module_utils.memset import me
|
|||
|
||||
|
||||
def api_validation(args=None):
|
||||
'''
|
||||
"""
|
||||
Perform some validation which will be enforced by Memset's API (see:
|
||||
https://www.memset.com/apidocs/methods_dns.html#dns.zone_domain_create)
|
||||
'''
|
||||
"""
|
||||
# zone domain length must be less than 250 chars
|
||||
if len(args['domain']) > 250:
|
||||
stderr = 'Zone domain must be less than 250 characters in length.'
|
||||
if len(args["domain"]) > 250:
|
||||
stderr = "Zone domain must be less than 250 characters in length."
|
||||
module.fail_json(failed=True, msg=stderr)
|
||||
|
||||
|
||||
def check(args=None):
|
||||
'''
|
||||
"""
|
||||
Support for running with check mode.
|
||||
'''
|
||||
"""
|
||||
retvals = dict()
|
||||
has_changed = False
|
||||
|
||||
api_method = 'dns.zone_domain_list'
|
||||
has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method)
|
||||
api_method = "dns.zone_domain_list"
|
||||
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method)
|
||||
|
||||
domain_exists = check_zone_domain(data=response, domain=args['domain'])
|
||||
domain_exists = check_zone_domain(data=response, domain=args["domain"])
|
||||
|
||||
# set changed to true if the operation would cause a change.
|
||||
has_changed = ((domain_exists and args['state'] == 'absent') or (not domain_exists and args['state'] == 'present'))
|
||||
has_changed = (domain_exists and args["state"] == "absent") or (not domain_exists and args["state"] == "present")
|
||||
|
||||
retvals['changed'] = has_changed
|
||||
retvals['failed'] = has_failed
|
||||
retvals["changed"] = has_changed
|
||||
retvals["failed"] = has_failed
|
||||
|
||||
return retvals
|
||||
|
||||
|
||||
def create_zone_domain(args=None, zone_exists=None, zone_id=None, payload=None):
|
||||
'''
|
||||
"""
|
||||
At this point we already know whether the containing zone exists,
|
||||
so we just need to create the domain (or exit if it already exists).
|
||||
'''
|
||||
"""
|
||||
has_changed, has_failed = False, False
|
||||
msg = None
|
||||
|
||||
api_method = 'dns.zone_domain_list'
|
||||
_has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method)
|
||||
api_method = "dns.zone_domain_list"
|
||||
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method)
|
||||
|
||||
for zone_domain in response.json():
|
||||
if zone_domain['domain'] == args['domain']:
|
||||
if zone_domain["domain"] == args["domain"]:
|
||||
# zone domain already exists, nothing to change.
|
||||
has_changed = False
|
||||
break
|
||||
else:
|
||||
# we need to create the domain
|
||||
api_method = 'dns.zone_domain_create'
|
||||
payload['domain'] = args['domain']
|
||||
payload['zone_id'] = zone_id
|
||||
has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method, payload=payload)
|
||||
api_method = "dns.zone_domain_create"
|
||||
payload["domain"] = args["domain"]
|
||||
payload["zone_id"] = zone_id
|
||||
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload)
|
||||
if not has_failed:
|
||||
has_changed = True
|
||||
|
||||
|
|
@ -145,23 +145,23 @@ def create_zone_domain(args=None, zone_exists=None, zone_id=None, payload=None):
|
|||
|
||||
|
||||
def delete_zone_domain(args=None, payload=None):
|
||||
'''
|
||||
"""
|
||||
Deletion is pretty simple, domains are always unique so we
|
||||
we don't need to do any sanity checking to avoid deleting the
|
||||
wrong thing.
|
||||
'''
|
||||
"""
|
||||
has_changed, has_failed = False, False
|
||||
msg, memset_api = None, None
|
||||
|
||||
api_method = 'dns.zone_domain_list'
|
||||
_has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method)
|
||||
api_method = "dns.zone_domain_list"
|
||||
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method)
|
||||
|
||||
domain_exists = check_zone_domain(data=response, domain=args['domain'])
|
||||
domain_exists = check_zone_domain(data=response, domain=args["domain"])
|
||||
|
||||
if domain_exists:
|
||||
api_method = 'dns.zone_domain_delete'
|
||||
payload['domain'] = args['domain']
|
||||
has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method, payload=payload)
|
||||
api_method = "dns.zone_domain_delete"
|
||||
payload["domain"] = args["domain"]
|
||||
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload)
|
||||
if not has_failed:
|
||||
has_changed = True
|
||||
memset_api = response.json()
|
||||
|
|
@ -172,31 +172,31 @@ def delete_zone_domain(args=None, payload=None):
|
|||
|
||||
|
||||
def create_or_delete_domain(args=None):
|
||||
'''
|
||||
"""
|
||||
We need to perform some initial sanity checking and also look
|
||||
up required info before handing it off to create or delete.
|
||||
'''
|
||||
"""
|
||||
retvals, payload = dict(), dict()
|
||||
has_changed, has_failed = False, False
|
||||
msg, stderr, memset_api = None, None, None
|
||||
|
||||
# get the zones and check if the relevant zone exists.
|
||||
api_method = 'dns.zone_list'
|
||||
has_failed, msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method)
|
||||
api_method = "dns.zone_list"
|
||||
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method)
|
||||
|
||||
if has_failed:
|
||||
# this is the first time the API is called; incorrect credentials will
|
||||
# manifest themselves at this point so we need to ensure the user is
|
||||
# informed of the reason.
|
||||
retvals['failed'] = has_failed
|
||||
retvals['msg'] = msg
|
||||
retvals["failed"] = has_failed
|
||||
retvals["msg"] = msg
|
||||
if response.status_code is not None:
|
||||
retvals['stderr'] = f"API returned an error: {response.status_code}"
|
||||
retvals["stderr"] = f"API returned an error: {response.status_code}"
|
||||
else:
|
||||
retvals['stderr'] = response.stderr
|
||||
retvals["stderr"] = response.stderr
|
||||
return retvals
|
||||
|
||||
zone_exists, msg, counter, zone_id = get_zone_id(zone_name=args['zone'], current_zones=response.json())
|
||||
zone_exists, msg, counter, zone_id = get_zone_id(zone_name=args["zone"], current_zones=response.json())
|
||||
|
||||
if not zone_exists:
|
||||
# the zone needs to be unique - this isn't a requirement of Memset's API but it
|
||||
|
|
@ -207,19 +207,21 @@ def create_or_delete_domain(args=None):
|
|||
elif counter > 1:
|
||||
stderr = f"{args['zone']} matches multiple zones, cannot create domain."
|
||||
|
||||
retvals['failed'] = has_failed
|
||||
retvals['msg'] = stderr
|
||||
retvals["failed"] = has_failed
|
||||
retvals["msg"] = stderr
|
||||
return retvals
|
||||
|
||||
if args['state'] == 'present':
|
||||
has_failed, has_changed, msg = create_zone_domain(args=args, zone_exists=zone_exists, zone_id=zone_id, payload=payload)
|
||||
if args["state"] == "present":
|
||||
has_failed, has_changed, msg = create_zone_domain(
|
||||
args=args, zone_exists=zone_exists, zone_id=zone_id, payload=payload
|
||||
)
|
||||
|
||||
if args['state'] == 'absent':
|
||||
if args["state"] == "absent":
|
||||
has_failed, has_changed, memset_api, msg = delete_zone_domain(args=args, payload=payload)
|
||||
|
||||
retvals['changed'] = has_changed
|
||||
retvals['failed'] = has_failed
|
||||
for val in ['msg', 'stderr', 'memset_api']:
|
||||
retvals["changed"] = has_changed
|
||||
retvals["failed"] = has_failed
|
||||
for val in ["msg", "stderr", "memset_api"]:
|
||||
if val is not None:
|
||||
retvals[val] = eval(val)
|
||||
|
||||
|
|
@ -230,17 +232,17 @@ def main():
|
|||
global module
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
state=dict(default='present', choices=['present', 'absent'], type='str'),
|
||||
api_key=dict(required=True, type='str', no_log=True),
|
||||
domain=dict(required=True, aliases=['name'], type='str'),
|
||||
zone=dict(required=True, type='str')
|
||||
state=dict(default="present", choices=["present", "absent"], type="str"),
|
||||
api_key=dict(required=True, type="str", no_log=True),
|
||||
domain=dict(required=True, aliases=["name"], type="str"),
|
||||
zone=dict(required=True, type="str"),
|
||||
),
|
||||
supports_check_mode=True
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
# populate the dict with the user-provided vars.
|
||||
args = dict(module.params)
|
||||
args['check_mode'] = module.check_mode
|
||||
args["check_mode"] = module.check_mode
|
||||
|
||||
# validate some API-specific limitations.
|
||||
api_validation(args=args)
|
||||
|
|
@ -252,19 +254,21 @@ def main():
|
|||
|
||||
# we would need to populate the return values with the API's response
|
||||
# in several places so it is easier to do it at the end instead.
|
||||
if not retvals['failed']:
|
||||
if args['state'] == 'present' and not module.check_mode:
|
||||
if not retvals["failed"]:
|
||||
if args["state"] == "present" and not module.check_mode:
|
||||
payload = dict()
|
||||
payload['domain'] = args['domain']
|
||||
api_method = 'dns.zone_domain_info'
|
||||
_has_failed, _msg, response = memset_api_call(api_key=args['api_key'], api_method=api_method, payload=payload)
|
||||
retvals['memset_api'] = response.json()
|
||||
payload["domain"] = args["domain"]
|
||||
api_method = "dns.zone_domain_info"
|
||||
_has_failed, _msg, response = memset_api_call(
|
||||
api_key=args["api_key"], api_method=api_method, payload=payload
|
||||
)
|
||||
retvals["memset_api"] = response.json()
|
||||
|
||||
if retvals['failed']:
|
||||
if retvals["failed"]:
|
||||
module.fail_json(**retvals)
|
||||
else:
|
||||
module.exit_json(**retvals)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue