1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: glavado@whitestack.com or fbravo@whitestack.com
27 from osm_mon
.core
.config
import Config
29 log
= logging
.getLogger(__name__
)
33 def __init__(self
, config
: Config
):
35 self
.url
= config
.get("grafana", "url")
36 grafana_user
= config
.get("grafana", "user")
37 grafana_password
= config
.get("grafana", "password")
39 "content-type": "application/json",
40 "authorization": "Basic %s"
42 (grafana_user
+ ":" + grafana_password
).encode("utf-8")
46 def get_all_dashboard_uids(self
):
47 # Gets only dashboards that were automated by OSM (with tag 'osm_automated')
48 response
= requests
.request(
49 "GET", self
.url
+ "/api/search?tag=osm_automated", headers
=self
.headers
51 dashboards
= response
.json()
53 for dashboard
in dashboards
:
54 dashboard_uids
.append(dashboard
["uid"])
55 log
.debug("Searching for all dashboard uids: %s", dashboard_uids
)
58 def get_dashboard_status(self
, uid
):
59 response
= requests
.request(
60 "GET", self
.url
+ "/api/dashboards/uid/" + uid
, headers
=self
.headers
62 log
.debug("Searching for dashboard result: %s", response
.text
)
65 def create_dashboard(self
, uid
, name
, json_file
, project_name
=None):
67 with
open(json_file
) as f
:
68 dashboard_data
= f
.read()
70 dashboard_data
= dashboard_data
.replace("OSM_ID", uid
).replace(
73 dashboard_json_data
= json
.loads(dashboard_data
)
76 folder_name
= project_name
79 response_folder_id
= requests
.request(
81 self
.url
+ "/api/folders/{}".format(folder_name
),
84 if response_folder_id
.status_code
== 200:
85 folder_id
= json
.loads(response_folder_id
.text
)["id"]
86 dashboard_json_data
["folderId"] = folder_id
87 dashboard_json_data
["overwrite"] = False
89 response
= self
.send_request_for_creating_dashboard(dashboard_json_data
)
91 # Admin dashboard will be created if already exists. Rest will remain same.
92 if json
.loads(response
.text
).get("status") == "name-exists":
93 # Delete any previous project-admin dashboard if it already exist.
95 self
.delete_admin_dashboard()
96 response
= self
.send_request_for_creating_dashboard(
103 if project_name
is not None:
105 response_team
= requests
.request(
107 self
.url
+ "/api/teams/search?name={}".format(name
),
108 headers
=self
.headers
,
111 # Remove default permissions of admin user's dashboard so that it is not visible to non-admin users
112 if len(json
.loads(response_team
.text
)["teams"]) == 0:
113 # As team information is not available so it is admin user
114 dahboard_id
= json
.loads(response
.text
)["id"]
117 self
.url
+ "/api/dashboards/id/{}/permissions".format(dahboard_id
),
118 headers
=self
.headers
,
121 log
.info("Dashboard %s is created in Grafana", name
)
124 log
.exception("Exception processing message: ")
126 def send_request_for_creating_dashboard(self
, dashboard_data
):
127 response
= requests
.request(
129 self
.url
+ "/api/dashboards/db/",
130 data
=json
.dumps(dashboard_data
),
131 headers
=self
.headers
,
135 def delete_dashboard(self
, uid
):
136 response
= requests
.request(
137 "DELETE", self
.url
+ "/api/dashboards/uid/" + uid
, headers
=self
.headers
139 log
.debug("Dashboard %s deleted from Grafana", uid
)
142 def delete_admin_dashboard(self
):
145 self
.url
+ "/api/dashboards/db/osm-project-status-admin",
146 headers
=self
.headers
,
148 log
.debug("Dashboard osm-project-status-admin deleted from Grafana")
150 def create_grafana_users(self
, user
):
151 email
= "{}@osm.etsi.org".format(user
)
158 response_users
= requests
.request(
160 self
.url
+ "/api/admin/users/",
162 headers
=self
.headers
,
164 json_data
= json
.loads(response_users
.text
)
165 url
= "/api/org/users/{}/".format(json_data
["id"])
166 permission_payload
= {
170 "PATCH", self
.url
+ url
, json
=permission_payload
, headers
=self
.headers
172 log
.info("New user %s created in Grafana", user
)
173 return response_users
175 # Create Grafana team with member
176 def create_grafana_teams_members(
177 self
, project_name
, user_name
, is_admin
, proj_list
179 # Check if user exist in Grafana
180 user_response
= requests
.request(
182 self
.url
+ "/api/users/lookup?loginOrEmail={}".format(user_name
),
183 headers
=self
.headers
,
185 user_obj
= json
.loads(user_response
.text
)
186 if user_response
.status_code
!= 200:
187 user_response
= self
.create_grafana_users(user_name
)
188 user_obj
= json
.loads(user_response
.text
)
190 user_id
= user_obj
["id"]
193 team_objs
= requests
.request(
195 self
.url
+ "/api/users/{}/teams".format(user_id
),
196 headers
=self
.headers
,
198 team_obj
= json
.loads(team_objs
.text
)
201 for team
in team_obj
:
202 team_list
.append(team
["name"])
204 proj_unlink
= set(team_list
) - set(proj_list
)
205 for prj
in proj_unlink
:
206 response_team
= requests
.request(
208 self
.url
+ "/api/teams/search?name={}".format(prj
),
209 headers
=self
.headers
,
211 team_id
= json
.loads(response_team
.text
)["teams"][0]["id"]
214 self
.url
+ "/api/teams/{}/members/{}".format(team_id
, user_id
),
215 headers
=self
.headers
,
217 if project_name
!= "admin":
219 response_team
= requests
.request(
221 self
.url
+ "/api/teams/search?name={}".format(project_name
),
222 headers
=self
.headers
,
225 # Search if team in Grafana corresponding to the project already exists
226 if not json
.loads(response_team
.text
)["teams"]:
227 self
.create_grafana_teams(project_name
)
228 response_team
= requests
.request(
230 self
.url
+ "/api/teams/search?name={}".format(project_name
),
231 headers
=self
.headers
,
233 team_id
= json
.loads(response_team
.text
)["teams"][0]["id"]
234 if project_name
not in team_list
:
235 # Create a team in Grafana corresponding to the project as it doesn't exist
236 member_payload
= {"userId": user_id
}
239 self
.url
+ "/api/teams/{}/members".format(team_id
),
241 headers
=self
.headers
,
243 # Check if user role or project name is admin
244 if is_admin
or project_name
== "admin":
245 # Give admin righsts to user
246 url
= "/api/org/users/{}/".format(user_id
)
247 permission_payload
= {
251 "PATCH", self
.url
+ url
, json
=permission_payload
, headers
=self
.headers
253 log
.info("User %s is assigned Admin permission", user_name
)
255 # Give editor rights to user
256 url
= "/api/org/users/{}/".format(user_id
)
257 permission_payload
= {
261 "PATCH", self
.url
+ url
, json
=permission_payload
, headers
=self
.headers
263 log
.info("User %s is assigned Editor permission", user_name
)
265 # Create team in Grafana
266 def create_grafana_teams(self
, team_name
):
271 "POST", self
.url
+ "/api/teams", json
=team_payload
, headers
=self
.headers
273 log
.info("New team %s created in Grafana", team_name
)
275 # Create folder in Grafana
276 def create_grafana_folders(self
, folder_name
):
277 folder_payload
= {"uid": folder_name
, "title": folder_name
}
279 "POST", self
.url
+ "/api/folders", json
=folder_payload
, headers
=self
.headers
281 log
.info("Dashboard folder %s created", folder_name
)
283 response_team
= requests
.request(
285 self
.url
+ "/api/teams/search?name={}".format(folder_name
),
286 headers
=self
.headers
,
288 # Create team if it doesn't already exists
289 if len(json
.loads(response_team
.text
)["teams"]) == 0:
290 self
.create_grafana_teams(folder_name
)
291 response_team
= requests
.request(
293 self
.url
+ "/api/teams/search?name={}".format(folder_name
),
294 headers
=self
.headers
,
296 # Assign required permission to the team's folder
297 team_id
= json
.loads(response_team
.text
)["teams"][0]["id"]
300 {"teamId": team_id
, "permission": 2},
305 self
.url
+ "/api/folders/{}/permissions".format(folder_name
),
306 json
=permission_data
,
307 headers
=self
.headers
,
310 # delete user from grafana
311 def delete_grafana_users(self
, user_name
):
313 response_id
= requests
.request(
315 self
.url
+ "/api/users/lookup?loginOrEmail={}".format(user_name
),
316 headers
=self
.headers
,
319 user_id
= json
.loads(response_id
.text
)["id"]
321 log
.exception("Exception processing message: ")
323 response
= requests
.request(
325 self
.url
+ "/api/admin/users/{}".format(user_id
),
326 headers
=self
.headers
,
328 log
.info("User %s deleted in Grafana", user_name
)
331 # delete team from grafana
332 def delete_grafana_team(self
, project_name
):
333 # Delete Grafana folder
336 self
.url
+ "/api/folders/{}".format(project_name
),
337 headers
=self
.headers
,
339 # Delete Grafana team
340 team_obj
= requests
.request(
342 self
.url
+ "/api/teams/search?name={}".format(project_name
),
343 headers
=self
.headers
,
345 team_id
= json
.loads(team_obj
.text
)["teams"][0]["id"]
346 response
= requests
.request(
347 "DELETE", self
.url
+ "/api/teams/{}".format(team_id
), headers
=self
.headers
349 log
.info("Team %s deleted in Grafana", project_name
)
352 # update grafana team
353 def update_grafana_teams(self
, project_new_name
, project_old_name
):
354 team_obj
= requests
.request(
356 self
.url
+ "/api/teams/search?name={}".format(project_old_name
),
357 headers
=self
.headers
,
359 team_id
= json
.loads(team_obj
.text
)["teams"][0]["id"]
361 "name": project_new_name
,
363 response
= requests
.request(
365 self
.url
+ "/api/teams/{}".format(team_id
),
367 headers
=self
.headers
,
369 log
.info("Grafana team updated %s", response
.text
)
372 # remove member from grafana team
373 def remove_grafana_team_member(self
, user_name
, project_data
):
375 response_id
= requests
.request(
377 self
.url
+ "/api/users/lookup?loginOrEmail={}".format(user_name
),
378 headers
=self
.headers
,
380 user_id
= json
.loads(response_id
.text
)["id"]
381 for project
in project_data
:
383 team_obj
= requests
.request(
385 self
.url
+ "/api/teams/search?name={}".format(project
["project"]),
386 headers
=self
.headers
,
388 team_id
= json
.loads(team_obj
.text
)["teams"][0]["id"]
389 response
= requests
.request(
391 self
.url
+ "/api/teams/{}/members/{}".format(team_id
, user_id
),
392 headers
=self
.headers
,