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
35 def __init__(self
, config
):
36 self
.auth_url
= config
.get("keystone", "url")
37 self
.username
= config
.get("keystone", "service_user")
38 self
.project_name
= config
.get("keystone", "service_project")
39 self
.project_domain_name_list
= config
.get(
40 "keystone", "service_project_domain_name"
42 self
.password
= config
.get("keystone", "service_password")
43 self
.user_domain_name_list
= config
.get("keystone", "domain_name").split(",")
45 self
.auth
= v3
.Password(
46 auth_url
=self
.auth_url
,
47 user_domain_name
=self
.user_domain_name_list
[0],
48 username
=self
.username
,
49 password
=self
.password
,
50 project_domain_name
=self
.project_domain_name_list
[0],
51 project_name
=self
.project_name
,
54 self
.keystone_session
= session
.Session(auth
=self
.auth
)
55 self
.keystone_client
= client
.Client(
56 session
=self
.keystone_session
, endpoint_override
=self
.auth_url
59 def getProjects(self
):
61 Grabs projects from keystone using the client and session build in the constructor
63 return self
.keystone_client
.projects
.list()
65 def getProjectsById(self
, user_id
):
67 Grabs projects filtered by user ID from keystone using the client and session build in the constructor
69 return self
.keystone_client
.projects
.list(user
=user_id
)
73 Grabs users from keystone using the client and session build in the constructor
75 domain_list
= self
.keystone_client
.domains
.list()
77 for domain
in domain_list
:
78 users
.extend(self
.keystone_client
.users
.list(domain
=domain
.id))
81 def getUserById(self
, user_id
):
83 Grabs user object from keystone using user id
85 return self
.keystone_client
.users
.get(user_id
)
87 def getRoleById(self
, role_id
):
89 Grabs role object from keystone using id
91 return self
.keystone_client
.roles
.get(role_id
)
93 def getRoleByName(self
, role
):
95 Grabs role object from keystone using name
97 return self
.keystone_client
.roles
.list(name
=role
)
99 def getProjectsByProjectId(self
, project_id
):
101 Grabs projects object from keystone using id
103 return self
.keystone_client
.projects
.get(project_id
)
105 def getProjectsByProjectName(self
, project
):
107 Grabs projects object from keystone name
109 return self
.keystone_client
.projects
.list(name
=project
)