Fix for bug 1396 - Error creating dashboard with same name in two different users 72/10172/1
authorAtul Agarwal <atul.agarwal@altran.com>
Wed, 6 Jan 2021 13:29:19 +0000 (13:29 +0000)
committerpalsus <subhankar.pal@aricent.com>
Tue, 12 Jan 2021 14:07:40 +0000 (15:07 +0100)
Change-Id: I9c8508f1df10ff8bfcc405fc3626924cb2b78ba5
Signed-off-by: Atul Agarwal <atul.agarwal@altran.com>
(cherry picked from commit bc79632385cfd40077c89fa3493db646c0f2bb34)

osm_mon/dashboarder/backends/grafana.py
osm_mon/dashboarder/dashboarder.py
osm_mon/dashboarder/service.py

index 6fb6b94..c38a844 100644 (file)
@@ -62,9 +62,22 @@ class GrafanaBackend:
                 dashboard_data = f.read()\r
 \r
             dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name)\r
+            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
 \r
             response = requests.request(\r
-                "POST", self.url + "/api/dashboards/db/", data=dashboard_data, headers=self.headers)\r
+                "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers)
+
             # get team id\r
             if project_name is not None:\r
                 name = project_name\r
@@ -74,17 +87,17 @@ class GrafanaBackend:
                 team_id = json.loads(response_team.text)["teams"][0]["id"]\r
                 permission_data = {"items": [{"teamId": team_id, "permission": 2}, ]}\r
                 # provide permission to dashboard\r
-                dahboard_id = json.loads(response.text)["id"]\r
                 response = requests.request(\r
-                    "POST", self.url + "/api/dashboards/id/{}/permissions".format(dahboard_id), json=permission_data,\r
+                    "POST", self.url + "/api/folders/{}/permissions".format(project_name), json=permission_data,
                     headers=self.headers)\r
+            log.info("Dashboard %s is created in Grafana", name)
             return response\r
         except Exception:\r
             log.exception("Exception processing message: ")\r
 \r
     def delete_dashboard(self, uid):\r
         response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)\r
-        log.debug("Delete dashboard result: %s", response.text)\r
+        log.debug("Dashboard %s deleted from Grafana", uid)
         return response\r
 \r
     def create_grafana_users(self, user):\r
@@ -100,8 +113,8 @@ class GrafanaBackend:
         json_data = json.loads(response_users.text)\r
         url = "/api/org/users/{}/".format(json_data["id"])\r
         permission_payload = {"role": "Editor", }\r
-        requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)\r
-        log.info("Grafana user created: %s", response_users.text)\r
+        requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)
+        log.info("New user %s created in Grafana", user)
         return response_users\r
 \r
     # create grafana team with member\r
@@ -146,14 +159,25 @@ class GrafanaBackend:
             url = "/api/org/users/{}/".format(user_id)\r
             permission_payload = {"role": "Admin", }\r
             requests.request("PATCH", self.url + url, json=permission_payload, headers=self.headers)\r
-        log.info("Member added to grafana team")\r
+            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\r
 \r
     # create grafana team\r
     def create_grafana_teams(self, team_name):\r
         team_payload = {"name": team_name, }\r
-        response = requests.request("POST", self.url + "/api/teams", json=team_payload, headers=self.headers)\r
-        log.info("Grafana Team is created: %s", response.text)\r
+        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)
 \r
     def delete_grafana_users(self, user_name):\r
         # find user id\r
@@ -165,15 +189,19 @@ class GrafanaBackend:
             log.exception("Exception processing message: ")\r
         # delete user\r
         response = requests.request("DELETE", self.url + "/api/admin/users/{}".format(user_id), headers=self.headers)\r
-        log.info("Grafana user deleted: %s", response.text)\r
+        log.info("User %s deleted in Grafana", user_name)
         return response\r
 \r
     def delete_grafana_team(self, project_name):\r
+        # 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),\r
                                     headers=self.headers)\r
         team_id = json.loads(team_obj.text)["teams"][0]["id"]\r
         response = requests.request("DELETE", self.url + "/api/teams/{}".format(team_id), headers=self.headers)\r
-        log.info("Grafana team deleated: %s", response.text)\r
+        log.info("Team %s deleted in Grafana", project_name)
         return response\r
 \r
     def update_grafana_teams(self, project_new_name, project_old_name):\r
@@ -183,4 +211,4 @@ class GrafanaBackend:
         data = {"name": project_new_name, }\r
         response = requests.request("PUT", self.url + "/api/teams/{}".format(team_id), json=data, headers=self.headers)\r
         log.info("Grafana team updated %s", response.text)\r
-        return response\r
+        return response
index cab9a68..22aa1bf 100644 (file)
@@ -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"]
index 10508e4..3f83550 100644 (file)
@@ -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)