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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2020, Simon Dodsley (simon@purestorage.com)
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = r"""
---
module: purefa_fs
version_added: '1.5.0'
short_description: Manage FlashArray File Systems
description:
- Create/Delete FlashArray File Systems
author:
- Pure Storage Ansible Team (@sdodsley) <pure-ansible-team@purestorage.com>
options:
name:
description:
- Name of the file system
type: str
required: true
state:
description:
- Define whether the file system should exist or not.
default: present
choices: [ absent, present ]
type: str
eradicate:
description:
- Define whether to eradicate the file system on delete or leave in trash.
type: bool
default: false
rename:
description:
- Value to rename the specified file system to
- Rename only applies to the container the current filesystem is in.
- There is no requirement to specify the pod name as this is implied.
type: str
move:
description:
- Move a filesystem in and out of a pod
- Provide the name of pod to move the filesystem to
- Pod names must be unique in the array
- To move to the local array, specify C(local)
- This is not idempotent - use C(ignore_errors) in the play
type: str
version_added: '1.13.0'
extends_documentation_fragment:
- purestorage.flasharray.purestorage.fa
"""
EXAMPLES = r"""
- name: Create file system foo
purestorage.flasharray.purefa_fs:
name: foo
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete and eradicate file system foo
purestorage.flasharray.purefa_fs:
name: foo
eradicate: true
state: absent
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Rename file system foo to bar
purestorage.flasharray.purefa_fs:
name: foo
rename: bar
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
"""
RETURN = r"""
"""
HAS_PURESTORAGE = True
try:
from pypureclient import flasharray
except ImportError:
HAS_PURESTORAGE = False
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
get_array,
purefa_argument_spec,
)
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
LooseVersion,
)
MIN_REQUIRED_API_VERSION = "2.2"
REPL_SUPPORT_API = "2.13"
def delete_fs(module, array):
"""Delete a file system"""
changed = True
if not module.check_mode:
try:
file_system = flasharray.FileSystemPatch(destroyed=True)
array.patch_file_systems(
names=[module.params["name"]], file_system=file_system
)
except Exception:
module.fail_json(
msg="Failed to delete file system {0}".format(module.params["name"])
)
if module.params["eradicate"]:
try:
array.delete_file_systems(names=[module.params["name"]])
except Exception:
module.fail_json(
msg="Eradication of file system {0} failed".format(
module.params["name"]
)
)
module.exit_json(changed=changed)
def recover_fs(module, array):
"""Recover a deleted file system"""
changed = True
if not module.check_mode:
try:
file_system = flasharray.FileSystemPatch(destroyed=False)
array.patch_file_systems(
names=[module.params["name"]], file_system=file_system
)
except Exception:
module.fail_json(
msg="Failed to recover file system {0}".format(module.params["name"])
)
module.exit_json(changed=changed)
def eradicate_fs(module, array):
"""Eradicate a file system"""
changed = True
if not module.check_mode:
try:
array.delete_file_systems(names=[module.params["name"]])
except Exception:
module.fail_json(
msg="Failed to eradicate file system {0}".format(module.params["name"])
)
module.exit_json(changed=changed)
def rename_fs(module, array):
"""Rename a file system"""
changed = False
target_name = module.params["rename"]
if "::" in module.params["name"]:
pod_name = module.params["name"].split("::")[0]
target_name = pod_name + "::" + module.params["rename"]
try:
target = list(array.get_file_systems(names=[target_name]).items)[0]
except Exception:
target = None
if not target:
changed = True
if not module.check_mode:
try:
file_system = flasharray.FileSystemPatch(name=target_name)
array.patch_file_systems(
names=[module.params["name"]], file_system=file_system
)
except Exception:
module.fail_json(
msg="Failed to rename file system {0}".format(module.params["name"])
)
else:
module.fail_json(
msg="Target file system {0} already exists".format(module.params["rename"])
)
module.exit_json(changed=changed)
def create_fs(module, array):
"""Create a file system"""
changed = True
if "::" in module.params["name"]:
pod_name = module.params["name"].split("::")[0]
try:
pod = list(array.get_pods(names=[pod_name]).items)[0]
except Exception:
module.fail_json(
msg="Failed to create filesystem. Pod {0} does not exist".format(
pod_name
)
)
if pod.promotion_status == "demoted":
module.fail_json(msg="Filesystem cannot be created in a demoted pod")
if not module.check_mode:
try:
array.post_file_systems(names=[module.params["name"]])
except Exception:
module.fail_json(
msg="Failed to create file system {0}".format(module.params["name"])
)
module.exit_json(changed=changed)
def move_fs(module, array):
"""Move filesystem between pods or local array"""
changed = False
target_exists = False
pod_name = ""
fs_name = module.params["name"]
if "::" in module.params["name"]:
fs_name = module.params["name"].split("::")[1]
pod_name = module.params["name"].split("::")[0]
if module.params["move"] == "local":
target_location = ""
if "::" not in module.params["name"]:
module.fail_json(msg="Source and destination [local] cannot be the same.")
try:
target_exists = list(array.get_file_systems(names=[fs_name]).items)[0]
except Exception:
target_exists = False
if target_exists:
module.fail_json(msg="Target filesystem {0} already exists".format(fs_name))
else:
try:
pod = list(array.get_pods(names=[module.params["move"]]).items)[0]
if len(pod.arrays) > 1:
module.fail_json(msg="Filesystem cannot be moved into a stretched pod")
if pod.link_target_count != 0:
module.fail_json(
msg="Filesystem cannot be moved into a linked source pod"
)
if pod.promotion_status == "demoted":
module.fail_json(msg="Volume cannot be moved into a demoted pod")
except Exception:
module.fail_json(
msg="Failed to move filesystem. Pod {0} does not exist".format(pod_name)
)
if "::" in module.params["name"]:
pod = list(array.get_pods(names=[module.params["move"]]).items)[0]
if len(pod.arrays) > 1:
module.fail_json(
msg="Filesystem cannot be moved out of a stretched pod"
)
if pod.linked_target_count != 0:
module.fail_json(
msg="Filesystem cannot be moved out of a linked source pod"
)
if pod.promotion_status == "demoted":
module.fail_json(msg="Volume cannot be moved out of a demoted pod")
target_location = module.params["move"]
changed = True
if not module.check_mode:
file_system = flasharray.FileSystemPatch(
pod=flasharray.Reference(name=target_location)
)
move_res = array.patch_file_systems(
names=[module.params["name"]], file_system=file_system
)
if move_res.status_code != 200:
module.fail_json(
msg="Move of filesystem {0} failed. Error: {1}".format(
module.params["name"], move_res.errors[0].message
)
)
module.exit_json(changed=changed)
def main():
argument_spec = purefa_argument_spec()
argument_spec.update(
dict(
state=dict(type="str", default="present", choices=["absent", "present"]),
eradicate=dict(type="bool", default=False),
name=dict(type="str", required=True),
move=dict(type="str"),
rename=dict(type="str"),
)
)
mutually_exclusive = [["move", "rename"]]
module = AnsibleModule(
argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True
)
if not HAS_PURESTORAGE:
module.fail_json(msg="py-pure-client sdk is required for this module")
array = get_array(module)
api_version = array.get_rest_version()
if LooseVersion(MIN_REQUIRED_API_VERSION) > LooseVersion(api_version):
module.fail_json(
msg="FlashArray REST version not supported. "
"Minimum version required: {0}".format(MIN_REQUIRED_API_VERSION)
)
if (
LooseVersion(REPL_SUPPORT_API) > LooseVersion(api_version)
and "::" in module.params["name"]
):
module.fail_json(
msg="Filesystem Replication is only supported in Purity//FA 6.3.0 or higher"
)
state = module.params["state"]
try:
filesystem = list(array.get_file_systems(names=[module.params["name"]]).items)[
0
]
exists = True
except Exception:
exists = False
if state == "present" and not exists and not module.params["move"]:
create_fs(module, array)
elif (
state == "present"
and exists
and module.params["move"]
and not filesystem.destroyed
):
move_fs(module, array)
elif (
state == "present"
and exists
and module.params["rename"]
and not filesystem.destroyed
):
rename_fs(module, array)
elif (
state == "present"
and exists
and filesystem.destroyed
and not module.params["rename"]
and not module.params["move"]
):
recover_fs(module, array)
elif (
state == "present" and exists and filesystem.destroyed and module.params["move"]
):
module.fail_json(
msg="Filesystem {0} exists, but in destroyed state".format(
module.params["name"]
)
)
elif state == "absent" and exists and not filesystem.destroyed:
delete_fs(module, array)
elif (
state == "absent"
and exists
and module.params["eradicate"]
and filesystem.destroyed
):
eradicate_fs(module, array)
module.exit_json(changed=False)
if __name__ == "__main__":
main()
|