| 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)
|
| 65 |
|
| 66 | response = requests.request(
|
| 67 | "POST", self.url + "/api/dashboards/db/", data=dashboard_data, headers=self.headers)
|
| 68 | # get team id
|
| 69 | if project_name is not None:
|
| 70 | name = project_name
|
| 71 | response_team = requests.request(
|
| 72 | "GET", self.url + "/api/teams/search?name={}".format(name), headers=self.headers)
|
| 73 | if len(json.loads(response_team.text)["teams"]) > 0:
|
| 74 | team_id = json.loads(response_team.text)["teams"][0]["id"]
|
| 75 | permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]}
|
| 76 | # provide permission to dashboard
|
| 77 | dahboard_id = json.loads(response.text)["id"]
|
| 78 | response = requests.request(
|
| 79 | "POST", self.url + "/api/dashboards/id/{}/permissions".format(dahboard_id), json=permission_data,
|
| 80 | headers=self.headers)
|
| 81 | return response
|
| 82 | except Exception:
|
| 83 | log.exception("Exception processing message: ")
|
| 84 |
|
| 85 | def delete_dashboard(self, uid):
|
| 86 | response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)
|
| 87 | log.debug("Delete dashboard result: %s", response.text)
|
| 88 | return response
|
| 89 |
|
| 90 | def create_grafana_users(self, user):
|
| 91 | email = "{}@osm.etsi.org".format(user)
|
| 92 | user_payload = {
|
| 93 | "name": user,
|
| 94 | "email": email,
|
| 95 | "login": user,
|
| 96 | "password": user,
|
| 97 | }
|
| 98 | response_users = requests.request("POST", self.url + "/api/admin/users/", json=user_payload,
|
| 99 | headers=self.headers)
|
| 100 | json_data = json.loads(response_users.text)
|
| 101 | url = "/api/org/users/{}/".format(json_data["id"])
|
| 102 | permission_payload = {"role": "Editor", }
|
| 103 | requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)
|
| 104 | log.info("Grafana user created: %s", response_users.text)
|
| 105 | return response_users
|
| 106 |
|
| 107 | # create grafana team with member
|
| 108 | def create_grafana_teams_members(self, project_name, user_name, is_admin, proj_list):
|
| 109 | # check if user exist in grafana or not
|
| 110 | user_response = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
|
| 111 | headers=self.headers)
|
| 112 | user_obj = json.loads(user_response.text)
|
| 113 | if user_response.status_code != 200:
|
| 114 | user_response = self.create_grafana_users(user_name)
|
| 115 | user_obj = json.loads(user_response.text)
|
| 116 |
|
| 117 | user_id = user_obj["id"]
|
| 118 |
|
| 119 | # Get Teams for user
|
| 120 | team_objs = requests.request("GET", self.url + "/api/users/{}/teams".format(user_id), headers=self.headers)
|
| 121 | team_obj = json.loads(team_objs.text)
|
| 122 | team_list = []
|
| 123 | if len(team_obj):
|
| 124 | for team in team_obj:
|
| 125 | team_list.append(team["name"])
|
| 126 |
|
| 127 | proj_unlink = set(team_list) - set(proj_list)
|
| 128 | for prj in proj_unlink:
|
| 129 | response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(prj),
|
| 130 | headers=self.headers)
|
| 131 | team_id = json.loads(response_team.text)["teams"][0]["id"]
|
| 132 | requests.request("DELETE", self.url + "/api/teams/{}/members/{}".format(team_id, user_id),
|
| 133 | headers=self.headers)
|
| 134 | # add member to team
|
| 135 | response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name),
|
| 136 | headers=self.headers)
|
| 137 | team_id = json.loads(response_team.text)["teams"][0]["id"]
|
| 138 | if project_name not in team_list:
|
| 139 | member_payload = {
|
| 140 | "userId": user_id
|
| 141 | }
|
| 142 | requests.request("POST", self.url + "/api/teams/{}/members".format(team_id), json=member_payload,
|
| 143 | headers=self.headers)
|
| 144 | # if role is admin change permission to admin
|
| 145 | if is_admin:
|
| 146 | url = "/api/org/users/{}/".format(user_id)
|
| 147 | permission_payload = {"role": "Admin", }
|
| 148 | requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)
|
| 149 | log.info("Member added to grafana team")
|
| 150 | return response_team
|
| 151 |
|
| 152 | # create grafana team
|
| 153 | def create_grafana_teams(self, team_name):
|
| 154 | team_payload = {"name": team_name, }
|
| 155 | response = requests.request("POST", self.url + "/api/teams", json=team_payload, headers=self.headers)
|
| 156 | log.info("Grafana Team is created: %s", response.text)
|
| 157 |
|
| 158 | def delete_grafana_users(self, user_name):
|
| 159 | # find user id
|
| 160 | response_id = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
|
| 161 | headers=self.headers)
|
| 162 | try:
|
| 163 | user_id = json.loads(response_id.text)["id"]
|
| 164 | except Exception:
|
| 165 | log.exception("Exception processing message: ")
|
| 166 | # delete user
|
| 167 | response = requests.request("DELETE", self.url + "/api/admin/users/{}".format(user_id), headers=self.headers)
|
| 168 | log.info("Grafana user deleted: %s", response.text)
|
| 169 | return response
|
| 170 |
|
| 171 | def delete_grafana_team(self, project_name):
|
| 172 | team_obj = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name),
|
| 173 | headers=self.headers)
|
| 174 | team_id = json.loads(team_obj.text)["teams"][0]["id"]
|
| 175 | response = requests.request("DELETE", self.url + "/api/teams/{}".format(team_id), headers=self.headers)
|
| 176 | log.info("Grafana team deleated: %s", response.text)
|
| 177 | return response
|
| 178 |
|
| 179 | def update_grafana_teams(self, project_new_name, project_old_name):
|
| 180 | team_obj = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_old_name),
|
| 181 | headers=self.headers)
|
| 182 | team_id = json.loads(team_obj.text)["teams"][0]["id"]
|
| 183 | data = {"name": project_new_name, }
|
| 184 | response = requests.request("PUT", self.url + "/api/teams/{}".format(team_id), json=data, headers=self.headers)
|
| 185 | log.info("Grafana team updated %s", response.text)
|
| 186 | return response
|