Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2021, 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_dirsnap
20 version_added: '1.9.0'
21 short_description: Manage FlashArray File System Directory Snapshots
22 description:
23 - Create/Delete FlashArray File System directory snapshots
24 - A full snapshot name is constructed in the form of DIR.CLIENT_NAME.SUFFIX
25 where DIR is the managed directory name, CLIENT_NAME is the client name,
26 and SUFFIX is the suffix.
27 - The client visible snapshot name is CLIENT_NAME.SUFFIX.
28 author:
29 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
30 options:
31 name:
32 description:
33 - Name of the directory to snapshot
34 type: str
35 required: true
36 state:
37 description:
38 - Define whether the directory snapshot should exist or not.
39 default: present
40 choices: [ absent, present ]
41 type: str
42 filesystem:
43 description:
44 - Name of the filesystem the directory links to.
45 type: str
46 required: true
47 eradicate:
48 description:
49 - Define whether to eradicate the snapshot on delete or leave in trash
50 type: bool
51 default: false
52 client:
53 description:
54 - The client name portion of the client visible snapshot name
55 type: str
56 required: true
57 suffix:
58 description:
59 - Snapshot suffix to use
60 type: str
61 new_client:
62 description:
63 - The new client name when performing a rename
64 type: str
65 version_added: '1.12.0'
66 new_suffix:
67 description:
68 - The new suffix when performing a rename
69 type: str
70 version_added: '1.12.0'
71 rename:
72 description:
73 - Whether to rename a directory snapshot
74 - The snapshot client name and suffix can be changed
75 - Required with I(new_client) and I(new_suffix)
76 type: bool
77 default: false
78 version_added: '1.12.0'
79 keep_for:
80 description:
81 - Retention period, after which snapshots will be eradicated
82 - Specify in seconds. Range 300 - 31536000 (5 minutes to 1 year)
83 - Value of 0 will set no retention period.
84 - If not specified on create will default to 0 (no retention period)
85 type: int
86 context:
87 description:
88 - Name of fleet member on which to perform the operation.
89 - This requires the array receiving the request is a member of a fleet
90 and the context name to be a member of the same fleet.
91 type: str
92 default: ""
93 version_added: '1.39.0'
94 extends_documentation_fragment:
95 - purestorage.flasharray.purestorage.fa
96 """
97
98 EXAMPLES = r"""
99 - name: Create a snapshot directory foo in filesysten bar for client test with suffix test
100 purestorage.flasharray.purefa_dirsnap:
101 name: foo
102 filesystem: bar
103 client: test
104 suffix: test
105 fa_url: 10.10.10.2
106 api_token: e31060a7-21fc-e277-6240-25983c6c4592
107
108 - name: Update retention time for a snapshot foo:bar.client.test
109 purestorage.flasharray.purefa_dirsnap:
110 name: foo
111 filesystem: bar
112 client: client
113 suffix: test
114 keep_for: 300 # 5 minutes
115 fa_url: 10.10.10.2
116 api_token: e31060a7-21fc-e277-6240-25983c6c4592
117
118 - name: Delete snapshot foo:bar.client.test
119 purestorage.flasharray.purefa_dirsnap:
120 name: foo
121 filesystem: bar
122 client: client
123 suffix: test
124 state: absent
125 fa_url: 10.10.10.2
126 api_token: e31060a7-21fc-e277-6240-25983c6c4592
127
128 - name: Recover deleted snapshot foo:bar.client.test
129 purestorage.flasharray.purefa_dirsnap:
130 name: foo
131 filesystem: bar
132 client: client
133 suffix: test
134 fa_url: 10.10.10.2
135 api_token: e31060a7-21fc-e277-6240-25983c6c4592
136
137 - name: Delete and eradicate snapshot foo:bar.client.test
138 purestorage.flasharray.purefa_dirsnap:
139 name: foo
140 filesystem: bar
141 client: client
142 suffix: test
143 state: absent
144 eradicate: true
145 fa_url: 10.10.10.2
146 api_token: e31060a7-21fc-e277-6240-25983c6c4592
147
148 - name: Eradicate deleted snapshot foo:bar.client.test
149 purestorage.flasharray.purefa_dirsnap:
150 name: foo
151 filesystem: bar
152 client: client
153 suffix: test
154 eradicate: true
155 state: absent
156 fa_url: 10.10.10.2
157 api_token: e31060a7-21fc-e277-6240-25983c6c4592
158
159 - name: Rename snapshot
160 purestorage.flasharray.purefa_dirsnap:
161 name: foo
162 filesystem: bar
163 client: client
164 suffix: test
165 rename: true
166 new_client: client2
167 new_suffix: test2
168 fa_url: 10.10.10.2
169 api_token: e31060a7-21fc-e277-6240-25983c6c4592
170 """
171
172 RETURN = r"""
173 """
174
175 HAS_PURESTORAGE = True
176 try:
177 from pypureclient.flasharray import DirectorySnapshotPost, DirectorySnapshotPatch
178 except ImportError:
179 HAS_PURESTORAGE = False
180
181 import re
182 from ansible.module_utils.basic import AnsibleModule
183 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
184 get_array,
185 purefa_argument_spec,
186 )
187 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
188 LooseVersion,
189 )
190
191 MIN_REQUIRED_API_VERSION = "2.2"
192 MIN_RENAME_API_VERSION = "2.10"
193 CONTEXT_VERSION = "2.42"
194
195
196 def eradicate_snap(module, array):
197 """Eradicate a filesystem snapshot"""
198 api_version = array.get_rest_version()
199 changed = True
200 if not module.check_mode:
201 snapname = (
202 module.params["filesystem"]
203 + ":"
204 + module.params["name"]
205 + "."
206 + module.params["client"]
207 + "."
208 + module.params["suffix"]
209 )
210 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
211 res = array.delete_directory_snapshots(
212 names=[snapname], context_names=[module.params["context"]]
213 )
214 else:
215 res = array.delete_directory_snapshots(names=[snapname])
216 if res.status_code != 200:
217 module.fail_json(
218 msg="Failed to eradicate filesystem snapshot {0}. Error: {1}".format(
219 snapname, res.errors[0].message
220 )
221 )
222 module.exit_json(changed=changed)
223
224
225 def delete_snap(module, array):
226 """Delete a filesystem snapshot"""
227 api_version = array.get_rest_version()
228 changed = True
229 if not module.check_mode:
230 snapname = (
231 module.params["filesystem"]
232 + ":"
233 + module.params["name"]
234 + "."
235 + module.params["client"]
236 + "."
237 + module.params["suffix"]
238 )
239 directory_snapshot = DirectorySnapshotPatch(destroyed=True)
240 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
241 res = array.patch_directory_snapshots(
242 names=[snapname],
243 directory_snapshot=directory_snapshot,
244 context_names=[module.params["context"]],
245 )
246 else:
247 res = array.patch_directory_snapshots(
248 names=[snapname], directory_snapshot=directory_snapshot
249 )
250 if res.status_code != 200:
251 module.fail_json(
252 msg="Failed to delete filesystem snapshot {0}. Error: {1}".format(
253 snapname, res.errors[0].message
254 )
255 )
256 if module.params["eradicate"]:
257 eradicate_snap(module, array)
258 module.exit_json(changed=changed)
259
260
261 def update_snap(module, array, snap_detail):
262 """Update a filesystem snapshot retention time"""
263 api_version = array.get_rest_version()
264 changed = False
265 snapname = (
266 module.params["filesystem"]
267 + ":"
268 + module.params["name"]
269 + "."
270 + module.params["client"]
271 + "."
272 + module.params["suffix"]
273 )
274 if module.params["rename"]:
275 if not module.params["new_client"]:
276 new_client = module.params["client"]
277 else:
278 new_client = module.params["new_client"]
279 if not module.params["new_suffix"]:
280 new_suffix = module.params["suffix"]
281 else:
282 new_suffix = module.params["new_suffix"]
283 new_snapname = (
284 module.params["filesystem"]
285 + ":"
286 + module.params["name"]
287 + "."
288 + new_client
289 + "."
290 + new_suffix
291 )
292 directory_snapshot = DirectorySnapshotPatch(
293 client_name=new_client, suffix=new_suffix
294 )
295 changed = True
296 if not module.check_mode:
297 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
298 res = array.patch_directory_snapshots(
299 names=[snapname],
300 directory_snapshot=directory_snapshot,
301 context_names=[module.params["context"]],
302 )
303 else:
304 res = array.patch_directory_snapshots(
305 names=[snapname], directory_snapshot=directory_snapshot
306 )
307 if res.status_code != 200:
308 module.fail_json(
309 msg="Failed to rename snapshot {0}. Error: {1}".format(
310 snapname, res.errors[0].message
311 )
312 )
313 else:
314 snapname = new_snapname
315 if not module.params["keep_for"] or module.params["keep_for"] == 0:
316 keep_for = None
317 elif 300 <= module.params["keep_for"] <= 31536000:
318 keep_for = module.params["keep_for"] * 1000
319 else:
320 module.fail_json(msg="keep_for not in range of 300 - 31536000")
321 if snap_detail.destroyed:
322 changed = True
323 if not module.check_mode:
324 directory_snapshot = DirectorySnapshotPatch(destroyed=False)
325 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
326 res = array.patch_directory_snapshots(
327 names=[snapname],
328 directory_snapshot=directory_snapshot,
329 context_names=[module.params["context"]],
330 )
331 else:
332 res = array.patch_directory_snapshots(
333 names=[snapname], directory_snapshot=directory_snapshot
334 )
335 if res.status_code != 200:
336 module.fail_json(
337 msg="Failed to recover snapshot {0}. Error: {1}".format(
338 snapname, res.errors[0].message
339 )
340 )
341 if keep_for != 0: # Set a new keep-for after recovery if requested
342 directory_snapshot = DirectorySnapshotPatch(keep_for=keep_for)
343 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
344 res = array.patch_directory_snapshots(
345 names=[snapname],
346 directory_snapshot=directory_snapshot,
347 context_names=[module.params["context"]],
348 )
349 else:
350 res = array.patch_directory_snapshots(
351 names=[snapname], directory_snapshot=directory_snapshot
352 )
353 if res.status_code != 200:
354 module.fail_json(
355 msg="Failed to retention time for snapshot {0}. Error: {1}".format(
356 snapname, res.errors[0].message
357 )
358 )
359 if keep_for:
360 directory_snapshot = DirectorySnapshotPatch(keep_for=keep_for)
361 changed = True
362 if not module.check_mode:
363 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
364 res = array.patch_directory_snapshots(
365 names=[snapname],
366 directory_snapshot=directory_snapshot,
367 context_names=[module.params["context"]],
368 )
369 else:
370 res = array.patch_directory_snapshots(
371 names=[snapname], directory_snapshot=directory_snapshot
372 )
373 if res.status_code != 200:
374 module.fail_json(
375 msg="Failed to retention time for snapshot {0}. Error: {1}".format(
376 snapname, res.errors[0].message
377 )
378 )
379 if module.params["rename"] and keep_for:
380 directory_snapshot = DirectorySnapshotPatch(keep_for=keep_for)
381 changed = True
382 if not module.check_mode:
383 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
384 res = array.patch_directory_snapshots(
385 names=[new_snapname],
386 directory_snapshot=directory_snapshot,
387 context_names=[module.params["context"]],
388 )
389 else:
390 res = array.patch_directory_snapshots(
391 names=[new_snapname], directory_snapshot=directory_snapshot
392 )
393 if res.status_code != 200:
394 module.fail_json(
395 msg="Failed to retention time for renamed snapshot {0}. Error: {1}".format(
396 snapname, res.errors[0].message
397 )
398 )
399 module.exit_json(changed=changed)
400
401
402 def create_snap(module, array):
403 """Create a filesystem snapshot"""
404 api_version = array.get_rest_version()
405 changed = True
406 if not module.check_mode:
407 if not module.params["keep_for"] or module.params["keep_for"] == 0:
408 keep_for = None
409 elif 300 <= module.params["keep_for"] <= 31536000:
410 keep_for = module.params["keep_for"] * 1000
411 else:
412 module.fail_json(msg="keep_for not in range of 300 - 31536000")
413 directory = module.params["filesystem"] + ":" + module.params["name"]
414 if module.params["suffix"]:
415 directory_snapshot = DirectorySnapshotPost(
416 client_name=module.params["client"],
417 keep_for=keep_for,
418 suffix=module.params["suffix"],
419 )
420 else:
421 directory_snapshot = DirectorySnapshotPost(
422 client_name=module.params["client"], keep_for=keep_for
423 )
424 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
425 res = array.post_directory_snapshots(
426 source_names=[directory],
427 directory_snapshot=directory_snapshot,
428 context_names=[module.params["context"]],
429 )
430 else:
431 res = array.post_directory_snapshots(
432 source_names=[directory], directory_snapshot=directory_snapshot
433 )
434 if res.status_code != 200:
435 module.fail_json(
436 msg="Failed to create client {0} snapshot for {1}. Error: {2}".format(
437 module.params["client"], directory, res.errors[0].message
438 )
439 )
440 module.exit_json(changed=changed)
441
442
443 def main():
444 argument_spec = purefa_argument_spec()
445 argument_spec.update(
446 dict(
447 state=dict(type="str", default="present", choices=["absent", "present"]),
448 filesystem=dict(type="str", required=True),
449 name=dict(type="str", required=True),
450 eradicate=dict(type="bool", default=False),
451 client=dict(type="str", required=True),
452 suffix=dict(type="str"),
453 rename=dict(type="bool", default=False),
454 new_client=dict(type="str"),
455 new_suffix=dict(type="str"),
456 keep_for=dict(type="int"),
457 context=dict(type="str", default=""),
458 )
459 )
460
461 required_if = [["state", "absent", ["suffix"]]]
462
463 module = AnsibleModule(
464 argument_spec, required_if=required_if, supports_check_mode=True
465 )
466
467 if module.params["rename"]:
468 if not module.params["new_client"] and not module.params["new_suffix"]:
469 module.fail_json(msg="Rename requires one of: new_client, new_suffix")
470
471 if not HAS_PURESTORAGE:
472 module.fail_json(msg="py-pure-client sdk is required for this module")
473
474 client_pattern = re.compile(
475 "^(?=.*[a-zA-Z-])[a-zA-Z0-9]([a-zA-Z0-9-]{0,56}[a-zA-Z0-9])?$"
476 )
477 suffix_pattern = re.compile(
478 "^(?=.*[a-zA-Z-])[a-zA-Z0-9]([a-zA-Z0-9-]{0,63}[a-zA-Z0-9])?$"
479 )
480 if module.params["suffix"]:
481 if not suffix_pattern.match(module.params["suffix"]):
482 module.fail_json(
483 msg="Suffix name {0} does not conform to the suffix name rules.".format(
484 module.params["suffix"]
485 )
486 )
487 if module.params["new_suffix"]:
488 if not suffix_pattern.match(module.params["new_suffix"]):
489 module.fail_json(
490 msg="Suffix rename {0} does not conform to the suffix name rules.".format(
491 module.params["new_suffix"]
492 )
493 )
494 if module.params["client"]:
495 if not client_pattern.match(module.params["client"]):
496 module.fail_json(
497 msg="Client name {0} does not conform to the client name rules.".format(
498 module.params["client"]
499 )
500 )
501
502 array = get_array(module)
503 api_version = array.get_rest_version()
504 if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
505 module.fail_json(
506 msg="FlashArray REST version not supported. "
507 "Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
508 )
509 if module.params["rename"] and LooseVersion(MIN_RENAME_API_VERSION) > LooseVersion(
510 api_version
511 ):
512 module.fail_json(
513 msg="Directory snapshot rename not supported. "
514 "Minimum Purity//FA version required: 6.2.1"
515 )
516 state = module.params["state"]
517 snapshot_root = module.params["filesystem"] + ":" + module.params["name"]
518 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
519 res = array.get_directories(
520 filter='name="' + snapshot_root + '"',
521 total_item_count=True,
522 context_names=[module.params["context"]],
523 )
524 else:
525 res = array.get_directories(
526 filter='name="' + snapshot_root + '"', total_item_count=True
527 )
528 if bool(res.total_item_count == 0):
529 module.fail_json(msg="Directory {0} does not exist.".format(snapshot_root))
530 snap_exists = False
531 if module.params["suffix"]:
532 snap_detail = array.get_directory_snapshots(
533 filter="name='"
534 + snapshot_root
535 + "."
536 + module.params["client"]
537 + "."
538 + module.params["suffix"]
539 + "'",
540 total_item_count=True,
541 )
542 if bool(snap_detail.status_code == 200):
543 snap_exists = bool(snap_detail.total_item_count != 0)
544 if snap_exists:
545 snap_facts = list(snap_detail.items)[0]
546 if state == "present" and not snap_exists and not module.params["rename"]:
547 create_snap(module, array)
548 elif state == "present" and snap_exists and module.params["suffix"]:
549 update_snap(module, array, snap_facts)
550 elif state == "absent" and snap_exists and not snap_facts.destroyed:
551 delete_snap(module, array)
552 elif (
553 state == "absent"
554 and snap_exists
555 and snap_facts.destroyed
556 and module.params["eradicate"]
557 ):
558 eradicate_snap(module, array)
559 else:
560 module.exit_json(changed=False)
561
562 module.exit_json(changed=False)
563
564
565 if __name__ == "__main__":
566 main()