- def create_grafana_team_member(self, project_data, userid):
- user = self.common_db.get_user_by_id(userid)
- user_name = user["username"]
- proj_list = []
- for project in project_data:
- proj_list.append(project["project"])
- for proj in project_data:
- role_obj = self.common_db.get_role_by_name(proj["role"])
- is_admin = role_obj["permissions"].get("admin")
- self.grafana.create_grafana_teams_members(proj["project"], user_name, is_admin, proj_list)
+ def create_grafana_team_member(self, project_data, userid=None, project_list=None, user=None):
+ if user:
+ user_name = user
+ else:
+ try:
+ # Get user details from commondb
+ user = self.common_db.get_user_by_id(userid)
+ user_name = user["username"]
+ except Exception as e:
+ # User not found in commondb
+ if self.keystone:
+ # Search user in keystone
+ user = self.keystone.getUserById(userid)
+ user_name = user.name
+ else:
+ log.info('User %s not found', userid)
+ log.debug('Exception %s' % e)
+ if project_list:
+ # user-project mapping is done by osm cli
+ for proj in project_data:
+ project = self.common_db.get_project(proj["project"])
+ proj_name = project['name']
+ role_obj = self.common_db.get_role_by_id(proj["role"])
+ is_admin = role_obj["permissions"].get("admin")
+ self.grafana.create_grafana_teams_members(proj_name, user_name, is_admin, project_list)
+ else:
+ # user-project mapping is done by osm ui
+ proj_list = []
+ if self.keystone:
+ users_proj_list = self.keystone.getProjectsById(userid)
+ for project in users_proj_list:
+ proj_list.append(project.name)
+ else:
+ users_proj_list = user.get("project_role_mappings")
+ for project in users_proj_list:
+ proj_data = self.common_db.get_project(project["project"])
+ proj_list.append(proj_data['name'])
+ for proj in project_data:
+ if self.keystone:
+ # Backend authentication type is keystone
+ try:
+ # Getting project and role objects from keystone using ids
+ role_obj = self.keystone.getRoleById(proj["role"])
+ proj_data = self.keystone.getProjectsByProjectId(proj["project"])
+ log.info('role object {} {}'.format(role_obj.permissions, proj_data.name))
+ is_admin = role_obj.permissions['admin']
+ except Exception:
+ # Getting project and role objects from keystone using names
+ role_obj = self.keystone.getRoleByName(proj["role"])[0]
+ proj_data = self.keystone.getProjectsByProjectName(proj["project"])[0]
+ is_admin = role_obj.to_dict().get("permissions").get('admin')
+ log.info('role object {} {}'.format(role_obj.to_dict(), proj_data.name))
+ proj_name = proj_data.name
+ else:
+ # Backend authentication type is internal
+ try:
+ # Getting project and role object from commondb using names
+ role_obj = self.common_db.get_role_by_name(proj["role"])
+ proj_name = proj["project"]
+ except Exception:
+ # Getting project and role object from commondb using ids
+ role_obj = self.common_db.get_role_by_id(proj["role"])
+ proj_data = self.common_db.get_project(proj["project"])
+ proj_name = proj_data['name']
+ is_admin = role_obj["permissions"].get("admin")
+ self.grafana.create_grafana_teams_members(proj_name, user_name, is_admin, proj_list)