Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_dns.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_dns
20 version_added: '1.0.0'
21 short_description: Configure FlashArray DNS settings
22 description:
23 - Set or erase configuration for the DNS settings.
24 - Nameservers provided will overwrite any existing nameservers.
25 - From Purity//FA 6.3.3 DNS setting for FA-File can be configured separately
26 to the management DNS settings
27 author:
28 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
29 options:
30 name:
31 description:
32 - Name of the DNS configuration.
33 - Default value only supported for management service
34 default: management
35 type: str
36 version_added: 1.14.0
37 state:
38 description:
39 - Set or delete directory service configuration
40 default: present
41 type: str
42 choices: [ absent, present ]
43 domain:
44 description:
45 - Domain suffix to be appended when perofrming DNS lookups.
46 type: str
47 nameservers:
48 description:
49 - List of up to 3 unique DNS server IP addresses. These can be
50 IPv4 or IPv6 - No validation is done of the addresses is performed.
51 type: list
52 elements: str
53 service:
54 description:
55 - Type of service the DNS will work with
56 type: str
57 version_added: 1.14.0
58 choices: [ management, file ]
59 default: management
60 source:
61 description:
62 - A virtual network interface (vif)
63 type: str
64 version_added: 1.14.0
65 context:
66 description:
67 - Name of fleet member on which to perform the operation.
68 - This requires the array receiving the request is a member of a fleet
69 and the context name to be a member of the same fleet.
70 type: str
71 default: ""
72 version_added: 1.40.0
73 extends_documentation_fragment:
74 - purestorage.flasharray.purestorage.fa
75 """
76
77 EXAMPLES = r"""
78 - name: Delete existing management DNS settings
79 purestorage.flasharray.purefa_dns:
80 state: absent
81 fa_url: 10.10.10.2
82 api_token: e31060a7-21fc-e277-6240-25983c6c4592
83
84 - name: Set managemnt DNS settings
85 purestorage.flasharray.purefa_dns:
86 domain: purestorage.com
87 nameservers:
88 - 8.8.8.8
89 - 8.8.4.4
90 fa_url: 10.10.10.2
91 api_token: e31060a7-21fc-e277-6240-25983c6c4592
92
93 - name: Set file DNS settings
94 purestorage.flasharray.purefa_dns:
95 domain: purestorage.com
96 nameservers:
97 - 8.8.8.8
98 - 8.8.4.4
99 name: ad_dns
100 service: file
101 fa_url: 10.10.10.2
102 api_token: e31060a7-21fc-e277-6240-25983c6c4592
103
104 - name: Delete file DNS settings
105 purestorage.flasharray.purefa_dns:
106 state: absent
107 name: ad_dns
108 fa_url: 10.10.10.2
109 api_token: e31060a7-21fc-e277-6240-25983c6c4592
110 """
111
112 RETURN = r"""
113 """
114
115 HAS_PURESTORAGE = True
116 try:
117 from pypureclient.flasharray import DnsPost, DnsPatch, ReferenceNoId
118 except ImportError:
119 HAS_PURESTORAGE = False
120
121 from ansible.module_utils.basic import AnsibleModule
122 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
123 LooseVersion,
124 )
125 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
126 get_array,
127 purefa_argument_spec,
128 )
129
130 MULTIPLE_DNS = "2.15"
131 CONTEXT_API_VERSION = "2.47"
132
133
134 def remove(duplicate):
135 final_list = []
136 for num in duplicate:
137 if num not in final_list:
138 final_list.append(num)
139 return final_list
140
141
142 def _get_source(module, array):
143 api_version = array.get_rest_version()
144 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
145 res = array.get_network_interfaces(
146 names=[module.params["source"]], context_names=[module.params["context"]]
147 )
148 else:
149 res = array.get_network_interfaces(names=[module.params["source"]])
150 return bool(res.status_code == 200)
151
152
153 def delete_dns(module, array):
154 """Delete DNS settings"""
155 changed = False
156 api_version = array.get_rest_version()
157 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
158 current_dns = list(
159 array.get_dns(context_names=[module.params["context"]]).items
160 )[0]
161 else:
162 current_dns = list(array.get_dns().items)[0]
163 if getattr(current_dns, "domain", None) in ["", None] and getattr(
164 current_dns, "nameservers", None
165 ) in [[""], None]:
166 module.exit_json(changed=changed)
167 else:
168 changed = True
169 if not module.check_mode:
170 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
171 res = array.delete_dns(
172 names=["management"], context_names=[module.params["context"]]
173 )
174 else:
175 res = array.delete_dns(names=["management"])
176 if res.status_code != 200:
177 module.fail_json(
178 msg="Delete DNS settings failed. Error: {0}".format(
179 res.errors[0].message
180 )
181 )
182 module.exit_json(changed=changed)
183
184
185 def create_dns(module, array):
186 """Set DNS settings"""
187 changed = False
188 api_version = array.get_rest_version()
189 current_dns = list(array.get_dns().items)[0]
190 if current_dns["domain"] != module.params["domain"] or sorted(
191 module.params["nameservers"]
192 ) != sorted(current_dns["nameservers"]):
193 changed = True
194 if not module.check_mode:
195 res = array.patch_dns(
196 names=["management"],
197 dns=DnsPatch(
198 domain=module.params["domain"],
199 nameservers=module.params["nameservers"][0:3],
200 ),
201 )
202 if res.status_code != 200:
203 module.fail_json(
204 msg="Set DNS settings failed. Error: {0}".format(res.errors[0].message)
205 )
206 module.exit_json(changed=changed)
207
208
209 def update_multi_dns(module, array):
210 """Update a DNS configuration"""
211 changed = False
212 api_version = array.get_rest_version()
213 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
214 current_dns = list(
215 array.get_dns(
216 names=[module.params["name"]], context_names=[module.params["context"]]
217 ).items
218 )[0]
219 else:
220 current_dns = list(array.get_dns(names=[module.params["name"]]).items)[0]
221 new_dns = current_dns
222 if module.params["domain"] and current_dns.domain != module.params["domain"]:
223 new_dns.domain = module.params["domain"]
224 changed = True
225 if module.params["service"] and current_dns.services != [module.params["service"]]:
226 module.fail_json(msg="Changing service type is not permitted")
227 if module.params["nameservers"] and sorted(current_dns.nameservers) != sorted(
228 module.params["nameservers"]
229 ):
230 new_dns.nameservers = module.params["nameservers"]
231 changed = True
232 if (module.params["source"] or module.params["source"] == "") and getattr(
233 current_dns.source, "name", ""
234 ) != module.params["source"]:
235 new_dns.source.name = module.params["source"]
236 changed = True
237 if changed and not module.check_mode:
238 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
239 res = array.patch_dns(
240 names=[module.params["name"]],
241 dns=DnsPatch(
242 domain=new_dns.domain,
243 nameservers=new_dns.nameservers,
244 source=ReferenceNoId(name=module.params["source"]),
245 ),
246 context_names=[module.params["context"]],
247 )
248 else:
249 res = array.patch_dns(
250 names=[module.params["name"]],
251 dns=DnsPatch(
252 domain=new_dns.domain,
253 nameservers=new_dns.nameservers,
254 source=ReferenceNoId(name=module.params["source"]),
255 ),
256 )
257 if res.status_code != 200:
258 module.fail_json(
259 msg="Update to DNS service {0} failed. Error: {1}".format(
260 module.params["name"], res.errors[0].message
261 )
262 )
263 module.exit_json(changed=changed)
264
265
266 def delete_multi_dns(module, array):
267 """Delete a DNS configuration"""
268 changed = True
269 api_version = array.get_rest_version()
270 if module.params["name"] == "management":
271 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
272 res = array.patch_dns(
273 names=[module.params["name"]],
274 dns=DnsPatch(domain="", nameservers=[]),
275 context_names=[module.params["context"]],
276 )
277 else:
278 res = array.patch_dns(
279 names=[module.params["name"]],
280 dns=DnsPatch(domain="", nameservers=[]),
281 )
282 if res.status_code != 200:
283 module.fail_json(
284 msg="Management DNS configuration not deleted. Error: {0}".format(
285 res.errors[0].message
286 )
287 )
288 else:
289 if not module.check_mode:
290 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
291 res = array.delete_dns(
292 names=[module.params["name"]],
293 context_names=[module.params["context"]],
294 )
295 else:
296 res = array.delete_dns(names=[module.params["name"]])
297 if res.status_code != 200:
298 module.fail_json(
299 msg="Failed to delete DNS configuration {0}. Error: {1}".format(
300 module.params["name"], res.errors[0].message
301 )
302 )
303 module.exit_json(changed=changed)
304
305
306 def create_multi_dns(module, array):
307 """Create a DNS configuration"""
308 changed = True
309 api_version = array.get_rest_version()
310 if not module.check_mode:
311 if module.params["service"] == "file":
312 if module.params["source"]:
313 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
314 res = array.post_dns(
315 names=[module.params["name"]],
316 dns=DnsPost(
317 services=[module.params["service"]],
318 domain=module.params["domain"],
319 nameservers=module.params["nameservers"],
320 source=ReferenceNoId(name=module.params["source"].lower()),
321 ),
322 context_names=[module.params["context"]],
323 )
324 else:
325 res = array.post_dns(
326 names=[module.params["name"]],
327 dns=DnsPost(
328 services=[module.params["service"]],
329 domain=module.params["domain"],
330 nameservers=module.params["nameservers"],
331 source=ReferenceNoId(name=module.params["source"].lower()),
332 ),
333 )
334 else:
335 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
336 res = array.post_dns(
337 names=[module.params["name"]],
338 dns=DnsPost(
339 services=[module.params["service"]],
340 domain=module.params["domain"],
341 nameservers=module.params["nameservers"],
342 ),
343 context_names=[module.params["context"]],
344 )
345 else:
346 res = array.post_dns(
347 names=[module.params["name"]],
348 dns=DnsPost(
349 services=[module.params["service"]],
350 domain=module.params["domain"],
351 nameservers=module.params["nameservers"],
352 ),
353 )
354 else:
355 if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version):
356 res = array.post_dns(
357 names=[module.params["name"]],
358 dns=DnsPost(
359 services=[module.params["service"]],
360 domain=module.params["domain"],
361 nameservers=module.params["nameservers"],
362 ),
363 context_names=[module.params["context"]],
364 )
365 else:
366 res = array.post_dns(
367 names=[module.params["name"]],
368 dns=DnsPost(
369 services=[module.params["service"]],
370 domain=module.params["domain"],
371 nameservers=module.params["nameservers"],
372 ),
373 )
374 if res.status_code != 200:
375 module.fail_json(
376 msg="Failed to create {0} DNS configuration {1}. Error: {2}".format(
377 module.params["service"],
378 module.params["name"],
379 res.errors[0].message,
380 )
381 )
382 module.exit_json(changed=changed)
383
384
385 def main():
386 argument_spec = purefa_argument_spec()
387 argument_spec.update(
388 dict(
389 state=dict(type="str", default="present", choices=["absent", "present"]),
390 name=dict(type="str", default="management"),
391 service=dict(
392 type="str", default="management", choices=["management", "file"]
393 ),
394 domain=dict(type="str"),
395 source=dict(type="str"),
396 nameservers=dict(type="list", elements="str"),
397 context=dict(type="str", default=""),
398 )
399 )
400
401 module = AnsibleModule(argument_spec, supports_check_mode=True)
402
403 state = module.params["state"]
404 array = get_array(module)
405 api_version = array.get_rest_version()
406 if module.params["nameservers"]:
407 module.params["nameservers"] = remove(module.params["nameservers"])
408 if module.params["service"] == "management":
409 module.params["nameservers"] = module.params["nameservers"][0:3]
410
411 if LooseVersion(MULTIPLE_DNS) <= LooseVersion(api_version):
412 configs = list(array.get_dns().items)
413 exists = False
414 for config in configs:
415 if config.name == module.params["name"]:
416 exists = True
417 if (
418 module.params["service"] == "management"
419 and module.params["name"] != "management"
420 and not exists
421 ):
422 module.warn("Overriding configuration name to management")
423 module.params["name"] = "management"
424 if module.params["source"] and not _get_source(module, array):
425 module.fail_json(
426 msg="Specified VIF {0} does not exist.".format(module.params["source"])
427 )
428 if state == "present" and exists:
429 update_multi_dns(module, array)
430 elif state == "present" and not exists:
431 if len(configs) == 2:
432 module.fail_json(
433 msg="Only 2 DNS configurations are currently "
434 "supported. One for management and one for file services"
435 )
436 create_multi_dns(module, array)
437 elif exists and state == "absent":
438 delete_multi_dns(module, array)
439 else:
440 module.exit_json(changed=False)
441 else:
442 if state == "absent":
443 delete_dns(module, array)
444 elif state == "present":
445 if not module.params["domain"] or not module.params["nameservers"]:
446 module.fail_json(
447 msg="`domain` and `nameservers` are required for DNS configuration"
448 )
449 create_dns(module, array)
450 else:
451 module.exit_json(changed=False)
452
453
454 if __name__ == "__main__":
455 main()