blob: 60e4d7c82a18d091adfbea5f0de4e5655b68ec0e [file] [log] [blame]
palsus0ab64072021-02-09 18:23:03 +00001# -*- coding: utf-8 -*-
2
3# Copyright 2018 Whitestack, LLC
4# *************************************************************
5
6# This file is part of OSM Monitoring module
7# All Rights Reserved to Whitestack, LLC
8
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License. You may obtain
11# a copy of the License at
12
13# http://www.apache.org/licenses/LICENSE-2.0
14
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18# License for the specific language governing permissions and limitations
19# under the License.
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact: glavado@whitestack.com or fbravo@whitestack.com
22##
23import logging
24import requests
25import base64
26import json
27from osm_mon.core.config import Config
28
29log = logging.getLogger(__name__)
30
31
32class GrafanaBackend:
33 def __init__(self, config: Config):
34 self.conf = config
garciadeblas8e4179f2021-05-14 16:47:03 +020035 self.url = config.get("grafana", "url")
palsus0ab64072021-02-09 18:23:03 +000036 grafana_user = config.get("grafana", "user")
37 grafana_password = config.get("grafana", "password")
38 self.headers = {
garciadeblas8e4179f2021-05-14 16:47:03 +020039 "content-type": "application/json",
40 "authorization": "Basic %s"
41 % base64.b64encode(
42 (grafana_user + ":" + grafana_password).encode("utf-8")
43 ).decode(),
palsus0ab64072021-02-09 18:23:03 +000044 }
45
46 def get_all_dashboard_uids(self):
47 # Gets only dashboards that were automated by OSM (with tag 'osm_automated')
garciadeblas8e4179f2021-05-14 16:47:03 +020048 response = requests.request(
49 "GET", self.url + "/api/search?tag=osm_automated", headers=self.headers
50 )
palsus0ab64072021-02-09 18:23:03 +000051 dashboards = response.json()
52 dashboard_uids = []
53 for dashboard in dashboards:
garciadeblas8e4179f2021-05-14 16:47:03 +020054 dashboard_uids.append(dashboard["uid"])
palsus0ab64072021-02-09 18:23:03 +000055 log.debug("Searching for all dashboard uids: %s", dashboard_uids)
56 return dashboard_uids
57
Atul Agarwal5d7b0d12021-10-19 17:20:52 +000058 def get_all_datasource_names(self, datasource_name_substr):
59 # Gets only dashboards that were created for prom-operator
60 response = requests.request(
61 "GET", self.url + "/api/datasources", headers=self.headers
62 )
63 datasources = response.json()
64 datasource_names = []
65 for datasource in datasources:
66 if datasource["name"].startswith(datasource_name_substr):
67 datasource_names.append(datasource["name"])
68 log.debug("Searching for all datasource names: %s", datasource_names)
69 return datasource_names
70
palsus0ab64072021-02-09 18:23:03 +000071 def get_dashboard_status(self, uid):
garciadeblas8e4179f2021-05-14 16:47:03 +020072 response = requests.request(
73 "GET", self.url + "/api/dashboards/uid/" + uid, headers=self.headers
74 )
palsus0ab64072021-02-09 18:23:03 +000075 log.debug("Searching for dashboard result: %s", response.text)
76 return response
77
Atul Agarwal5d7b0d12021-10-19 17:20:52 +000078 def create_dashboard(self, uid, name, json_file, project_name=None, datasource_name=None):
palsus0ab64072021-02-09 18:23:03 +000079 try:
80 with open(json_file) as f:
81 dashboard_data = f.read()
82
garciadeblas8e4179f2021-05-14 16:47:03 +020083 dashboard_data = dashboard_data.replace("OSM_ID", uid).replace(
84 "OSM_NAME", name
85 )
Atul Agarwal5d7b0d12021-10-19 17:20:52 +000086 if datasource_name:
87 dashboard_data = dashboard_data.replace("OSM_DATASOURCE_NAME", datasource_name)
Atul Agarwalbc796322021-01-06 13:29:19 +000088 dashboard_json_data = json.loads(dashboard_data)
palsus6e73f232021-02-14 19:06:33 +000089 # Get folder id
Atul Agarwalbc796322021-01-06 13:29:19 +000090 if project_name:
91 folder_name = project_name
92 else:
93 folder_name = name
94 response_folder_id = requests.request(
garciadeblas8e4179f2021-05-14 16:47:03 +020095 "GET",
96 self.url + "/api/folders/{}".format(folder_name),
97 headers=self.headers,
98 )
Atul Agarwalbc796322021-01-06 13:29:19 +000099 if response_folder_id.status_code == 200:
100 folder_id = json.loads(response_folder_id.text)["id"]
101 dashboard_json_data["folderId"] = folder_id
102 dashboard_json_data["overwrite"] = False
palsus0ab64072021-02-09 18:23:03 +0000103
Atul Agarwal7af74e42021-03-16 11:02:14 +0000104 response = self.send_request_for_creating_dashboard(dashboard_json_data)
Atul Agarwalbc796322021-01-06 13:29:19 +0000105
Atul Agarwal7af74e42021-03-16 11:02:14 +0000106 # Admin dashboard will be created if already exists. Rest will remain same.
palsusa1b4bb32021-03-03 20:07:07 +0000107 if json.loads(response.text).get("status") == "name-exists":
Atul Agarwal7af74e42021-03-16 11:02:14 +0000108 # Delete any previous project-admin dashboard if it already exist.
garciadeblas8e4179f2021-05-14 16:47:03 +0200109 if name == "admin":
Atul Agarwal7af74e42021-03-16 11:02:14 +0000110 self.delete_admin_dashboard()
garciadeblas8e4179f2021-05-14 16:47:03 +0200111 response = self.send_request_for_creating_dashboard(
112 dashboard_json_data
113 )
Atul Agarwal7af74e42021-03-16 11:02:14 +0000114 else:
115 return
palsusa1b4bb32021-03-03 20:07:07 +0000116
palsus6e73f232021-02-14 19:06:33 +0000117 # Get team id
palsus0ab64072021-02-09 18:23:03 +0000118 if project_name is not None:
119 name = project_name
120 response_team = requests.request(
garciadeblas8e4179f2021-05-14 16:47:03 +0200121 "GET",
122 self.url + "/api/teams/search?name={}".format(name),
123 headers=self.headers,
124 )
palsus6e73f232021-02-14 19:06:33 +0000125
palsusa1b4bb32021-03-03 20:07:07 +0000126 # Remove default permissions of admin user's dashboard so that it is not visible to non-admin users
127 if len(json.loads(response_team.text)["teams"]) == 0:
palsus6e73f232021-02-14 19:06:33 +0000128 # As team information is not available so it is admin user
129 dahboard_id = json.loads(response.text)["id"]
palsus6e73f232021-02-14 19:06:33 +0000130 requests.request(
garciadeblas8e4179f2021-05-14 16:47:03 +0200131 "POST",
132 self.url + "/api/dashboards/id/{}/permissions".format(dahboard_id),
133 headers=self.headers,
134 )
palsus6e73f232021-02-14 19:06:33 +0000135
Atul Agarwalbc796322021-01-06 13:29:19 +0000136 log.info("Dashboard %s is created in Grafana", name)
palsus0ab64072021-02-09 18:23:03 +0000137 return response
138 except Exception:
139 log.exception("Exception processing message: ")
140
Atul Agarwal5d7b0d12021-10-19 17:20:52 +0000141 def create_datasource(self, datasource_name, datasource_type, datasource_url):
142 try:
143 datasource_data = {
144 "name": datasource_name,
145 "type": datasource_type,
146 "url": datasource_url,
147 "access": "proxy",
148 "readOnly": False,
149 "basicAuth": False
150 }
151 response = requests.request(
152 "POST",
153 self.url + "/api/datasources",
154 data=json.dumps(datasource_data),
155 headers=self.headers,
156 )
157 log.info("Datasource %s is created in Grafana", datasource_name)
158 log.info("************* response: {}".format(response.__dict__))
159 return response
160 except Exception:
161 log.exception("Exception processing request for creating datasource: ")
162
Atul Agarwal7af74e42021-03-16 11:02:14 +0000163 def send_request_for_creating_dashboard(self, dashboard_data):
164 response = requests.request(
garciadeblas8e4179f2021-05-14 16:47:03 +0200165 "POST",
166 self.url + "/api/dashboards/db/",
167 data=json.dumps(dashboard_data),
168 headers=self.headers,
169 )
Atul Agarwal7af74e42021-03-16 11:02:14 +0000170 return response
171
palsus0ab64072021-02-09 18:23:03 +0000172 def delete_dashboard(self, uid):
garciadeblas8e4179f2021-05-14 16:47:03 +0200173 response = requests.request(
174 "DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers
175 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000176 log.debug("Dashboard %s deleted from Grafana", uid)
palsus0ab64072021-02-09 18:23:03 +0000177 return response
178
Atul Agarwal5d7b0d12021-10-19 17:20:52 +0000179 def delete_datasource(self, datasource_name):
180 response = requests.request(
181 "DELETE", self.url + "/api/datasources/name/" + datasource_name, headers=self.headers
182 )
183 log.debug("Datasource %s deleted from Grafana", datasource_name)
184 return response
185
Atul Agarwal7af74e42021-03-16 11:02:14 +0000186 def delete_admin_dashboard(self):
187 requests.request(
garciadeblas8e4179f2021-05-14 16:47:03 +0200188 "DELETE",
189 self.url + "/api/dashboards/db/osm-project-status-admin",
190 headers=self.headers,
191 )
Atul Agarwal7af74e42021-03-16 11:02:14 +0000192 log.debug("Dashboard osm-project-status-admin deleted from Grafana")
193
palsus0ab64072021-02-09 18:23:03 +0000194 def create_grafana_users(self, user):
195 email = "{}@osm.etsi.org".format(user)
196 user_payload = {
197 "name": user,
198 "email": email,
199 "login": user,
200 "password": user,
201 }
garciadeblas8e4179f2021-05-14 16:47:03 +0200202 response_users = requests.request(
203 "POST",
204 self.url + "/api/admin/users/",
205 json=user_payload,
206 headers=self.headers,
207 )
palsus0ab64072021-02-09 18:23:03 +0000208 json_data = json.loads(response_users.text)
209 url = "/api/org/users/{}/".format(json_data["id"])
garciadeblas8e4179f2021-05-14 16:47:03 +0200210 permission_payload = {
211 "role": "Editor",
212 }
213 requests.request(
214 "PATCH", self.url + url, json=permission_payload, headers=self.headers
215 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000216 log.info("New user %s created in Grafana", user)
palsus0ab64072021-02-09 18:23:03 +0000217 return response_users
218
Atul Agarwal46dc3bd2021-08-27 12:33:57 +0000219 # Get Grafana users
220 def get_grafana_users(self):
221 response_users = requests.request(
222 "GET",
223 self.url + "/api/users",
224 headers=self.headers,
225 )
226 user_list = []
227 users = json.loads(response_users.text)
228 for user in users:
229 if user["name"] and user["name"] != "admin":
230 user_list.append(user["name"])
231 return user_list
232
palsus6e73f232021-02-14 19:06:33 +0000233 # Create Grafana team with member
garciadeblas8e4179f2021-05-14 16:47:03 +0200234 def create_grafana_teams_members(
235 self, project_name, user_name, is_admin, proj_list
236 ):
palsus6e73f232021-02-14 19:06:33 +0000237 # Check if user exist in Grafana
garciadeblas8e4179f2021-05-14 16:47:03 +0200238 user_response = requests.request(
239 "GET",
240 self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
241 headers=self.headers,
242 )
palsus0ab64072021-02-09 18:23:03 +0000243 user_obj = json.loads(user_response.text)
244 if user_response.status_code != 200:
245 user_response = self.create_grafana_users(user_name)
246 user_obj = json.loads(user_response.text)
247
248 user_id = user_obj["id"]
249
palsus6e73f232021-02-14 19:06:33 +0000250 # Get teams for user
garciadeblas8e4179f2021-05-14 16:47:03 +0200251 team_objs = requests.request(
252 "GET",
253 self.url + "/api/users/{}/teams".format(user_id),
254 headers=self.headers,
255 )
palsus0ab64072021-02-09 18:23:03 +0000256 team_obj = json.loads(team_objs.text)
257 team_list = []
258 if len(team_obj):
259 for team in team_obj:
260 team_list.append(team["name"])
261
262 proj_unlink = set(team_list) - set(proj_list)
263 for prj in proj_unlink:
garciadeblas8e4179f2021-05-14 16:47:03 +0200264 response_team = requests.request(
265 "GET",
266 self.url + "/api/teams/search?name={}".format(prj),
267 headers=self.headers,
268 )
palsus0ab64072021-02-09 18:23:03 +0000269 team_id = json.loads(response_team.text)["teams"][0]["id"]
garciadeblas8e4179f2021-05-14 16:47:03 +0200270 requests.request(
271 "DELETE",
272 self.url + "/api/teams/{}/members/{}".format(team_id, user_id),
273 headers=self.headers,
274 )
palsus0ab64072021-02-09 18:23:03 +0000275 if project_name != "admin":
palsus6e73f232021-02-14 19:06:33 +0000276 # Add member to team
garciadeblas8e4179f2021-05-14 16:47:03 +0200277 response_team = requests.request(
278 "GET",
279 self.url + "/api/teams/search?name={}".format(project_name),
280 headers=self.headers,
281 )
palsus0ab64072021-02-09 18:23:03 +0000282
palsus6e73f232021-02-14 19:06:33 +0000283 # Search if team in Grafana corresponding to the project already exists
palsus0ab64072021-02-09 18:23:03 +0000284 if not json.loads(response_team.text)["teams"]:
palsus0ab64072021-02-09 18:23:03 +0000285 self.create_grafana_teams(project_name)
garciadeblas8e4179f2021-05-14 16:47:03 +0200286 response_team = requests.request(
287 "GET",
288 self.url + "/api/teams/search?name={}".format(project_name),
289 headers=self.headers,
290 )
palsus0ab64072021-02-09 18:23:03 +0000291 team_id = json.loads(response_team.text)["teams"][0]["id"]
292 if project_name not in team_list:
palsus6e73f232021-02-14 19:06:33 +0000293 # Create a team in Grafana corresponding to the project as it doesn't exist
garciadeblas8e4179f2021-05-14 16:47:03 +0200294 member_payload = {"userId": user_id}
295 requests.request(
296 "POST",
297 self.url + "/api/teams/{}/members".format(team_id),
298 json=member_payload,
299 headers=self.headers,
300 )
palsus6e73f232021-02-14 19:06:33 +0000301 # Check if user role or project name is admin
garciadeblas8e4179f2021-05-14 16:47:03 +0200302 if is_admin or project_name == "admin":
palsus6e73f232021-02-14 19:06:33 +0000303 # Give admin righsts to user
palsus0ab64072021-02-09 18:23:03 +0000304 url = "/api/org/users/{}/".format(user_id)
garciadeblas8e4179f2021-05-14 16:47:03 +0200305 permission_payload = {
306 "role": "Admin",
307 }
308 requests.request(
309 "PATCH", self.url + url, json=permission_payload, headers=self.headers
310 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000311 log.info("User %s is assigned Admin permission", user_name)
312 else:
palsus6e73f232021-02-14 19:06:33 +0000313 # Give editor rights to user
Atul Agarwalbc796322021-01-06 13:29:19 +0000314 url = "/api/org/users/{}/".format(user_id)
garciadeblas8e4179f2021-05-14 16:47:03 +0200315 permission_payload = {
316 "role": "Editor",
317 }
318 requests.request(
319 "PATCH", self.url + url, json=permission_payload, headers=self.headers
320 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000321 log.info("User %s is assigned Editor permission", user_name)
palsus0ab64072021-02-09 18:23:03 +0000322
palsus6e73f232021-02-14 19:06:33 +0000323 # Create team in Grafana
palsus0ab64072021-02-09 18:23:03 +0000324 def create_grafana_teams(self, team_name):
garciadeblas8e4179f2021-05-14 16:47:03 +0200325 team_payload = {
326 "name": team_name,
327 }
328 requests.request(
329 "POST", self.url + "/api/teams", json=team_payload, headers=self.headers
330 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000331 log.info("New team %s created in Grafana", team_name)
332
palsus6e73f232021-02-14 19:06:33 +0000333 # Create folder in Grafana
Atul Agarwalbc796322021-01-06 13:29:19 +0000334 def create_grafana_folders(self, folder_name):
335 folder_payload = {"uid": folder_name, "title": folder_name}
garciadeblas8e4179f2021-05-14 16:47:03 +0200336 requests.request(
337 "POST", self.url + "/api/folders", json=folder_payload, headers=self.headers
338 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000339 log.info("Dashboard folder %s created", folder_name)
palsus0ab64072021-02-09 18:23:03 +0000340
garciadeblas8e4179f2021-05-14 16:47:03 +0200341 response_team = requests.request(
342 "GET",
343 self.url + "/api/teams/search?name={}".format(folder_name),
344 headers=self.headers,
345 )
palsusa1b4bb32021-03-03 20:07:07 +0000346 # Create team if it doesn't already exists
347 if len(json.loads(response_team.text)["teams"]) == 0:
348 self.create_grafana_teams(folder_name)
garciadeblas8e4179f2021-05-14 16:47:03 +0200349 response_team = requests.request(
350 "GET",
351 self.url + "/api/teams/search?name={}".format(folder_name),
352 headers=self.headers,
353 )
palsusa1b4bb32021-03-03 20:07:07 +0000354 # Assign required permission to the team's folder
355 team_id = json.loads(response_team.text)["teams"][0]["id"]
garciadeblas8e4179f2021-05-14 16:47:03 +0200356 permission_data = {
357 "items": [
358 {"teamId": team_id, "permission": 2},
359 ]
360 }
361 requests.request(
362 "POST",
363 self.url + "/api/folders/{}/permissions".format(folder_name),
364 json=permission_data,
365 headers=self.headers,
366 )
palsusa1b4bb32021-03-03 20:07:07 +0000367
Atul Agarwal806fe452021-03-15 09:16:09 +0000368 # delete user from grafana
palsus0ab64072021-02-09 18:23:03 +0000369 def delete_grafana_users(self, user_name):
palsus6e73f232021-02-14 19:06:33 +0000370 # Get user id
garciadeblas8e4179f2021-05-14 16:47:03 +0200371 response_id = requests.request(
372 "GET",
373 self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
374 headers=self.headers,
375 )
palsus0ab64072021-02-09 18:23:03 +0000376 try:
377 user_id = json.loads(response_id.text)["id"]
378 except Exception:
379 log.exception("Exception processing message: ")
palsus6e73f232021-02-14 19:06:33 +0000380 # Delete user
garciadeblas8e4179f2021-05-14 16:47:03 +0200381 response = requests.request(
382 "DELETE",
383 self.url + "/api/admin/users/{}".format(user_id),
384 headers=self.headers,
385 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000386 log.info("User %s deleted in Grafana", user_name)
palsus0ab64072021-02-09 18:23:03 +0000387 return response
388
Atul Agarwal806fe452021-03-15 09:16:09 +0000389 # delete team from grafana
palsus0ab64072021-02-09 18:23:03 +0000390 def delete_grafana_team(self, project_name):
palsus6e73f232021-02-14 19:06:33 +0000391 # Delete Grafana folder
garciadeblas8e4179f2021-05-14 16:47:03 +0200392 requests.request(
393 "DELETE",
394 self.url + "/api/folders/{}".format(project_name),
395 headers=self.headers,
396 )
palsus6e73f232021-02-14 19:06:33 +0000397 # Delete Grafana team
garciadeblas8e4179f2021-05-14 16:47:03 +0200398 team_obj = requests.request(
399 "GET",
400 self.url + "/api/teams/search?name={}".format(project_name),
401 headers=self.headers,
402 )
palsus0ab64072021-02-09 18:23:03 +0000403 team_id = json.loads(team_obj.text)["teams"][0]["id"]
garciadeblas8e4179f2021-05-14 16:47:03 +0200404 response = requests.request(
405 "DELETE", self.url + "/api/teams/{}".format(team_id), headers=self.headers
406 )
Atul Agarwalbc796322021-01-06 13:29:19 +0000407 log.info("Team %s deleted in Grafana", project_name)
palsus0ab64072021-02-09 18:23:03 +0000408 return response
409
Atul Agarwal806fe452021-03-15 09:16:09 +0000410 # update grafana team
palsus0ab64072021-02-09 18:23:03 +0000411 def update_grafana_teams(self, project_new_name, project_old_name):
garciadeblas8e4179f2021-05-14 16:47:03 +0200412 team_obj = requests.request(
413 "GET",
414 self.url + "/api/teams/search?name={}".format(project_old_name),
415 headers=self.headers,
416 )
palsus0ab64072021-02-09 18:23:03 +0000417 team_id = json.loads(team_obj.text)["teams"][0]["id"]
garciadeblas8e4179f2021-05-14 16:47:03 +0200418 data = {
419 "name": project_new_name,
420 }
421 response = requests.request(
422 "PUT",
423 self.url + "/api/teams/{}".format(team_id),
424 json=data,
425 headers=self.headers,
426 )
palsus0ab64072021-02-09 18:23:03 +0000427 log.info("Grafana team updated %s", response.text)
Atul Agarwalbc796322021-01-06 13:29:19 +0000428 return response
Atul Agarwal806fe452021-03-15 09:16:09 +0000429
430 # remove member from grafana team
431 def remove_grafana_team_member(self, user_name, project_data):
432 # Get user id
garciadeblas8e4179f2021-05-14 16:47:03 +0200433 response_id = requests.request(
434 "GET",
435 self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
436 headers=self.headers,
437 )
Atul Agarwal806fe452021-03-15 09:16:09 +0000438 user_id = json.loads(response_id.text)["id"]
439 for project in project_data:
440 # Get team id
garciadeblas8e4179f2021-05-14 16:47:03 +0200441 team_obj = requests.request(
442 "GET",
443 self.url + "/api/teams/search?name={}".format(project["project"]),
444 headers=self.headers,
445 )
Atul Agarwal806fe452021-03-15 09:16:09 +0000446 team_id = json.loads(team_obj.text)["teams"][0]["id"]
garciadeblas8e4179f2021-05-14 16:47:03 +0200447 response = requests.request(
448 "DELETE",
449 self.url + "/api/teams/{}/members/{}".format(team_id, user_id),
450 headers=self.headers,
451 )
Atul Agarwal806fe452021-03-15 09:16:09 +0000452 return response