ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 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 | 9efbe95 | 2015-01-29 12:43:09 +0000 | [diff] [blame] | 16 | from tempest.common import cred_provider |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 17 | from tempest import config |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 18 | from tempest import exceptions |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 19 | from tempest.lib import auth |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 20 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 21 | CONF = config.CONF |
| 22 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 23 | |
| 24 | class Manager(object): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 25 | """Base manager class |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 26 | |
| 27 | Manager objects are responsible for providing a configuration object |
| 28 | and a client object for a test case to use in performing actions. |
| 29 | """ |
| 30 | |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 31 | def __init__(self, credentials): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 32 | """Initialization of base manager class |
| 33 | |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 34 | Credentials to be used within the various client classes managed by the |
| 35 | Manager object must be defined. |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 36 | |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 37 | :param credentials: type Credentials or TestResources |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 38 | """ |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 39 | self.credentials = credentials |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 40 | # Check if passed or default credentials are valid |
| 41 | if not self.credentials.is_valid(): |
| 42 | raise exceptions.InvalidCredentials() |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 43 | self.auth_version = CONF.identity.auth_version |
Andrea Frittoli (andreaf) | f9e0126 | 2015-05-22 10:24:12 -0700 | [diff] [blame] | 44 | # Tenant isolation creates TestResources, but |
| 45 | # PreProvisionedCredentialProvider and some tests create Credentials |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 46 | if isinstance(credentials, cred_provider.TestResources): |
| 47 | creds = self.credentials.credentials |
| 48 | else: |
| 49 | creds = self.credentials |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 50 | # Creates an auth provider for the credentials |
Andrea Frittoli (andreaf) | c625bcf | 2015-10-09 12:09:05 +0100 | [diff] [blame] | 51 | self.auth_provider = get_auth_provider(creds, pre_auth=True) |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 52 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 53 | |
Andrea Frittoli | 9001235 | 2015-02-25 21:58:02 +0000 | [diff] [blame] | 54 | def get_auth_provider_class(credentials): |
| 55 | if isinstance(credentials, auth.KeystoneV3Credentials): |
| 56 | return auth.KeystoneV3AuthProvider, CONF.identity.uri_v3 |
| 57 | else: |
| 58 | return auth.KeystoneV2AuthProvider, CONF.identity.uri |
| 59 | |
| 60 | |
Andrea Frittoli (andreaf) | c625bcf | 2015-10-09 12:09:05 +0100 | [diff] [blame] | 61 | def get_auth_provider(credentials, pre_auth=False): |
Andrea Frittoli | 9001235 | 2015-02-25 21:58:02 +0000 | [diff] [blame] | 62 | default_params = { |
| 63 | 'disable_ssl_certificate_validation': |
| 64 | CONF.identity.disable_ssl_certificate_validation, |
| 65 | 'ca_certs': CONF.identity.ca_certificates_file, |
| 66 | 'trace_requests': CONF.debug.trace_requests |
| 67 | } |
| 68 | if credentials is None: |
| 69 | raise exceptions.InvalidCredentials( |
| 70 | 'Credentials must be specified') |
| 71 | auth_provider_class, auth_url = get_auth_provider_class( |
| 72 | credentials) |
Andrea Frittoli (andreaf) | c625bcf | 2015-10-09 12:09:05 +0100 | [diff] [blame] | 73 | _auth_provider = auth_provider_class(credentials, auth_url, |
| 74 | **default_params) |
| 75 | if pre_auth: |
| 76 | _auth_provider.set_auth() |
| 77 | return _auth_provider |