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 | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 16 | from tempest import auth |
| 17 | from tempest import config |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 18 | from tempest import exceptions |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 19 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 20 | CONF = config.CONF |
| 21 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 22 | |
| 23 | class Manager(object): |
| 24 | |
| 25 | """ |
| 26 | Base manager class |
| 27 | |
| 28 | Manager objects are responsible for providing a configuration object |
| 29 | and a client object for a test case to use in performing actions. |
| 30 | """ |
| 31 | |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 32 | def __init__(self, username=None, password=None, tenant_name=None): |
| 33 | """ |
| 34 | We allow overriding of the credentials used within the various |
| 35 | client classes managed by the Manager object. Left as None, the |
| 36 | standard username/password/tenant_name[/domain_name] is used. |
| 37 | |
| 38 | :param credentials: Override of the credentials |
| 39 | """ |
| 40 | self.auth_version = CONF.identity.auth_version |
| 41 | # FIXME(andreaf) Change Manager __init__ to accept a credentials dict |
| 42 | if username is None or password is None: |
| 43 | # Tenant None is a valid use case |
| 44 | self.credentials = self.get_default_credentials() |
| 45 | else: |
| 46 | self.credentials = dict(username=username, password=password, |
| 47 | tenant_name=tenant_name) |
| 48 | if self.auth_version == 'v3': |
| 49 | self.credentials['domain_name'] = 'Default' |
| 50 | # Creates an auth provider for the credentials |
| 51 | self.auth_provider = self.get_auth_provider(self.credentials) |
| 52 | # FIXME(andreaf) unused |
Maru Newby | dec13ec | 2012-08-30 11:19:17 -0700 | [diff] [blame] | 53 | self.client_attr_names = [] |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 54 | |
Sean Dague | 43cd905 | 2013-07-19 12:20:04 -0400 | [diff] [blame] | 55 | # we do this everywhere, have it be part of the super class |
| 56 | def _validate_credentials(self, username, password, tenant_name): |
| 57 | if None in (username, password, tenant_name): |
| 58 | msg = ("Missing required credentials. " |
| 59 | "username: %(u)s, password: %(p)s, " |
| 60 | "tenant_name: %(t)s" % |
| 61 | {'u': username, 'p': password, 't': tenant_name}) |
| 62 | raise exceptions.InvalidConfiguration(msg) |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 63 | |
| 64 | @classmethod |
| 65 | def get_auth_provider_class(cls, auth_version): |
| 66 | if auth_version == 'v2': |
| 67 | return auth.KeystoneV2AuthProvider |
| 68 | else: |
| 69 | return auth.KeystoneV3AuthProvider |
| 70 | |
| 71 | def get_default_credentials(self): |
| 72 | return dict( |
| 73 | username=CONF.identity.username, |
| 74 | password=CONF.identity.password, |
| 75 | tenant_name=CONF.identity.tenant_name |
| 76 | ) |
| 77 | |
Andrea Frittoli | 3099ffb | 2014-03-12 18:43:31 +0000 | [diff] [blame] | 78 | def get_auth_provider(self, credentials): |
| 79 | if credentials is None: |
| 80 | raise exceptions.InvalidCredentials( |
| 81 | 'Credentials must be specified') |
Andrea Frittoli | f9cde7e | 2014-02-18 09:57:04 +0000 | [diff] [blame] | 82 | auth_provider_class = self.get_auth_provider_class(self.auth_version) |
Andrea Frittoli | 3099ffb | 2014-03-12 18:43:31 +0000 | [diff] [blame] | 83 | return auth_provider_class( |
| 84 | client_type=getattr(self, 'client_type', None), |
| 85 | interface=getattr(self, 'interface', None), |
| 86 | credentials=credentials) |