Fix for bug 1396 - Error creating dashboard with same name in two different users
[osm/MON.git] / osm_mon / dashboarder / service.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: glavado@whitestack.com or fbravo@whitestack.com
22 ##
23 import logging
24
25 from osm_mon.core.common_db import CommonDbClient
26 from osm_mon.core.config import Config
27 from osm_mon.dashboarder.backends.grafana import GrafanaBackend
28 from osm_mon import __path__ as mon_path
29 from osm_mon.dashboarder.utils import find_in_list
30
31 log = logging.getLogger(__name__)
32
33
34 class DashboarderService:
35 def __init__(self, config: Config):
36 self.conf = config
37 self.common_db = CommonDbClient(self.conf)
38 self.grafana = GrafanaBackend(self.conf)
39
40 def create_dashboards(self):
41 # TODO lavado: migrate these methods to mongo change streams
42 # Lists all dashboards and OSM resources for later comparisons
43 dashboard_uids = self.grafana.get_all_dashboard_uids()
44 osm_resource_uids = []
45
46 # Reads existing project list and creates a dashboard for each
47 projects = self.common_db.get_projects()
48 for project in projects:
49 project_id = project['_id']
50 # Collect Project IDs for periodical dashboard clean-up
51 osm_resource_uids.append(project_id)
52 dashboard_path = '{}/dashboarder/templates/project_scoped.json'.format(mon_path[0])
53 if project_id not in dashboard_uids:
54 project_name = project['name']
55 self.grafana.create_grafana_folders(project_name)
56 self.grafana.create_dashboard(project_id, project_name,
57 dashboard_path)
58 log.debug('Created dashboard for Project: %s', project_id)
59 else:
60 log.debug('Dashboard already exists')
61
62 # Reads existing NS list and creates a dashboard for each
63 # TODO lavado: only create for ACTIVE NSRs
64 nsrs = self.common_db.get_nsrs()
65 for nsr in nsrs:
66 nsr_id = nsr['_id']
67 dashboard_path = '{}/dashboarder/templates/ns_scoped.json'.format(mon_path[0])
68 # Collect NS IDs for periodical dashboard clean-up
69 osm_resource_uids.append(nsr_id)
70 # Check if the NSR's VNFDs contain metrics
71 # Only one DF at the moment, support for this feature is comming in the future
72 vnfds_profiles = nsr['nsd']["df"][0]['vnf-profile']
73 for vnf_profile in vnfds_profiles:
74 try:
75 vnfd = self.common_db.get_vnfd_by_id(vnf_profile['vnfd-id'])
76 # If there are metrics, create dashboard (if exists)
77 vdu_found = find_in_list(vnfd["vdu"], lambda a_vdu: "monitoring-parameter" in a_vdu)
78 if vdu_found:
79 if nsr_id not in dashboard_uids:
80 nsr_name = nsr['name']
81 project_id = nsr["_admin"]["projects_read"][0]
82 project_details = self.common_db.get_project(project_id)
83 project_name = project_details["name"]
84 self.grafana.create_dashboard(nsr_id, nsr_name,
85 dashboard_path, project_name)
86 log.debug('Created dashboard for NS: %s', nsr_id)
87 else:
88 log.debug('Dashboard already exists')
89 break
90 else:
91 log.debug('NS does not has metrics')
92 except Exception:
93 log.exception("VNFD is not valid or has been renamed")
94 continue
95
96 # Delete obsolete dashboards
97 for dashboard_uid in dashboard_uids:
98 if dashboard_uid not in osm_resource_uids:
99 self.grafana.delete_dashboard(dashboard_uid)
100 log.debug('Deleted obsolete dashboard: %s', dashboard_uid)
101 else:
102 log.debug('All dashboards in use')
103
104 def create_grafana_user(self, user):
105 self.grafana.create_grafana_users(user)
106
107 def create_grafana_team_member(self, project_data, userid):
108 user = self.common_db.get_user_by_id(userid)
109 user_name = user["username"]
110 proj_list = []
111 for project in project_data:
112 proj_list.append(project["project"])
113 for proj in project_data:
114 role_obj = self.common_db.get_role_by_name(proj["role"])
115 is_admin = role_obj["permissions"].get("admin")
116 self.grafana.create_grafana_teams_members(proj["project"], user_name, is_admin, proj_list)
117
118 def create_grafana_team(self, team_name):
119 self.grafana.create_grafana_teams(team_name)
120
121 def delete_grafana_user(self, user_name):
122 self.grafana.delete_grafana_users(user_name)
123
124 def delete_grafana_team(self, project_name):
125 self.grafana.delete_grafana_team(project_name)
126
127 def update_grafana_team(self, project_new_name, project_old_name):
128 self.grafana.update_grafana_teams(project_new_name, project_old_name)