| 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_hg |
| 20 |
version_added: '1.0.0' |
| 21 |
short_description: Manage hostgroups on Pure Storage FlashArrays |
| 22 |
description: |
| 23 |
- Create, delete or modify hostgroups on Pure Storage FlashArrays. |
| 24 |
author: |
| 25 |
- Pure Storage ansible Team (@sdodsley) <[email protected]> |
| 26 |
options: |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the hostgroup. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
aliases: [ hostgroup ] |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Define whether the hostgroup should exist or not. |
| 36 |
type: str |
| 37 |
default: present |
| 38 |
choices: [ absent, present ] |
| 39 |
host: |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
description: |
| 43 |
- List of existing hosts to add to hostgroup. |
| 44 |
- Note that hostnames are case-sensitive however FlashArray hostnames are unique |
| 45 |
and ignore case - you cannot have I(hosta) and I(hostA) |
| 46 |
volume: |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
description: |
| 50 |
- List of existing volumes to add to hostgroup. |
| 51 |
- Note that volumes are case-sensitive however FlashArray volume names are unique |
| 52 |
and ignore case - you cannot have I(volumea) and I(volumeA) |
| 53 |
lun: |
| 54 |
description: |
| 55 |
- LUN ID to assign to volume for hostgroup. Must be unique. |
| 56 |
- Only applicable when only one volume is specified for connection. |
| 57 |
- If not provided the ID will be automatically assigned. |
| 58 |
- Range for LUN ID is 1 to 4095. |
| 59 |
type: int |
| 60 |
rename: |
| 61 |
description: |
| 62 |
- New name of hostgroup |
| 63 |
type: str |
| 64 |
version_added: '1.10.0' |
| 65 |
eradicate: |
| 66 |
description: |
| 67 |
- Whether to eradicate a deleted host group or not |
| 68 |
type: bool |
| 69 |
default: false |
| 70 |
version_added: '1.32.0' |
| 71 |
context: |
| 72 |
description: |
| 73 |
- Name of fleet member on which to perform the operation. |
| 74 |
- This requires the array receiving the request is a member of a fleet |
| 75 |
and the context name to be a member of the same fleet. |
| 76 |
type: str |
| 77 |
default: "" |
| 78 |
version_added: '1.33.0' |
| 79 |
extends_documentation_fragment: |
| 80 |
- purestorage.flasharray.purestorage.fa |
| 81 |
""" |
| 82 |
|
| 83 |
EXAMPLES = r""" |
| 84 |
- name: Create empty hostgroup |
| 85 |
purestorage.flasharray.purefa_hg: |
| 86 |
name: foo |
| 87 |
fa_url: 10.10.10.2 |
| 88 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 89 |
|
| 90 |
- name: Add hosts and volumes to existing or new hostgroup |
| 91 |
purestorage.flasharray.purefa_hg: |
| 92 |
name: foo |
| 93 |
host: |
| 94 |
- host1 |
| 95 |
- host2 |
| 96 |
volume: |
| 97 |
- vol1 |
| 98 |
- vol2 |
| 99 |
fa_url: 10.10.10.2 |
| 100 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 101 |
|
| 102 |
- name: Delete hosts and volumes from hostgroup |
| 103 |
purestorage.flasharray.purefa_hg: |
| 104 |
name: foo |
| 105 |
host: |
| 106 |
- host1 |
| 107 |
- host2 |
| 108 |
volume: |
| 109 |
- vol1 |
| 110 |
- vol2 |
| 111 |
fa_url: 10.10.10.2 |
| 112 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 113 |
state: absent |
| 114 |
|
| 115 |
# This will disconnect all hosts and volumes in the hostgroup |
| 116 |
- name: Delete hostgroup |
| 117 |
purestorage.flasharray.purefa_hg: |
| 118 |
name: foo |
| 119 |
fa_url: 10.10.10.2 |
| 120 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 121 |
state: absent |
| 122 |
|
| 123 |
- name: Rename hostgroup |
| 124 |
purestorage.flasharray.purefa_hg: |
| 125 |
name: foo |
| 126 |
rename: bar |
| 127 |
fa_url: 10.10.10.2 |
| 128 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 129 |
|
| 130 |
- name: Create host group with hosts and volumes |
| 131 |
purestorage.flasharray.purefa_hg: |
| 132 |
name: bar |
| 133 |
host: |
| 134 |
- host1 |
| 135 |
- host2 |
| 136 |
volume: |
| 137 |
- vol1 |
| 138 |
- vol2 |
| 139 |
fa_url: 10.10.10.2 |
| 140 |
api_token: e31060a7-21fc-e277-6240-25983c6c4592 |
| 141 |
""" |
| 142 |
|
| 143 |
RETURN = r""" |
| 144 |
""" |
| 145 |
|
| 146 |
HAS_PURESTORAGE = True |
| 147 |
try: |
| 148 |
from pypureclient.flasharray import ( |
| 149 |
ConnectionPost, |
| 150 |
HostGroupPatch, |
| 151 |
HostPatch, |
| 152 |
ReferenceNoId, |
| 153 |
) |
| 154 |
except ImportError: |
| 155 |
HAS_PURESTORAGE = False |
| 156 |
|
| 157 |
|
| 158 |
from ansible.module_utils.basic import AnsibleModule |
| 159 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import ( |
| 160 |
get_array, |
| 161 |
purefa_argument_spec, |
| 162 |
) |
| 163 |
from ansible_collections.purestorage.flasharray.plugins.module_utils.version import ( |
| 164 |
LooseVersion, |
| 165 |
) |
| 166 |
|
| 167 |
CONTEXT_API_VERSION = "2.38" |
| 168 |
|
| 169 |
|
| 170 |
def rename_exists(module, array): |
| 171 |
"""Determine if rename target already exists""" |
| 172 |
api_version = array.get_rest_version() |
| 173 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 174 |
return bool( |
| 175 |
array.get_host_groups( |
| 176 |
names=[module.params["rename"]], |
| 177 |
context_names=[module.params["context"]], |
| 178 |
).status_code |
| 179 |
== 200 |
| 180 |
) |
| 181 |
return bool( |
| 182 |
array.get_host_groups(names=[module.params["rename"]]).status_code == 200 |
| 183 |
) |
| 184 |
|
| 185 |
|
| 186 |
def get_hostgroup_hosts(module, array): |
| 187 |
api_version = array.get_rest_version() |
| 188 |
hostgroup = None |
| 189 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 190 |
res = array.get_host_groups_hosts( |
| 191 |
group_names=[module.params["name"]], |
| 192 |
context_names=[module.params["context"]], |
| 193 |
) |
| 194 |
else: |
| 195 |
res = array.get_host_groups_hosts(group_names=[module.params["name"]]) |
| 196 |
if res.status_code == 200: |
| 197 |
hostgroup = list(res.items) |
| 198 |
return hostgroup |
| 199 |
|
| 200 |
|
| 201 |
def get_hostgroup(module, array): |
| 202 |
api_version = array.get_rest_version() |
| 203 |
hostgroup = None |
| 204 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 205 |
res = array.get_host_groups( |
| 206 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 207 |
) |
| 208 |
else: |
| 209 |
res = array.get_host_groups(names=[module.params["name"]]) |
| 210 |
if res.status_code == 200: |
| 211 |
hostgroup = list(res.items)[0] |
| 212 |
return hostgroup |
| 213 |
|
| 214 |
|
| 215 |
def make_hostgroup(module, array): |
| 216 |
api_version = array.get_rest_version() |
| 217 |
if module.params["rename"]: |
| 218 |
module.fail_json( |
| 219 |
msg="Hostgroup {0} does not exist - rename failed.".format( |
| 220 |
module.params["name"] |
| 221 |
) |
| 222 |
) |
| 223 |
changed = True |
| 224 |
if not module.check_mode: |
| 225 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 226 |
res = array.post_host_groups( |
| 227 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 228 |
) |
| 229 |
else: |
| 230 |
res = array.post_host_groups(names=[module.params["name"]]) |
| 231 |
if res.status_code != 200: |
| 232 |
module.fail_json( |
| 233 |
msg="Failed to create hostgroup {0}. Error: {1}".format( |
| 234 |
module.params["name"], res.errors[0].message |
| 235 |
) |
| 236 |
) |
| 237 |
if module.params["host"]: |
| 238 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 239 |
res = array.post_host_groups_hosts( |
| 240 |
context_names=[module.params["context"]], |
| 241 |
group_names=[module.params["name"]], |
| 242 |
member_names=module.params["host"], |
| 243 |
) |
| 244 |
else: |
| 245 |
res = array.post_host_groups_hosts( |
| 246 |
group_names=[module.params["name"]], |
| 247 |
member_names=module.params["host"], |
| 248 |
) |
| 249 |
if res.status_code != 200: |
| 250 |
module.fail_json( |
| 251 |
msg="Failed to add host to hostgroup. Error: {0}".format( |
| 252 |
res.errors[0].message |
| 253 |
) |
| 254 |
) |
| 255 |
if module.params["volume"]: |
| 256 |
if len(module.params["volume"]) == 1 and module.params["lun"]: |
| 257 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 258 |
res = array.post_connections( |
| 259 |
host_group_names=[module.params["name"]], |
| 260 |
context_names=[module.params["context"]], |
| 261 |
volume_names=[module.params["volume"][0]], |
| 262 |
connection=ConnectionPost(lun=module.params["lun"]), |
| 263 |
) |
| 264 |
else: |
| 265 |
res = array.post_connections( |
| 266 |
host_group_names=[module.params["name"]], |
| 267 |
volume_names=[module.params["volume"][0]], |
| 268 |
connection=ConnectionPost(lun=module.params["lun"]), |
| 269 |
) |
| 270 |
if res.status_code != 200: |
| 271 |
module.fail_json( |
| 272 |
msg="Failed to add volume {0} with LUN ID {1}. Error: {2}".format( |
| 273 |
module.params["volume"][0], |
| 274 |
module.params["lun"], |
| 275 |
res.errors[0].message, |
| 276 |
) |
| 277 |
) |
| 278 |
else: |
| 279 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 280 |
res = array.post_connections( |
| 281 |
host_group_names=[module.params["name"]], |
| 282 |
context_names=[module.params["context"]], |
| 283 |
volume_names=module.params["volume"], |
| 284 |
) |
| 285 |
else: |
| 286 |
res = array.post_connections( |
| 287 |
host_group_names=[module.params["name"]], |
| 288 |
volume_names=module.params["volume"], |
| 289 |
) |
| 290 |
if res.status_code != 200: |
| 291 |
module.fail_json( |
| 292 |
msg="Failed to add volumes to hostgroup. Error: {0}".format( |
| 293 |
res.errors[0].message |
| 294 |
) |
| 295 |
) |
| 296 |
module.exit_json(changed=changed) |
| 297 |
|
| 298 |
|
| 299 |
def update_hostgroup(module, array): |
| 300 |
api_version = array.get_rest_version() |
| 301 |
changed = False |
| 302 |
renamed = False |
| 303 |
hgroup = get_hostgroup_hosts(module, array) |
| 304 |
current_hostgroup = module.params["name"] |
| 305 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 306 |
volumes = list( |
| 307 |
array.get_connections( |
| 308 |
host_group_names=[module.params["name"]], |
| 309 |
context_names=[module.params["context"]], |
| 310 |
).items |
| 311 |
) |
| 312 |
else: |
| 313 |
volumes = list( |
| 314 |
array.get_connections(host_group_names=[module.params["name"]]).items |
| 315 |
) |
| 316 |
if module.params["state"] == "present": |
| 317 |
if module.params["rename"]: |
| 318 |
if not rename_exists(module, array): |
| 319 |
if not module.check_mode: |
| 320 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 321 |
res = array.patch_host_groups( |
| 322 |
names=[module.params["name"]], |
| 323 |
context_names=[module.params["context"]], |
| 324 |
host_group=HostGroupPatch(name=module.params["rename"]), |
| 325 |
) |
| 326 |
else: |
| 327 |
res = array.patch_host_groups( |
| 328 |
names=[module.params["name"]], |
| 329 |
host_group=HostGroupPatch(name=module.params["rename"]), |
| 330 |
) |
| 331 |
if res.status_code != 200: |
| 332 |
module.fail_json( |
| 333 |
msg="Rename to {0} failed. Error: {1}".format( |
| 334 |
module.params["rename"], res.errors[0].message |
| 335 |
) |
| 336 |
) |
| 337 |
current_hostgroup = module.params["rename"] |
| 338 |
renamed = True |
| 339 |
else: |
| 340 |
module.warn( |
| 341 |
"Rename failed. Hostgroup {0} already exists. Continuing with other changes...".format( |
| 342 |
module.params["rename"] |
| 343 |
) |
| 344 |
) |
| 345 |
if module.params["host"]: |
| 346 |
hosts = list(module.params["host"]) |
| 347 |
hghosts = [] |
| 348 |
for host in hgroup: |
| 349 |
hghosts.append(host.member.name) |
| 350 |
new_hosts = list(set(hosts).difference(hghosts)) |
| 351 |
if new_hosts: |
| 352 |
if not module.check_mode: |
| 353 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 354 |
res = array.patch_hosts( |
| 355 |
host=HostPatch( |
| 356 |
host_group=ReferenceNoId(name=current_hostgroup) |
| 357 |
), |
| 358 |
names=new_hosts, |
| 359 |
context_names=[module.params["context"]], |
| 360 |
) |
| 361 |
else: |
| 362 |
res = array.patch_hosts( |
| 363 |
host=HostPatch( |
| 364 |
host_group=ReferenceNoId(name=current_hostgroup) |
| 365 |
), |
| 366 |
names=new_hosts, |
| 367 |
) |
| 368 |
if res.status_code != 200: |
| 369 |
module.fail_json( |
| 370 |
msg="Failed to add host(s) to hostgroup. Error: {0}".format( |
| 371 |
res.errors[0].message |
| 372 |
) |
| 373 |
) |
| 374 |
changed = True |
| 375 |
if module.params["volume"]: |
| 376 |
if volumes: |
| 377 |
current_vols = [vol.volume.name for vol in volumes] |
| 378 |
vols = list(module.params["volume"]) |
| 379 |
new_volumes = list(set(vols).difference(set(current_vols))) |
| 380 |
if len(new_volumes) == 1 and module.params["lun"]: |
| 381 |
if not module.check_mode: |
| 382 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion( |
| 383 |
api_version |
| 384 |
): |
| 385 |
res = array.post_connections( |
| 386 |
host_group_names=[current_hostgroup], |
| 387 |
context_names=[module.params["context"]], |
| 388 |
volume_names=[new_volumes[0]], |
| 389 |
connection=ConnectionPost(lun=module.params["lun"]), |
| 390 |
) |
| 391 |
else: |
| 392 |
res = array.post_connections( |
| 393 |
host_group_names=[current_hostgroup], |
| 394 |
volume_names=[new_volumes[0]], |
| 395 |
connection=ConnectionPost(lun=module.params["lun"]), |
| 396 |
) |
| 397 |
if res.status_code != 200: |
| 398 |
module.fail_json( |
| 399 |
msg="Failed to add volume {0} with LUN ID {1}. Error: {2}".format( |
| 400 |
new_volumes[0], |
| 401 |
module.params["lun"], |
| 402 |
res.errors[0].message, |
| 403 |
) |
| 404 |
) |
| 405 |
changed = True |
| 406 |
else: |
| 407 |
for cvol in new_volumes: |
| 408 |
if not module.check_mode: |
| 409 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion( |
| 410 |
api_version |
| 411 |
): |
| 412 |
res = array.post_connections( |
| 413 |
host_group_names=[current_hostgroup], |
| 414 |
context_names=[module.params["context"]], |
| 415 |
volume_names=[cvol], |
| 416 |
) |
| 417 |
else: |
| 418 |
res = array.post_connections( |
| 419 |
host_group_names=[current_hostgroup], |
| 420 |
volume_names=[cvol], |
| 421 |
) |
| 422 |
if res.status_code != 200: |
| 423 |
module.fail_json( |
| 424 |
msg="Failed to connect volume {0} to hostgroup {1}. Error: {2}".format( |
| 425 |
cvol, current_hostgroup, res.errors[0].message |
| 426 |
) |
| 427 |
) |
| 428 |
changed = True |
| 429 |
else: |
| 430 |
if len(module.params["volume"]) == 1 and module.params["lun"]: |
| 431 |
if not module.check_mode: |
| 432 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion( |
| 433 |
api_version |
| 434 |
): |
| 435 |
res = array.post_connections( |
| 436 |
host_group_names=[current_hostgroup], |
| 437 |
context_names=[module.params["context"]], |
| 438 |
volume_names=[module.params["volume"][0]], |
| 439 |
connection=ConnectionPost(lun=module.params["lun"]), |
| 440 |
) |
| 441 |
else: |
| 442 |
res = array.post_connections( |
| 443 |
host_group_names=[current_hostgroup], |
| 444 |
volume_names=[module.params["volume"][0]], |
| 445 |
connection=ConnectionPost(lun=module.params["lun"]), |
| 446 |
) |
| 447 |
if res.status_code != 200: |
| 448 |
module.fail_json( |
| 449 |
msg="Failed to add volume {0} with LUN ID {1}. Error: {2}".format( |
| 450 |
module.params["volume"], |
| 451 |
module.params["lun"], |
| 452 |
res.errors[0].message, |
| 453 |
) |
| 454 |
) |
| 455 |
changed = True |
| 456 |
else: |
| 457 |
for cvol in module.params["volume"]: |
| 458 |
if not module.check_mode: |
| 459 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion( |
| 460 |
api_version |
| 461 |
): |
| 462 |
res = array.post_connections( |
| 463 |
host_group_names=[current_hostgroup], |
| 464 |
context_names=[module.params["context"]], |
| 465 |
volume_names=[cvol], |
| 466 |
) |
| 467 |
else: |
| 468 |
res = array.post_connections( |
| 469 |
host_group_names=[current_hostgroup], |
| 470 |
volume_names=[cvol], |
| 471 |
) |
| 472 |
if res.status_code != 200: |
| 473 |
module.fail_json( |
| 474 |
msg="Failed to connect volume {0} to hostgroup {1}. Error: {2}".format( |
| 475 |
cvol, current_hostgroup, res.errors[0].message |
| 476 |
) |
| 477 |
) |
| 478 |
changed = True |
| 479 |
else: |
| 480 |
if module.params["host"]: |
| 481 |
old_hosts = list(module.params["host"]) |
| 482 |
hosts = [] |
| 483 |
for host in hgroup: |
| 484 |
hosts.append(host.member.name) |
| 485 |
old_hosts = list(set(old_hosts).intersection(hosts)) |
| 486 |
if old_hosts: |
| 487 |
if not module.check_mode: |
| 488 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 489 |
res = array.delete_host_groups_hosts( |
| 490 |
group_names=[current_hostgroup], |
| 491 |
member_names=old_hosts, |
| 492 |
context_names=[module.params["context"]], |
| 493 |
) |
| 494 |
else: |
| 495 |
res = array.delete_host_groups_hosts( |
| 496 |
group_names=[current_hostgroup], member_names=old_hosts |
| 497 |
) |
| 498 |
if res.status_code != 200: |
| 499 |
module.fail_json( |
| 500 |
msg="Failed to remove hosts {0} from hostgroup {1}. Error: {2}".format( |
| 501 |
old_hosts, current_hostgroup, res.errors[0].message |
| 502 |
) |
| 503 |
) |
| 504 |
changed = True |
| 505 |
if module.params["volume"]: |
| 506 |
old_vols = list(module.params["volume"]) |
| 507 |
old_volumes = list( |
| 508 |
set(old_vols).intersection(set([vol.volume.name for vol in volumes])) |
| 509 |
) |
| 510 |
if old_volumes: |
| 511 |
changed = True |
| 512 |
if not module.check_mode: |
| 513 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 514 |
res = array.delete_connections( |
| 515 |
host_group_names=[current_hostgroup], |
| 516 |
volume_names=old_volumes, |
| 517 |
context_names=[module.params["context"]], |
| 518 |
) |
| 519 |
else: |
| 520 |
res = array.delete_connections( |
| 521 |
host_group_names=[current_hostgroup], |
| 522 |
volume_names=old_volumes, |
| 523 |
) |
| 524 |
if res.status_code != 200: |
| 525 |
module.fail_json( |
| 526 |
msg="Failed to disconnect volume {0} from hostgroup {1}. Error: {2}".format( |
| 527 |
cvol, current_hostgroup, res.errors[0].message |
| 528 |
) |
| 529 |
) |
| 530 |
changed = changed or renamed |
| 531 |
module.exit_json(changed=changed) |
| 532 |
|
| 533 |
|
| 534 |
def delete_hostgroup(module, array): |
| 535 |
api_version = array.get_rest_version() |
| 536 |
changed = False |
| 537 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 538 |
res = array.get_connections( |
| 539 |
host_group_names=[module.params["name"]], |
| 540 |
context_names=[module.params["context"]], |
| 541 |
) |
| 542 |
else: |
| 543 |
res = array.get_connections(host_group_names=[module.params["name"]]) |
| 544 |
if res.status_code == 200: |
| 545 |
vols = list(res.items) |
| 546 |
else: |
| 547 |
module.fail_json( |
| 548 |
msg="Failed to get volume connection for hostgroup {0}. Error: {1}".format( |
| 549 |
module.params["hostgroup"], res.errors[0].message |
| 550 |
) |
| 551 |
) |
| 552 |
remove_vols = [vol.volume.name for vol in vols] |
| 553 |
if remove_vols: |
| 554 |
changed = True |
| 555 |
if not module.check_mode: |
| 556 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 557 |
res = array.delete_connections( |
| 558 |
host_group_names=[module.params["name"]], |
| 559 |
volume_names=remove_vols, |
| 560 |
context_names=[module.params["context"]], |
| 561 |
) |
| 562 |
else: |
| 563 |
res = array.delete_connections( |
| 564 |
host_group_names=[module.params["name"]], volume_names=remove_vols |
| 565 |
) |
| 566 |
if res.status_code != 200: |
| 567 |
module.fail_json( |
| 568 |
msg="Failed to disconnect volumes {0} from hostgroup {1} Error: {2}".format( |
| 569 |
vols, module.params["name"], res.errors[0].message |
| 570 |
) |
| 571 |
) |
| 572 |
hgroup = get_hostgroup_hosts(module, array) |
| 573 |
hghosts = [] |
| 574 |
for host in hgroup: |
| 575 |
hghosts.append(host.member.name) |
| 576 |
if hghosts: |
| 577 |
changed = True |
| 578 |
if not module.check_mode: |
| 579 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 580 |
res = array.delete_host_groups_hosts( |
| 581 |
group_names=[hgroup[0].group.name], |
| 582 |
member_names=hghosts, |
| 583 |
context_names=[module.params["context"]], |
| 584 |
) |
| 585 |
else: |
| 586 |
res = array.delete_host_groups_hosts( |
| 587 |
group_names=[hgroup[0].group.name], member_names=hghosts |
| 588 |
) |
| 589 |
if res.status_code != 200: |
| 590 |
module.fail_json( |
| 591 |
msg="Failed to remove hosts from hostgroup. Error: {0}".format( |
| 592 |
res.errors[0].message |
| 593 |
) |
| 594 |
) |
| 595 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 596 |
res = array.delete_host_groups( |
| 597 |
names=[module.params["name"]], context_names=[module.params["context"]] |
| 598 |
) |
| 599 |
else: |
| 600 |
res = array.delete_host_groups(names=[module.params["name"]]) |
| 601 |
if res.status_code != 200: |
| 602 |
module.fail_json( |
| 603 |
msg="Failed to delete hostgroup {0}. Error: {1}".format( |
| 604 |
module.params["name"], res.errors[0].message |
| 605 |
) |
| 606 |
) |
| 607 |
module.exit_json(changed=changed) |
| 608 |
|
| 609 |
|
| 610 |
def main(): |
| 611 |
argument_spec = purefa_argument_spec() |
| 612 |
argument_spec.update( |
| 613 |
dict( |
| 614 |
name=dict(type="str", required=True, aliases=["hostgroup"]), |
| 615 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 616 |
host=dict(type="list", elements="str"), |
| 617 |
lun=dict(type="int"), |
| 618 |
rename=dict(type="str"), |
| 619 |
volume=dict(type="list", elements="str"), |
| 620 |
eradicate=dict(type="bool", default=False), |
| 621 |
context=dict(type="str", default=""), |
| 622 |
) |
| 623 |
) |
| 624 |
|
| 625 |
module = AnsibleModule(argument_spec, supports_check_mode=True) |
| 626 |
|
| 627 |
state = module.params["state"] |
| 628 |
array = get_array(module) |
| 629 |
api_version = array.get_rest_version() |
| 630 |
hostgroup = get_hostgroup(module, array) |
| 631 |
|
| 632 |
if module.params["host"]: |
| 633 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 634 |
res = array.get_hosts( |
| 635 |
names=module.params["host"], context_names=[module.params["context"]] |
| 636 |
) |
| 637 |
else: |
| 638 |
res = array.get_hosts(names=module.params["host"]) |
| 639 |
if res.status_code == 400: |
| 640 |
module.fail_json(msg="Host {0} not found".format(res.errors[0].context)) |
| 641 |
if module.params["lun"] and state == "present": |
| 642 |
if not module.params["volume"]: |
| 643 |
module.fail(msg="LUN ID must be specified with a volume name") |
| 644 |
if len(module.params["volume"]) > 1: |
| 645 |
module.fail_json(msg="LUN ID cannot be specified with multiple volumes.") |
| 646 |
|
| 647 |
if module.params["lun"] and not 1 <= module.params["lun"] <= 4095: |
| 648 |
module.fail_json( |
| 649 |
msg="LUN ID of {0} is out of range (1 to 4095)".format(module.params["lun"]) |
| 650 |
) |
| 651 |
|
| 652 |
if module.params["volume"]: |
| 653 |
if LooseVersion(CONTEXT_API_VERSION) <= LooseVersion(api_version): |
| 654 |
res = array.get_volumes( |
| 655 |
names=module.params["volume"], context_names=[module.params["context"]] |
| 656 |
) |
| 657 |
else: |
| 658 |
res = array.get_volumes(names=module.params["volume"]) |
| 659 |
if res.status_code == 400: |
| 660 |
module.fail_json(msg="Volume {0} not found".format(res.errors[0].context)) |
| 661 |
if hostgroup and state == "present": |
| 662 |
update_hostgroup(module, array) |
| 663 |
elif hostgroup and module.params["volume"] is not None and state == "absent": |
| 664 |
update_hostgroup(module, array) |
| 665 |
elif hostgroup and module.params["host"] is not None and state == "absent": |
| 666 |
update_hostgroup(module, array) |
| 667 |
elif hostgroup and state == "absent": |
| 668 |
delete_hostgroup(module, array) |
| 669 |
elif hostgroup is None and state == "absent": |
| 670 |
module.exit_json(changed=False) |
| 671 |
else: |
| 672 |
make_hostgroup(module, array) |
| 673 |
|
| 674 |
module.exit_json(changed=False) |
| 675 |
|
| 676 |
|
| 677 |
if __name__ == "__main__": |
| 678 |
main() |