bug 1435 multiple admin and service dashboards 16/10316/1
authorpalsus <subhankar.pal@aricent.com>
Sun, 14 Feb 2021 19:06:33 +0000 (19:06 +0000)
committerpalsus <subhankar.pal@aricent.com>
Tue, 16 Feb 2021 04:15:48 +0000 (05:15 +0100)
Change-Id: I9f30ffb9b3423ecb3be6d1a974fc7d0da0a99313
Signed-off-by: palsus <subhankar.pal@aricent.com>
(cherry picked from commit 6e73f233234e410f9d117f1a77832636eda87d8b)

osm_mon/dashboarder/backends/grafana.py

index 140a192..ff87507 100644 (file)
@@ -63,7 +63,7 @@ class GrafanaBackend:
 
             dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name)
             dashboard_json_data = json.loads(dashboard_data)
-            # get folder id
+            # Get folder id
             if project_name:
                 folder_name = project_name
             else:
@@ -78,18 +78,29 @@ class GrafanaBackend:
             response = requests.request(
                 "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers)
 
-            # get team id
+            # 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}, ]}
-                # provide permission to dashboard
+                # 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:
+                # 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)
+
             log.info("Dashboard %s is created in Grafana", name)
             return response
         except Exception:
@@ -117,9 +128,9 @@ class GrafanaBackend:
         log.info("New user %s created in Grafana", user)
         return response_users
 
-    # create grafana team with member
+    # Create Grafana team with member
     def create_grafana_teams_members(self, project_name, user_name, is_admin, proj_list):
-        # check if user exist in grafana or not
+        # Check if user exist in Grafana
         user_response = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
                                          headers=self.headers)
         user_obj = json.loads(user_response.text)
@@ -129,7 +140,7 @@ class GrafanaBackend:
 
         user_id = user_obj["id"]
 
-        # Get Teams for user
+        # Get teams for user
         team_objs = requests.request("GET", self.url + "/api/users/{}/teams".format(user_id), headers=self.headers)
         team_obj = json.loads(team_objs.text)
         team_list = []
@@ -145,64 +156,67 @@ class GrafanaBackend:
             requests.request("DELETE", self.url + "/api/teams/{}/members/{}".format(team_id, user_id),
                              headers=self.headers)
         if project_name != "admin":
-            # add member to team
+            # Add member to team
             response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name),
                                              headers=self.headers)
 
+            # Search if team in Grafana corresponding to the project already exists
             if not json.loads(response_team.text)["teams"]:
-                # team doesn't exist in grafana. Creating it first
                 self.create_grafana_teams(project_name)
                 response_team = requests.request("GET", self.url + "/api/teams/search?name={}".format(project_name),
                                                  headers=self.headers)
             team_id = json.loads(response_team.text)["teams"][0]["id"]
             if project_name not in team_list:
+                # Create a team in Grafana corresponding to the project as it doesn't exist
                 member_payload = {
                     "userId": user_id
                 }
                 requests.request("POST", self.url + "/api/teams/{}/members".format(team_id), json=member_payload,
                                  headers=self.headers)
-        # if role is admin change permission to admin
-        if is_admin:
+        # Check if user role or project name is admin
+        if is_admin or project_name == 'admin':
+            # Give admin righsts to user
             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("User %s is assigned Admin permission", user_name)
         else:
+            # Give editor rights to user
             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)
 
-    # create grafana team
+    # Create team in Grafana
     def create_grafana_teams(self, team_name):
         team_payload = {"name": team_name, }
         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
+    # Create folder in Grafana
     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
+        # Get user id
         response_id = requests.request("GET", self.url + "/api/users/lookup?loginOrEmail={}".format(user_name),
                                        headers=self.headers)
         try:
             user_id = json.loads(response_id.text)["id"]
         except Exception:
             log.exception("Exception processing message: ")
-        # delete user
+        # Delete user
         response = requests.request("DELETE", self.url + "/api/admin/users/{}".format(user_id), headers=self.headers)
         log.info("User %s deleted in Grafana", user_name)
         return response
 
     def delete_grafana_team(self, project_name):
-        # delete grafana folder
+        # Delete Grafana folder
         requests.request("DELETE", self.url + "/api/folders/{}".format(project_name),
                          headers=self.headers)
-        # delete grafana team
+        # 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"]