Ken'ichi Ohmichi | 6ea3f98 | 2015-11-09 12:41:13 +0000 | [diff] [blame] | 1 | # 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 Frittoli | e1ed695 | 2017-09-14 06:31:52 -0600 | [diff] [blame] | 16 | from tempest import config |
| 17 | from tempest.lib.common import cred_client |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 18 | from tempest.lib import exceptions as lib_exc |
Ken'ichi Ohmichi | 6ea3f98 | 2015-11-09 12:41:13 +0000 | [diff] [blame] | 19 | |
Andrea Frittoli | e1ed695 | 2017-09-14 06:31:52 -0600 | [diff] [blame] | 20 | CONF = config.CONF |
| 21 | |
Ken'ichi Ohmichi | 6ea3f98 | 2015-11-09 12:41:13 +0000 | [diff] [blame] | 22 | |
| 23 | def 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 Ohmichi | d9fed31 | 2015-11-09 13:05:32 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def 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 Frittoli | e1ed695 | 2017-09-14 06:31:52 -0600 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def 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) |