| 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_endpoint |
| 20 |
short_description: Manage VMware protocol-endpoints on Pure Storage FlashArrays |
| 21 |
version_added: '1.0.0' |
| 22 |
description: |
| 23 |
- Create, delete or eradicate the an endpoint on a Pure Storage FlashArray. |
| 24 |
author: |
| 25 |
- Pure Storage Ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the endpoint. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
state: |
| 33 |
description: |
| 34 |
- Define whether the endpoint should exist or not. |
| 35 |
default: present |
| 36 |
choices: [ absent, present ] |
| 37 |
type: str |
| 38 |
eradicate: |
| 39 |
description: |
| 40 |
- Define whether to eradicate the endpoint on delete or leave in trash. |
| 41 |
type: bool |
| 42 |
default: false |
| 43 |
rename: |
| 44 |
description: |
| 45 |
- Value to rename the specified endpoint to. |
| 46 |
- Rename only applies to the container the current endpoint is in. |
| 47 |
type: str |
| 48 |
host: |
| 49 |
description: |
| 50 |
- name of host to attach endpoint to |
| 51 |
type: str |
| 52 |
hgroup: |
| 53 |
description: |
| 54 |
- name of hostgroup to attach endpoint to |
| 55 |
type: str |
| 56 |
context: |
| 57 |
description: |
| 58 |
- Name of fleet member on which to perform the volume operation. |
| 59 |
- This requires the array receiving the request is a member of a fleet |
| 60 |
and the context name to be a member of the same fleet. |
| 61 |
type: str |
| 62 |
default: "" |
| 63 |
version_added: '1.39.0' |
| 64 |
container_version: |
| 65 |
description: |
| 66 |
- Defines vCenter and EXSi host compatibility of the protocol endpoint |
| 67 |
and its associated container. |
| 68 |
type: int |
| 69 |
choices: [1, 2, 3] |
| 70 |
default: 1 |
| 71 |
version_added: '1.39.0' |
| 72 |
extends_documentation_fragment: |
| 73 |
- purestorage.flasharray.purestorage.fa |
| 74 |
""" |
| 75 |
|
| 76 |
EXAMPLES = r""" |
| 77 |
- name: Create new endpoint named foo |
| 78 |
purestorage.flasharray.purefa_endpoint: |
| 79 |
name: test-endpoint |
| 80 |
fa_url: 10.10.10.2 |
| 81 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 82 |
state: present |
| 83 |
|
| 84 |
- name: Delete and eradicate endpoint named foo |
| 85 |
purestorage.flasharray.purefa_endpoint: |
| 86 |
name: foo |
| 87 |
eradicate: true |
| 88 |
fa_url: 10.10.10.2 |
| 89 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 90 |
state: absent |
| 91 |
|
| 92 |
- name: Rename endpoint foo to bar |
| 93 |
purestorage.flasharray.purefa_endpoint: |
| 94 |
name: foo |
| 95 |
rename: bar |
| 96 |
fa_url: 10.10.10.2 |
| 97 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 98 |
""" |
| 99 |
|
| 100 |
RETURN = r""" |
| 101 |
volume: |
| 102 |
description: A dictionary describing the changed volume. Only some |
| 103 |
attributes below will be returned with various actions. |
| 104 |
type: dict |
| 105 |
returned: success |
| 106 |
contains: |
| 107 |
source: |
| 108 |
description: Volume name of source volume used for volume copy |
| 109 |
type: str |
| 110 |
serial: |
| 111 |
description: Volume serial number |
| 112 |
type: str |
| 113 |
sample: '361019ECACE43D83000120A4' |
| 114 |
created: |
| 115 |
description: Volume creation time |
| 116 |
type: str |
| 117 |
sample: '2019-03-13T22:49:24Z' |
| 118 |
name: |
| 119 |
description: Volume name |
| 120 |
type: str |
| 121 |
""" |
| 122 |
|
| 123 |
HAS_PURESTORAGE = True |
| 124 |
try: |
| 125 |
from pypureclient.flasharray import ( |
| 126 |
VolumePost, |
| 127 |
VolumePatch, |
| 128 |
ProtocolEndpoint, |
| 129 |
) |
| 130 |
except ImportError: |
| 131 |
HAS_PURESTORAGE = False |
| 132 |
|
| 133 |
import time |
| 134 |
from ansible.module_utils.basic import AnsibleModule |
| 135 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 136 |
get_array, |
| 137 |
purefa_argument_spec, |
| 138 |
) |
| 139 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 140 |
LooseVersion, |
| 141 |
) |
| 142 |
|
| 143 |
CONTEXT_VERSION = "2.38" |
| 144 |
|
| 145 |
|
| 146 |
def _volfact(module, array, volume_name): |
| 147 |
api_version = array.get_rest_version() |
| 148 |
if not module.check_mode: |
| 149 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 150 |
volume_data = list( |
| 151 |
array.get_volumes( |
| 152 |
names=[volume_name], context_names=[module.params["context"]] |
| 153 |
).items |
| 154 |
)[0] |
| 155 |
else: |
| 156 |
volume_data = list(array.get_volumes(names=[volume_name]).items)[0] |
| 157 |
volfact = { |
| 158 |
"name": volume_data.name, |
| 159 |
"source": getattr(volume_data.source, "name", None), |
| 160 |
"serial": volume_data.serial, |
| 161 |
"created": time.strftime( |
| 162 |
"%Y-%m-%d %H:%M:%S", time.localtime(volume_data.created / 1000) |
| 163 |
), |
| 164 |
"destroyed": volume_data.destroyed, |
| 165 |
} |
| 166 |
else: |
| 167 |
volfact = {} |
| 168 |
return volfact |
| 169 |
|
| 170 |
|
| 171 |
def get_volume(module, volume, array): |
| 172 |
"""Return Volume or None""" |
| 173 |
api_version = array.get_rest_version() |
| 174 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 175 |
res = array.get_volumes( |
| 176 |
names=[volume], context_names=[module.params["context"]] |
| 177 |
) |
| 178 |
else: |
| 179 |
res = array.get_volumes(names=[volume]) |
| 180 |
if res.status_code != 200: |
| 181 |
return None |
| 182 |
return list(res.items)[0] |
| 183 |
|
| 184 |
|
| 185 |
def create_endpoint(module, array): |
| 186 |
"""Create Endpoint""" |
| 187 |
changed = True |
| 188 |
api_version = array.get_rest_version() |
| 189 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 190 |
vg_exists = bool( |
| 191 |
array.get_volume_groups( |
| 192 |
context_names=[module.params["context"]], |
| 193 |
names=[module.params["name"].split("/")[0]], |
| 194 |
).status_code |
| 195 |
!= 200 |
| 196 |
) |
| 197 |
else: |
| 198 |
vg_exists = bool( |
| 199 |
array.get_volume_groups( |
| 200 |
names=[module.params["name"].split("/")[0]] |
| 201 |
).status_code |
| 202 |
!= 200 |
| 203 |
) |
| 204 |
if "/" in module.params["name"] and not vg_exists: |
| 205 |
module.fail_json( |
| 206 |
msg="Failed to create endpoint {0}. Volume Group does not exist.".format( |
| 207 |
module.params["name"] |
| 208 |
) |
| 209 |
) |
| 210 |
if not module.check_mode: |
| 211 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 212 |
res = array.post_volumes( |
| 213 |
names=[module.params["name"]], |
| 214 |
volume=VolumePost( |
| 215 |
subtype="protocol_endpoint", |
| 216 |
protocol_endpoint=ProtocolEndpoint( |
| 217 |
container_version=str(module.params["container_version"]) |
| 218 |
), |
| 219 |
), |
| 220 |
context_names=[module.params["context"]], |
| 221 |
) |
| 222 |
else: |
| 223 |
res = array.post_volumes( |
| 224 |
names=[module.params["name"]], |
| 225 |
volume=VolumePost( |
| 226 |
subtype="protocol_endpoint", |
| 227 |
protocol_endpoint=ProtocolEndpoint( |
| 228 |
container_version=str(module.params["container_version"]) |
| 229 |
), |
| 230 |
), |
| 231 |
) |
| 232 |
if res.status_code != 200: |
| 233 |
module.fail_json( |
| 234 |
msg="Endpoint {0} creation failed. Error: {1}".format( |
| 235 |
module.params["name"], res.errors[0].message |
| 236 |
) |
| 237 |
) |
| 238 |
if module.params["host"]: |
| 239 |
if not module.check_mode: |
| 240 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 241 |
res = array.post_connections( |
| 242 |
host_names=[module.params["host"]], |
| 243 |
volume_names=[module.params["name"]], |
| 244 |
context_names=[module.params["context"]], |
| 245 |
) |
| 246 |
else: |
| 247 |
res = array.post_connections( |
| 248 |
host_names=[module.params["host"]], |
| 249 |
volume_names=[module.params["name"]], |
| 250 |
) |
| 251 |
if res.status_code != 200: |
| 252 |
module.fail_json( |
| 253 |
msg="Failed to attach endpoint {0} to host {1}. Error: {2}".format( |
| 254 |
module.params["name"], |
| 255 |
module.params["host"], |
| 256 |
res.errors[0].message, |
| 257 |
) |
| 258 |
) |
| 259 |
if module.params["hgroup"]: |
| 260 |
if not module.check_mode: |
| 261 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 262 |
res = array.post_connections( |
| 263 |
host_group_names=[module.params["hgroup"]], |
| 264 |
volume_names=[module.params["name"]], |
| 265 |
context_names=[module.params["context"]], |
| 266 |
) |
| 267 |
else: |
| 268 |
res = array.post_connections( |
| 269 |
host_group_names=[module.params["hgroup"]], |
| 270 |
volume_names=[module.params["name"]], |
| 271 |
) |
| 272 |
if res.status_code != 200: |
| 273 |
module.fail_json( |
| 274 |
msg="Failed to attach endpoint {0} to hostgroup {1}. Error: {2}".format( |
| 275 |
module.params["name"], |
| 276 |
module.params["hgroup"], |
| 277 |
res.errors[0].message, |
| 278 |
) |
| 279 |
) |
| 280 |
|
| 281 |
module.exit_json( |
| 282 |
changed=changed, volume=_volfact(module, array, module.params["name"]) |
| 283 |
) |
| 284 |
|
| 285 |
|
| 286 |
def rename_endpoint(module, array): |
| 287 |
"""Rename endpoint within a container, ie vgroup or local array""" |
| 288 |
changed = False |
| 289 |
api_version = array.get_rest_version() |
| 290 |
target_name = module.params["rename"] |
| 291 |
if "/" in module.params["rename"] or "::" in module.params["rename"]: |
| 292 |
module.fail_json(msg="Target endpoint cannot include a container name") |
| 293 |
if "/" in module.params["name"]: |
| 294 |
vgroup_name = module.params["name"].split("/")[0] |
| 295 |
target_name = vgroup_name + "/" + module.params["rename"] |
| 296 |
if get_volume(module, target_name, array): |
| 297 |
module.fail_json(msg="Target {0} already exists.".format(target_name)) |
| 298 |
changed = True |
| 299 |
if not module.check_mode: |
| 300 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 301 |
res = array.patch_volumes( |
| 302 |
names=[module.params["name"]], |
| 303 |
volume=VolumePatch(name=target_name), |
| 304 |
context_names=[module.params["context"]], |
| 305 |
) |
| 306 |
else: |
| 307 |
res = array.patch_volumes( |
| 308 |
names=[module.params["name"]], volume=VolumePatch(name=target_name) |
| 309 |
) |
| 310 |
if res.status_code != 200: |
| 311 |
module.fail_json( |
| 312 |
msg="Rename endpoint {0} to {1} failed. Error: {2}".format( |
| 313 |
module.params["name"], module.params["rename"], res.errors[0].message |
| 314 |
) |
| 315 |
) |
| 316 |
|
| 317 |
module.exit_json(changed=changed, volume=_volfact(module, array, target_name)) |
| 318 |
|
| 319 |
|
| 320 |
def delete_endpoint(module, array): |
| 321 |
"""Delete Endpoint""" |
| 322 |
changed = True |
| 323 |
api_version = array.get_rest_version() |
| 324 |
if not module.check_mode: |
| 325 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 326 |
res = array.patch_volumes( |
| 327 |
names=[module.params["name"]], |
| 328 |
volume=VolumePatch(destroyed=True), |
| 329 |
context_names=[module.params["context"]], |
| 330 |
) |
| 331 |
else: |
| 332 |
res = array.patch_volumes( |
| 333 |
names=[module.params["name"]], volume=VolumePatch(destroyed=True) |
| 334 |
) |
| 335 |
if res.status_code != 200: |
| 336 |
module.fail_json( |
| 337 |
msg="Failed to delete endpoint {0}. Error: {1}".format( |
| 338 |
module.params["name"], res.errors[0].name |
| 339 |
) |
| 340 |
) |
| 341 |
if module.params["eradicate"]: |
| 342 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 343 |
res = array.delete_volumes( |
| 344 |
names=[module.params["name"]], |
| 345 |
context_names=[module.params["context"]], |
| 346 |
) |
| 347 |
else: |
| 348 |
res = array.delete_volumes(names=[module.params["name"]]) |
| 349 |
if res.status_code != 200: |
| 350 |
module.fail_json( |
| 351 |
msg="Eradicate endpoint {0} failed. Error: {1}".format( |
| 352 |
module.params["name"], res.errors[0].message |
| 353 |
) |
| 354 |
) |
| 355 |
module.exit_json(changed=changed, volume=[]) |
| 356 |
module.exit_json( |
| 357 |
changed=changed, volume=_volfact(module, array, module.params["name"]) |
| 358 |
) |
| 359 |
|
| 360 |
|
| 361 |
def recover_endpoint(module, array): |
| 362 |
"""Recover Deleted Endpoint""" |
| 363 |
changed = True |
| 364 |
api_version = array.get_rest_version() |
| 365 |
if not module.check_mode: |
| 366 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 367 |
res = array.patch_volumes( |
| 368 |
names=[module.params["name"]], |
| 369 |
volume=VolumePatch(destroyed=False), |
| 370 |
context_names=[module.params["context"]], |
| 371 |
) |
| 372 |
else: |
| 373 |
res = array.patch_volumes( |
| 374 |
names=[module.params["name"]], volume=VolumePatch(destroyed=False) |
| 375 |
) |
| 376 |
if res.ststus_code != 200: |
| 377 |
module.fail_json( |
| 378 |
msg="Recovery of endpoint {0} failed. Error: {1}".format( |
| 379 |
module.params["name"], res.errors[0].message |
| 380 |
) |
| 381 |
) |
| 382 |
module.exit_json( |
| 383 |
changed=changed, volume=_volfact(module, array, module.params["name"]) |
| 384 |
) |
| 385 |
|
| 386 |
|
| 387 |
def eradicate_endpoint(module, array): |
| 388 |
"""Eradicate Deleted Endpoint""" |
| 389 |
changed = True |
| 390 |
api_version = array.get_rest_version() |
| 391 |
if not module.check_mode: |
| 392 |
if module.params["eradicate"]: |
| 393 |
if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version): |
| 394 |
res = array.delete_volumes( |
| 395 |
names=[module.params["name"]], |
| 396 |
context_names=[module.params["context"]], |
| 397 |
) |
| 398 |
else: |
| 399 |
res = array.delete_volumes(names=[module.params["name"]]) |
| 400 |
if res.status_code != 200: |
| 401 |
module.fail_json( |
| 402 |
msg="Eradication of endpoint {0} failed. Error: {1}".format( |
| 403 |
module.params["name"], res.errors[0].message |
| 404 |
) |
| 405 |
) |
| 406 |
module.exit_json(changed=changed, volume=[]) |
| 407 |
|
| 408 |
|
| 409 |
def main(): |
| 410 |
argument_spec = purefa_argument_spec() |
| 411 |
argument_spec.update( |
| 412 |
dict( |
| 413 |
name=dict(type="str", required=True), |
| 414 |
rename=dict(type="str"), |
| 415 |
host=dict(type="str"), |
| 416 |
hgroup=dict(type="str"), |
| 417 |
eradicate=dict(type="bool", default=False), |
| 418 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 419 |
context=dict(type="str", default=""), |
| 420 |
container_version=dict(type="int", choices=[1, 2, 3], default=1), |
| 421 |
) |
| 422 |
) |
| 423 |
|
| 424 |
mutually_exclusive = [["rename", "eradicate"], ["host", "hgroup"]] |
| 425 |
|
| 426 |
module = AnsibleModule( |
| 427 |
argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True |
| 428 |
) |
| 429 |
|
| 430 |
if not HAS_PURESTORAGE: |
| 431 |
module.fail_json(msg="py-pure-client sdk is required for this module") |
| 432 |
state = module.params["state"] |
| 433 |
array = get_array(module) |
| 434 |
volume = get_volume(module, module.params["name"], array) |
| 435 |
if volume and volume.subtype != "protocol_endpoint": |
| 436 |
module.fail_json( |
| 437 |
msg="Volume {0} is a true volume. Please use the purefa_volume module".format( |
| 438 |
module.params["name"] |
| 439 |
) |
| 440 |
) |
| 441 |
|
| 442 |
if state == "present" and not volume: |
| 443 |
create_endpoint(module, array) |
| 444 |
elif state == "present" and module.params["rename"]: |
| 445 |
rename_endpoint(module, array) |
| 446 |
elif state == "present" and volume and volume.destroyed: |
| 447 |
recover_endpoint(module, array) |
| 448 |
elif state == "absent": |
| 449 |
delete_endpoint(module, array) |
| 450 |
elif state == "absent" and volume and volume.destroyed: |
| 451 |
eradicate_endpoint(module, array) |
| 452 |
else: |
| 453 |
module.exit_json(changed=False) |
| 454 |
|
| 455 |
module.exit_json(changed=False) |
| 456 |
|
| 457 |
|
| 458 |
if __name__ == "__main__": |
| 459 |
main() |