| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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_default_protection |
| 20 |
version_added: '1.14.0' |
| 21 |
short_description: Manage SafeMode default protection for a Pure Storage FlashArray |
| 22 |
description: |
| 23 |
- Configure automatic protection group membership for new volumes and copied volumes |
| 24 |
array wide, or at the pod level. |
| 25 |
- Requires a minimum of Purity 6.3.4 |
| 26 |
author: |
| 27 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 28 |
options: |
| 29 |
scope: |
| 30 |
description: |
| 31 |
- The scope of the default protection group |
| 32 |
type: str |
| 33 |
choices: [ array, pod ] |
| 34 |
default: array |
| 35 |
name: |
| 36 |
description: |
| 37 |
- The name of the protection group to assign or remove as default for the scope. |
| 38 |
- If I(scope) is I(pod) only the short-name for the pod protection group is needed. |
| 39 |
See examples |
| 40 |
elements: str |
| 41 |
type: list |
| 42 |
required: true |
| 43 |
pod: |
| 44 |
description: |
| 45 |
- name of the pod to apply the default protection to. |
| 46 |
- Only required for I(scope) is I(pod) |
| 47 |
type: str |
| 48 |
state: |
| 49 |
description: |
| 50 |
- Define whether to add or delete the protection group to the default list |
| 51 |
default: present |
| 52 |
choices: [ absent, present ] |
| 53 |
type: str |
| 54 |
context: |
| 55 |
description: |
| 56 |
- Name of fleet member on which to perform the operation. |
| 57 |
- This requires the array receiving the request is a member of a fleet |
| 58 |
and the context name to be a member of the same fleet. |
| 59 |
type: str |
| 60 |
default: "" |
| 61 |
version_added: '1.37.0' |
| 62 |
extends_documentation_fragment: |
| 63 |
- purestorage.flasharray.purestorage.fa |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = r""" |
| 67 |
- name: Add protection group foo::bar as default for pod foo |
| 68 |
purestorage.flasharray.purefa_default_protection: |
| 69 |
name: bar |
| 70 |
pod: foo |
| 71 |
scope: pod |
| 72 |
fa_url: 10.10.10.2 |
| 73 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 74 |
|
| 75 |
- name: Add protection group foo as default for array |
| 76 |
purestorage.flasharray.purefa_default_protection: |
| 77 |
name: foo |
| 78 |
scope: array |
| 79 |
fa_url: 10.10.10.2 |
| 80 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 81 |
|
| 82 |
- name: Remove protection group foo from array default protection |
| 83 |
purestorage.flasharray.purefa_default_protection: |
| 84 |
name: foo |
| 85 |
scope: array |
| 86 |
fa_url: 10.10.10.2 |
| 87 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 88 |
state: absent |
| 89 |
|
| 90 |
- name: Clear default protection for the array |
| 91 |
purestorage.flasharray.purefa_default_protection: |
| 92 |
name: '' |
| 93 |
scope: array |
| 94 |
fa_url: 10.10.10.2 |
| 95 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 96 |
state: absent |
| 97 |
""" |
| 98 |
|
| 99 |
RETURN = r""" |
| 100 |
""" |
| 101 |
|
| 102 |
HAS_PURESTORAGE = True |
| 103 |
try: |
| 104 |
from pypureclient import flasharray |
| 105 |
except ImportError: |
| 106 |
HAS_PURESTORAGE = False |
| 107 |
|
| 108 |
from ansible.module_utils.basic import AnsibleModule |
| 109 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 110 |
get_array, |
| 111 |
purefa_argument_spec, |
| 112 |
) |
| 113 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 114 |
LooseVersion, |
| 115 |
) |
| 116 |
|
| 117 |
DEFAULT_API_VERSION = "2.16" |
| 118 |
CONTEXT_API_VERSION = "2.38" |
| 119 |
|
| 120 |
|
| 121 |
def _get_pod(module, array): |
| 122 |
"""Return Pod or None""" |
| 123 |
api_version = array.get_rest_version() |
| 124 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 125 |
res = array.get_pods( |
| 126 |
names=[module.params["pod"]], |
| 127 |
context_names=[module.params["context"]], |
| 128 |
) |
| 129 |
else: |
| 130 |
res = array.get_pods(names=[module.params["pod"]]) |
| 131 |
if res.status_code == 200: |
| 132 |
return list(res.items)[0] |
| 133 |
return None |
| 134 |
|
| 135 |
|
| 136 |
def _get_pg(module, array, pod): |
| 137 |
"""Return Protection Group or None""" |
| 138 |
api_version = array.get_rest_version() |
| 139 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 140 |
res = array.get_protection_groups( |
| 141 |
names=[pod], |
| 142 |
context_names=[module.params["context"]], |
| 143 |
) |
| 144 |
else: |
| 145 |
res = array.get_protection_groups(names=[pod]) |
| 146 |
if res.status_code == 200: |
| 147 |
return list(res.items)[0] |
| 148 |
return None |
| 149 |
|
| 150 |
|
| 151 |
def create_default(module, array): |
| 152 |
"""Create Default Protection""" |
| 153 |
api_version = array.get_rest_version() |
| 154 |
changed = True |
| 155 |
pg_list = [] |
| 156 |
if not module.check_mode: |
| 157 |
for pgroup in module.params["name"]: |
| 158 |
if module.params["scope"] == "array": |
| 159 |
pg_list.append( |
| 160 |
flasharray.DefaultProtectionReference( |
| 161 |
name=pgroup, type="protection_group" |
| 162 |
) |
| 163 |
) |
| 164 |
else: |
| 165 |
pg_list.append( |
| 166 |
flasharray.DefaultProtectionReference( |
| 167 |
name=module.params["pod"] + "::" + pgroup, |
| 168 |
type="protection_group", |
| 169 |
) |
| 170 |
) |
| 171 |
if module.params["scope"] == "array": |
| 172 |
protection = flasharray.ContainerDefaultProtection( |
| 173 |
name="", type="", default_protections=pg_list |
| 174 |
) |
| 175 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 176 |
res = array.patch_container_default_protections( |
| 177 |
names=[""], |
| 178 |
container_default_protection=protection, |
| 179 |
context_names=[module.params["context"]], |
| 180 |
) |
| 181 |
else: |
| 182 |
res = array.patch_container_default_protections( |
| 183 |
names=[""], container_default_protection=protection |
| 184 |
) |
| 185 |
else: |
| 186 |
protection = flasharray.ContainerDefaultProtection( |
| 187 |
name=module.params["pod"], type="pod", default_protections=pg_list |
| 188 |
) |
| 189 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 190 |
res = array.patch_container_default_protections( |
| 191 |
names=[module.params["pod"]], |
| 192 |
container_default_protection=protection, |
| 193 |
context_names=[module.params["context"]], |
| 194 |
) |
| 195 |
else: |
| 196 |
res = array.patch_container_default_protections( |
| 197 |
names=[module.params["pod"]], |
| 198 |
container_default_protection=protection, |
| 199 |
) |
| 200 |
if res.status_code != 200: |
| 201 |
module.fail_json( |
| 202 |
msg="Failed to set default protection. Error: {0}".format( |
| 203 |
res.errors[0].message |
| 204 |
) |
| 205 |
) |
| 206 |
|
| 207 |
module.exit_json(changed=changed) |
| 208 |
|
| 209 |
|
| 210 |
def update_default(module, array, current_default): |
| 211 |
"""Update Default Protection""" |
| 212 |
api_version = array.get_rest_version() |
| 213 |
changed = False |
| 214 |
current = [] |
| 215 |
for default in current_default: |
| 216 |
if module.params["scope"] == "array": |
| 217 |
current.append(default.name) |
| 218 |
else: |
| 219 |
current.append(default.name.split(":")[-1]) |
| 220 |
pg_list = [] |
| 221 |
if module.params["state"] == "present": |
| 222 |
if current: |
| 223 |
new_list = sorted(list(set(module.params["name"] + current))) |
| 224 |
else: |
| 225 |
new_list = sorted(list(set(module.params["name"]))) |
| 226 |
elif current: |
| 227 |
new_list = sorted(list(set(current).difference(module.params["name"]))) |
| 228 |
else: |
| 229 |
new_list = [] |
| 230 |
if not new_list: |
| 231 |
delete_default(module, array) |
| 232 |
elif new_list == current: |
| 233 |
changed = False |
| 234 |
else: |
| 235 |
changed = True |
| 236 |
if not module.check_mode: |
| 237 |
for pgroup in new_list: |
| 238 |
if module.params["scope"] == "array": |
| 239 |
pg_list.append( |
| 240 |
flasharray.DefaultProtectionReference( |
| 241 |
name=pgroup, type="protection_group" |
| 242 |
) |
| 243 |
) |
| 244 |
else: |
| 245 |
pg_list.append( |
| 246 |
flasharray.DefaultProtectionReference( |
| 247 |
name=module.params["pod"] + "::" + pgroup, |
| 248 |
type="protection_group", |
| 249 |
) |
| 250 |
) |
| 251 |
if module.params["scope"] == "array": |
| 252 |
protection = flasharray.ContainerDefaultProtection( |
| 253 |
name="", type="", default_protections=pg_list |
| 254 |
) |
| 255 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 256 |
res = array.patch_container_default_protections( |
| 257 |
names=[""], |
| 258 |
container_default_protection=protection, |
| 259 |
context_names=[module.params["context"]], |
| 260 |
) |
| 261 |
else: |
| 262 |
res = array.patch_container_default_protections( |
| 263 |
names=[""], container_default_protection=protection |
| 264 |
) |
| 265 |
else: |
| 266 |
protection = flasharray.ContainerDefaultProtection( |
| 267 |
name=module.params["pod"], |
| 268 |
type="pod", |
| 269 |
default_protections=pg_list, |
| 270 |
) |
| 271 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 272 |
res = array.patch_container_default_protections( |
| 273 |
names=[module.params["pod"]], |
| 274 |
container_default_protection=protection, |
| 275 |
context_names=[module.params["context"]], |
| 276 |
) |
| 277 |
else: |
| 278 |
res = array.patch_container_default_protections( |
| 279 |
names=[module.params["pod"]], |
| 280 |
container_default_protection=protection, |
| 281 |
) |
| 282 |
if res.status_code != 200: |
| 283 |
module.fail_json( |
| 284 |
msg="Failed to update default protection. Error: {0}".format( |
| 285 |
res.errors[0].message |
| 286 |
) |
| 287 |
) |
| 288 |
module.exit_json(changed=changed) |
| 289 |
|
| 290 |
|
| 291 |
def delete_default(module, array): |
| 292 |
"""Delete Default Protection""" |
| 293 |
api_version = array.get_rest_version() |
| 294 |
changed = True |
| 295 |
if not module.check_mode: |
| 296 |
if module.params["scope"] == "array": |
| 297 |
protection = flasharray.ContainerDefaultProtection( |
| 298 |
name="", type="", default_protections=[] |
| 299 |
) |
| 300 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 301 |
res = array.patch_container_default_protections( |
| 302 |
names=[""], |
| 303 |
container_default_protection=protection, |
| 304 |
context_names=[module.params["context"]], |
| 305 |
) |
| 306 |
else: |
| 307 |
res = array.patch_container_default_protections( |
| 308 |
names=[""], container_default_protection=protection |
| 309 |
) |
| 310 |
else: |
| 311 |
protection = flasharray.ContainerDefaultProtection( |
| 312 |
name=module.params["pod"], type="pod", default_protections=[] |
| 313 |
) |
| 314 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 315 |
res = array.patch_container_default_protections( |
| 316 |
names=[module.params["pod"]], |
| 317 |
container_default_protection=[], |
| 318 |
context_names=[module.params["context"]], |
| 319 |
) |
| 320 |
else: |
| 321 |
res = array.patch_container_default_protections( |
| 322 |
names=[module.params["pod"]], container_default_protection=[] |
| 323 |
) |
| 324 |
if res.status_code != 200: |
| 325 |
if res.status_code == 403: |
| 326 |
module.fail_json( |
| 327 |
msg="Removing protection groups in SafeMode from default " |
| 328 |
"protection is not allowed." |
| 329 |
) |
| 330 |
module.fail_json( |
| 331 |
msg="Failed to delete default protection. Error: {0}".format( |
| 332 |
res.errors[0].message |
| 333 |
) |
| 334 |
) |
| 335 |
module.exit_json(changed=changed) |
| 336 |
|
| 337 |
|
| 338 |
def main(): |
| 339 |
argument_spec = purefa_argument_spec() |
| 340 |
argument_spec.update( |
| 341 |
dict( |
| 342 |
name=dict(type="list", elements="str", required=True), |
| 343 |
pod=dict(type="str"), |
| 344 |
scope=dict(type="str", default="array", choices=["array", "pod"]), |
| 345 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 346 |
context=dict(type="str", default=""), |
| 347 |
) |
| 348 |
) |
| 349 |
|
| 350 |
required_if = [["scope", "pod", ["pod"]]] |
| 351 |
module = AnsibleModule( |
| 352 |
argument_spec, required_if=required_if, supports_check_mode=True |
| 353 |
) |
| 354 |
state = module.params["state"] |
| 355 |
if not HAS_PURESTORAGE: |
| 356 |
module.fail_json( |
| 357 |
msg="py-pure-client sdk is required to support 'count' parameter" |
| 358 |
) |
| 359 |
module.params["name"] = sorted(module.params["name"]) |
| 360 |
array = get_array(module) |
| 361 |
api_version = array.get_rest_version() |
| 362 |
if LooseVersion(DEFAULT_API_VERSION) > LooseVersion(api_version): |
| 363 |
module.fail_json( |
| 364 |
msg="Default Protection is not supported. Purity//FA 6.3.4, or higher, is required." |
| 365 |
) |
| 366 |
if module.params["scope"] == "pod": |
| 367 |
if not _get_pod(module, array): |
| 368 |
module.fail_json( |
| 369 |
msg="Invalid pod {0} specified.".format(module.params["pod"]) |
| 370 |
) |
| 371 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 372 |
ret = array.get_container_default_protections( |
| 373 |
names=[module.params["pod"]], |
| 374 |
context_names=[module.params["context"]], |
| 375 |
) |
| 376 |
else: |
| 377 |
ret = array.get_container_default_protections(names=[module.params["pod"]]) |
| 378 |
current_default = list(ret.items)[0].default_protections |
| 379 |
else: |
| 380 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 381 |
ret = array.get_container_default_protections( |
| 382 |
context_names=[module.params["context"]] |
| 383 |
) |
| 384 |
else: |
| 385 |
ret = array.get_container_default_protections() |
| 386 |
current_default = list(ret.items)[0].default_protections |
| 387 |
for pgroup in module.params["name"]: |
| 388 |
if module.params["scope"] == "pod": |
| 389 |
pod_name = module.params["pod"] + "::" + pgroup |
| 390 |
else: |
| 391 |
pod_name = pgroup |
| 392 |
if not _get_pg(module, array, pod_name) and state == "present": |
| 393 |
module.fail_json(msg="Protection Group {0} does not exist".format(pod_name)) |
| 394 |
|
| 395 |
if state == "present" and not current_default: |
| 396 |
create_default(module, array) |
| 397 |
elif state == "absent" and not current_default: |
| 398 |
module.exit_json(changed=False) |
| 399 |
elif state == "present" and current_default: |
| 400 |
update_default(module, array, current_default) |
| 401 |
elif state == "absent" and current_default and module.params["name"] != [""]: |
| 402 |
update_default(module, array, current_default) |
| 403 |
elif state == "absent" and current_default and module.params["name"] == [""]: |
| 404 |
delete_default(module, array) |
| 405 |
|
| 406 |
module.exit_json(changed=False) |
| 407 |
|
| 408 |
|
| 409 |
if __name__ == "__main__": |
| 410 |
main() |