Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2024, 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_realm
20 version_added: '1.33.0'
21 short_description: Manage realms on Pure Storage FlashArrays
22 description:
23 - Create, delete or modify realms on Pure Storage FlashArrays.
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 name:
28 description:
29 - The name of the realm.
30 - This has to be unique and not equal to any existing realm or pod.
31 type: str
32 required: true
33 state:
34 description:
35 - Define whether the realm should exist or not.
36 type: str
37 default: present
38 choices: [ absent, present ]
39 eradicate:
40 description:
41 - Define whether to eradicate the realm on delete or leave in trash.
42 type : bool
43 default: false
44 quota:
45 description:
46 - Logical quota limit of the realm in K, M, G, T or P units, or bytes.
47 - This must be a multiple of 512.
48 type: str
49 bw_qos:
50 description:
51 - Bandwidth limit for realm in M or G units.
52 M will set MB/s
53 G will set GB/s
54 To clear an existing QoS setting use 0 (zero)
55 type: str
56 iops_qos:
57 description:
58 - IOPs limit for realm - use value or K or M
59 K will mean 1000
60 M will mean 1000000
61 To clear an existing IOPs setting use 0 (zero)
62 type: str
63 rename:
64 description:
65 - Value to rename the specified realm to
66 - This has to be unique and not equal to any existing realm or pods.
67 type: str
68 ignore_usage:
69 description:
70 - Flag used to override checks for quota management
71 operations.
72 - If set to true, realm usage is not checked against the
73 quota_limits that are set.
74 - If set to false, the actual logical bytes in use are prevented
75 from exceeding the limits set in the realm.
76 - Client operations might be impacted.
77 - If the limit exceeds the quota, the operation is not allowed.
78 default: false
79 type: bool
80 delete_contents:
81 description:
82 - This enables you to eradicate realms with contents.
83 type: bool
84 default: False
85 extends_documentation_fragment:
86 - purestorage.flasharray.purestorage.fa
87 """
88
89 EXAMPLES = r"""
90 - name: Create new realm
91 purestorage.flasharray.purefa_realm:
92 name: foo
93 bw_qos: 50M
94 iops_qos: 100
95 fa_url: 10.10.10.2
96 api_token: e31060a7-21fc-e277-6240-25983c6c4592
97
98 - name: Update realm QoS limits
99 purestorage.flasharray.purefa_realm:
100 name: foo
101 bw_qos: 0
102 iops_qos: 5555
103 fa_url: 10.10.10.2
104 api_token: e31060a7-21fc-e277-6240-25983c6c4592
105
106 - name: Destroy realm
107 purestorage.flasharray.purefa_realm:
108 name: foo
109 fa_url: 10.10.10.2
110 api_token: e31060a7-21fc-e277-6240-25983c6c4592
111 state: absent
112
113 - name: Recover deleted realm
114 purestorage.flasharray.purefa_realm:
115 name: foo
116 fa_url: 10.10.10.2
117 api_token: e31060a7-21fc-e277-6240-25983c6c4592
118
119 - name: Destroy and Eradicate realm
120 purestorage.flasharray.purefa_realm:
121 name: foo
122 eradicate: true
123 fa_url: 10.10.10.2
124 api_token: e31060a7-21fc-e277-6240-25983c6c4592
125 state: absent
126
127 - name: Rename realm foo to bar
128 purestorage.flasharray.purefa_realm:
129 name: foo
130 rename: bar
131 fa_url: 10.10.10.2
132 api_token: e31060a7-21fc-e277-6240-25983c6c4592
133 """
134
135 RETURN = r"""
136 """
137
138 HAS_PURESTORAGE = True
139 try:
140 from pypureclient.flasharray import RealmPatch, RealmPost, ContainerQos
141 except ImportError:
142 HAS_PURESTORAGE = False
143
144 from ansible.module_utils.basic import AnsibleModule
145 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
146 get_array,
147 purefa_argument_spec,
148 )
149 from ansible_collections.purestorage.flasharray.plugins.module_utils.common import (
150 human_to_bytes,
151 human_to_real,
152 )
153 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
154 LooseVersion,
155 )
156
157 MINIMUM_API_VERSION = "2.36"
158
159
160 def get_pending_realm(module, array):
161 """Get Deleted realm"""
162 vgroup = None
163 res = array.get_realms(names=[module.params["name"]])
164 if res.status_code == 200:
165 vgroup = list(res.items)[0].destroyed
166 return vgroup
167
168
169 def get_realm(module, array):
170 """Get Realm"""
171 vgroup = None
172 res = array.get_realms(names=[module.params["name"]])
173 if res.status_code == 200:
174 if not list(res.items)[0].destroyed:
175 vgroup = True
176 return vgroup
177
178
179 def rename_realm(module, array):
180 changed = True
181 if not module.check_mode:
182 res = array.patch_realm(
183 names=[module.params["name"]],
184 realm=RealmPatch(name=module.params["rename"]),
185 )
186 if res.status_code != 200:
187 module.fail_json(
188 msg="Rename to {0} failed. Error: {1}".format(
189 module.params["rename"], res.errors[0].message
190 )
191 )
192 module.exit_json(changed=changed)
193
194
195 def make_realm(module, array):
196 """Create Realm"""
197 changed = True
198 if module.params["quota"]:
199 quota = int(human_to_bytes(module.params["quota"]))
200 if quota % 512 != 0:
201 module.fail_json(msg="Quota not a multiple of 512 bytes")
202 if quota < 1048576:
203 module.fail_json(
204 msg="Quota must be a value greater than or equal to 1048576 bytes"
205 )
206 res = array.post_realms(
207 names=[module.params["name"]], realm=RealmPost(quota_limit=quota)
208 )
209 if res.status_code != 200:
210 module.fail_json(
211 msg="Creation of realm {0} failed. Error: {1}".format(
212 module.params["name"], res.errors[0].message
213 )
214 )
215 else:
216 res = array.post_realms(names=[module.params["name"]])
217 if res.status_code != 200:
218 module.fail_json(
219 msg="Creation of realm {0} failed. Error: {1}".format(
220 module.params["name"], res.errors[0].message
221 )
222 )
223 if module.params["bw_qos"] and not module.params["iops_qos"]:
224 if int(human_to_bytes(module.params["bw_qos"])) in range(1048576, 549755813888):
225 changed = True
226 if not module.check_mode:
227 res = array.patch_realms(
228 names=[module.params["name"]],
229 realm=RealmPatch(
230 qos=ContainerQos(
231 bandwidth_limit=int(human_to_bytes(module.params["bw_qos"]))
232 )
233 ),
234 )
235 if res.status_code != 200:
236 module.fail_json(
237 msg="Setting of realm {0} QoS failed. Error {1}".format(
238 module.params["name"],
239 res.errors[0].name,
240 )
241 )
242 else:
243 module.fail_json(
244 msg="Bandwidth QoS value {0} out of range.".format(
245 module.params["bw_qos"]
246 )
247 )
248 elif module.params["iops_qos"] and not module.params["bw_qos"]:
249 if int(human_to_real(module.params["iops_qos"])) in range(100, 100000000):
250 changed = True
251 if not module.check_mode:
252 res = array.patch_realms(
253 names=[module.params["name"]],
254 realm=RealmPatch(
255 qos=ContainerQos(
256 iops_limit=int(human_to_real(module.params["iops_qos"]))
257 )
258 ),
259 )
260 if res.status_code != 200:
261 module.fail_json(
262 msg="Setting of realm {0} QoS failed. Error {1}".format(
263 module.params["name"],
264 res.errors[0].name,
265 )
266 )
267 else:
268 module.fail_json(
269 msg="IOPs QoS value {0} out of range.".format(module.params["iops_qos"])
270 )
271 elif module.params["iops_qos"] and module.params["bw_qos"]:
272 bw_qos_size = int(human_to_bytes(module.params["bw_qos"]))
273 if int(human_to_real(module.params["iops_qos"])) in range(
274 100, 100000000
275 ) and bw_qos_size in range(1048576, 549755813888):
276 changed = True
277 if not module.check_mode:
278 res = array.patch_realms(
279 names=[module.params["name"]],
280 realm=RealmPatch(
281 qos=ContainerQos(
282 iops_limit=int(human_to_real(module.params["iops_qos"])),
283 bandwidth_limit=int(
284 human_to_bytes(module.params["bw_qos"])
285 ),
286 ),
287 ),
288 )
289 if res.status_code != 200:
290 module.fail_json(
291 msg="Setting of realm {0} QoS failed. Error {1}".format(
292 module.params["name"],
293 res.errors[0].name,
294 )
295 )
296 else:
297 module.fail_json(msg="IOPs or Bandwidth QoS value out of range.")
298
299 module.exit_json(changed=changed)
300
301
302 def update_realm(module, array):
303 """Update Realm"""
304 changed = False
305 realm = list(array.get_realms(names=[module.params["name"]]).items)[0]
306 current_realm = {
307 "quota": realm.quota_limit,
308 "bw": getattr(realm.qos, "bandwidth_limit", 0),
309 "iops": getattr(realm.qos, "iops_limit", 0),
310 }
311 if module.params["bw_qos"]:
312 if int(human_to_bytes(module.params["bw_qos"])) != current_realm["bw"]:
313 if int(human_to_bytes(module.params["bw_qos"])) in range(
314 1048576, 549755813888
315 ):
316 changed = True
317 if not module.check_mode:
318 res = array.patch_realms(
319 names=[module.params["name"]],
320 realm=RealmPatch(
321 qos=ContainerQos(
322 bandwidth_limit=int(
323 human_to_bytes(module.params["bw_qos"])
324 )
325 )
326 ),
327 )
328 if res.status_code != 200:
329 module.fail_json(
330 msg="Realm {0} Bandwidth QoS change failed. Error: {1}".format(
331 module.params["name"],
332 res.errors[0].message,
333 )
334 )
335 else:
336 module.fail_json(
337 msg="Bandwidth QoS value {0} out of range.".format(
338 module.params["bw_qos"]
339 )
340 )
341 if module.params["iops_qos"]:
342 if int(human_to_real(module.params["iops_qos"])) != current_realm["iops"]:
343 if int(human_to_real(module.params["iops_qos"])) in range(100, 100000000):
344 changed = True
345 if not module.check_mode:
346 res = array.patch_realms(
347 names=[module.params["name"]],
348 realm=RealmPatch(
349 qos=ContainerQos(
350 iops_limit=int(human_to_real(module.params["iops_qos"]))
351 )
352 ),
353 )
354 if res.status_code != 200:
355 module.fail_json(
356 msg="Realm {0} IOPs QoS change failed. Error: {1}".format(
357 module.params["name"], res.errors[0].message
358 )
359 )
360 else:
361 module.fail_json(
362 msg="Bandwidth QoS value {0} out of range.".format(
363 module.params["bw_qos"]
364 )
365 )
366 if module.params["quota"]:
367 if int(human_to_bytes(module.params["quota"])) != current_realm["quota"]:
368 if int(human_to_bytes(module.params["quota"])) % 512 == 0:
369 changed = True
370 if not module.check_mode:
371 res = array.patch_realms(
372 names=[module.params["name"]],
373 realm=RealmPatch(
374 quota_limit=int(human_to_bytes(module.params["quota"]))
375 ),
376 )
377 if res.status_code != 200:
378 module.fail_json(
379 msg="Realm {0} quota change failed. Error: {1}".format(
380 module.params["name"], res.errors[0].message
381 )
382 )
383 else:
384 module.fail_json(
385 msg="Realm value {0} not a multiple of 512.".format(
386 module.params["bw_qos"]
387 )
388 )
389
390 module.exit_json(changed=changed)
391
392
393 def recover_realm(module, array):
394 """Recover Realm"""
395 changed = True
396 if not module.check_mode:
397 res = array.patch_realms(
398 names=[module.params["name"]], realm=RealmPatch(destroyed=False)
399 )
400 if res.status_code != 200:
401 module.fail_json(
402 msg="Recovery of realm {0} failed. Error: {1}".format(
403 module.params["name"], res.errors[0].message
404 )
405 )
406
407 module.exit_json(changed=changed)
408
409
410 def eradicate_realm(module, array):
411 """Eradicate Realm"""
412 changed = True
413 if not module.check_mode:
414 res = array.delete_realms(
415 names=[module.params["name"]],
416 eradicate_contents=module.params["delete_contents"],
417 )
418 if res.status_code != 200:
419 module.fail_json(
420 msg="Eradicating realm {0} failed.Error: {1}".format(
421 module.params["name"], res.errors[0].message
422 )
423 )
424 module.exit_json(changed=changed)
425
426
427 def delete_realm(module, array):
428 """Delete Realm"""
429 changed = True
430 if not module.check_mode:
431 res = array.patch_realms(
432 names=[module.params["name"]],
433 destroy_contents=module.params["delete_contents"],
434 ignore_usage=module.params["ignore_usage"],
435 realm=RealmPatch(destroyed=True),
436 )
437 if res.status_code != 200:
438 module.fail_json(
439 msg="Deleting realm {0} failed. Error: {1}".format(
440 module.params["name"], res.errors[0].message
441 )
442 )
443 if module.params["eradicate"]:
444 eradicate_realm(module, array)
445
446 module.exit_json(changed=changed)
447
448
449 def main():
450 argument_spec = purefa_argument_spec()
451 argument_spec.update(
452 dict(
453 name=dict(type="str", required=True),
454 state=dict(type="str", default="present", choices=["absent", "present"]),
455 bw_qos=dict(type="str"),
456 iops_qos=dict(type="str"),
457 quota=dict(type="str"),
458 eradicate=dict(type="bool", default=False),
459 rename=dict(type="str"),
460 delete_contents=dict(type="bool", default=False),
461 ignore_usage=dict(type="bool", default=False),
462 )
463 )
464
465 module = AnsibleModule(argument_spec, supports_check_mode=True)
466
467 state = module.params["state"]
468 array = get_array(module)
469 api_version = array.get_rest_version()
470 if LooseVersion(MINIMUM_API_VERSION) > LooseVersion(api_version):
471 module.fail_json(
472 msg="Realms are not supported. Purity//FA 6.6.11, or higher, is required."
473 )
474 realm = get_realm(module, array)
475 xrealm = get_pending_realm(module, array)
476
477 if xrealm and state == "present":
478 recover_realm(module, array)
479 elif realm and state == "absent":
480 delete_realm(module, array)
481 elif xrealm and state == "absent" and module.params["eradicate"]:
482 eradicate_realm(module, array)
483 elif not realm and not xrealm and state == "present":
484 make_realm(module, array)
485 elif state == "present" and realm and module.params["rename"] and not xrealm:
486 rename_realm(module, array)
487 elif realm and state == "present":
488 update_realm(module, array)
489 elif realm is None and state == "absent":
490 module.exit_json(changed=False)
491
492 module.exit_json(changed=False)
493
494
495 if __name__ == "__main__":
496 main()