blob: 4f1a883dc05011bf54ea4b7d84c9b9cd368d02ca [file] [log] [blame]
Matthew Treinishb86cda92013-07-29 11:22:23 -04001# Copyright 2013 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
zhongjun5b68f502017-07-04 15:28:05 +080015import ipaddress
16
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050017import netaddr
Doug Hellmann583ce2c2015-03-11 14:55:46 +000018from oslo_log import log as logging
Andrea Frittolic3280152015-02-26 12:42:34 +000019import six
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050020
Matthew Treinish3787e4c2016-10-07 21:25:33 -040021from tempest.lib.common import cred_client
Matthew Treinish00ab6be2016-10-07 16:29:18 -040022from tempest.lib.common import cred_provider
Matthew Treinish0650aed2016-10-07 16:36:46 -040023from tempest.lib.common.utils import data_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050024from tempest.lib import exceptions as lib_exc
Andrea Frittolidcd91002017-07-18 11:34:13 +010025from tempest.lib.services import clients
Matthew Treinishb86cda92013-07-29 11:22:23 -040026
27LOG = logging.getLogger(__name__)
28
29
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070030class DynamicCredentialProvider(cred_provider.CredentialProvider):
Masayuki Igawaa1c3af32017-09-07 10:22:37 +090031 """Creates credentials dynamically for tests
32
33 A credential provider that, based on an initial set of
34 admin credentials, creates new credentials on the fly for
35 tests to use and then discard.
36
37 :param str identity_version: identity API version to use `v2` or `v3`
38 :param str admin_role: name of the admin role added to admin users
39 :param str name: names of dynamic resources include this parameter
40 when specified
41 :param str credentials_domain: name of the domain where the users
42 are created. If not defined, the project
43 domain from admin_credentials is used
44 :param dict network_resources: network resources to be created for
45 the created credentials
46 :param Credentials admin_creds: initial admin credentials
47 :param bool identity_admin_domain_scope: Set to true if admin should be
48 scoped to the domain. By
49 default this is False and the
50 admin role is scoped to the
51 project.
52 :param str identity_admin_role: The role name to use for admin
53 :param list extra_roles: A list of strings for extra roles that should
54 be assigned to all created users
55 :param bool neutron_available: Whether we are running in an environemnt
56 with neutron
57 :param bool create_networks: Whether dynamic project networks should be
58 created or not
59 :param project_network_cidr: The CIDR to use for created project
60 networks
61 :param project_network_mask_bits: The network mask bits to use for
62 created project networks
63 :param public_network_id: The id for the public network to use
64 :param identity_admin_endpoint_type: The endpoint type for identity
65 admin clients. Defaults to public.
66 :param identity_uri: Identity URI of the target cloud
67 """
Matthew Treinishb86cda92013-07-29 11:22:23 -040068
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +010069 def __init__(self, identity_version, name=None, network_resources=None,
Matthew Treinish75abbcf2016-10-07 16:19:12 -040070 credentials_domain=None, admin_role=None, admin_creds=None,
71 identity_admin_domain_scope=False,
72 identity_admin_role='admin', extra_roles=None,
73 neutron_available=False, create_networks=True,
74 project_network_cidr=None, project_network_mask_bits=None,
Andrea Frittolidcd91002017-07-18 11:34:13 +010075 public_network_id=None, resource_prefix=None,
76 identity_admin_endpoint_type='public', identity_uri=None):
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070077 super(DynamicCredentialProvider, self).__init__(
Andrea Frittolidcd91002017-07-18 11:34:13 +010078 identity_version=identity_version, identity_uri=identity_uri,
79 admin_role=admin_role, name=name,
80 credentials_domain=credentials_domain,
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010081 network_resources=network_resources)
82 self.network_resources = network_resources
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070083 self._creds = {}
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050084 self.ports = []
Matthew Treinish0650aed2016-10-07 16:36:46 -040085 self.resource_prefix = resource_prefix or ''
Matthew Treinish75abbcf2016-10-07 16:19:12 -040086 self.neutron_available = neutron_available
87 self.create_networks = create_networks
88 self.project_network_cidr = project_network_cidr
89 self.project_network_mask_bits = project_network_mask_bits
90 self.public_network_id = public_network_id
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010091 self.default_admin_creds = admin_creds
Matthew Treinish75abbcf2016-10-07 16:19:12 -040092 self.identity_admin_domain_scope = identity_admin_domain_scope
93 self.identity_admin_role = identity_admin_role or 'admin'
Andrea Frittolidcd91002017-07-18 11:34:13 +010094 self.identity_admin_endpoint_type = identity_admin_endpoint_type
Matthew Treinish75abbcf2016-10-07 16:19:12 -040095 self.extra_roles = extra_roles or []
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -060096 (self.identity_admin_client,
97 self.tenants_admin_client,
Daniel Mellado82c83a52015-12-09 15:16:49 +000098 self.users_admin_client,
Daniel Mellado7aea5342016-02-09 09:10:12 +000099 self.roles_admin_client,
Daniel Mellado91a26b62016-02-11 11:13:04 +0000100 self.domains_admin_client,
John Warren3961acd2015-10-02 14:38:53 -0400101 self.networks_admin_client,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000102 self.routers_admin_client,
John Warren49c0fe52015-10-22 12:35:54 -0400103 self.subnets_admin_client,
John Warrenf9606e92015-12-10 12:12:42 -0500104 self.ports_admin_client,
Andrea Frittolidcd91002017-07-18 11:34:13 +0100105 self.security_groups_admin_client) = self._get_admin_clients(
106 identity_admin_endpoint_type)
John Warren3961acd2015-10-02 14:38:53 -0400107 # Domain where isolated credentials are provisioned (v3 only).
Andrea Frittolic3280152015-02-26 12:42:34 +0000108 # Use that of the admin account is None is configured.
109 self.creds_domain_name = None
110 if self.identity_version == 'v3':
111 self.creds_domain_name = (
David Kranz87fc7e92015-07-28 14:05:20 -0400112 self.default_admin_creds.project_domain_name or
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +0100113 self.credentials_domain)
Jamie Lennox15350172015-08-17 10:54:25 +1000114 self.creds_client = cred_client.get_creds_client(
Daniel Melladob04da902015-11-20 17:43:12 +0100115 self.identity_admin_client,
116 self.tenants_admin_client,
Daniel Mellado82c83a52015-12-09 15:16:49 +0000117 self.users_admin_client,
Daniel Mellado7aea5342016-02-09 09:10:12 +0000118 self.roles_admin_client,
Daniel Mellado91a26b62016-02-11 11:13:04 +0000119 self.domains_admin_client,
Daniel Melladob04da902015-11-20 17:43:12 +0100120 self.creds_domain_name)
Matthew Treinishb86cda92013-07-29 11:22:23 -0400121
Andrea Frittolidcd91002017-07-18 11:34:13 +0100122 def _get_admin_clients(self, endpoint_type):
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +0000123 """Returns a tuple with instances of the following admin clients
124
125 (in this order):
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500126 identity
127 network
Matthew Treinishb86cda92013-07-29 11:22:23 -0400128 """
Andrea Frittolidcd91002017-07-18 11:34:13 +0100129 os = clients.ServiceClients(self.default_admin_creds,
130 self.identity_uri)
131 params = {'endpoint_type': endpoint_type}
Andrea Frittolic3280152015-02-26 12:42:34 +0000132 if self.identity_version == 'v2':
Andrea Frittolidcd91002017-07-18 11:34:13 +0100133 return (os.identity_v2.IdentityClient(**params),
134 os.identity_v2.TenantsClient(**params),
135 os.identity_v2.UsersClient(**params),
136 os.identity_v2.RolesClient(**params), None,
137 os.network.NetworksClient(),
138 os.network.RoutersClient(),
139 os.network.SubnetsClient(),
140 os.network.PortsClient(),
141 os.network.SecurityGroupsClient())
Andrea Frittolic3280152015-02-26 12:42:34 +0000142 else:
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100143 # We use a dedicated client manager for identity client in case we
144 # need a different token scope for them.
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400145 scope = 'domain' if self.identity_admin_domain_scope else 'project'
Andrea Frittolidcd91002017-07-18 11:34:13 +0100146 identity_os = clients.ServiceClients(self.default_admin_creds,
147 self.identity_uri,
148 scope=scope)
149 return (identity_os.identity_v3.IdentityClient(**params),
150 identity_os.identity_v3.ProjectsClient(**params),
151 identity_os.identity_v3.UsersClient(**params),
152 identity_os.identity_v3.RolesClient(**params),
153 identity_os.identity_v3.DomainsClient(**params),
154 os.network.NetworksClient(),
155 os.network.RoutersClient(),
156 os.network.SubnetsClient(),
157 os.network.PortsClient(),
158 os.network.SecurityGroupsClient())
Matthew Treinishb86cda92013-07-29 11:22:23 -0400159
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300160 def _create_creds(self, admin=False, roles=None):
161 """Create credentials with random name.
Sean Dague6969b902014-01-28 06:48:37 -0500162
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300163 Creates project and user. When admin flag is True create user
164 with admin role. Assign user with additional roles (for example
165 _member_) and roles requested by caller.
Sean Dague6969b902014-01-28 06:48:37 -0500166
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300167 :param admin: Flag if to assign to the user admin role
168 :type admin: bool
169 :param roles: Roles to assign for the user
170 :type roles: list
171 :return: Readonly Credentials with network resources
Sean Dague6969b902014-01-28 06:48:37 -0500172 """
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300173 root = self.name
Sean Dague6969b902014-01-28 06:48:37 -0500174
Matthew Treinish0650aed2016-10-07 16:36:46 -0400175 project_name = data_utils.rand_name(root, prefix=self.resource_prefix)
Andrea Frittolic3280152015-02-26 12:42:34 +0000176 project_desc = project_name + "-desc"
177 project = self.creds_client.create_project(
178 name=project_name, description=project_desc)
Sean Dague6969b902014-01-28 06:48:37 -0500179
Andrea Frittoli (andreaf)ef9b01f2016-06-02 10:25:25 +0100180 # NOTE(andreaf) User and project can be distinguished from the context,
181 # having the same ID in both makes it easier to match them and debug.
182 username = project_name
LingxianKong9c713d22015-06-09 15:19:55 +0800183 user_password = data_utils.rand_password()
Matthew Treinish0650aed2016-10-07 16:36:46 -0400184 email = data_utils.rand_name(
185 root, prefix=self.resource_prefix) + "@example.com"
Andrea Frittolic3280152015-02-26 12:42:34 +0000186 user = self.creds_client.create_user(
LingxianKong9c713d22015-06-09 15:19:55 +0800187 username, user_password, project, email)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400188 role_assigned = False
Matthew Treinishb86cda92013-07-29 11:22:23 -0400189 if admin:
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300190 self.creds_client.assign_user_role(user, project, self.admin_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400191 role_assigned = True
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100192 if (self.identity_version == 'v3' and
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400193 self.identity_admin_domain_scope):
Andrea Frittoli (andreaf)4bee2e72015-09-22 13:06:18 +0100194 self.creds_client.assign_user_role_on_domain(
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400195 user, self.identity_admin_role)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500196 # Add roles specified in config file
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400197 for conf_role in self.extra_roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000198 self.creds_client.assign_user_role(user, project, conf_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400199 role_assigned = True
Matthew Treinish976e8df2014-12-19 14:21:54 -0500200 # Add roles requested by caller
201 if roles:
202 for role in roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000203 self.creds_client.assign_user_role(user, project, role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400204 role_assigned = True
205 # NOTE(mtreinish) For a user to have access to a project with v3 auth
206 # it must beassigned a role on the project. So we need to ensure that
207 # our newly created user has a role on the newly created project.
208 if self.identity_version == 'v3' and not role_assigned:
Adam Youngb226f8e2016-06-25 21:41:36 -0400209 try:
210 self.creds_client.create_user_role('Member')
211 except lib_exc.Conflict:
212 LOG.warning('Member role already exists, ignoring conflict.')
Matthew Treinish32f98a42015-07-14 19:58:46 -0400213 self.creds_client.assign_user_role(user, project, 'Member')
214
LingxianKong9c713d22015-06-09 15:19:55 +0800215 creds = self.creds_client.get_credentials(user, project, user_password)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400216 return cred_provider.TestResources(creds)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500217
218 def _create_network_resources(self, tenant_id):
edannon6cc6fbc2016-05-03 11:56:12 +0300219 """The function creates network resources in the given tenant.
220
221 The function checks if network_resources class member is empty,
222 In case it is, it will create a network, a subnet and a router for
223 the tenant according to the given tenant id parameter.
224 Otherwise it will create a network resource according
225 to the values from network_resources dict.
226
227 :param tenant_id: The tenant id to create resources for.
228 :type tenant_id: str
229 :raises: InvalidConfiguration, Exception
230 :returns: network resources(network,subnet,router)
231 :rtype: tuple
232 """
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500233 network = None
234 subnet = None
235 router = None
Matthew Treinish9f756a02014-01-15 10:26:07 -0500236 # Make sure settings
237 if self.network_resources:
238 if self.network_resources['router']:
239 if (not self.network_resources['subnet'] or
240 not self.network_resources['network']):
Matthew Treinish4217a702016-10-07 17:27:11 -0400241 raise lib_exc.InvalidConfiguration(
Matthew Treinish9f756a02014-01-15 10:26:07 -0500242 'A router requires a subnet and network')
243 elif self.network_resources['subnet']:
244 if not self.network_resources['network']:
Matthew Treinish4217a702016-10-07 17:27:11 -0400245 raise lib_exc.InvalidConfiguration(
Matthew Treinish9f756a02014-01-15 10:26:07 -0500246 'A subnet requires a network')
247 elif self.network_resources['dhcp']:
Matthew Treinish4217a702016-10-07 17:27:11 -0400248 raise lib_exc.InvalidConfiguration('DHCP requires a subnet')
Matthew Treinish9f756a02014-01-15 10:26:07 -0500249
Matthew Treinish0650aed2016-10-07 16:36:46 -0400250 rand_name_root = data_utils.rand_name(
251 self.name, prefix=self.resource_prefix)
Matthew Treinish9f756a02014-01-15 10:26:07 -0500252 if not self.network_resources or self.network_resources['network']:
Matthew Treinish0650aed2016-10-07 16:36:46 -0400253 network_name = rand_name_root + "-network"
Matthew Treinish9f756a02014-01-15 10:26:07 -0500254 network = self._create_network(network_name, tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500255 try:
Matthew Treinish9f756a02014-01-15 10:26:07 -0500256 if not self.network_resources or self.network_resources['subnet']:
Matthew Treinish0650aed2016-10-07 16:36:46 -0400257 subnet_name = rand_name_root + "-subnet"
Matthew Treinish9f756a02014-01-15 10:26:07 -0500258 subnet = self._create_subnet(subnet_name, tenant_id,
259 network['id'])
260 if not self.network_resources or self.network_resources['router']:
Matthew Treinish0650aed2016-10-07 16:36:46 -0400261 router_name = rand_name_root + "-router"
Matthew Treinish9f756a02014-01-15 10:26:07 -0500262 router = self._create_router(router_name, tenant_id)
263 self._add_router_interface(router['id'], subnet['id'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500264 except Exception:
Andrea Frittoli (andreaf)d9a18b02016-02-29 15:27:34 +0000265 try:
266 if router:
267 self._clear_isolated_router(router['id'], router['name'])
268 if subnet:
269 self._clear_isolated_subnet(subnet['id'], subnet['name'])
270 if network:
271 self._clear_isolated_network(network['id'],
272 network['name'])
273 except Exception as cleanup_exception:
274 msg = "There was an exception trying to setup network " \
275 "resources for tenant %s, and this error happened " \
276 "trying to clean them up: %s"
Jordan Pittier525ec712016-12-07 17:51:26 +0100277 LOG.warning(msg, tenant_id, cleanup_exception)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500278 raise
279 return network, subnet, router
280
281 def _create_network(self, name, tenant_id):
John Warren94d8faf2015-09-15 12:22:24 -0400282 resp_body = self.networks_admin_client.create_network(
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100283 name=name, tenant_id=tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500284 return resp_body['network']
285
286 def _create_subnet(self, subnet_name, tenant_id, network_id):
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400287 base_cidr = netaddr.IPNetwork(self.project_network_cidr)
288 mask_bits = self.project_network_mask_bits
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500289 for subnet_cidr in base_cidr.subnet(mask_bits):
290 try:
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100291 if self.network_resources:
John Warren3961acd2015-10-02 14:38:53 -0400292 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100293 create_subnet(
294 network_id=network_id, cidr=str(subnet_cidr),
295 name=subnet_name,
296 tenant_id=tenant_id,
297 enable_dhcp=self.network_resources['dhcp'],
zhongjun5b68f502017-07-04 15:28:05 +0800298 ip_version=(ipaddress.ip_network(
299 six.text_type(subnet_cidr)).version))
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500300 else:
John Warren3961acd2015-10-02 14:38:53 -0400301 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100302 create_subnet(network_id=network_id,
303 cidr=str(subnet_cidr),
304 name=subnet_name,
305 tenant_id=tenant_id,
zhongjun5b68f502017-07-04 15:28:05 +0800306 ip_version=(ipaddress.ip_network(
307 six.text_type(subnet_cidr)).version))
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500308 break
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900309 except lib_exc.BadRequest as e:
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500310 if 'overlaps with another subnet' not in str(e):
311 raise
312 else:
David Kranzd4210412014-11-21 08:37:45 -0500313 message = 'Available CIDR for subnet creation could not be found'
314 raise Exception(message)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500315 return resp_body['subnet']
316
317 def _create_router(self, router_name, tenant_id):
zhufl6b7040a2017-01-18 16:38:34 +0800318 kwargs = {'name': router_name,
319 'tenant_id': tenant_id}
320 if self.public_network_id:
321 kwargs['external_gateway_info'] = dict(
322 network_id=self.public_network_id)
323 resp_body = self.routers_admin_client.create_router(**kwargs)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500324 return resp_body['router']
325
326 def _add_router_interface(self, router_id, subnet_id):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000327 self.routers_admin_client.add_router_interface(router_id,
piyush11078694aca952015-12-17 12:54:44 +0530328 subnet_id=subnet_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500329
Andrea Frittoli9612e812014-03-13 10:57:26 +0000330 def get_credentials(self, credential_type):
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700331 if self._creds.get(str(credential_type)):
332 credentials = self._creds[str(credential_type)]
Matthew Treinishb86cda92013-07-29 11:22:23 -0400333 else:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500334 if credential_type in ['primary', 'alt', 'admin']:
335 is_admin = (credential_type == 'admin')
336 credentials = self._create_creds(admin=is_admin)
337 else:
338 credentials = self._create_creds(roles=credential_type)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700339 self._creds[str(credential_type)] = credentials
Andrea Frittolifc315902014-03-20 09:21:44 +0000340 # Maintained until tests are ported
Jordan Pittier525ec712016-12-07 17:51:26 +0100341 LOG.info("Acquired dynamic creds:\n credentials: %s", credentials)
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400342 if (self.neutron_available and
343 self.create_networks):
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500344 network, subnet, router = self._create_network_resources(
Andrea Frittolifc315902014-03-20 09:21:44 +0000345 credentials.tenant_id)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400346 credentials.set_resources(network=network, subnet=subnet,
347 router=router)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500348 LOG.info("Created isolated network resources for : \n"
Jordan Pittier525ec712016-12-07 17:51:26 +0100349 + " credentials: %s", credentials)
Andrea Frittoli9612e812014-03-13 10:57:26 +0000350 return credentials
Matthew Treinishb86cda92013-07-29 11:22:23 -0400351
Andrea Frittoli9612e812014-03-13 10:57:26 +0000352 def get_primary_creds(self):
353 return self.get_credentials('primary')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400354
Andrea Frittoli9612e812014-03-13 10:57:26 +0000355 def get_admin_creds(self):
356 return self.get_credentials('admin')
Andrea Frittolifc315902014-03-20 09:21:44 +0000357
Andrea Frittoli9612e812014-03-13 10:57:26 +0000358 def get_alt_creds(self):
359 return self.get_credentials('alt')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400360
Matthew Treinish976e8df2014-12-19 14:21:54 -0500361 def get_creds_by_roles(self, roles, force_new=False):
362 roles = list(set(roles))
363 # The roles list as a str will become the index as the dict key for
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700364 # the created credentials set in the dynamic_creds dict.
365 exist_creds = self._creds.get(str(roles))
Matthew Treinish976e8df2014-12-19 14:21:54 -0500366 # If force_new flag is True 2 cred sets with the same roles are needed
367 # handle this by creating a separate index for old one to store it
368 # separately for cleanup
369 if exist_creds and force_new:
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700370 new_index = str(roles) + '-' + str(len(self._creds))
371 self._creds[new_index] = exist_creds
372 del self._creds[str(roles)]
Matthew Treinish976e8df2014-12-19 14:21:54 -0500373 return self.get_credentials(roles)
374
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500375 def _clear_isolated_router(self, router_id, router_name):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000376 client = self.routers_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500377 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000378 client.delete_router(router_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900379 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100380 LOG.warning('router with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000381 router_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500382
383 def _clear_isolated_subnet(self, subnet_id, subnet_name):
John Warren3961acd2015-10-02 14:38:53 -0400384 client = self.subnets_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500385 try:
John Warren3961acd2015-10-02 14:38:53 -0400386 client.delete_subnet(subnet_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900387 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100388 LOG.warning('subnet with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000389 subnet_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500390
391 def _clear_isolated_network(self, network_id, network_name):
John Warren94d8faf2015-09-15 12:22:24 -0400392 net_client = self.networks_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500393 try:
394 net_client.delete_network(network_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900395 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100396 LOG.warning('network with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000397 network_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500398
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100399 def _cleanup_default_secgroup(self, tenant):
John Warrenf9606e92015-12-10 12:12:42 -0500400 nsg_client = self.security_groups_admin_client
401 resp_body = nsg_client.list_security_groups(tenant_id=tenant,
David Kranz34e88122014-12-11 15:24:05 -0500402 name="default")
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100403 secgroups_to_delete = resp_body['security_groups']
404 for secgroup in secgroups_to_delete:
405 try:
John Warrenf9606e92015-12-10 12:12:42 -0500406 nsg_client.delete_security_group(secgroup['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900407 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100408 LOG.warning('Security group %s, id %s not found for clean-up',
409 secgroup['name'], secgroup['id'])
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100410
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500411 def _clear_isolated_net_resources(self):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000412 client = self.routers_admin_client
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700413 for cred in self._creds:
414 creds = self._creds.get(cred)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400415 if (not creds or not any([creds.router, creds.network,
416 creds.subnet])):
417 continue
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800418 LOG.debug("Clearing network: %(network)s, "
Matthew Treinishfe094ea2014-12-09 01:19:27 +0000419 "subnet: %(subnet)s, router: %(router)s",
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400420 {'network': creds.network, 'subnet': creds.subnet,
421 'router': creds.router})
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800422 if (not self.network_resources or
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400423 (self.network_resources.get('router') and creds.subnet)):
Matthew Treinish9f756a02014-01-15 10:26:07 -0500424 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000425 client.remove_router_interface(
piyush11078694aca952015-12-17 12:54:44 +0530426 creds.router['id'],
427 subnet_id=creds.subnet['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900428 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100429 LOG.warning('router with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000430 creds.router['name'])
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400431 self._clear_isolated_router(creds.router['id'],
432 creds.router['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800433 if (not self.network_resources or
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800434 self.network_resources.get('subnet')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400435 self._clear_isolated_subnet(creds.subnet['id'],
436 creds.subnet['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800437 if (not self.network_resources or
438 self.network_resources.get('network')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400439 self._clear_isolated_network(creds.network['id'],
440 creds.network['name'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500441
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700442 def clear_creds(self):
443 if not self._creds:
Matthew Treinishb86cda92013-07-29 11:22:23 -0400444 return
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500445 self._clear_isolated_net_resources()
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700446 for creds in six.itervalues(self._creds):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400447 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000448 self.creds_client.delete_user(creds.user_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900449 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100450 LOG.warning("user with name: %s not found for delete",
zhangguoqing6c096642016-01-04 06:17:21 +0000451 creds.username)
zhufl2344ea62016-06-01 14:44:00 +0800452 # NOTE(zhufl): Only when neutron's security_group ext is
453 # enabled, _cleanup_default_secgroup will not raise error. But
Andrea Frittolicd368412017-08-14 21:37:56 +0100454 # here cannot use test_utils.is_extension_enabled for it will cause
zhufl2344ea62016-06-01 14:44:00 +0800455 # "circular dependency". So here just use try...except to
456 # ensure tenant deletion without big changes.
Matthew Treinishb86cda92013-07-29 11:22:23 -0400457 try:
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400458 if self.neutron_available:
Andrea Frittolic3280152015-02-26 12:42:34 +0000459 self._cleanup_default_secgroup(creds.tenant_id)
zhufl2344ea62016-06-01 14:44:00 +0800460 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100461 LOG.warning("failed to cleanup tenant %s's secgroup",
zhufl2344ea62016-06-01 14:44:00 +0800462 creds.tenant_name)
463 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000464 self.creds_client.delete_project(creds.tenant_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900465 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100466 LOG.warning("tenant with name: %s not found for delete",
zhangguoqing6c096642016-01-04 06:17:21 +0000467 creds.tenant_name)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700468 self._creds = {}
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100469
470 def is_multi_user(self):
471 return True
Yair Fried76488d72014-10-21 10:13:19 +0300472
473 def is_multi_tenant(self):
474 return True
Matthew Treinish4a596932015-03-06 20:37:01 -0500475
476 def is_role_available(self, role):
477 return True