| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from __future__ import (absolute_import, division, print_function) |
| 14 |
__metaclass__ = type |
| 15 |
|
| 16 |
DOCUMENTATION = r''' |
| 17 |
--- |
| 18 |
module: ome_user |
| 19 |
short_description: Create, modify or delete a user on OpenManage Enterprise |
| 20 |
version_added: "2.0.0" |
| 21 |
description: This module creates, modifies or deletes a user on OpenManage Enterprise. |
| 22 |
extends_documentation_fragment: |
| 23 |
- dellemc.openmanage.ome_auth_options |
| 24 |
options: |
| 25 |
state: |
| 26 |
type: str |
| 27 |
description: |
| 28 |
- C(present) creates a user in case the I(UserName) provided inside I(attributes) does not exist. |
| 29 |
- C(present) modifies a user in case the I(UserName) provided inside I(attributes) exists. |
| 30 |
- C(absent) deletes an existing user. |
| 31 |
choices: [present, absent] |
| 32 |
default: present |
| 33 |
user_id: |
| 34 |
description: |
| 35 |
- Unique ID of the user to be deleted. |
| 36 |
- Either I(user_id) or I(name) is mandatory for C(absent) operation. |
| 37 |
type: int |
| 38 |
name: |
| 39 |
type: str |
| 40 |
description: |
| 41 |
- Unique Name of the user to be deleted. |
| 42 |
- Either I(user_id) or I(name) is mandatory for C(absent) operation. |
| 43 |
attributes: |
| 44 |
type: dict |
| 45 |
default: {} |
| 46 |
description: |
| 47 |
- >- |
| 48 |
Payload data for the user operations. It can take the following attributes for C(present). |
| 49 |
- >- |
| 50 |
UserTypeId, DirectoryServiceId, Description, Name, Password, UserName, RoleId, Locked, Enabled. |
| 51 |
- >- |
| 52 |
OME will throw error if required parameter is not provided for operation. |
| 53 |
- >- |
| 54 |
Refer OpenManage Enterprise API Reference Guide for more details. |
| 55 |
requirements: |
| 56 |
- "python >= 3.9.6" |
| 57 |
author: "Sajna Shetty(@Sajna-Shetty)" |
| 58 |
notes: |
| 59 |
- Run this module from a system that has direct access to Dell OpenManage Enterprise. |
| 60 |
- This module does not support C(check_mode). |
| 61 |
''' |
| 62 |
|
| 63 |
EXAMPLES = r''' |
| 64 |
--- |
| 65 |
- name: Create user with required parameters |
| 66 |
dellemc.openmanage.ome_user: |
| 67 |
hostname: "192.168.0.1" |
| 68 |
username: "username" |
| 69 |
password: "password" |
| 70 |
ca_path: "/path/to/ca_cert.pem" |
| 71 |
attributes: |
| 72 |
UserName: "user1" |
| 73 |
Password: "UserPassword" |
| 74 |
RoleId: "10" |
| 75 |
Enabled: true |
| 76 |
|
| 77 |
- name: Create user with all parameters |
| 78 |
dellemc.openmanage.ome_user: |
| 79 |
hostname: "192.168.0.1" |
| 80 |
username: "username" |
| 81 |
password: "password" |
| 82 |
ca_path: "/path/to/ca_cert.pem" |
| 83 |
attributes: |
| 84 |
UserName: "user2" |
| 85 |
Description: "user2 description" |
| 86 |
Password: "UserPassword" |
| 87 |
RoleId: "10" |
| 88 |
Enabled: true |
| 89 |
DirectoryServiceId: 0 |
| 90 |
UserTypeId: 1 |
| 91 |
Locked: false |
| 92 |
Name: "user2" |
| 93 |
|
| 94 |
- name: Modify existing user |
| 95 |
dellemc.openmanage.ome_user: |
| 96 |
hostname: "192.168.0.1" |
| 97 |
username: "username" |
| 98 |
password: "password" |
| 99 |
ca_path: "/path/to/ca_cert.pem" |
| 100 |
state: "present" |
| 101 |
attributes: |
| 102 |
UserName: "user3" |
| 103 |
RoleId: "10" |
| 104 |
Enabled: true |
| 105 |
Description: "Modify user Description" |
| 106 |
|
| 107 |
- name: Delete existing user using id |
| 108 |
dellemc.openmanage.ome_user: |
| 109 |
hostname: "192.168.0.1" |
| 110 |
username: "username" |
| 111 |
password: "password" |
| 112 |
ca_path: "/path/to/ca_cert.pem" |
| 113 |
state: "absent" |
| 114 |
user_id: 1234 |
| 115 |
|
| 116 |
- name: Delete existing user using name |
| 117 |
dellemc.openmanage.ome_user: |
| 118 |
hostname: "192.168.0.1" |
| 119 |
username: "username" |
| 120 |
password: "password" |
| 121 |
ca_path: "/path/to/ca_cert.pem" |
| 122 |
state: "absent" |
| 123 |
name: "name" |
| 124 |
''' |
| 125 |
|
| 126 |
RETURN = r''' |
| 127 |
--- |
| 128 |
msg: |
| 129 |
description: Overall status of the user operation. |
| 130 |
returned: always |
| 131 |
type: str |
| 132 |
sample: "Successfully created a User" |
| 133 |
user_status: |
| 134 |
description: Details of the user operation, when I(state) is C(present). |
| 135 |
returned: When I(state) is C(present). |
| 136 |
type: dict |
| 137 |
sample: |
| 138 |
{ |
| 139 |
"Description": "Test user creation", |
| 140 |
"DirectoryServiceId": 0, |
| 141 |
"Enabled": true, |
| 142 |
"Id": "61546", |
| 143 |
"IsBuiltin": false, |
| 144 |
"Locked": false, |
| 145 |
"Name": "test", |
| 146 |
"Password": null, |
| 147 |
"PlainTextPassword": null, |
| 148 |
"RoleId": "10", |
| 149 |
"UserName": "test", |
| 150 |
"UserTypeId": 1 |
| 151 |
} |
| 152 |
''' |
| 153 |
|
| 154 |
import json |
| 155 |
from ssl import SSLError |
| 156 |
from ansible_collections.dellemc.openmanage.plugins.module_utils.ome import RestOME, OmeAnsibleModule |
| 157 |
from ansible.module_utils.six.moves.urllib.error import URLError, HTTPError |
| 158 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 159 |
|
| 160 |
|
| 161 |
def _validate_inputs(module): |
| 162 |
"""both user_id and name are not acceptable in case of state is absent""" |
| 163 |
state = module.params['state'] |
| 164 |
user_id = module.params.get('user_id') |
| 165 |
name = module.params.get('name') |
| 166 |
if state != 'present' and (user_id is None and name is None): |
| 167 |
fail_module(module, msg="One of the following 'user_id' or 'name' " |
| 168 |
"option is required for state 'absent'") |
| 169 |
|
| 170 |
|
| 171 |
def get_user_id_from_name(rest_obj, name): |
| 172 |
"""Get the account id using account name""" |
| 173 |
user_id = None |
| 174 |
if name is not None: |
| 175 |
resp = rest_obj.invoke_request('GET', 'AccountService/Accounts') |
| 176 |
if resp.success: |
| 177 |
for user in resp.json_data.get('value'): |
| 178 |
if 'UserName' in user and user['UserName'] == name: |
| 179 |
return user['Id'] |
| 180 |
return user_id |
| 181 |
|
| 182 |
|
| 183 |
def _get_resource_parameters(module, rest_obj): |
| 184 |
state = module.params["state"] |
| 185 |
payload = module.params.get("attributes") |
| 186 |
if state == "present": |
| 187 |
name = payload.get('UserName') |
| 188 |
user_id = get_user_id_from_name(rest_obj, name) |
| 189 |
if user_id is not None: |
| 190 |
payload.update({"Id": user_id}) |
| 191 |
path = "AccountService/Accounts('{user_id}')".format(user_id=user_id) |
| 192 |
method = 'PUT' |
| 193 |
else: |
| 194 |
path = "AccountService/Accounts" |
| 195 |
method = 'POST' |
| 196 |
else: |
| 197 |
user_id = module.params.get("user_id") |
| 198 |
if user_id is None: |
| 199 |
name = module.params.get('name') |
| 200 |
user_id = get_user_id_from_name(rest_obj, name) |
| 201 |
if user_id is None: |
| 202 |
fail_module(module, msg="Unable to get the account because the specified account " |
| 203 |
"does not exist in the system.") |
| 204 |
path = "AccountService/Accounts('{user_id}')".format(user_id=user_id) |
| 205 |
method = 'DELETE' |
| 206 |
return method, path, payload |
| 207 |
|
| 208 |
|
| 209 |
def password_no_log(attributes): |
| 210 |
if isinstance(attributes, dict) and 'Password' in attributes: |
| 211 |
attributes['Password'] = "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER" |
| 212 |
|
| 213 |
|
| 214 |
def fail_module(module, **failmsg): |
| 215 |
password_no_log(module.params.get("attributes")) |
| 216 |
module.fail_json(**failmsg) |
| 217 |
|
| 218 |
|
| 219 |
def exit_module(module, response, http_method): |
| 220 |
password_no_log(module.params.get("attributes")) |
| 221 |
msg_dict = {'POST': "Successfully created a User", |
| 222 |
'PUT': "Successfully modified a User", |
| 223 |
'DELETE': "Successfully deleted the User"} |
| 224 |
state_msg = msg_dict[http_method] |
| 225 |
if response.status_code != 204: |
| 226 |
module.exit_json(msg=state_msg, changed=True, user_status=response.json_data) |
| 227 |
else: |
| 228 |
|
| 229 |
module.exit_json(msg=state_msg, changed=True) |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
specs = { |
| 234 |
"state": {"required": False, "type": 'str', "default": "present", |
| 235 |
"choices": ['present', 'absent']}, |
| 236 |
"user_id": {"required": False, "type": 'int'}, |
| 237 |
"name": {"required": False, "type": 'str'}, |
| 238 |
"attributes": {"required": False, "type": 'dict', "default": {}}, |
| 239 |
} |
| 240 |
|
| 241 |
module = OmeAnsibleModule( |
| 242 |
argument_spec=specs, |
| 243 |
mutually_exclusive=[['user_id', 'name'], ], |
| 244 |
required_if=[['state', 'present', ['attributes']], ], |
| 245 |
supports_check_mode=False) |
| 246 |
|
| 247 |
try: |
| 248 |
_validate_inputs(module) |
| 249 |
with RestOME(module.params, req_session=True) as rest_obj: |
| 250 |
method, path, payload = _get_resource_parameters(module, rest_obj) |
| 251 |
resp = rest_obj.invoke_request(method, path, data=payload) |
| 252 |
if resp.success: |
| 253 |
exit_module(module, resp, method) |
| 254 |
except HTTPError as err: |
| 255 |
fail_module(module, msg=str(err), user_status=json.load(err)) |
| 256 |
except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError, OSError, SSLError) as err: |
| 257 |
fail_module(module, msg=str(err)) |
| 258 |
|
| 259 |
|
| 260 |
if __name__ == '__main__': |
| 261 |
main() |