1 # -*- coding: utf-8 -*-
3 # Copyright 2021 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: fbravo@whitestack.com
24 from keystoneauth1
.identity
import v3
25 from keystoneauth1
import session
26 from keystoneclient
.v3
import client
29 class KeystoneConnection
:
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
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(",")
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
51 self
.keystone_session
= session
.Session(auth
=self
.auth
)
52 self
.keystone_client
= client
.Client(session
=self
.keystone_session
, endpoint_override
=self
.auth_url
)
54 def getProjects(self
):
56 Grabs projects from keystone using the client and session build in the constructor
58 return self
.keystone_client
.projects
.list()
60 def getProjectsById(self
, user_id
):
62 Grabs projects filtered by user ID from keystone using the client and session build in the constructor
64 return self
.keystone_client
.projects
.list(user
=user_id
)
66 def getUserById(self
, user_id
):
68 Grabs user object from keystone using user id
70 return self
.keystone_client
.users
.get(user_id
)
72 def getRoleById(self
, role_id
):
74 Grabs role object from keystone using id
76 return self
.keystone_client
.roles
.get(role_id
)
78 def getRoleByName(self
, role
):
80 Grabs role object from keystone using name
82 return self
.keystone_client
.roles
.list(name
=role
)
84 def getProjectsByProjectId(self
, project_id
):
86 Grabs projects object from keystone using id
88 return self
.keystone_client
.projects
.get(project_id
)
90 def getProjectsByProjectName(self
, project
):
92 Grabs projects object from keystone name
94 return self
.keystone_client
.projects
.list(name
=project
)