| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
from __future__ import (absolute_import, division, print_function) |
| 13 |
__metaclass__ = type |
| 14 |
|
| 15 |
DOCUMENTATION = """ |
| 16 |
--- |
| 17 |
module: ome_groups |
| 18 |
short_description: Manages static device groups on OpenManage Enterprise |
| 19 |
description: This module allows to create, modify, and delete static device groups on OpenManage Enterprise. |
| 20 |
version_added: "3.5.0" |
| 21 |
author: |
| 22 |
- "Jagadeesh N V(@jagadeeshnv)" |
| 23 |
- "Mangirish Kenkare(@MangirishK)" |
| 24 |
extends_documentation_fragment: |
| 25 |
- dellemc.openmanage.oment_auth_options |
| 26 |
options: |
| 27 |
state: |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- C(present) allows to create or modify a device group. |
| 31 |
- C(absent) allows to delete a device group. |
| 32 |
choices: [present, absent] |
| 33 |
default: present |
| 34 |
name: |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
description: |
| 38 |
- Name of the device group to be created, modified, or deleted. |
| 39 |
- If I(state) is absent, multiple names can be provided. |
| 40 |
- This option is case insensitive. |
| 41 |
- This option is mutually exclusive with I(group_id). |
| 42 |
group_id: |
| 43 |
type: list |
| 44 |
elements: int |
| 45 |
description: |
| 46 |
- ID of the device group to be created, modified, or deleted. |
| 47 |
- If I(state) is absent, multiple IDs can be provided. |
| 48 |
- This option is mutually exclusive with I(name). |
| 49 |
new_name: |
| 50 |
type: str |
| 51 |
description: |
| 52 |
- New name for the existing device group. |
| 53 |
- This is applicable only when I(state) is C(present). |
| 54 |
description: |
| 55 |
type: str |
| 56 |
description: |
| 57 |
- Description for the device group. |
| 58 |
- This is applicable only when I(state) is C(present). |
| 59 |
parent_group_name: |
| 60 |
type: str |
| 61 |
default: "Static Groups" |
| 62 |
description: |
| 63 |
- Name of the parent device group under which the device group to be created or modified. |
| 64 |
- This is applicable only when I(state) is C(present). |
| 65 |
- C(NOTE) If device group with such a name does not exist, device group with I(parent_group_name) is created. |
| 66 |
- This option is case insensitive. |
| 67 |
- This option is mutually exclusive with I(parent_group_id). |
| 68 |
parent_group_id: |
| 69 |
type: int |
| 70 |
description: |
| 71 |
- ID of the parent device group under which the device group to be created or modified. |
| 72 |
- This is applicable only when I(state) is C(present). |
| 73 |
- This option is mutually exclusive with I(parent_group_name). |
| 74 |
requirements: |
| 75 |
- "python >= 3.9.6" |
| 76 |
notes: |
| 77 |
- This module manages only static device groups on Dell OpenManage Enterprise. |
| 78 |
- If a device group with the name I(parent_group_name) does not exist, a new device group with the same name is created. |
| 79 |
- Make sure the entered parent group is not the descendant of the provided group. |
| 80 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 81 |
- This module supports C(check_mode). |
| 82 |
""" |
| 83 |
|
| 84 |
EXAMPLES = """ |
| 85 |
--- |
| 86 |
- name: Create a new device group |
| 87 |
dellemc.openmanage.ome_groups: |
| 88 |
hostname: "192.168.0.1" |
| 89 |
username: "username" |
| 90 |
password: "password" |
| 91 |
ca_path: "/path/to/ca_cert.pem" |
| 92 |
name: "group 1" |
| 93 |
description: "Group 1 description" |
| 94 |
parent_group_name: "group parent 1" |
| 95 |
|
| 96 |
- name: Modify a device group using the group ID |
| 97 |
dellemc.openmanage.ome_groups: |
| 98 |
hostname: "192.168.0.1" |
| 99 |
username: "username" |
| 100 |
password: "password" |
| 101 |
ca_path: "/path/to/ca_cert.pem" |
| 102 |
group_id: 1234 |
| 103 |
description: "Group description updated" |
| 104 |
parent_group_name: "group parent 2" |
| 105 |
|
| 106 |
- name: Delete a device group using the device group name |
| 107 |
dellemc.openmanage.ome_groups: |
| 108 |
hostname: "192.168.0.1" |
| 109 |
username: "username" |
| 110 |
password: "password" |
| 111 |
ca_path: "/path/to/ca_cert.pem" |
| 112 |
state: absent |
| 113 |
name: "group 1" |
| 114 |
|
| 115 |
- name: Delete multiple device groups using the group IDs |
| 116 |
dellemc.openmanage.ome_groups: |
| 117 |
hostname: "192.168.0.1" |
| 118 |
username: "username" |
| 119 |
password: "password" |
| 120 |
ca_path: "/path/to/ca_cert.pem" |
| 121 |
state: absent |
| 122 |
group_id: |
| 123 |
- 1234 |
| 124 |
- 5678 |
| 125 |
""" |
| 126 |
|
| 127 |
RETURN = """ |
| 128 |
--- |
| 129 |
msg: |
| 130 |
type: str |
| 131 |
description: Overall status of the device group operation. |
| 132 |
returned: always |
| 133 |
sample: "Successfully deleted the device group(s)." |
| 134 |
group_status: |
| 135 |
description: Details of the device group operation status. |
| 136 |
returned: success |
| 137 |
type: dict |
| 138 |
sample: { |
| 139 |
"Description": "my group description", |
| 140 |
"Id": 12123, |
| 141 |
"MembershipTypeId": 12, |
| 142 |
"Name": "group 1", |
| 143 |
"ParentId": 12345, |
| 144 |
"TypeId": 3000, |
| 145 |
"IdOwner": 30, |
| 146 |
"CreatedBy": "admin", |
| 147 |
"CreationTime": "2021-01-01 10:10:10.100", |
| 148 |
"DefinitionDescription": "UserDefined", |
| 149 |
"DefinitionId": 400, |
| 150 |
"GlobalStatus": 5000, |
| 151 |
"HasAttributes": false, |
| 152 |
"UpdatedBy": "", |
| 153 |
"UpdatedTime": "2021-01-01 11:11:10.100", |
| 154 |
"Visible": true |
| 155 |
} |
| 156 |
group_ids: |
| 157 |
type: list |
| 158 |
elements: int |
| 159 |
description: List of the deleted device group IDs. |
| 160 |
returned: when I(state) is C(absent) |
| 161 |
sample: [1234, 5678] |
| 162 |
invalid_groups: |
| 163 |
type: list |
| 164 |
elements: str |
| 165 |
description: List of the invalid device group IDs or names. |
| 166 |
returned: when I(state) is C(absent) |
| 167 |
sample: [1234, 5678] |
| 168 |
error_info: |
| 169 |
description: Details of the HTTP Error. |
| 170 |
returned: on HTTP error |
| 171 |
type: dict |
| 172 |
sample: { |
| 173 |
"error": { |
| 174 |
"code": "Base.1.0.GeneralError", |
| 175 |
"message": "A general error has occurred. See ExtendedInfo for more information.", |
| 176 |
"@Message.ExtendedInfo": [ |
| 177 |
{ |
| 178 |
"MessageId": "CGRP9013", |
| 179 |
"RelatedProperties": [], |
| 180 |
"Message": "Unable to update group 12345 with the provided parent 54321 because a group/parent |
| 181 |
relationship already exists.", |
| 182 |
"MessageArgs": [ |
| 183 |
"12345", |
| 184 |
"54321" |
| 185 |
], |
| 186 |
"Severity": "Warning", |
| 187 |
"Resolution": "Make sure the entered parent ID does not create a bidirectional relationship and retry |
| 188 |
the operation." |
| 189 |
} |
| 190 |
] |
| 191 |
} |
| 192 |
} |
| 193 |
""" |
| 194 |
|
| 195 |
import json |
| 196 |
import time |
| 197 |
from ssl import SSLError |
| 198 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 199 |
from ansible.module_utils.urls import ConnectionError |
| 200 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 201 |
|
| 202 |
GROUP_URI = "GroupService/Groups" |
| 203 |
OP_URI = "GroupService/Actions/GroupService.{op}Group" |
| 204 |
|
| 205 |
MULTIPLE_GROUPS_MSG = "Provide only one unique device group when state is present." |
| 206 |
NONEXIST_GROUP_ID = "A device group with the provided ID does not exist." |
| 207 |
NONEXIST_PARENT_ID = "A parent device group with the provided ID does not exist." |
| 208 |
INVALID_PARENT = "The provided parent device group is not a valid user-defined static device group." |
| 209 |
INVALID_GROUPS_DELETE = "Provide valid static device group(s) for deletion." |
| 210 |
INVALID_GROUPS_MODIFY = "Provide valid static device group for modification." |
| 211 |
PARENT_CREATION_FAILED = "Unable to create a parent device group with the name {pname}." |
| 212 |
CREATE_SUCCESS = "Successfully {op}d the device group." |
| 213 |
GROUP_PARENT_SAME = "Provided parent and the device group cannot be the same." |
| 214 |
GROUP_NAME_EXISTS = "Unable to rename the group because a group with the provided name '{gname}' already exists." |
| 215 |
DELETE_SUCCESS = "Successfully deleted the device group(s)." |
| 216 |
NO_CHANGES_MSG = "No changes found to be applied." |
| 217 |
CHANGES_FOUND = "Changes found to be applied." |
| 218 |
STATIC_ROOT = 'Static Groups' |
| 219 |
SETTLING_TIME = 2 |
| 220 |
|
| 221 |
|
| 222 |
def get_valid_groups(module, rest_obj, group_arg, group_set): |
| 223 |
parent = {} |
| 224 |
static_root = {} |
| 225 |
group_dict = {} |
| 226 |
group_resp = rest_obj.get_all_items_with_pagination(GROUP_URI) |
| 227 |
if module.params.get('state') == 'absent': |
| 228 |
group_dict = dict([(str(g[group_arg]).lower(), g) for g in group_resp.get("value") |
| 229 |
if str(g[group_arg]).lower() in group_set]) |
| 230 |
else: |
| 231 |
parg = module.params.get('parent_group_id') |
| 232 |
if parg: |
| 233 |
pkey = 'Id' |
| 234 |
else: |
| 235 |
pkey = 'Name' |
| 236 |
parg = module.params.get('parent_group_name') |
| 237 |
count = 0 |
| 238 |
for g in group_resp.get("value"): |
| 239 |
if str(g[group_arg]).lower() in group_set: |
| 240 |
group_dict = g |
| 241 |
count = count + 1 |
| 242 |
if str(g[pkey]).lower() == str(parg).lower(): |
| 243 |
parent = g |
| 244 |
count = count + 1 |
| 245 |
if g['Name'] == STATIC_ROOT: |
| 246 |
static_root = g |
| 247 |
count = count + 1 |
| 248 |
if count == 3: |
| 249 |
break |
| 250 |
return group_dict, parent, static_root |
| 251 |
|
| 252 |
|
| 253 |
def is_valid_static_group(grp): |
| 254 |
if grp['TypeId'] == 3000 and grp['MembershipTypeId'] == 12: |
| 255 |
return True |
| 256 |
return False |
| 257 |
|
| 258 |
|
| 259 |
def create_parent(rest_obj, module, static_root): |
| 260 |
try: |
| 261 |
prt = static_root |
| 262 |
payload = {} |
| 263 |
payload['MembershipTypeId'] = 12 |
| 264 |
payload['Name'] = module.params.get('parent_group_name') |
| 265 |
payload['ParentId'] = prt['Id'] |
| 266 |
prt_resp = rest_obj.invoke_request('POST', OP_URI.format(op='Create'), data={"GroupModel": payload}) |
| 267 |
return int(prt_resp.json_data) |
| 268 |
except Exception: |
| 269 |
return static_root['Id'] |
| 270 |
|
| 271 |
|
| 272 |
def get_parent_id(rest_obj, module, parent, static_root): |
| 273 |
parent_id = module.params.get("parent_group_id") |
| 274 |
if parent_id: |
| 275 |
if not parent: |
| 276 |
module.exit_json(msg=NONEXIST_PARENT_ID, failed=True) |
| 277 |
if parent['Name'] != STATIC_ROOT: |
| 278 |
if not is_valid_static_group(parent): |
| 279 |
module.exit_json(msg=INVALID_PARENT, failed=True) |
| 280 |
return parent['Id'] |
| 281 |
else: |
| 282 |
if parent: |
| 283 |
if parent['Name'] != STATIC_ROOT: |
| 284 |
if not is_valid_static_group(parent): |
| 285 |
module.exit_json(msg=INVALID_PARENT, failed=True) |
| 286 |
return parent['Id'] |
| 287 |
else: |
| 288 |
if module.check_mode: |
| 289 |
return 0 |
| 290 |
else: |
| 291 |
prtid = create_parent(rest_obj, module, static_root) |
| 292 |
time.sleep(SETTLING_TIME) |
| 293 |
return prtid |
| 294 |
return static_root['Id'] |
| 295 |
|
| 296 |
|
| 297 |
def get_ome_group_by_name(rest_obj, name): |
| 298 |
grp = {} |
| 299 |
try: |
| 300 |
resp = rest_obj.invoke_request("GET", GROUP_URI, query_param={"$filter": "Name eq '{0}'".format(name)}) |
| 301 |
group_resp = resp.json_data.get('value') |
| 302 |
if group_resp: |
| 303 |
grp = group_resp[0] |
| 304 |
except Exception: |
| 305 |
grp = {} |
| 306 |
return grp |
| 307 |
|
| 308 |
|
| 309 |
def get_ome_group_by_id(rest_obj, id): |
| 310 |
grp = {} |
| 311 |
try: |
| 312 |
resp = rest_obj.invoke_request('GET', GROUP_URI + "({0})".format(id)) |
| 313 |
grp = resp.json_data |
| 314 |
except Exception: |
| 315 |
grp = {} |
| 316 |
return grp |
| 317 |
|
| 318 |
|
| 319 |
def exit_group_operation(module, rest_obj, payload, operation): |
| 320 |
group_resp = rest_obj.invoke_request('POST', OP_URI.format(op=operation), data={"GroupModel": payload}) |
| 321 |
cid = int(group_resp.json_data) |
| 322 |
time.sleep(SETTLING_TIME) |
| 323 |
try: |
| 324 |
grp = get_ome_group_by_id(rest_obj, cid) |
| 325 |
group = rest_obj.strip_substr_dict(grp) |
| 326 |
except Exception: |
| 327 |
payload['Id'] = cid |
| 328 |
group = payload |
| 329 |
module.exit_json(changed=True, msg=CREATE_SUCCESS.format(op=operation.lower()), group_status=group) |
| 330 |
|
| 331 |
|
| 332 |
def create_group(rest_obj, module, parent, static_root): |
| 333 |
payload = {} |
| 334 |
payload['MembershipTypeId'] = 12 |
| 335 |
mparams = module.params |
| 336 |
payload['Name'] = mparams.get('name')[0] |
| 337 |
if mparams.get('parent_group_name').lower() == payload['Name'].lower(): |
| 338 |
module.exit_json(msg=GROUP_PARENT_SAME, failed=True) |
| 339 |
parent_id = get_parent_id(rest_obj, module, parent, static_root) |
| 340 |
payload['ParentId'] = parent_id |
| 341 |
if mparams.get('description'): |
| 342 |
payload['Description'] = mparams.get('description') |
| 343 |
if module.check_mode: |
| 344 |
module.exit_json(changed=True, msg=CHANGES_FOUND) |
| 345 |
exit_group_operation(module, rest_obj, payload, 'Create') |
| 346 |
|
| 347 |
|
| 348 |
def modify_group(rest_obj, module, valid_group_dict, parent, static_root): |
| 349 |
if not is_valid_static_group(valid_group_dict): |
| 350 |
module.exit_json(msg=INVALID_GROUPS_MODIFY, failed=True) |
| 351 |
grp = valid_group_dict |
| 352 |
diff = 0 |
| 353 |
payload = dict([(k, grp.get(k)) for k in ["Name", "Description", "MembershipTypeId", "ParentId", "Id"]]) |
| 354 |
new_name = module.params.get('new_name') |
| 355 |
if new_name: |
| 356 |
if new_name != payload['Name']: |
| 357 |
dup_grp = get_ome_group_by_name(rest_obj, new_name) |
| 358 |
if dup_grp: |
| 359 |
module.exit_json(msg=GROUP_NAME_EXISTS.format(gname=new_name), failed=True) |
| 360 |
payload['Name'] = new_name |
| 361 |
diff += 1 |
| 362 |
desc = module.params.get('description') |
| 363 |
if desc: |
| 364 |
if desc != payload['Description']: |
| 365 |
payload['Description'] = desc |
| 366 |
diff += 1 |
| 367 |
parent_id = get_parent_id(rest_obj, module, parent, static_root) |
| 368 |
if parent_id == payload['Id']: |
| 369 |
module.exit_json(msg=GROUP_PARENT_SAME, failed=True) |
| 370 |
if parent_id != payload['ParentId']: |
| 371 |
payload['ParentId'] = parent_id |
| 372 |
diff += 1 |
| 373 |
if diff == 0: |
| 374 |
gs = rest_obj.strip_substr_dict(grp) |
| 375 |
module.exit_json(msg=NO_CHANGES_MSG, group_status=gs) |
| 376 |
if module.check_mode: |
| 377 |
module.exit_json(changed=True, msg=CHANGES_FOUND) |
| 378 |
exit_group_operation(module, rest_obj, payload, 'Update') |
| 379 |
|
| 380 |
|
| 381 |
def delete_groups(rest_obj, module, group_set, group_dict): |
| 382 |
deletables = [] |
| 383 |
invalids = [] |
| 384 |
for g in group_set: |
| 385 |
grp = group_dict.get(str(g).lower()) |
| 386 |
if grp: |
| 387 |
if is_valid_static_group(grp): |
| 388 |
deletables.append(grp['Id']) |
| 389 |
else: |
| 390 |
invalids.append(g) |
| 391 |
if invalids: |
| 392 |
module.exit_json(msg=INVALID_GROUPS_DELETE, invalid_groups=invalids, failed=True) |
| 393 |
if module.check_mode: |
| 394 |
module.exit_json(changed=True, msg=CHANGES_FOUND, group_ids=deletables) |
| 395 |
rest_obj.invoke_request("POST", OP_URI.format(op='Delete'), data={"GroupIds": deletables}) |
| 396 |
module.exit_json(changed=True, msg=DELETE_SUCCESS, group_ids=deletables) |
| 397 |
|
| 398 |
|
| 399 |
def main(): |
| 400 |
specs = { |
| 401 |
"name": {"type": "list", "elements": 'str'}, |
| 402 |
"group_id": {"type": "list", "elements": 'int'}, |
| 403 |
"state": {"type": "str", "choices": ["present", "absent"], "default": "present"}, |
| 404 |
"description": {"type": "str"}, |
| 405 |
"new_name": {"type": "str"}, |
| 406 |
"parent_group_name": {"type": "str", "default": STATIC_ROOT}, |
| 407 |
"parent_group_id": {"type": "int"}, |
| 408 |
} |
| 409 |
|
| 410 |
module = OmeAnsibleModule( |
| 411 |
argument_spec=specs, |
| 412 |
required_if=[ |
| 413 |
["state", "present", ("new_name", "description", "parent_group_name", "parent_group_id"), True], |
| 414 |
], |
| 415 |
mutually_exclusive=[ |
| 416 |
("name", "group_id"), ("parent_group_name", "parent_group_id"), |
| 417 |
], |
| 418 |
required_one_of=[("name", "group_id")], |
| 419 |
supports_check_mode=True |
| 420 |
) |
| 421 |
|
| 422 |
try: |
| 423 |
if module.params.get('name'): |
| 424 |
group_arg = 'Name' |
| 425 |
group_set = set(v.lower() for v in module.params.get('name')) |
| 426 |
else: |
| 427 |
group_arg = 'Id' |
| 428 |
group_set = set(str(v).lower() for v in module.params.get('group_id')) |
| 429 |
if len(group_set) != 1 and module.params['state'] == 'present': |
| 430 |
module.exit_json(msg=MULTIPLE_GROUPS_MSG, failed=True) |
| 431 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 432 |
valid_group_dict, parent, static_root = get_valid_groups(module, rest_obj, group_arg, group_set) |
| 433 |
if module.params["state"] == "absent": |
| 434 |
if valid_group_dict: |
| 435 |
delete_groups(rest_obj, module, group_set, valid_group_dict) |
| 436 |
module.exit_json(msg=NO_CHANGES_MSG) |
| 437 |
else: |
| 438 |
if valid_group_dict: |
| 439 |
modify_group(rest_obj, module, valid_group_dict, parent, static_root) |
| 440 |
elif group_arg == 'Id': |
| 441 |
module.exit_json(msg=NONEXIST_GROUP_ID, failed=True) |
| 442 |
create_group(rest_obj, module, parent, static_root) |
| 443 |
except HTTPError as err: |
| 444 |
module.exit_json(msg=str(err), error_info=json.load(err), failed=True) |
| 445 |
except URLError as err: |
| 446 |
module.exit_json(msg=str(err), unreachable=True) |
| 447 |
except (IOError, ValueError, SSLError, TypeError, ConnectionError, AttributeError, IndexError, KeyError, OSError) as err: |
| 448 |
module.exit_json(msg=str(err), failed=True) |
| 449 |
|
| 450 |
|
| 451 |
if __name__ == '__main__': |
| 452 |
main() |