From bc79632385cfd40077c89fa3493db646c0f2bb34 Mon Sep 17 00:00:00 2001 From: Atul Agarwal Date: Wed, 6 Jan 2021 13:29:19 +0000 Subject: [PATCH] Fix for bug 1396 - Error creating dashboard with same name in two different users Change-Id: I9c8508f1df10ff8bfcc405fc3626924cb2b78ba5 Signed-off-by: Atul Agarwal --- osm_mon/dashboarder/backends/grafana.py | 52 +++++++++++++++++++------ osm_mon/dashboarder/dashboarder.py | 1 - osm_mon/dashboarder/service.py | 1 + 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/osm_mon/dashboarder/backends/grafana.py b/osm_mon/dashboarder/backends/grafana.py index 6fb6b94..c38a844 100644 --- a/osm_mon/dashboarder/backends/grafana.py +++ b/osm_mon/dashboarder/backends/grafana.py @@ -62,9 +62,22 @@ class GrafanaBackend: dashboard_data = f.read() dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name) + dashboard_json_data = json.loads(dashboard_data) + # get folder id + if project_name: + folder_name = project_name + else: + folder_name = name + response_folder_id = requests.request( + "GET", self.url + "/api/folders/{}".format(folder_name), headers=self.headers) + if response_folder_id.status_code == 200: + folder_id = json.loads(response_folder_id.text)["id"] + dashboard_json_data["folderId"] = folder_id + dashboard_json_data["overwrite"] = False response = requests.request( - "POST", self.url + "/api/dashboards/db/", data=dashboard_data, headers=self.headers) + "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers) + # get team id if project_name is not None: name = project_name @@ -74,17 +87,17 @@ class GrafanaBackend: team_id = json.loads(response_team.text)["teams"][0]["id"] permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]} # provide permission to dashboard - dahboard_id = json.loads(response.text)["id"] response = requests.request( - "POST", self.url + "/api/dashboards/id/{}/permissions".format(dahboard_id), json=permission_data, + "POST", self.url + "/api/folders/{}/permissions".format(project_name), json=permission_data, headers=self.headers) + log.info("Dashboard %s is created in Grafana", name) return response except Exception: log.exception("Exception processing message: ") def delete_dashboard(self, uid): response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers) - log.debug("Delete dashboard result: %s", response.text) + log.debug("Dashboard %s deleted from Grafana", uid) return response def create_grafana_users(self, user): @@ -100,8 +113,8 @@ class GrafanaBackend: json_data = json.loads(response_users.text) url = "/api/org/users/{}/".format(json_data["id"]) permission_payload = {"role": "Editor", } - requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers) - log.info("Grafana user created: %s", response_users.text) + requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers) + log.info("New user %s created in Grafana", user) return response_users # create grafana team with member @@ -146,14 +159,25 @@ class GrafanaBackend: url = "/api/org/users/{}/".format(user_id) permission_payload = {"role": "Admin", } requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers) - log.info("Member added to grafana team") + log.info("User %s is assigned Admin permission", user_name) + else: + url = "/api/org/users/{}/".format(user_id) + permission_payload = {"role": "Editor", } + requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers) + log.info("User %s is assigned Editor permission", user_name) return response_team # create grafana team def create_grafana_teams(self, team_name): team_payload = {"name": team_name, } - response = requests.request("POST", self.url + "/api/teams", json=team_payload, headers=self.headers) - log.info("Grafana Team is created: %s", response.text) + requests.request("POST", self.url + "/api/teams", json=team_payload, headers=self.headers) + log.info("New team %s created in Grafana", team_name) + + # create grafana folder + def create_grafana_folders(self, folder_name): + folder_payload = {"uid": folder_name, "title": folder_name} + requests.request("POST", self.url + "/api/folders", json=folder_payload, headers=self.headers) + log.info("Dashboard folder %s created", folder_name) def delete_grafana_users(self, user_name): # find user id @@ -165,15 +189,19 @@ class GrafanaBackend: log.exception("Exception processing message: ") # delete user response = requests.request("DELETE", self.url + "/api/admin/users/{}".format(user_id), headers=self.headers) - log.info("Grafana user deleted: %s", response.text) + log.info("User %s deleted in Grafana", user_name) return response def delete_grafana_team(self, project_name): + # delete grafana folder + requests.request("DELETE", self.url + "/api/folders/{}".format(project_name), + headers=self.headers) + # delete grafana team team_obj = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name), headers=self.headers) team_id = json.loads(team_obj.text)["teams"][0]["id"] response = requests.request("DELETE", self.url + "/api/teams/{}".format(team_id), headers=self.headers) - log.info("Grafana team deleated: %s", response.text) + log.info("Team %s deleted in Grafana", project_name) return response def update_grafana_teams(self, project_new_name, project_old_name): @@ -183,4 +211,4 @@ class GrafanaBackend: data = {"name": project_new_name, } response = requests.request("PUT", self.url + "/api/teams/{}".format(team_id), json=data, headers=self.headers) log.info("Grafana team updated %s", response.text) - return response + return response diff --git a/osm_mon/dashboarder/dashboarder.py b/osm_mon/dashboarder/dashboarder.py index cab9a68..22aa1bf 100644 --- a/osm_mon/dashboarder/dashboarder.py +++ b/osm_mon/dashboarder/dashboarder.py @@ -60,7 +60,6 @@ class Dashboarder: log.debug("Received message from kafka for deleting user") user = values['username'] self.service.delete_grafana_user(user) - log.info("Grafana user deleted: %s", user) elif topic == "users" and key == "edited": log.debug("Received message from kafka for associating user to team") user_id = values["_id"] diff --git a/osm_mon/dashboarder/service.py b/osm_mon/dashboarder/service.py index 10508e4..3f83550 100644 --- a/osm_mon/dashboarder/service.py +++ b/osm_mon/dashboarder/service.py @@ -52,6 +52,7 @@ class DashboarderService: dashboard_path = '{}/dashboarder/templates/project_scoped.json'.format(mon_path[0]) if project_id not in dashboard_uids: project_name = project['name'] + self.grafana.create_grafana_folders(project_name) self.grafana.create_dashboard(project_id, project_name, dashboard_path) log.debug('Created dashboard for Project: %s', project_id) -- 2.25.1