Fix for bug 1460 Admin dashboard in Grafana not created when using keystone as backend 97/10497/5
authorAtul Agarwal <Atul.Agarwal@Altran.com>
Tue, 16 Mar 2021 11:02:14 +0000 (11:02 +0000)
committerpalsus <subhankar.pal@aricent.com>
Thu, 18 Mar 2021 11:45:45 +0000 (12:45 +0100)
Change-Id: Ia13e86445ba5cef9b5a8b5e9b1507060d345b22e
Signed-off-by: Atul Agarwal <Atul.Agarwal@Altran.com>
osm_mon/dashboarder/backends/grafana.py
osm_mon/dashboarder/service.py

index a1cbcba..73459f7 100644 (file)
@@ -75,12 +75,16 @@ class GrafanaBackend:
                 dashboard_json_data["folderId"] = folder_id
                 dashboard_json_data["overwrite"] = False
 
-            response = requests.request(
-                "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_json_data), headers=self.headers)
+            response = self.send_request_for_creating_dashboard(dashboard_json_data)
 
-            # Don't create the dashboard if already exists.
+            # Admin dashboard will be created if already exists. Rest will remain same.
             if json.loads(response.text).get("status") == "name-exists":
-                return
+                # Delete any previous project-admin dashboard if it already exist.
+                if name == 'admin':
+                    self.delete_admin_dashboard()
+                    response = self.send_request_for_creating_dashboard(dashboard_json_data)
+                else:
+                    return
 
             # Get team id
             if project_name is not None:
@@ -101,11 +105,21 @@ class GrafanaBackend:
         except Exception:
             log.exception("Exception processing message: ")
 
+    def send_request_for_creating_dashboard(self, dashboard_data):
+        response = requests.request(
+            "POST", self.url + "/api/dashboards/db/", data=json.dumps(dashboard_data), headers=self.headers)
+        return response
+
     def delete_dashboard(self, uid):
         response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers)
         log.debug("Dashboard %s deleted from Grafana", uid)
         return response
 
+    def delete_admin_dashboard(self):
+        requests.request(
+            "DELETE", self.url + "/api/dashboards/db/osm-project-status-admin", headers=self.headers)
+        log.debug("Dashboard osm-project-status-admin deleted from Grafana")
+
     def create_grafana_users(self, user):
         email = "{}@osm.etsi.org".format(user)
         user_payload = {
index d791fa9..441f881 100644 (file)
@@ -58,9 +58,10 @@ class DashboarderService:
                 )
             except Exception:
                 log.error('Cannot retrieve projects from keystone')
+        else:
+            projects.extend(self.common_db.get_projects())
 
         # Reads existing project list and creates a dashboard for each
-        projects.extend(self.common_db.get_projects())
         for project in projects:
             project_id = project['_id']
             # Collect Project IDs for periodical dashboard clean-up