Fix for bug 1460 admin dashboard creation error if already exists 48/10448/6
authorpalsus <subhankar.pal@aricent.com>
Wed, 3 Mar 2021 20:07:07 +0000 (20:07 +0000)
committerpalsus <subhankar.pal@aricent.com>
Thu, 4 Mar 2021 10:52:53 +0000 (11:52 +0100)
Change-Id: Ib068bd2f30cb15baa939ef7cd380f29836335722
Signed-off-by: palsus <subhankar.pal@aricent.com>
osm_mon/dashboarder/backends/grafana.py

index ff87507..eeb5e75 100644 (file)
@@ -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),