| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2018, Simon Weald <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: memset_dns_reload |
| 11 |
author: "Simon Weald (@glitchcrab)" |
| 12 |
short_description: Request reload of Memset's DNS infrastructure, |
| 13 |
notes: |
| 14 |
- DNS reload requests are a best-effort service provided by Memset; these generally happen every 15 minutes by default, |
| 15 |
however you can request an immediate reload if later tasks rely on the records being created. An API key generated using |
| 16 |
the Memset customer control panel is required with the following minimum scope - C(dns.reload). If you wish to poll the |
| 17 |
job status to wait until the reload has completed, then C(job.status) is also required. |
| 18 |
description: |
| 19 |
- Request a reload of Memset's DNS infrastructure, and optionally poll until it finishes. |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._attributes |
| 22 |
attributes: |
| 23 |
check_mode: |
| 24 |
support: none |
| 25 |
diff_mode: |
| 26 |
support: none |
| 27 |
options: |
| 28 |
api_key: |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
description: |
| 32 |
- The API key obtained from the Memset control panel. |
| 33 |
poll: |
| 34 |
default: false |
| 35 |
type: bool |
| 36 |
description: |
| 37 |
- If V(true), it polls the reload job's status and return when the job has completed (unless the 30 second timeout is |
| 38 |
reached first). If the timeout is reached then the task does not return as failed, but stderr indicates that the polling |
| 39 |
failed. |
| 40 |
""" |
| 41 |
|
| 42 |
EXAMPLES = r""" |
| 43 |
- name: Submit DNS reload and poll |
| 44 |
community.general.memset_dns_reload: |
| 45 |
api_key: 5eb86c9196ab03919abcf03857163741 |
| 46 |
poll: true |
| 47 |
delegate_to: localhost |
| 48 |
""" |
| 49 |
|
| 50 |
RETURN = r""" |
| 51 |
memset_api: |
| 52 |
description: Raw response from the Memset API. |
| 53 |
returned: always |
| 54 |
type: complex |
| 55 |
contains: |
| 56 |
error: |
| 57 |
description: Whether the job ended in error state. |
| 58 |
returned: always |
| 59 |
type: bool |
| 60 |
sample: true |
| 61 |
finished: |
| 62 |
description: Whether the job completed before the result was returned. |
| 63 |
returned: always |
| 64 |
type: bool |
| 65 |
sample: true |
| 66 |
id: |
| 67 |
description: Job ID. |
| 68 |
returned: always |
| 69 |
type: str |
| 70 |
sample: "c9cc8ad2a3e3fb8c63ed83c424928ef8" |
| 71 |
status: |
| 72 |
description: Job status. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
sample: "DONE" |
| 76 |
type: |
| 77 |
description: Job type. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: "dns" |
| 81 |
""" |
| 82 |
|
| 83 |
from time import sleep |
| 84 |
|
| 85 |
from ansible.module_utils.basic import AnsibleModule |
| 86 |
|
| 87 |
from ansible_collections.community.general.plugins.module_utils._memset import memset_api_call |
| 88 |
|
| 89 |
|
| 90 |
def poll_reload_status(api_key=None, job_id=None, payload=None): |
| 91 |
""" |
| 92 |
We poll the `job.status` endpoint every 5 seconds up to a |
| 93 |
maximum of 6 times. This is a relatively arbitrary choice of |
| 94 |
timeout, however requests rarely take longer than 15 seconds |
| 95 |
to complete. |
| 96 |
""" |
| 97 |
memset_api, stderr, msg = None, None, None |
| 98 |
payload["id"] = job_id |
| 99 |
|
| 100 |
api_method = "job.status" |
| 101 |
_has_failed, _msg, response = memset_api_call(api_key=api_key, api_method=api_method, payload=payload) |
| 102 |
|
| 103 |
while not response.json()["finished"]: |
| 104 |
counter = 0 |
| 105 |
while counter < 6: |
| 106 |
sleep(5) |
| 107 |
_has_failed, msg, response = memset_api_call(api_key=api_key, api_method=api_method, payload=payload) |
| 108 |
counter += 1 |
| 109 |
if response.json()["error"]: |
| 110 |
# the reload job was submitted but polling failed. Don't return this as an overall task failure. |
| 111 |
stderr = "Reload submitted successfully, but the Memset API returned a job error when attempting to poll the reload status." |
| 112 |
else: |
| 113 |
memset_api = response.json() |
| 114 |
msg = None |
| 115 |
|
| 116 |
return memset_api, msg, stderr |
| 117 |
|
| 118 |
|
| 119 |
def reload_dns(args=None): |
| 120 |
""" |
| 121 |
DNS reloads are a single API call and therefore there's not much |
| 122 |
which can go wrong outside of auth errors. |
| 123 |
""" |
| 124 |
retvals, payload = dict(), dict() |
| 125 |
has_changed, has_failed = False, False |
| 126 |
memset_api, msg, stderr = None, None, None |
| 127 |
|
| 128 |
api_method = "dns.reload" |
| 129 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method) |
| 130 |
|
| 131 |
if has_failed: |
| 132 |
# this is the first time the API is called; incorrect credentials will |
| 133 |
# manifest themselves at this point so we need to ensure the user is |
| 134 |
# informed of the reason. |
| 135 |
retvals["failed"] = has_failed |
| 136 |
if response.status_code is not None: |
| 137 |
retvals["memset_api"] = response.json() |
| 138 |
else: |
| 139 |
retvals["stderr"] = response.stderr |
| 140 |
retvals["msg"] = msg |
| 141 |
return retvals |
| 142 |
|
| 143 |
# set changed to true if the reload request was accepted. |
| 144 |
has_changed = True |
| 145 |
memset_api = msg |
| 146 |
# empty msg var as we don't want to return the API's json response twice. |
| 147 |
msg = None |
| 148 |
|
| 149 |
if args["poll"]: |
| 150 |
# hand off to the poll function. |
| 151 |
job_id = response.json()["id"] |
| 152 |
memset_api, msg, stderr = poll_reload_status(api_key=args["api_key"], job_id=job_id, payload=payload) |
| 153 |
|
| 154 |
# assemble return variables. |
| 155 |
retvals["failed"] = has_failed |
| 156 |
retvals["changed"] = has_changed |
| 157 |
for val in ["msg", "stderr", "memset_api"]: |
| 158 |
if val is not None: |
| 159 |
retvals[val] = eval(val) |
| 160 |
|
| 161 |
return retvals |
| 162 |
|
| 163 |
|
| 164 |
def main(): |
| 165 |
global module |
| 166 |
module = AnsibleModule( |
| 167 |
argument_spec=dict(api_key=dict(required=True, type="str", no_log=True), poll=dict(default=False, type="bool")), |
| 168 |
supports_check_mode=False, |
| 169 |
) |
| 170 |
|
| 171 |
# populate the dict with the user-provided vars. |
| 172 |
args = dict(module.params) |
| 173 |
|
| 174 |
retvals = reload_dns(args) |
| 175 |
|
| 176 |
if retvals["failed"]: |
| 177 |
module.fail_json(**retvals) |
| 178 |
else: |
| 179 |
module.exit_json(**retvals) |
| 180 |
|
| 181 |
|
| 182 |
if __name__ == "__main__": |
| 183 |
main() |