From: Atul Agarwal Date: Fri, 27 Aug 2021 12:33:57 +0000 (+0000) Subject: Cleanup of grafana users X-Git-Tag: v10.0.2^0 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FMON.git;a=commitdiff_plain;h=98a6cc5f8f38a950bbf7c27b41f5422badb382fa Cleanup of grafana users Change-Id: Ife6dc3a26dbfa20de4d51b1964c1617f133f9b8d Signed-off-by: Atul Agarwal (cherry picked from commit 46dc3bd2e07fd6e140267f52df185b7a3ee3d2ca) --- diff --git a/osm_mon/core/keystone.py b/osm_mon/core/keystone.py index f5732bc..1ebb94e 100644 --- a/osm_mon/core/keystone.py +++ b/osm_mon/core/keystone.py @@ -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 diff --git a/osm_mon/dashboarder/backends/grafana.py b/osm_mon/dashboarder/backends/grafana.py index acacf12..e5fde98 100644 --- a/osm_mon/dashboarder/backends/grafana.py +++ b/osm_mon/dashboarder/backends/grafana.py @@ -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 diff --git a/osm_mon/dashboarder/dashboarder.py b/osm_mon/dashboarder/dashboarder.py index a9c8a57..c3f77b2 100644 --- a/osm_mon/dashboarder/dashboarder.py +++ b/osm_mon/dashboarder/dashboarder.py @@ -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") diff --git a/osm_mon/dashboarder/service.py b/osm_mon/dashboarder/service.py index 48e8150..0a1f947 100644 --- a/osm_mon/dashboarder/service.py +++ b/osm_mon/dashboarder/service.py @@ -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 ):