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