| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 1 | # -*- 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 | ##
|
| 23 | import logging
|
| 24 | import requests
|
| 25 | import base64
|
| 26 | import json
|
| 27 | from osm_mon.core.config import Config
|
| 28 |
|
| 29 | log = logging.getLogger(__name__)
|
| 30 |
|
| 31 |
|
| 32 | class GrafanaBackend:
|
| 33 | def __init__(self, config: Config):
|
| 34 | self.conf = config
|
| 35 | self.url = config.get('grafana', 'url')
|
| 36 | grafana_user = config.get("grafana", "user")
|
| 37 | grafana_password = config.get("grafana", "password")
|
| 38 | self.headers = {
|
| 39 | 'content-type': "application/json",
|
| 40 | 'authorization': "Basic %s" % base64.b64encode(
|
| 41 | (grafana_user + ":" + grafana_password).encode("utf-8")).decode()
|
| 42 | }
|
| 43 |
|
| 44 | def get_all_dashboard_uids(self):
|
| 45 | # Gets only dashboards that were automated by OSM (with tag 'osm_automated')
|
| 46 | response = requests.request("GET", self.url + "/api/search?tag=osm_automated", headers=self.headers)
|
| 47 | dashboards = response.json()
|
| 48 | dashboard_uids = []
|
| 49 | for dashboard in dashboards:
|
| 50 | dashboard_uids.append(dashboard['uid'])
|
| 51 | log.debug("Searching for all dashboard uids: %s", dashboard_uids)
|
| 52 | return dashboard_uids
|
| 53 |
|
| 54 | def get_dashboard_status(self, uid):
|
| 55 | response = requests.request("GET", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)
|
| 56 | log.debug("Searching for dashboard result: %s", response.text)
|
| 57 | return response
|
| 58 |
|
| 59 | def create_dashboard(self, uid, name, json_file, project_name=None):
|
| 60 | try:
|
| 61 | with open(json_file) as f:
|
| 62 | dashboard_data = f.read()
|
| 63 |
|
| 64 | dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name)
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 65 | dashboard_json_data = json.loads(dashboard_data) |
| 66 | # get folder id |
| 67 | if project_name: |
| 68 | folder_name = project_name |
| 69 | else: |
| 70 | folder_name = name |
| 71 | response_folder_id = requests.request( |
| 72 | "GET", self.url + "/api/folders/{}".format(folder_name), headers=self.headers) |
| 73 | if response_folder_id.status_code == 200: |
| 74 | folder_id = json.loads(response_folder_id.text)["id"] |
| 75 | dashboard_json_data["folderId"] = folder_id |
| 76 | dashboard_json_data["overwrite"] = False |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 77 |
|
| 78 | response = requests.request(
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 79 | "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers) |
| 80 | |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 81 | # get team id
|
| 82 | if project_name is not None:
|
| 83 | name = project_name
|
| 84 | response_team = requests.request(
|
| 85 | "GET", self.url + "/api/teams/search?name={}".format(name), headers=self.headers)
|
| 86 | if len(json.loads(response_team.text)["teams"]) > 0:
|
| 87 | team_id = json.loads(response_team.text)["teams"][0]["id"]
|
| 88 | permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]}
|
| 89 | # provide permission to dashboard
|
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 90 | response = requests.request(
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 91 | "POST", self.url + "/api/folders/{}/permissions".format(project_name), json=permission_data, |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 92 | headers=self.headers)
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 93 | log.info("Dashboard %s is created in Grafana", name) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 94 | return response
|
| 95 | except Exception:
|
| 96 | log.exception("Exception processing message: ")
|
| 97 |
|
| 98 | def delete_dashboard(self, uid):
|
| 99 | response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 100 | log.debug("Dashboard %s deleted from Grafana", uid) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 101 | return response
|
| 102 |
|
| 103 | def create_grafana_users(self, user):
|
| 104 | email = "{}@osm.etsi.org".format(user)
|
| 105 | user_payload = {
|
| 106 | "name": user,
|
| 107 | "email": email,
|
| 108 | "login": user,
|
| 109 | "password": user,
|
| 110 | }
|
| 111 | response_users = requests.request("POST", self.url + "/api/admin/users/", json=user_payload,
|
| 112 | headers=self.headers)
|
| 113 | json_data = json.loads(response_users.text)
|
| 114 | url = "/api/org/users/{}/".format(json_data["id"])
|
| 115 | permission_payload = {"role": "Editor", }
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 116 | requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers) |
| 117 | log.info("New user %s created in Grafana", user) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 118 | return response_users
|
| 119 |
|
| 120 | # create grafana team with member
|
| 121 | def create_grafana_teams_members(self, project_name, user_name, is_admin, proj_list):
|
| 122 | # check if user exist in grafana or not
|
| 123 | user_response = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
|
| 124 | headers=self.headers)
|
| 125 | user_obj = json.loads(user_response.text)
|
| 126 | if user_response.status_code != 200:
|
| 127 | user_response = self.create_grafana_users(user_name)
|
| 128 | user_obj = json.loads(user_response.text)
|
| 129 |
|
| 130 | user_id = user_obj["id"]
|
| 131 |
|
| 132 | # Get Teams for user
|
| 133 | team_objs = requests.request("GET", self.url + "/api/users/{}/teams".format(user_id), headers=self.headers)
|
| 134 | team_obj = json.loads(team_objs.text)
|
| 135 | team_list = []
|
| 136 | if len(team_obj):
|
| 137 | for team in team_obj:
|
| 138 | team_list.append(team["name"])
|
| 139 |
|
| 140 | proj_unlink = set(team_list) - set(proj_list)
|
| 141 | for prj in proj_unlink:
|
| 142 | response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(prj),
|
| 143 | headers=self.headers)
|
| 144 | team_id = json.loads(response_team.text)["teams"][0]["id"]
|
| 145 | requests.request("DELETE", self.url + "/api/teams/{}/members/{}".format(team_id, user_id),
|
| 146 | headers=self.headers)
|
| 147 | # add member to team
|
| 148 | response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name),
|
| 149 | headers=self.headers)
|
| 150 | team_id = json.loads(response_team.text)["teams"][0]["id"]
|
| 151 | if project_name not in team_list:
|
| 152 | member_payload = {
|
| 153 | "userId": user_id
|
| 154 | }
|
| 155 | requests.request("POST", self.url + "/api/teams/{}/members".format(team_id), json=member_payload,
|
| 156 | headers=self.headers)
|
| 157 | # if role is admin change permission to admin
|
| 158 | if is_admin:
|
| 159 | url = "/api/org/users/{}/".format(user_id)
|
| 160 | permission_payload = {"role": "Admin", }
|
| 161 | requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 162 | log.info("User %s is assigned Admin permission", user_name) |
| 163 | else: |
| 164 | url = "/api/org/users/{}/".format(user_id) |
| 165 | permission_payload = {"role": "Editor", } |
| 166 | requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers) |
| 167 | log.info("User %s is assigned Editor permission", user_name) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 168 | return response_team
|
| 169 |
|
| 170 | # create grafana team
|
| 171 | def create_grafana_teams(self, team_name):
|
| 172 | team_payload = {"name": team_name, }
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 173 | requests.request("POST", self.url + "/api/teams", json=team_payload, headers=self.headers) |
| 174 | log.info("New team %s created in Grafana", team_name) |
| 175 | |
| 176 | # create grafana folder |
| 177 | def create_grafana_folders(self, folder_name): |
| 178 | folder_payload = {"uid": folder_name, "title": folder_name} |
| 179 | requests.request("POST", self.url + "/api/folders", json=folder_payload, headers=self.headers) |
| 180 | log.info("Dashboard folder %s created", folder_name) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 181 |
|
| 182 | def delete_grafana_users(self, user_name):
|
| 183 | # find user id
|
| 184 | response_id = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
|
| 185 | headers=self.headers)
|
| 186 | try:
|
| 187 | user_id = json.loads(response_id.text)["id"]
|
| 188 | except Exception:
|
| 189 | log.exception("Exception processing message: ")
|
| 190 | # delete user
|
| 191 | response = requests.request("DELETE", self.url + "/api/admin/users/{}".format(user_id), headers=self.headers)
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 192 | log.info("User %s deleted in Grafana", user_name) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 193 | return response
|
| 194 |
|
| 195 | def delete_grafana_team(self, project_name):
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 196 | # delete grafana folder |
| 197 | requests.request("DELETE", self.url + "/api/folders/{}".format(project_name), |
| 198 | headers=self.headers) |
| 199 | # delete grafana team |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 200 | team_obj = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name),
|
| 201 | headers=self.headers)
|
| 202 | team_id = json.loads(team_obj.text)["teams"][0]["id"]
|
| 203 | response = requests.request("DELETE", self.url + "/api/teams/{}".format(team_id), headers=self.headers)
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 204 | log.info("Team %s deleted in Grafana", project_name) |
| agarwalat | 85a9185 | 2020-10-08 12:24:47 +0000 | [diff] [blame] | 205 | return response
|
| 206 |
|
| 207 | def update_grafana_teams(self, project_new_name, project_old_name):
|
| 208 | team_obj = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_old_name),
|
| 209 | headers=self.headers)
|
| 210 | team_id = json.loads(team_obj.text)["teams"][0]["id"]
|
| 211 | data = {"name": project_new_name, }
|
| 212 | response = requests.request("PUT", self.url + "/api/teams/{}".format(team_id), json=data, headers=self.headers)
|
| 213 | log.info("Grafana team updated %s", response.text)
|
| Atul Agarwal | bc79632 | 2021-01-06 13:29:19 +0000 | [diff] [blame^] | 214 | return response |