Refactor keystone endpoint tests to the proper test class.
[snaps.git] / snaps / openstack / utils / keystone_utils.py
1 # Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 import requests
16 from keystoneclient.client import Client
17 from keystoneauth1.identity import v3, v2
18 from keystoneauth1 import session
19 import logging
20
21
22 logger = logging.getLogger('keystone_utils')
23
24 V2_VERSION = 'v2.0'
25
26
27 def get_session_auth(os_creds):
28     """
29     Return the session auth for keystone session
30     :param os_creds: the OpenStack credentials (OSCreds) object
31     :return: the auth
32     """
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)
40     else:
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)
45     return auth
46
47
48 def keystone_session(os_creds):
49     """
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
53     """
54     logger.debug('Retrieving Keystone Session')
55
56     auth = get_session_auth(os_creds)
57
58     req_session = None
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)
63
64
65 def keystone_client(os_creds):
66     """
67     Returns the keystone client
68     :param os_creds: the OpenStack credentials (OSCreds) object
69     :return: the client
70     """
71     return Client(version=os_creds.identity_api_version, session=keystone_session(os_creds))
72
73
74 def get_endpoint(os_creds, service_type, endpoint_type='publicURL'):
75     """
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
81     """
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)
85
86
87 def get_project(keystone=None, os_creds=None, project_name=None):
88     """
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
94     """
95     if not project_name:
96         return None
97
98     if not keystone:
99         if os_creds:
100             keystone = keystone_client(os_creds)
101         else:
102             raise Exception('Cannot lookup project without the proper credentials')
103
104     if keystone.version == V2_VERSION:
105         projects = keystone.tenants.list()
106     else:
107         projects = keystone.projects.list(**{'name': project_name})
108
109     for project in projects:
110         if project.name == project_name:
111             return project
112
113     return None
114
115
116 def create_project(keystone, project_settings):
117     """
118     Creates a project
119     :param keystone: the Keystone client
120     :param project_settings: the project configuration
121     :return:
122     """
123     if keystone.version == V2_VERSION:
124         return keystone.tenants.create(project_settings.name, project_settings.description, project_settings.enabled)
125
126     return keystone.projects.create(project_settings.name, project_settings.domain,
127                                     description=project_settings.description,
128                                     enabled=project_settings.enabled)
129
130
131 def delete_project(keystone, project):
132     """
133     Deletes a project
134     :param keystone: the Keystone clien
135     :param project: the OpenStack project object
136     """
137     if keystone.version == V2_VERSION:
138         keystone.tenants.delete(project)
139     else:
140         keystone.projects.delete(project)
141
142
143 def get_user(keystone, username, project_name=None):
144     """
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)
149     :return:
150     """
151     project = get_project(keystone=keystone, project_name=project_name)
152
153     if project:
154         users = keystone.users.list(tenant_id=project.id)
155     else:
156         users = keystone.users.list()
157
158     for user in users:
159         if user.name == username:
160             return user
161
162     return None
163
164
165 def create_user(keystone, user_settings):
166     """
167     Creates a user
168     :param keystone: the Keystone client
169     :param user_settings: the user configuration
170     :return:
171     """
172     project = None
173     if user_settings.project_name:
174         project = get_project(keystone=keystone, project_name=user_settings.project_name)
175
176     if keystone.version == V2_VERSION:
177         project_id = None
178         if project:
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)
182     else:
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)
189
190
191 def delete_user(keystone, user):
192     """
193     Deletes a user
194     :param keystone: the Keystone client
195     :param user: the OpenStack user object
196     """
197     keystone.users.delete(user)
198
199
200 def create_role(keystone, name):
201     """
202     Creates an OpenStack role
203     :param keystone: the keystone client
204     :param name: the role name
205     :return:
206     """
207     return keystone.roles.create(name)
208
209
210 def delete_role(keystone, role):
211     """
212     Deletes an OpenStack role
213     :param keystone: the keystone client
214     :param role: the role to delete
215     :return:
216     """
217     keystone.roles.delete(role)
218
219
220 def assoc_user_to_project(keystone, role, user, project):
221     """
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
227     :return:
228     """
229     if keystone.version == V2_VERSION:
230         keystone.roles.add_user_role(user, role, tenant=project)
231     else:
232         keystone.roles.grant(role, user=user, project=project)