| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
# Copyright: (c) 2020, Antoine Tanzilli (@Tailzip), Hong Viet Lê (@pomverte), Julien Alexandre (@jual), Marc Cyprien (@LeFameux) |
| 19 |
|
| 20 |
from __future__ import absolute_import, division, print_function |
| 21 |
|
| 22 |
DOCUMENTATION = """ |
| 23 |
--- |
| 24 |
module: grafana_user |
| 25 |
author: |
| 26 |
- Antoine Tanzilli (@Tailzip) |
| 27 |
- Hong Viet LE (@pomverte) |
| 28 |
- Julien Alexandre (@jual) |
| 29 |
- Marc Cyprien (@LeFameux) |
| 30 |
version_added: "1.0.0" |
| 31 |
short_description: Manage Grafana User |
| 32 |
description: |
| 33 |
- Create/update/delete Grafana User through the users and admin API. |
| 34 |
- Tested with Grafana v6.4.3 |
| 35 |
- Password update is not supported at the time |
| 36 |
options: |
| 37 |
name: |
| 38 |
description: |
| 39 |
- The name of the Grafana User. |
| 40 |
required: false |
| 41 |
type: str |
| 42 |
email: |
| 43 |
description: |
| 44 |
- The email of the Grafana User. |
| 45 |
required: false |
| 46 |
type: str |
| 47 |
login: |
| 48 |
description: |
| 49 |
- The login of the Grafana User. |
| 50 |
required: true |
| 51 |
type: str |
| 52 |
password: |
| 53 |
description: |
| 54 |
- The password of the Grafana User. |
| 55 |
- At the moment, this field is not updated yet. |
| 56 |
required: false |
| 57 |
type: str |
| 58 |
is_admin: |
| 59 |
description: |
| 60 |
- The Grafana User is an admin. |
| 61 |
required: false |
| 62 |
type: bool |
| 63 |
default: false |
| 64 |
state: |
| 65 |
description: |
| 66 |
- State if the user should be present in Grafana or not |
| 67 |
default: present |
| 68 |
type: str |
| 69 |
choices: ["present", "absent"] |
| 70 |
notes: |
| 71 |
- Unlike other modules from the collection, this module does not support C(grafana_api_key) authentication type. The Grafana API endpoint for users management |
| 72 |
requires basic auth and admin privileges. |
| 73 |
extends_documentation_fragment: |
| 74 |
- community.grafana.basic_auth |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = """ |
| 78 |
--- |
| 79 |
- name: Create or update a Grafana user |
| 80 |
community.grafana.grafana_user: |
| 81 |
url: "https://grafana.example.com" |
| 82 |
url_username: admin |
| 83 |
url_password: changeme |
| 84 |
name: "Bruce Wayne" |
| 85 |
email: [email protected] |
| 86 |
login: batman |
| 87 |
password: robin |
| 88 |
is_admin: true |
| 89 |
state: present |
| 90 |
|
| 91 |
- name: Delete a Grafana user |
| 92 |
community.grafana.grafana_user: |
| 93 |
url: "https://grafana.example.com" |
| 94 |
url_username: admin |
| 95 |
url_password: changeme |
| 96 |
login: batman |
| 97 |
state: absent |
| 98 |
""" |
| 99 |
|
| 100 |
RETURN = """ |
| 101 |
--- |
| 102 |
user: |
| 103 |
description: Information about the User |
| 104 |
returned: when state present |
| 105 |
type: complex |
| 106 |
contains: |
| 107 |
id: |
| 108 |
description: The User id |
| 109 |
returned: always |
| 110 |
type: int |
| 111 |
sample: |
| 112 |
- 42 |
| 113 |
email: |
| 114 |
description: The User email address |
| 115 |
returned: always |
| 116 |
type: str |
| 117 |
sample: |
| 118 |
- "[email protected]" |
| 119 |
login: |
| 120 |
description: The User login |
| 121 |
returned: always |
| 122 |
type: str |
| 123 |
sample: |
| 124 |
- "batman" |
| 125 |
theme: |
| 126 |
description: The Grafana theme |
| 127 |
returned: always |
| 128 |
type: str |
| 129 |
sample: |
| 130 |
- "light" |
| 131 |
orgId: |
| 132 |
description: The organization id that the team is part of. |
| 133 |
returned: always |
| 134 |
type: int |
| 135 |
sample: |
| 136 |
- 1 |
| 137 |
isGrafanaAdmin: |
| 138 |
description: The Grafana user permission for admin |
| 139 |
returned: always |
| 140 |
type: bool |
| 141 |
sample: |
| 142 |
- false |
| 143 |
isDisabled: |
| 144 |
description: The Grafana account status |
| 145 |
returned: always |
| 146 |
type: bool |
| 147 |
sample: |
| 148 |
- false |
| 149 |
isExternal: |
| 150 |
description: The Grafana account information on external user provider |
| 151 |
returned: always |
| 152 |
type: bool |
| 153 |
sample: |
| 154 |
- false |
| 155 |
""" |
| 156 |
|
| 157 |
import json |
| 158 |
|
| 159 |
from ansible.module_utils.basic import AnsibleModule |
| 160 |
from ansible.module_utils.urls import fetch_url, basic_auth_header |
| 161 |
from ansible_collections.community.grafana.plugins.module_utils import base |
| 162 |
from ansible.module_utils.six.moves.urllib.parse import quote |
| 163 |
|
| 164 |
__metaclass__ = type |
| 165 |
|
| 166 |
|
| 167 |
class GrafanaUserInterface(object): |
| 168 |
def __init__(self, module): |
| 169 |
self._module = module |
| 170 |
# {{{ Authentication header |
| 171 |
self.headers = {"Content-Type": "application/json"} |
| 172 |
self.headers["Authorization"] = basic_auth_header( |
| 173 |
module.params["url_username"], module.params["url_password"] |
| 174 |
) |
| 175 |
# }}} |
| 176 |
self.grafana_url = base.clean_url(module.params.get("url")) |
| 177 |
|
| 178 |
def _send_request(self, url, data=None, headers=None, method="GET"): |
| 179 |
if data is not None: |
| 180 |
data = json.dumps(data, sort_keys=True) |
| 181 |
if not headers: |
| 182 |
headers = [] |
| 183 |
|
| 184 |
full_url = "{grafana_url}{path}".format(grafana_url=self.grafana_url, path=url) |
| 185 |
resp, info = fetch_url( |
| 186 |
self._module, full_url, data=data, headers=headers, method=method |
| 187 |
) |
| 188 |
status_code = info["status"] |
| 189 |
if status_code == 404: |
| 190 |
return None |
| 191 |
elif status_code == 401: |
| 192 |
self._module.fail_json( |
| 193 |
failed=True, |
| 194 |
msg="Unauthorized to perform action '%s' on '%s' header: %s" |
| 195 |
% (method, full_url, self.headers), |
| 196 |
) |
| 197 |
elif status_code == 403: |
| 198 |
self._module.fail_json(failed=True, msg="Permission Denied") |
| 199 |
elif status_code == 200: |
| 200 |
return self._module.from_json(resp.read()) |
| 201 |
self._module.fail_json( |
| 202 |
failed=True, |
| 203 |
msg="Grafana Users API answered with HTTP %d" % status_code, |
| 204 |
body=self._module.from_json(resp.read()), |
| 205 |
) |
| 206 |
|
| 207 |
def create_user(self, name, email, login, password): |
| 208 |
# https://grafana.com/docs/http_api/admin/#global-users |
| 209 |
if not password: |
| 210 |
self._module.fail_json( |
| 211 |
failed=True, msg="missing required arguments: password" |
| 212 |
) |
| 213 |
url = "/api/admin/users" |
| 214 |
user = dict(name=name, email=email, login=login, password=password) |
| 215 |
self._send_request(url, data=user, headers=self.headers, method="POST") |
| 216 |
return self.get_user_from_login(login) |
| 217 |
|
| 218 |
def get_user_from_login(self, login): |
| 219 |
# https://grafana.com/docs/grafana/latest/http_api/user/#get-single-user-by-usernamelogin-or-email |
| 220 |
url = "/api/users/lookup?loginOrEmail={login}".format(login=quote(login)) |
| 221 |
return self._send_request(url, headers=self.headers, method="GET") |
| 222 |
|
| 223 |
def update_user(self, user_id, email, name, login): |
| 224 |
# https://grafana.com/docs/http_api/user/#user-update |
| 225 |
url = "/api/users/{user_id}".format(user_id=user_id) |
| 226 |
user = dict(email=email, name=name, login=login) |
| 227 |
self._send_request(url, data=user, headers=self.headers, method="PUT") |
| 228 |
return self.get_user_from_login(login) |
| 229 |
|
| 230 |
def update_user_permissions(self, user_id, is_admin): |
| 231 |
# https://grafana.com/docs/http_api/admin/#permissions |
| 232 |
url = "/api/admin/users/{user_id}/permissions".format(user_id=user_id) |
| 233 |
permissions = dict(isGrafanaAdmin=is_admin) |
| 234 |
return self._send_request( |
| 235 |
url, data=permissions, headers=self.headers, method="PUT" |
| 236 |
) |
| 237 |
|
| 238 |
def delete_user(self, user_id): |
| 239 |
# https://grafana.com/docs/http_api/admin/#delete-global-user |
| 240 |
url = "/api/admin/users/{user_id}".format(user_id=user_id) |
| 241 |
return self._send_request(url, headers=self.headers, method="DELETE") |
| 242 |
|
| 243 |
|
| 244 |
def is_user_update_required(target_user, email, name, login, is_admin): |
| 245 |
# compare value before in target_user object and param |
| 246 |
target_user_dict = dict( |
| 247 |
email=target_user.get("email"), |
| 248 |
name=target_user.get("name"), |
| 249 |
login=target_user.get("login"), |
| 250 |
is_admin=target_user.get("isGrafanaAdmin"), |
| 251 |
) |
| 252 |
param_dict = dict(email=email, name=name, login=login, is_admin=is_admin) |
| 253 |
return target_user_dict != param_dict |
| 254 |
|
| 255 |
|
| 256 |
def setup_module_object(): |
| 257 |
module = AnsibleModule( |
| 258 |
argument_spec=argument_spec, |
| 259 |
supports_check_mode=False, |
| 260 |
required_if=[ |
| 261 |
["state", "present", ["name", "email"]], |
| 262 |
], |
| 263 |
required_together=base.grafana_required_together(), |
| 264 |
) |
| 265 |
return module |
| 266 |
|
| 267 |
|
| 268 |
argument_spec = base.grafana_argument_spec() |
| 269 |
argument_spec.update( |
| 270 |
state=dict(choices=["present", "absent"], default="present"), |
| 271 |
name=dict(type="str", required=False), |
| 272 |
email=dict(type="str", required=False), |
| 273 |
login=dict(type="str", required=True), |
| 274 |
password=dict(type="str", required=False, no_log=True), |
| 275 |
is_admin=dict(type="bool", default=False), |
| 276 |
) |
| 277 |
argument_spec.pop("grafana_api_key") |
| 278 |
|
| 279 |
|
| 280 |
def main(): |
| 281 |
module = setup_module_object() |
| 282 |
state = module.params["state"] |
| 283 |
name = module.params["name"] |
| 284 |
email = module.params["email"] |
| 285 |
login = module.params["login"] |
| 286 |
password = module.params["password"] |
| 287 |
is_admin = module.params["is_admin"] |
| 288 |
|
| 289 |
grafana_iface = GrafanaUserInterface(module) |
| 290 |
|
| 291 |
# search user by login |
| 292 |
actual_grafana_user = grafana_iface.get_user_from_login(login) |
| 293 |
if state == "present": |
| 294 |
has_changed = False |
| 295 |
|
| 296 |
if actual_grafana_user is None: |
| 297 |
# create new user |
| 298 |
actual_grafana_user = grafana_iface.create_user( |
| 299 |
name, email, login, password |
| 300 |
) |
| 301 |
has_changed = True |
| 302 |
|
| 303 |
if is_user_update_required(actual_grafana_user, email, name, login, is_admin): |
| 304 |
# update found user |
| 305 |
actual_grafana_user_id = actual_grafana_user.get("id") |
| 306 |
if is_admin != actual_grafana_user.get("isGrafanaAdmin"): |
| 307 |
grafana_iface.update_user_permissions(actual_grafana_user_id, is_admin) |
| 308 |
actual_grafana_user = grafana_iface.update_user( |
| 309 |
actual_grafana_user_id, email, name, login |
| 310 |
) |
| 311 |
has_changed = True |
| 312 |
|
| 313 |
module.exit_json(changed=has_changed, user=actual_grafana_user) |
| 314 |
|
| 315 |
elif state == "absent": |
| 316 |
if actual_grafana_user is None: |
| 317 |
module.exit_json(message="No user found, nothing to do") |
| 318 |
result = grafana_iface.delete_user(actual_grafana_user.get("id")) |
| 319 |
module.exit_json(changed=True, message=result.get("message")) |
| 320 |
|
| 321 |
|
| 322 |
if __name__ == "__main__": |
| 323 |
main() |