d79ea89f09c7e2d1387e4f4aaf4898c5e831de02
[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()