f389085e1219475b4be992bb72179044a09ae7a5
[osm/MON.git] / osm_mon / core / keystone.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2021 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: fbravo@whitestack.com
22 ##
23
24 from keystoneauth1.identity import v3
25 from keystoneauth1 import session
26 from keystoneclient.v3 import client
27
28
29 class KeystoneConnection:
30 """
31 Object representing a connection with keystone, it's main use is to collect
32 projects and users from the OSM platform stored in keystone instead MongoDB
33 """
34 def __init__(self, config):
35 self.auth_url = config.get('keystone', 'url')
36 self.username = config.get('keystone', 'service_user')
37 self.project_name = config.get('keystone', 'service_project')
38 self.project_domain_name_list = config.get('keystone', 'service_project_domain_name').split(",")
39 self.password = config.get('keystone', 'service_password')
40 self.user_domain_name_list = config.get('keystone', 'domain_name').split(",")
41
42 self.auth = v3.Password(
43 auth_url=self.auth_url,
44 user_domain_name=self.user_domain_name_list[0],
45 username=self.username,
46 password=self.password,
47 project_domain_name=self.project_domain_name_list[0],
48 project_name=self.project_name
49 )
50
51 self.keystone_session = session.Session(auth=self.auth)
52 self.keystone_client = client.Client(session=self.keystone_session, endpoint_override=self.auth_url)
53
54 def getProjects(self):
55 """
56 Grabs projects from keystone using the client and session build in the constructor
57 """
58 return self.keystone_client.projects.list()
59
60 def getProjectsById(self, user_id):
61 """
62 Grabs projects filtered by user ID from keystone using the client and session build in the constructor
63 """
64 return self.keystone_client.projects.list(user=user_id)
65
66 def getUserById(self, user_id):
67 """
68 Grabs user object from keystone using user id
69 """
70 return self.keystone_client.users.get(user_id)
71
72 def getRoleById(self, role_id):
73 """
74 Grabs role object from keystone using id
75 """
76 return self.keystone_client.roles.get(role_id)
77
78 def getRoleByName(self, role):
79 """
80 Grabs role object from keystone using name
81 """
82 return self.keystone_client.roles.list(name=role)
83
84 def getProjectsByProjectId(self, project_id):
85 """
86 Grabs projects object from keystone using id
87 """
88 return self.keystone_client.projects.get(project_id)
89
90 def getProjectsByProjectName(self, project):
91 """
92 Grabs projects object from keystone name
93 """
94 return self.keystone_client.projects.list(name=project)