blob: c97e0d1e624cf556f499ff814d6c0c08b443ef62 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipes051075a2012-04-28 17:39:37 -04002# 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 Frittoli9efbe952015-01-29 12:43:09 +000016from tempest.common import cred_provider
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000017from tempest import config
Jay Pipes051075a2012-04-28 17:39:37 -040018from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050019from tempest.lib import auth
Jay Pipes051075a2012-04-28 17:39:37 -040020
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000021CONF = config.CONF
22
Jay Pipes051075a2012-04-28 17:39:37 -040023
24class Manager(object):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +000025 """Base manager class
Jay Pipes051075a2012-04-28 17:39:37 -040026
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)290b3e12015-10-08 10:25:02 +010031 def __init__(self, credentials):
Ken'ichi Ohmichi2e2ee192015-11-19 09:48:27 +000032 """Initialization of base manager class
33
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010034 Credentials to be used within the various client classes managed by the
35 Manager object must be defined.
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000036
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010037 :param credentials: type Credentials or TestResources
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000038 """
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010039 self.credentials = credentials
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000040 # Check if passed or default credentials are valid
41 if not self.credentials.is_valid():
42 raise exceptions.InvalidCredentials()
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010043 self.auth_version = CONF.identity.auth_version
Andrea Frittoli (andreaf)f9e01262015-05-22 10:24:12 -070044 # Tenant isolation creates TestResources, but
45 # PreProvisionedCredentialProvider and some tests create Credentials
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -040046 if isinstance(credentials, cred_provider.TestResources):
47 creds = self.credentials.credentials
48 else:
49 creds = self.credentials
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000050 # Creates an auth provider for the credentials
Andrea Frittoli (andreaf)c625bcf2015-10-09 12:09:05 +010051 self.auth_provider = get_auth_provider(creds, pre_auth=True)
Jay Pipes051075a2012-04-28 17:39:37 -040052
Andrea Frittolif9cde7e2014-02-18 09:57:04 +000053
Andrea Frittoli90012352015-02-25 21:58:02 +000054def 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)c625bcf2015-10-09 12:09:05 +010061def get_auth_provider(credentials, pre_auth=False):
Andrea Frittoli90012352015-02-25 21:58:02 +000062 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)c625bcf2015-10-09 12:09:05 +010073 _auth_provider = auth_provider_class(credentials, auth_url,
74 **default_params)
75 if pre_auth:
76 _auth_provider.set_auth()
77 return _auth_provider