1 # Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from keystoneclient.client import Client
17 from keystoneauth1.identity import v3, v2
18 from keystoneauth1 import session
22 logger = logging.getLogger('keystone_utils')
27 def get_session_auth(os_creds):
29 Return the session auth for keystone session
30 :param os_creds: the OpenStack credentials (OSCreds) object
33 if os_creds.identity_api_version == 3:
34 auth = v3.Password(auth_url=os_creds.auth_url,
35 username=os_creds.username,
36 password=os_creds.password,
37 project_name=os_creds.project_name,
38 user_domain_id=os_creds.user_domain_id,
39 project_domain_id=os_creds.project_domain_id)
41 auth = v2.Password(auth_url=os_creds.auth_url,
42 username=os_creds.username,
43 password=os_creds.password,
44 tenant_name=os_creds.project_name)
48 def keystone_session(os_creds):
50 Creates a keystone session used for authenticating OpenStack clients
51 :param os_creds: The connection credentials to the OpenStack API
52 :return: the client object
54 logger.debug('Retrieving Keystone Session')
56 auth = get_session_auth(os_creds)
59 if os_creds.proxy_settings:
60 req_session = requests.Session()
61 req_session.proxies = {'http': os_creds.proxy_settings.host + ':' + os_creds.proxy_settings.port}
62 return session.Session(auth=auth, session=req_session)
65 def keystone_client(os_creds):
67 Returns the keystone client
68 :param os_creds: the OpenStack credentials (OSCreds) object
71 return Client(version=os_creds.identity_api_version, session=keystone_session(os_creds))
74 def get_endpoint(os_creds, service_type, endpoint_type='publicURL'):
76 Returns the endpoint of specific service
77 :param os_creds: the OpenStack credentials (OSCreds) object
78 :param service_type: the type of specific service
79 :param endpoint_type: the type of endpoint
80 :return: the endpoint url
82 auth = get_session_auth(os_creds)
83 key_session = keystone_session(os_creds)
84 return key_session.get_endpoint(auth=auth, service_type=service_type, endpoint_type=endpoint_type)
87 def get_project(keystone=None, os_creds=None, project_name=None):
89 Returns the first project object or None if not found
90 :param keystone: the Keystone client
91 :param os_creds: the OpenStack credentials used to obtain the Keystone client if the keystone parameter is None
92 :param project_name: the name to query
93 :return: the ID or None
100 keystone = keystone_client(os_creds)
102 raise Exception('Cannot lookup project without the proper credentials')
104 if keystone.version == V2_VERSION:
105 projects = keystone.tenants.list()
107 projects = keystone.projects.list(**{'name': project_name})
109 for project in projects:
110 if project.name == project_name:
116 def create_project(keystone, project_settings):
119 :param keystone: the Keystone client
120 :param project_settings: the project configuration
123 if keystone.version == V2_VERSION:
124 return keystone.tenants.create(project_settings.name, project_settings.description, project_settings.enabled)
126 return keystone.projects.create(project_settings.name, project_settings.domain,
127 description=project_settings.description,
128 enabled=project_settings.enabled)
131 def delete_project(keystone, project):
134 :param keystone: the Keystone clien
135 :param project: the OpenStack project object
137 if keystone.version == V2_VERSION:
138 keystone.tenants.delete(project)
140 keystone.projects.delete(project)
143 def get_user(keystone, username, project_name=None):
145 Returns a user for a given name and optionally project
146 :param keystone: the keystone client
147 :param username: the username to lookup
148 :param project_name: the associated project (optional)
151 project = get_project(keystone=keystone, project_name=project_name)
154 users = keystone.users.list(tenant_id=project.id)
156 users = keystone.users.list()
159 if user.name == username:
165 def create_user(keystone, user_settings):
168 :param keystone: the Keystone client
169 :param user_settings: the user configuration
173 if user_settings.project_name:
174 project = get_project(keystone=keystone, project_name=user_settings.project_name)
176 if keystone.version == V2_VERSION:
179 project_id = project.id
180 return keystone.users.create(name=user_settings.name, password=user_settings.password,
181 email=user_settings.email, tenant_id=project_id, enabled=user_settings.enabled)
183 # TODO - need to support groups
184 return keystone.users.create(name=user_settings.name, password=user_settings.password,
185 email=user_settings.email, project=project,
186 # email=user_settings.email, project=project, group='default',
187 domain=user_settings.domain_name,
188 enabled=user_settings.enabled)
191 def delete_user(keystone, user):
194 :param keystone: the Keystone client
195 :param user: the OpenStack user object
197 keystone.users.delete(user)
200 def create_role(keystone, name):
202 Creates an OpenStack role
203 :param keystone: the keystone client
204 :param name: the role name
207 return keystone.roles.create(name)
210 def delete_role(keystone, role):
212 Deletes an OpenStack role
213 :param keystone: the keystone client
214 :param role: the role to delete
217 keystone.roles.delete(role)
220 def assoc_user_to_project(keystone, role, user, project):
222 Adds a user to a project
223 :param keystone: the Keystone client
224 :param role: the role used to join a project/user
225 :param user: the user to add to the project
226 :param project: the project to which to add a user
229 if keystone.version == V2_VERSION:
230 keystone.roles.add_user_role(user, role, tenant=project)
232 keystone.roles.grant(role, user=user, project=project)