Cleanup of grafana users 25/11125/1 v10.0.2
authorAtul Agarwal <atul.agarwal@altran.com>
Fri, 27 Aug 2021 12:33:57 +0000 (12:33 +0000)
committerpalsus <subhankar.pal@aricent.com>
Sat, 28 Aug 2021 03:08:45 +0000 (05:08 +0200)
Change-Id: Ife6dc3a26dbfa20de4d51b1964c1617f133f9b8d
Signed-off-by: Atul Agarwal <atul.agarwal@altran.com>
(cherry picked from commit 46dc3bd2e07fd6e140267f52df185b7a3ee3d2ca)

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

index f5732bc..1ebb94e 100644 (file)
@@ -68,6 +68,16 @@ class KeystoneConnection:
         """
         return self.keystone_client.projects.list(user=user_id)
 
+    def getUsers(self):
+        """
+        Grabs users from keystone using the client and session build in the constructor
+        """
+        domain_list = self.keystone_client.domains.list()
+        users = []
+        for domain in domain_list:
+            users.extend(self.keystone_client.users.list(domain=domain.id))
+        return users
+
     def getUserById(self, user_id):
         """
         Grabs user object from keystone using user id
index acacf12..e5fde98 100644 (file)
@@ -172,6 +172,20 @@ class GrafanaBackend:
         log.info("New user %s created in Grafana", user)
         return response_users
 
+    # Get Grafana users
+    def get_grafana_users(self):
+        response_users = requests.request(
+            "GET",
+            self.url + "/api/users",
+            headers=self.headers,
+        )
+        user_list = []
+        users = json.loads(response_users.text)
+        for user in users:
+            if user["name"] and user["name"] != "admin":
+                user_list.append(user["name"])
+        return user_list
+
     # Create Grafana team with member
     def create_grafana_teams_members(
         self, project_name, user_name, is_admin, proj_list
index a9c8a57..c3f77b2 100644 (file)
@@ -146,6 +146,7 @@ class Dashboarder:
                 time.sleep(int(self.conf.get("dashboarder", "interval")))
                 continue
             try:
+                self.grafana_cleanup()
                 self.create_dashboards()
                 time.sleep(int(self.conf.get("dashboarder", "interval")))
             except Exception:
@@ -154,3 +155,10 @@ class Dashboarder:
     def create_dashboards(self):
         self.service.create_dashboards()
         log.debug("Dashboarder Service > create_dashboards called!")
+
+    def grafana_cleanup(self):
+        # Cleaning up non existing users from grafana
+        self.service.delete_non_existing_users()
+        # TODO
+        # Cleanup of teams from grafana
+        log.debug("Deleted non existing users from dashbaorder service")
index 48e8150..0a1f947 100644 (file)
@@ -147,6 +147,18 @@ class DashboarderService:
     def create_grafana_user(self, user):
         self.grafana.create_grafana_users(user)
 
+    def delete_non_existing_users(self):
+        if self.keystone:
+            # Get users from keystone
+            users = self.keystone.getUsers()
+            usernames = []
+            for user in users:
+                usernames.append(user.name)
+            grafana_users = self.grafana.get_grafana_users()
+            users_to_be_deleted = list(set(grafana_users) - set(usernames))
+            for grafana_user in users_to_be_deleted:
+                self.grafana.delete_grafana_users(grafana_user)
+
     def create_grafana_team_member(
         self, project_data, userid=None, project_list=None, user=None
     ):