| 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_zone |
| 11 |
author: "Simon Weald (@glitchcrab)" |
| 12 |
short_description: Creates and deletes Memset DNS zones |
| 13 |
notes: |
| 14 |
- Zones can be thought of as a logical group of domains, all of which share the same DNS records (in other words they point |
| 15 |
to the same IP). An API key generated using the Memset customer control panel is needed with the following minimum scope |
| 16 |
- C(dns.zone_create), C(dns.zone_delete), C(dns.zone_list). |
| 17 |
description: |
| 18 |
- Manage DNS zones in a Memset account. |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
state: |
| 28 |
required: true |
| 29 |
description: |
| 30 |
- Indicates desired state of resource. |
| 31 |
type: str |
| 32 |
choices: [absent, present] |
| 33 |
api_key: |
| 34 |
required: true |
| 35 |
description: |
| 36 |
- The API key obtained from the Memset control panel. |
| 37 |
type: str |
| 38 |
name: |
| 39 |
required: true |
| 40 |
description: |
| 41 |
- The zone nickname; usually the same as the main domain. Ensure this value has at most 250 characters. |
| 42 |
type: str |
| 43 |
aliases: [nickname] |
| 44 |
ttl: |
| 45 |
description: |
| 46 |
- The default TTL for all records created in the zone. This must be a valid int from U(https://www.memset.com/apidocs/methods_dns.html#dns.zone_create). |
| 47 |
type: int |
| 48 |
default: 0 |
| 49 |
choices: [0, 300, 600, 900, 1800, 3600, 7200, 10800, 21600, 43200, 86400] |
| 50 |
force: |
| 51 |
default: false |
| 52 |
type: bool |
| 53 |
description: |
| 54 |
- Forces deletion of a zone and all zone domains/zone records it contains. |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
# Create the zone 'test' |
| 59 |
- name: Create zone |
| 60 |
community.general.memset_zone: |
| 61 |
name: test |
| 62 |
state: present |
| 63 |
api_key: 5eb86c9196ab03919abcf03857163741 |
| 64 |
ttl: 300 |
| 65 |
delegate_to: localhost |
| 66 |
|
| 67 |
# Force zone deletion |
| 68 |
- name: Force delete zone |
| 69 |
community.general.memset_zone: |
| 70 |
name: test |
| 71 |
state: absent |
| 72 |
api_key: 5eb86c9196ab03919abcf03857163741 |
| 73 |
force: true |
| 74 |
delegate_to: localhost |
| 75 |
""" |
| 76 |
|
| 77 |
RETURN = r""" |
| 78 |
memset_api: |
| 79 |
description: Zone info from the Memset API. |
| 80 |
returned: when state == present |
| 81 |
type: complex |
| 82 |
contains: |
| 83 |
domains: |
| 84 |
description: List of domains in this zone. |
| 85 |
returned: always |
| 86 |
type: list |
| 87 |
sample: [] |
| 88 |
id: |
| 89 |
description: Zone ID. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: "b0bb1ce851aeea6feeb2dc32fe83bf9c" |
| 93 |
nickname: |
| 94 |
description: Zone name. |
| 95 |
returned: always |
| 96 |
type: str |
| 97 |
sample: "example.com" |
| 98 |
records: |
| 99 |
description: List of DNS records for domains in this zone. |
| 100 |
returned: always |
| 101 |
type: list |
| 102 |
sample: [] |
| 103 |
ttl: |
| 104 |
description: Default TTL for domains in this zone. |
| 105 |
returned: always |
| 106 |
type: int |
| 107 |
sample: 300 |
| 108 |
""" |
| 109 |
|
| 110 |
from ansible.module_utils.basic import AnsibleModule |
| 111 |
|
| 112 |
from ansible_collections.community.general.plugins.module_utils._memset import check_zone, get_zone_id, memset_api_call |
| 113 |
|
| 114 |
|
| 115 |
def api_validation(args=None): |
| 116 |
""" |
| 117 |
Perform some validation which will be enforced by Memset's API (see: |
| 118 |
https://www.memset.com/apidocs/methods_dns.html#dns.zone_record_create) |
| 119 |
""" |
| 120 |
# zone domain length must be less than 250 chars. |
| 121 |
if len(args["name"]) > 250: |
| 122 |
stderr = "Zone name must be less than 250 characters in length." |
| 123 |
module.fail_json(failed=True, msg=stderr, stderr=stderr) |
| 124 |
|
| 125 |
|
| 126 |
def check(args=None): |
| 127 |
""" |
| 128 |
Support for running with check mode. |
| 129 |
""" |
| 130 |
retvals = dict() |
| 131 |
|
| 132 |
api_method = "dns.zone_list" |
| 133 |
has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method) |
| 134 |
|
| 135 |
zone_exists, counter = check_zone(data=response, name=args["name"]) |
| 136 |
|
| 137 |
# set changed to true if the operation would cause a change. |
| 138 |
has_changed = (zone_exists and args["state"] == "absent") or (not zone_exists and args["state"] == "present") |
| 139 |
|
| 140 |
retvals["changed"] = has_changed |
| 141 |
retvals["failed"] = has_failed |
| 142 |
|
| 143 |
return retvals |
| 144 |
|
| 145 |
|
| 146 |
def create_zone(args=None, zone_exists=None, payload=None): |
| 147 |
""" |
| 148 |
At this point we already know whether the zone exists, so we |
| 149 |
just need to make the API reflect the desired state. |
| 150 |
""" |
| 151 |
has_changed, has_failed = False, False |
| 152 |
msg, memset_api = None, None |
| 153 |
|
| 154 |
if not zone_exists: |
| 155 |
payload["ttl"] = args["ttl"] |
| 156 |
payload["nickname"] = args["name"] |
| 157 |
api_method = "dns.zone_create" |
| 158 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 159 |
if not has_failed: |
| 160 |
has_changed = True |
| 161 |
else: |
| 162 |
api_method = "dns.zone_list" |
| 163 |
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method) |
| 164 |
for zone in response.json(): |
| 165 |
if zone["nickname"] == args["name"]: |
| 166 |
break |
| 167 |
if zone["ttl"] != args["ttl"]: |
| 168 |
# update the zone if the desired TTL is different. |
| 169 |
payload["id"] = zone["id"] |
| 170 |
payload["ttl"] = args["ttl"] |
| 171 |
api_method = "dns.zone_update" |
| 172 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 173 |
if not has_failed: |
| 174 |
has_changed = True |
| 175 |
|
| 176 |
# populate return var with zone info. |
| 177 |
api_method = "dns.zone_list" |
| 178 |
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method) |
| 179 |
|
| 180 |
zone_exists, msg, counter, zone_id = get_zone_id(zone_name=args["name"], current_zones=response.json()) |
| 181 |
|
| 182 |
if zone_exists: |
| 183 |
payload = dict() |
| 184 |
payload["id"] = zone_id |
| 185 |
api_method = "dns.zone_info" |
| 186 |
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 187 |
memset_api = response.json() |
| 188 |
|
| 189 |
return has_failed, has_changed, memset_api, msg |
| 190 |
|
| 191 |
|
| 192 |
def delete_zone(args=None, zone_exists=None, payload=None): |
| 193 |
""" |
| 194 |
Deletion requires extra sanity checking as the zone cannot be |
| 195 |
deleted if it contains domains or records. Setting force=true |
| 196 |
will override this behaviour. |
| 197 |
""" |
| 198 |
has_changed, has_failed = False, False |
| 199 |
msg, memset_api = None, None |
| 200 |
|
| 201 |
if zone_exists: |
| 202 |
api_method = "dns.zone_list" |
| 203 |
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 204 |
counter = 0 |
| 205 |
for zone in response.json(): |
| 206 |
if zone["nickname"] == args["name"]: |
| 207 |
counter += 1 |
| 208 |
if counter == 1: |
| 209 |
for zone in response.json(): |
| 210 |
if zone["nickname"] == args["name"]: |
| 211 |
zone_id = zone["id"] |
| 212 |
domain_count = len(zone["domains"]) |
| 213 |
record_count = len(zone["records"]) |
| 214 |
if (domain_count > 0 or record_count > 0) and args["force"] is False: |
| 215 |
# we need to fail out if force was not explicitly set. |
| 216 |
stderr = "Zone contains domains or records and force was not used." |
| 217 |
has_failed = True |
| 218 |
has_changed = False |
| 219 |
module.fail_json(failed=has_failed, changed=has_changed, msg=msg, stderr=stderr, rc=1) |
| 220 |
api_method = "dns.zone_delete" |
| 221 |
payload["id"] = zone_id |
| 222 |
has_failed, msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method, payload=payload) |
| 223 |
if not has_failed: |
| 224 |
has_changed = True |
| 225 |
# return raw JSON from API in named var and then unset msg var so we aren't returning the same thing twice. |
| 226 |
memset_api = msg |
| 227 |
msg = None |
| 228 |
else: |
| 229 |
# zone names are not unique, so we cannot safely delete the requested |
| 230 |
# zone at this time. |
| 231 |
has_failed = True |
| 232 |
has_changed = False |
| 233 |
msg = "Unable to delete zone as multiple zones with the same name exist." |
| 234 |
else: |
| 235 |
has_failed, has_changed = False, False |
| 236 |
|
| 237 |
return has_failed, has_changed, memset_api, msg |
| 238 |
|
| 239 |
|
| 240 |
def create_or_delete(args=None): |
| 241 |
""" |
| 242 |
We need to perform some initial sanity checking and also look |
| 243 |
up required info before handing it off to create or delete. |
| 244 |
""" |
| 245 |
retvals, payload = dict(), dict() |
| 246 |
has_failed, has_changed = False, False |
| 247 |
msg, memset_api, stderr = None, None, None |
| 248 |
|
| 249 |
# get the zones and check if the relevant zone exists. |
| 250 |
api_method = "dns.zone_list" |
| 251 |
_has_failed, _msg, response = memset_api_call(api_key=args["api_key"], api_method=api_method) |
| 252 |
if _has_failed: |
| 253 |
# this is the first time the API is called; incorrect credentials will |
| 254 |
# manifest themselves at this point so we need to ensure the user is |
| 255 |
# informed of the reason. |
| 256 |
retvals["failed"] = _has_failed |
| 257 |
retvals["msg"] = _msg |
| 258 |
|
| 259 |
if response.stderr is not None: |
| 260 |
retvals["stderr"] = response.stderr |
| 261 |
|
| 262 |
return retvals |
| 263 |
|
| 264 |
zone_exists, _msg, counter, _zone_id = get_zone_id(zone_name=args["name"], current_zones=response.json()) |
| 265 |
|
| 266 |
if args["state"] == "present": |
| 267 |
has_failed, has_changed, memset_api, msg = create_zone(args=args, zone_exists=zone_exists, payload=payload) |
| 268 |
|
| 269 |
elif args["state"] == "absent": |
| 270 |
has_failed, has_changed, memset_api, msg = delete_zone(args=args, zone_exists=zone_exists, payload=payload) |
| 271 |
|
| 272 |
retvals["failed"] = has_failed |
| 273 |
retvals["changed"] = has_changed |
| 274 |
retvals["msg"] = msg |
| 275 |
retvals["stderr"] = stderr |
| 276 |
retvals["memset_api"] = memset_api |
| 277 |
|
| 278 |
return retvals |
| 279 |
|
| 280 |
|
| 281 |
def main(): |
| 282 |
global module |
| 283 |
module = AnsibleModule( |
| 284 |
argument_spec=dict( |
| 285 |
state=dict(required=True, choices=["present", "absent"], type="str"), |
| 286 |
api_key=dict(required=True, type="str", no_log=True), |
| 287 |
name=dict(required=True, aliases=["nickname"], type="str"), |
| 288 |
ttl=dict(default=0, choices=[0, 300, 600, 900, 1800, 3600, 7200, 10800, 21600, 43200, 86400], type="int"), |
| 289 |
force=dict(default=False, type="bool"), |
| 290 |
), |
| 291 |
supports_check_mode=True, |
| 292 |
) |
| 293 |
|
| 294 |
# populate the dict with the user-provided vars. |
| 295 |
args = dict(module.params) |
| 296 |
args["check_mode"] = module.check_mode |
| 297 |
|
| 298 |
# validate some API-specific limitations. |
| 299 |
api_validation(args=args) |
| 300 |
|
| 301 |
if module.check_mode: |
| 302 |
retvals = check(args) |
| 303 |
else: |
| 304 |
retvals = create_or_delete(args) |
| 305 |
|
| 306 |
if retvals["failed"]: |
| 307 |
module.fail_json(**retvals) |
| 308 |
else: |
| 309 |
module.exit_json(**retvals) |
| 310 |
|
| 311 |
|
| 312 |
if __name__ == "__main__": |
| 313 |
main() |