blob: 5d15988c09dde4f3e55dfda3773df322247a9fa4 [file] [log] [blame]
Jamie Lennox15350172015-08-17 10:54:25 +10001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import abc
14
15from oslo_log import log as logging
16import six
Andrea Frittoli (andreaf)278463c2015-10-08 15:04:09 +010017from tempest_lib import auth
Jamie Lennox15350172015-08-17 10:54:25 +100018from tempest_lib import exceptions as lib_exc
19
Jamie Lennox15350172015-08-17 10:54:25 +100020from tempest.services.identity.v2.json import identity_client as v2_identity
21
Jamie Lennox15350172015-08-17 10:54:25 +100022LOG = logging.getLogger(__name__)
23
24
25@six.add_metaclass(abc.ABCMeta)
26class CredsClient(object):
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +000027 """This class is a wrapper around the identity clients
28
29 to provide a single interface for managing credentials in both v2 and v3
30 cases. It's not bound to created credentials, only to a specific set of
31 admin credentials used for generating credentials.
Jamie Lennox15350172015-08-17 10:54:25 +100032 """
33
Daniel Mellado6b16b922015-12-07 12:43:08 +000034 def __init__(self, identity_client, projects_client=None,
35 roles_client=None):
Jamie Lennox15350172015-08-17 10:54:25 +100036 # The client implies version and credentials
37 self.identity_client = identity_client
Daniel Melladob04da902015-11-20 17:43:12 +010038 # this is temporary until the v3 project client is
39 # separated, then projects_client will become mandatory
40 self.projects_client = projects_client or identity_client
Daniel Mellado6b16b922015-12-07 12:43:08 +000041 self.roles_client = roles_client or identity_client
Jamie Lennox15350172015-08-17 10:54:25 +100042
43 def create_user(self, username, password, project, email):
44 user = self.identity_client.create_user(
45 username, password, project['id'], email)
46 if 'user' in user:
47 user = user['user']
48 return user
49
50 @abc.abstractmethod
51 def create_project(self, name, description):
52 pass
53
54 def _check_role_exists(self, role_name):
55 try:
56 roles = self._list_roles()
57 role = next(r for r in roles if r['name'] == role_name)
58 except StopIteration:
59 return None
60 return role
61
62 def create_user_role(self, role_name):
63 if not self._check_role_exists(role_name):
piyush110786afaaf262015-12-11 18:54:05 +053064 self.roles_client.create_role(name=role_name)
Jamie Lennox15350172015-08-17 10:54:25 +100065
66 def assign_user_role(self, user, project, role_name):
67 role = self._check_role_exists(role_name)
68 if not role:
69 msg = 'No "%s" role found' % role_name
70 raise lib_exc.NotFound(msg)
71 try:
Daniel Mellado6b16b922015-12-07 12:43:08 +000072 self.roles_client.assign_user_role(project['id'], user['id'],
73 role['id'])
Jamie Lennox15350172015-08-17 10:54:25 +100074 except lib_exc.Conflict:
75 LOG.debug("Role %s already assigned on project %s for user %s" % (
76 role['id'], project['id'], user['id']))
77
78 @abc.abstractmethod
79 def get_credentials(self, user, project, password):
Andrea Frittoli (andreaf)278463c2015-10-08 15:04:09 +010080 """Produces a Credentials object from the details provided
81
82 :param user: a user dict
83 :param project: a project dict
84 :param password: the password as a string
85 :return: a Credentials object with all the available credential details
86 """
Jamie Lennox15350172015-08-17 10:54:25 +100087 pass
88
89 def delete_user(self, user_id):
90 self.identity_client.delete_user(user_id)
91
92 def _list_roles(self):
Daniel Mellado6b16b922015-12-07 12:43:08 +000093 roles = self.roles_client.list_roles()['roles']
Jamie Lennox15350172015-08-17 10:54:25 +100094 return roles
95
96
97class V2CredsClient(CredsClient):
98
Daniel Mellado6b16b922015-12-07 12:43:08 +000099 def __init__(self, identity_client, projects_client, roles_client):
100 super(V2CredsClient, self).__init__(identity_client,
101 projects_client,
102 roles_client)
Daniel Melladob04da902015-11-20 17:43:12 +0100103
Jamie Lennox15350172015-08-17 10:54:25 +1000104 def create_project(self, name, description):
Daniel Melladob04da902015-11-20 17:43:12 +0100105 tenant = self.projects_client.create_tenant(
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530106 name=name, description=description)['tenant']
Jamie Lennox15350172015-08-17 10:54:25 +1000107 return tenant
108
109 def get_credentials(self, user, project, password):
Andrea Frittoli (andreaf)278463c2015-10-08 15:04:09 +0100110 # User and project already include both ID and name here,
111 # so there's no need to use the fill_in mode
112 return auth.get_credentials(
113 auth_url=None,
114 fill_in=False,
Jamie Lennox15350172015-08-17 10:54:25 +1000115 identity_version='v2',
116 username=user['name'], user_id=user['id'],
117 tenant_name=project['name'], tenant_id=project['id'],
118 password=password)
119
120 def delete_project(self, project_id):
Daniel Melladob04da902015-11-20 17:43:12 +0100121 self.projects_client.delete_tenant(project_id)
Jamie Lennox15350172015-08-17 10:54:25 +1000122
123
124class V3CredsClient(CredsClient):
125
126 def __init__(self, identity_client, domain_name):
127 super(V3CredsClient, self).__init__(identity_client)
128 try:
129 # Domain names must be unique, in any case a list is returned,
130 # selecting the first (and only) element
131 self.creds_domain = self.identity_client.list_domains(
132 params={'name': domain_name})['domains'][0]
133 except lib_exc.NotFound:
134 # TODO(andrea) we could probably create the domain on the fly
Andrea Frittoli (andreaf)278463c2015-10-08 15:04:09 +0100135 msg = "Requested domain %s could not be found" % domain_name
136 raise lib_exc.InvalidCredentials(msg)
Jamie Lennox15350172015-08-17 10:54:25 +1000137
138 def create_project(self, name, description):
139 project = self.identity_client.create_project(
140 name=name, description=description,
141 domain_id=self.creds_domain['id'])['project']
142 return project
143
144 def get_credentials(self, user, project, password):
Andrea Frittoli (andreaf)278463c2015-10-08 15:04:09 +0100145 # User, project and domain already include both ID and name here,
146 # so there's no need to use the fill_in mode.
147 return auth.get_credentials(
148 auth_url=None,
149 fill_in=False,
Jamie Lennox15350172015-08-17 10:54:25 +1000150 identity_version='v3',
151 username=user['name'], user_id=user['id'],
152 project_name=project['name'], project_id=project['id'],
153 password=password,
Andrea Frittoli (andreaf)278463c2015-10-08 15:04:09 +0100154 project_domain_id=self.creds_domain['id'],
Jamie Lennox15350172015-08-17 10:54:25 +1000155 project_domain_name=self.creds_domain['name'])
156
157 def delete_project(self, project_id):
158 self.identity_client.delete_project(project_id)
159
160 def _list_roles(self):
161 roles = self.identity_client.list_roles()['roles']
162 return roles
163
164
Daniel Melladob04da902015-11-20 17:43:12 +0100165def get_creds_client(identity_client,
166 projects_client=None,
Daniel Mellado6b16b922015-12-07 12:43:08 +0000167 roles_client=None,
Daniel Melladob04da902015-11-20 17:43:12 +0100168 project_domain_name=None):
Jamie Lennox15350172015-08-17 10:54:25 +1000169 if isinstance(identity_client, v2_identity.IdentityClient):
Daniel Mellado6b16b922015-12-07 12:43:08 +0000170 return V2CredsClient(identity_client, projects_client, roles_client)
Jamie Lennox15350172015-08-17 10:54:25 +1000171 else:
172 return V3CredsClient(identity_client, project_domain_name)