bug(keystone): dashboards for projects saved in keystone instead of mongoDB. Bug...
[osm/MON.git] / osm_mon / dashboarder / service.py
index 3f83550..32128c2 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 
-# Copyright 2018 Whitestack, LLC
+# Copyright 2021 Whitestack, LLC
 # *************************************************************
 
 # This file is part of OSM Monitoring module
@@ -24,6 +24,7 @@ import logging
 
 from osm_mon.core.common_db import CommonDbClient
 from osm_mon.core.config import Config
+from osm_mon.core.keystone import KeystoneConnection
 from osm_mon.dashboarder.backends.grafana import GrafanaBackend
 from osm_mon import __path__ as mon_path
 from osm_mon.dashboarder.utils import find_in_list
@@ -37,14 +38,29 @@ class DashboarderService:
         self.common_db = CommonDbClient(self.conf)
         self.grafana = GrafanaBackend(self.conf)
 
+        if bool(self.conf.get('keystone', 'enabled')):
+            self.keystone = KeystoneConnection(self.conf)
+        else:
+            self.keystone = None
+
     def create_dashboards(self):
         # TODO lavado: migrate these methods to mongo change streams
         # Lists all dashboards and OSM resources for later comparisons
         dashboard_uids = self.grafana.get_all_dashboard_uids()
         osm_resource_uids = []
+        projects = []
+
+        # Check if keystone is the auth/projects backend and get projects from there
+        if self.keystone:
+            try:
+                projects.extend(
+                    map(lambda project: {'_id': project.id, 'name': project.name}, self.keystone.getProjects())
+                )
+            except Exception:
+                log.error('Cannot retrieve projects from keystone')
 
         # Reads existing project list and creates a dashboard for each
-        projects = self.common_db.get_projects()
+        projects.extend(self.common_db.get_projects())
         for project in projects:
             project_id = project['_id']
             # Collect Project IDs for periodical dashboard clean-up