From a6bce214e3e96282daa5b9a092ddb92c2e60962c Mon Sep 17 00:00:00 2001 From: palsus Date: Wed, 3 Mar 2021 20:07:07 +0000 Subject: [PATCH] Fix for bug 1460 admin dashboard creation error if already exists Change-Id: Ib068bd2f30cb15baa939ef7cd380f29836335722 Signed-off-by: palsus (cherry picked from commit a1b4bb39a12821ac16957c2490b02253739a9987) --- osm_mon/dashboarder/backends/grafana.py | 30 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/osm_mon/dashboarder/backends/grafana.py b/osm_mon/dashboarder/backends/grafana.py index ff87507..eeb5e75 100644 --- a/osm_mon/dashboarder/backends/grafana.py +++ b/osm_mon/dashboarder/backends/grafana.py @@ -78,25 +78,20 @@ class GrafanaBackend: response = requests.request( "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers) + # Don't create the dashboard if already exists. + if json.loads(response.text).get("status") == "name-exists": + return + # Get team id if project_name is not None: name = project_name response_team = requests.request( "GET", self.url + "/api/teams/search?name={}".format(name), headers=self.headers) - # Check if team informaiton is available - if len(json.loads(response_team.text)["teams"]) > 0: - # As team information is available so it is a non-admin user - team_id = json.loads(response_team.text)["teams"][0]["id"] - permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]} - # Give required permission to the team's folder - response = requests.request( - "POST", self.url + "/api/folders/{}/permissions".format(project_name), json=permission_data, - headers=self.headers) - else: + # Remove default permissions of admin user's dashboard so that it is not visible to non-admin users + if len(json.loads(response_team.text)["teams"]) == 0: # As team information is not available so it is admin user dahboard_id = json.loads(response.text)["id"] - # Remove dashboard's default permissions so that it is only visible to admin user requests.request( "POST", self.url + "/api/dashboards/id/{}/permissions".format(dahboard_id), headers=self.headers) @@ -199,6 +194,19 @@ class GrafanaBackend: requests.request("POST", self.url + "/api/folders", json=folder_payload, headers=self.headers) log.info("Dashboard folder %s created", folder_name) + response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(folder_name), + headers=self.headers) + # Create team if it doesn't already exists + if len(json.loads(response_team.text)["teams"]) == 0: + self.create_grafana_teams(folder_name) + response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(folder_name), + headers=self.headers) + # Assign required permission to the team's folder + team_id = json.loads(response_team.text)["teams"][0]["id"] + permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]} + requests.request("POST", self.url + "/api/folders/{}/permissions".format(folder_name), + json=permission_data, headers=self.headers) + def delete_grafana_users(self, user_name): # Get user id response_id = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name), -- 2.25.1