blob: 6e496d39736713d6935e93135991fe6ffacaeeb3 [file] [log] [blame]
Ken'ichi Ohmichi6ea3f982015-11-09 12:41:13 +00001# Copyright 2015 NEC Corporation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# 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, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Andrea Frittolie1ed6952017-09-14 06:31:52 -060016from tempest import config
17from tempest.lib.common import cred_client
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050018from tempest.lib import exceptions as lib_exc
Ken'ichi Ohmichi6ea3f982015-11-09 12:41:13 +000019
Andrea Frittolie1ed6952017-09-14 06:31:52 -060020CONF = config.CONF
21
Ken'ichi Ohmichi6ea3f982015-11-09 12:41:13 +000022
23def get_tenant_by_name(client, tenant_name):
24 tenants = client.list_tenants()['tenants']
25 for tenant in tenants:
26 if tenant['name'] == tenant_name:
27 return tenant
28 raise lib_exc.NotFound('No such tenant(%s) in %s' % (tenant_name, tenants))
Ken'ichi Ohmichid9fed312015-11-09 13:05:32 +000029
30
31def get_user_by_username(client, tenant_id, username):
32 users = client.list_tenant_users(tenant_id)['users']
33 for user in users:
34 if user['name'] == username:
35 return user
36 raise lib_exc.NotFound('No such user(%s) in %s' % (username, users))
Andrea Frittolie1ed6952017-09-14 06:31:52 -060037
38
39def identity_utils(clients):
40 """A client that abstracts v2 and v3 identity operations.
41
42 This can be used for creating and tearing down projects in tests. It
43 should not be used for testing identity features.
44
45 :param clients: a client manager.
46 :return
47 """
48 if CONF.identity.auth_version == 'v2':
49 client = clients.identity_client
50 users_client = clients.users_client
51 project_client = clients.tenants_client
52 roles_client = clients.roles_client
53 domains_client = None
54 else:
55 client = clients.identity_v3_client
56 users_client = clients.users_v3_client
57 project_client = clients.projects_client
58 roles_client = clients.roles_v3_client
59 domains_client = clients.domains_client
60
61 try:
62 domain = client.auth_provider.credentials.project_domain_name
63 except AttributeError:
64 domain = CONF.auth.default_credentials_domain_name
65
66 return cred_client.get_creds_client(client, project_client,
67 users_client,
68 roles_client,
69 domains_client,
70 project_domain_name=domain)