Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2019, 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_offload
20 version_added: '1.0.0'
21 short_description: Create, modify and delete NFS, S3 or Azure offload targets
22 description:
23 - Create, modify and delete NFS, S3, Azure or GCP offload targets.
24 - You must have a correctly configured offload app installed and a correctly configured offload network for offload to work.
25 author:
26 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 state:
29 description:
30 - Define state of offload
31 default: present
32 choices: [ absent, present ]
33 type: str
34 name:
35 description:
36 - The name of the offload target
37 required: true
38 type: str
39 protocol:
40 description:
41 - Define which protocol the offload engine uses
42 - NFS is not a supported protocol from Purity//FA 6.6.0 and higher
43 default: nfs
44 choices: [ nfs, s3, azure, gcp ]
45 type: str
46 address:
47 description:
48 - The IP or FQDN address of the NFS server
49 type: str
50 share:
51 description:
52 - NFS export on the NFS server
53 type: str
54 options:
55 description:
56 - Additional mount options for the NFS share
57 - Supported mount options include I(port), I(rsize),
58 I(wsize), I(nfsvers), and I(tcp) or I(udp)
59 required: false
60 default: ""
61 type: str
62 access_key:
63 description:
64 - Access Key ID of the offload target
65 type: str
66 container:
67 description:
68 - Name of the blob container of the Azure target
69 default: offload
70 type: str
71 bucket:
72 description:
73 - Name of the bucket for the S3 or GCP target
74 type: str
75 account:
76 description:
77 - Name of the Azure blob storage account
78 type: str
79 secret:
80 description:
81 - Secret Access Key for the offload target
82 type: str
83 initialize:
84 description:
85 - Define whether to initialize the offload bucket
86 type: bool
87 default: true
88 placement:
89 description:
90 - AWS S3 placement strategy
91 type: str
92 choices: ['retention-based', 'aws-standard-class', 'aws-intelligent-tiering']
93 default: retention-based
94 profile:
95 description:
96 - The Offload target profile that will be selected for this target.
97 - This option allows more granular configuration for the target on top
98 of the protocol parameter
99 type: str
100 version_added: '1.21.0'
101 choices: ['azure', 'gcp', 'nfs', 'nfs-flashblade', 's3-aws', 's3-flashblade', 's3-scality-ring', 's3-wasabi-pay-as-you-go', 's3-wasabi-rcs', 's3-other']
102 uri:
103 description:
104 - The URI used to create a connection between the array and a non-AWS S3 offload target.
105 - Storage placement strategies are not supported for non-AWS S3 offload targets.
106 - Both the HTTP and HTTPS protocols are allowed.
107 type: str
108 version_added: '1.32.0'
109 auth_region:
110 description:
111 - The region that will be used for initial authentication request.
112 - This parameter is optional and should be used only when region autodetection fails.
113 type: str
114 version_added: '1.32.0'
115 context:
116 description:
117 - Name of fleet member on which to perform the operation.
118 - This requires the array receiving the request is a member of a fleet
119 and the context name to be a member of the same fleet.
120 type: str
121 default: ""
122 version_added: '1.39.0'
123 extends_documentation_fragment:
124 - purestorage.flasharray.purestorage.fa
125 """
126
127 EXAMPLES = r"""
128 - name: Create NFS offload target
129 purestorage.flasharray.purefa_offload:
130 name: nfs-offload
131 protocol: nfs
132 address: 10.21.200.4
133 share: "/offload_target"
134 fa_url: 10.10.10.2
135 api_token: e31060a7-21fc-e277-6240-25983c6c4592
136
137 - name: Create S3 offload target
138 purestorage.flasharray.purefa_offload:
139 name: s3-offload
140 protocol: s3
141 access_key: "3794fb12c6204e19195f"
142 bucket: offload-bucket
143 secret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
144 placement: aws-standard-class
145 fa_url: 10.10.10.2
146 api_token: e31060a7-21fc-e277-6240-25983c6c4592
147
148 - name: Create Azure offload target
149 purestorage.flasharray.purefa_offload:
150 name: azure-offload
151 protocol: azure
152 secret: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
153 container: offload-container
154 account: user1
155 fa_url: 10.10.10.2
156 api_token: e31060a7-21fc-e277-6240-25983c6c4592
157
158 - name: Delete offload target
159 purestorage.flasharray.purefa_offload:
160 name: nfs-offload
161 protocol: nfs
162 state: absent
163 fa_url: 10.10.10.2
164 api_token: e31060a7-21fc-e277-6240-25983c6c4592
165 """
166
167 RETURN = r"""
168 """
169
170
171 HAS_PURESTORAGE = True
172 try:
173 from pypureclient.flasharray import (
174 OffloadAzure,
175 OffloadGoogleCloud,
176 OffloadNfs,
177 OffloadPost,
178 OffloadS3,
179 )
180 except ImportError:
181 HAS_PURESTORAGE = False
182
183 import re
184
185 from ansible.module_utils.basic import AnsibleModule
186 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
187 get_array,
188 purefa_argument_spec,
189 )
190 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
191 LooseVersion,
192 )
193
194 REGEX_TARGET_NAME = re.compile(r"^[a-zA-Z0-9\-]*$")
195 MULTIOFFLOAD_LIMIT = 1
196 PROFILE_API_VERSION = "2.25"
197 NO_SNAP2NFS_VERSION = "2.27"
198 CONTEXT_VERSION = "2.38"
199
200
201 def get_target(module, array):
202 """Return target or None"""
203 api_version = array.get_rest_version()
204 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
205 res = array.get_offloads(
206 names=[module.params["name"]], context_names=[module.params["context"]]
207 )
208 else:
209 res = array.get_offloads(names=[module.params["name"]])
210 if res.status_code == 200:
211 return list(res.items)[0]
212 return None
213
214
215 def create_offload(module, array):
216 """Create offload target"""
217 changed = True
218 api_version = array.get_rest_version()
219 if not module.check_mode:
220 if module.params["protocol"] == "gcp":
221 if PROFILE_API_VERSION in api_version and module.params["profile"]:
222 bucket = OffloadGoogleCloud(
223 access_key_id=module.params["access_key"],
224 bucket=module.params["bucket"],
225 secret_access_key=module.params["secret"],
226 profile=module.params["profile"],
227 )
228 else:
229 bucket = OffloadGoogleCloud(
230 access_key_id=module.params["access_key"],
231 bucket=module.params["bucket"],
232 secret_access_key=module.params["secret"],
233 )
234 offload = OffloadPost(google_cloud=bucket)
235 if module.params["protocol"] == "azure":
236 if PROFILE_API_VERSION in api_version and module.params["profile"]:
237 bucket = OffloadAzure(
238 container_name=module.params["container"],
239 secret_access_key=module.params["secret"],
240 account_name=module.params[".bucket"],
241 profile=module.params["profile"],
242 )
243 else:
244 bucket = OffloadAzure(
245 container_name=module.params["container"],
246 secret_access_key=module.params["secret"],
247 account_name=module.params["bucket"],
248 )
249 offload = OffloadPost(azure=bucket)
250 if module.params["protocol"] == "s3":
251 if PROFILE_API_VERSION in api_version and module.params["profile"]:
252 if module.params["auth_region"]:
253 bucket = OffloadS3(
254 access_key_id=module.params["access_key"],
255 bucket=module.params["bucket"],
256 secret_access_key=module.params["secret"],
257 profile=module.params["profile"],
258 uri=module.params["uri"],
259 auth_region=module.params["auth_region"],
260 )
261 else:
262 bucket = OffloadS3(
263 access_key_id=module.params["access_key"],
264 bucket=module.params["bucket"],
265 secret_access_key=module.params["secret"],
266 profile=module.params["profile"],
267 uri=module.params["uri"],
268 )
269 else:
270 bucket = OffloadS3(
271 access_key_id=module.params["access_key"],
272 bucket=module.params["bucket"],
273 secret_access_key=module.params["secret"],
274 uri=module.params["uri"],
275 )
276 offload = OffloadPost(s3=bucket)
277 if module.params["protocol"] == "nfs":
278 if PROFILE_API_VERSION in api_version and module.params["profile"]:
279 bucket = OffloadNfs(
280 mount_point=module.params["share"],
281 address=module.params["address"],
282 mount_options=module.params["options"],
283 profile=module.params["profile"],
284 )
285 else:
286 bucket = OffloadNfs(
287 mount_point=module.params["share"],
288 address=module.params["address"],
289 mount_options=module.params["options"],
290 )
291 offload = OffloadPost(nfs=bucket)
292 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
293 res = array.post_offloads(
294 offload=offload,
295 initialize=module.params["initialize"],
296 names=[module.params["name"]],
297 context_names=[module.params["context"]],
298 )
299 else:
300 res = array.post_offloads(
301 offload=offload,
302 initialize=module.params["initialize"],
303 names=[module.params["name"]],
304 )
305 if res.status_code != 200:
306 module.fail_json(
307 msg="Failed to create {0} offload {1}. Error: {2}"
308 "Please perform diagnostic checks.".format(
309 module.params["protocol"].upper(),
310 module.params["name"],
311 res.errors[0].message,
312 )
313 )
314 module.exit_json(changed=changed)
315
316
317 def update_offload(module, array):
318 """Update offload target"""
319 changed = False
320 module.exit_json(changed=changed)
321
322
323 def delete_offload(module, array):
324 """Delete offload target"""
325 changed = True
326 api_version = array.get_rest_version()
327 if not module.check_mode:
328 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
329 res = array.delete_offloads(
330 names=[module.params["name"]], context_names=[module.params["context"]]
331 )
332 else:
333 res = array.delete_offloads(names=[module.params["name"]])
334 if res.status_code != 200:
335 module.fail_json(
336 msg="Failed to delete offload {0}. Error: {1}".format(
337 module.params["name"], res.errors[0].message
338 )
339 )
340 module.exit_json(changed=changed)
341
342
343 def main():
344 argument_spec = purefa_argument_spec()
345 argument_spec.update(
346 dict(
347 state=dict(type="str", default="present", choices=["present", "absent"]),
348 protocol=dict(
349 type="str", default="nfs", choices=["nfs", "s3", "azure", "gcp"]
350 ),
351 placement=dict(
352 type="str",
353 default="retention-based",
354 choices=[
355 "retention-based",
356 "aws-standard-class",
357 "aws-intelligent-tiering",
358 ],
359 ),
360 profile=dict(
361 type="str",
362 choices=[
363 "azure",
364 "gcp",
365 "nfs",
366 "nfs-flashblade",
367 "s3-aws",
368 "s3-flashblade",
369 "s3-scality-ring",
370 "s3-wasabi-pay-as-you-go",
371 "s3-wasabi-rcs",
372 "s3-other",
373 ],
374 ),
375 name=dict(type="str", required=True),
376 initialize=dict(default=True, type="bool"),
377 access_key=dict(type="str", no_log=False),
378 secret=dict(type="str", no_log=True),
379 bucket=dict(type="str"),
380 container=dict(type="str", default="offload"),
381 account=dict(type="str"),
382 share=dict(type="str"),
383 address=dict(type="str"),
384 options=dict(type="str", default=""),
385 uri=dict(type="str"),
386 auth_region=dict(type="str"),
387 context=dict(type="str", default=""),
388 )
389 )
390
391 required_if = []
392
393 if argument_spec["state"] == "present":
394 required_if = [
395 ("protocol", "nfs", ["address", "share"]),
396 ("protocol", "s3", ["access_key", "secret", "bucket"]),
397 ["protocol", "gcp", ["access_key", "secret", "bucket"]],
398 ("protocol", "azure", ["account", "secret"]),
399 ]
400
401 module = AnsibleModule(
402 argument_spec, required_if=required_if, supports_check_mode=True
403 )
404
405 if not HAS_PURESTORAGE:
406 module.fail_json(msg="py-pure-client sdk is required for this module")
407
408 array = get_array(module)
409 api_version = array.get_rest_version()
410
411 if (
412 LooseVersion(NO_SNAP2NFS_VERSION) <= LooseVersion(api_version)
413 and module.params["protocol"] == "nfs"
414 ):
415 module.fail_json(
416 msg="NFS offload target is not supported from Purity//FA 6.6.0 and higher"
417 )
418 if (
419 (
420 module.params["protocol"].lower() == "azure"
421 and module.params["profile"] != "azure"
422 )
423 or (
424 module.params["protocol"].lower() == "gcp"
425 and module.params["profile"] != "gcp"
426 )
427 or (
428 module.params["protocol"].lower() == "nfs"
429 and module.params["profile"] not in ["nfs", "nfs-flashblade"]
430 )
431 or (
432 module.params["protocol"].lower() == "s3"
433 and module.params["profile"]
434 not in [
435 "s3-aws",
436 "s3-flashblade",
437 "s3-scality-ring",
438 "s3-wasabi-pay-as-you-go",
439 "s3-wasabi-rcs",
440 "s3-other",
441 ]
442 )
443 ):
444 module.warn("Specified profile not valid, ignoring...")
445 module.params["profile"] = None
446
447 if (
448 not re.match(r"^[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9]$", module.params["name"])
449 or len(module.params["name"]) > 56
450 ):
451 module.fail_json(
452 msg="Target name invalid. "
453 "Target name must be between 1 and 56 characters (alphanumeric and -) in length "
454 "and begin and end with a letter or number. The name must include at least one letter."
455 )
456 if module.params["protocol"] in ["s3", "gcp"]:
457 if (
458 not re.match(r"^[a-z0-9][a-z0-9.\-]*[a-z0-9]$", module.params["bucket"])
459 or len(module.params["bucket"]) > 63
460 ):
461 module.fail_json(
462 msg="Bucket name invalid. "
463 "Bucket name must be between 3 and 63 characters "
464 "(lowercase, alphanumeric, dash or period) in length "
465 "and begin and end with a letter or number."
466 )
467
468 target = get_target(module, array)
469 if module.params["state"] == "present" and not target:
470 offloads = list(array.get_offloads().items)
471 if len(offloads) >= MULTIOFFLOAD_LIMIT:
472 module.fail_json(
473 msg="Cannot add offload target {0}. "
474 "Offload Target Limit of {1} would be exceeded.".format(
475 module.params["name"], MULTIOFFLOAD_LIMIT
476 )
477 )
478 if offloads and offloads[0].protocol != module.params["protocol"]:
479 module.fail_json(msg="Currently all offloads must be of the same type.")
480 create_offload(module, array)
481 elif module.params["state"] == "present" and target:
482 update_offload(module, array)
483 elif module.params["state"] == "absent" and target:
484 delete_offload(module, array)
485
486 module.exit_json(changed=False)
487
488
489 if __name__ == "__main__":
490 main()