blob: 41bf4f8acbc630a57dac601466ed86d2a1779c2f [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
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050015import netaddr
Doug Hellmann583ce2c2015-03-11 14:55:46 +000016from oslo_log import log as logging
Andrea Frittolic3280152015-02-26 12:42:34 +000017import six
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050018
Matthew Treinishb86cda92013-07-29 11:22:23 -040019from tempest import clients
Jamie Lennox15350172015-08-17 10:54:25 +100020from tempest.common import cred_client
Marc Kodererd2690fe2014-07-16 14:17:47 +020021from tempest.common import cred_provider
Andrea Frittoli (andreaf)8def7ca2015-05-13 14:24:19 +010022from tempest.common.utils import data_utils
Matthew Treinishb86cda92013-07-29 11:22:23 -040023from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050024from tempest.lib import exceptions as lib_exc
Matthew Treinishb86cda92013-07-29 11:22:23 -040025
26LOG = logging.getLogger(__name__)
27
28
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070029class DynamicCredentialProvider(cred_provider.CredentialProvider):
Matthew Treinishb86cda92013-07-29 11:22:23 -040030
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +010031 def __init__(self, identity_version, name=None, network_resources=None,
Matthew Treinish75abbcf2016-10-07 16:19:12 -040032 credentials_domain=None, admin_role=None, admin_creds=None,
33 identity_admin_domain_scope=False,
34 identity_admin_role='admin', extra_roles=None,
35 neutron_available=False, create_networks=True,
36 project_network_cidr=None, project_network_mask_bits=None,
37 public_network_id=None):
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010038 """Creates credentials dynamically for tests
39
40 A credential provider that, based on an initial set of
41 admin credentials, creates new credentials on the fly for
42 tests to use and then discard.
43
44 :param str identity_version: identity API version to use `v2` or `v3`
45 :param str admin_role: name of the admin role added to admin users
46 :param str name: names of dynamic resources include this parameter
47 when specified
48 :param str credentials_domain: name of the domain where the users
49 are created. If not defined, the project
50 domain from admin_credentials is used
51 :param dict network_resources: network resources to be created for
52 the created credentials
53 :param Credentials admin_creds: initial admin credentials
Matthew Treinish75abbcf2016-10-07 16:19:12 -040054 :param bool identity_admin_domain_scope: Set to true if admin should be
55 scoped to the domain. By
56 default this is False and the
57 admin role is scoped to the
58 project.
59 :param str identity_admin_role: The role name to use for admin
60 :param list extra_roles: A list of strings for extra roles that should
61 be assigned to all created users
62 :param bool neutron_available: Whether we are running in an environemnt
63 with neutron
64 :param bool create_networks: Whether dynamic project networks should be
65 created or not
66 :param project_network_cidr: The CIDR to use for created project
67 networks
68 :param project_network_mask_bits: The network mask bits to use for
69 created project networks
70 :param public_network_id: The id for the public network to use
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010071 """
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070072 super(DynamicCredentialProvider, self).__init__(
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010073 identity_version=identity_version, admin_role=admin_role,
74 name=name, credentials_domain=credentials_domain,
75 network_resources=network_resources)
76 self.network_resources = network_resources
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070077 self._creds = {}
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050078 self.ports = []
Matthew Treinish75abbcf2016-10-07 16:19:12 -040079 self.neutron_available = neutron_available
80 self.create_networks = create_networks
81 self.project_network_cidr = project_network_cidr
82 self.project_network_mask_bits = project_network_mask_bits
83 self.public_network_id = public_network_id
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010084 self.default_admin_creds = admin_creds
Matthew Treinish75abbcf2016-10-07 16:19:12 -040085 self.identity_admin_domain_scope = identity_admin_domain_scope
86 self.identity_admin_role = identity_admin_role or 'admin'
87 self.extra_roles = extra_roles or []
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -060088 (self.identity_admin_client,
89 self.tenants_admin_client,
Daniel Mellado82c83a52015-12-09 15:16:49 +000090 self.users_admin_client,
Daniel Mellado7aea5342016-02-09 09:10:12 +000091 self.roles_admin_client,
Daniel Mellado91a26b62016-02-11 11:13:04 +000092 self.domains_admin_client,
John Warren3961acd2015-10-02 14:38:53 -040093 self.networks_admin_client,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +000094 self.routers_admin_client,
John Warren49c0fe52015-10-22 12:35:54 -040095 self.subnets_admin_client,
John Warrenf9606e92015-12-10 12:12:42 -050096 self.ports_admin_client,
97 self.security_groups_admin_client) = self._get_admin_clients()
John Warren3961acd2015-10-02 14:38:53 -040098 # Domain where isolated credentials are provisioned (v3 only).
Andrea Frittolic3280152015-02-26 12:42:34 +000099 # Use that of the admin account is None is configured.
100 self.creds_domain_name = None
101 if self.identity_version == 'v3':
102 self.creds_domain_name = (
David Kranz87fc7e92015-07-28 14:05:20 -0400103 self.default_admin_creds.project_domain_name or
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +0100104 self.credentials_domain)
Jamie Lennox15350172015-08-17 10:54:25 +1000105 self.creds_client = cred_client.get_creds_client(
Daniel Melladob04da902015-11-20 17:43:12 +0100106 self.identity_admin_client,
107 self.tenants_admin_client,
Daniel Mellado82c83a52015-12-09 15:16:49 +0000108 self.users_admin_client,
Daniel Mellado7aea5342016-02-09 09:10:12 +0000109 self.roles_admin_client,
Daniel Mellado91a26b62016-02-11 11:13:04 +0000110 self.domains_admin_client,
Daniel Melladob04da902015-11-20 17:43:12 +0100111 self.creds_domain_name)
Matthew Treinishb86cda92013-07-29 11:22:23 -0400112
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500113 def _get_admin_clients(self):
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +0000114 """Returns a tuple with instances of the following admin clients
115
116 (in this order):
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500117 identity
118 network
Matthew Treinishb86cda92013-07-29 11:22:23 -0400119 """
Andrea Frittolic3280152015-02-26 12:42:34 +0000120 os = clients.Manager(self.default_admin_creds)
121 if self.identity_version == 'v2':
Daniel Mellado7aea5342016-02-09 09:10:12 +0000122 return (os.identity_client, os.tenants_client, os.users_client,
Ken'ichi Ohmichi43e7fcf2016-04-04 11:59:13 -0700123 os.roles_client, None,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000124 os.networks_client, os.routers_client, os.subnets_client,
125 os.ports_client, os.security_groups_client)
Andrea Frittolic3280152015-02-26 12:42:34 +0000126 else:
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100127 # We use a dedicated client manager for identity client in case we
128 # need a different token scope for them.
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400129 scope = 'domain' if self.identity_admin_domain_scope else 'project'
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100130 identity_os = clients.Manager(self.default_admin_creds,
131 scope=scope)
132 return (identity_os.identity_v3_client,
133 identity_os.projects_client,
134 identity_os.users_v3_client, identity_os.roles_v3_client,
135 identity_os.domains_client,
Ken'ichi Ohmichi43e7fcf2016-04-04 11:59:13 -0700136 os.networks_client, os.routers_client,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000137 os.subnets_client, os.ports_client,
138 os.security_groups_client)
Matthew Treinishb86cda92013-07-29 11:22:23 -0400139
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300140 def _create_creds(self, admin=False, roles=None):
141 """Create credentials with random name.
Sean Dague6969b902014-01-28 06:48:37 -0500142
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300143 Creates project and user. When admin flag is True create user
144 with admin role. Assign user with additional roles (for example
145 _member_) and roles requested by caller.
Sean Dague6969b902014-01-28 06:48:37 -0500146
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300147 :param admin: Flag if to assign to the user admin role
148 :type admin: bool
149 :param roles: Roles to assign for the user
150 :type roles: list
151 :return: Readonly Credentials with network resources
Sean Dague6969b902014-01-28 06:48:37 -0500152 """
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300153 root = self.name
Sean Dague6969b902014-01-28 06:48:37 -0500154
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300155 project_name = data_utils.rand_name(root)
Andrea Frittolic3280152015-02-26 12:42:34 +0000156 project_desc = project_name + "-desc"
157 project = self.creds_client.create_project(
158 name=project_name, description=project_desc)
Sean Dague6969b902014-01-28 06:48:37 -0500159
Andrea Frittoli (andreaf)ef9b01f2016-06-02 10:25:25 +0100160 # NOTE(andreaf) User and project can be distinguished from the context,
161 # having the same ID in both makes it easier to match them and debug.
162 username = project_name
LingxianKong9c713d22015-06-09 15:19:55 +0800163 user_password = data_utils.rand_password()
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300164 email = data_utils.rand_name(root) + "@example.com"
Andrea Frittolic3280152015-02-26 12:42:34 +0000165 user = self.creds_client.create_user(
LingxianKong9c713d22015-06-09 15:19:55 +0800166 username, user_password, project, email)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400167 role_assigned = False
Matthew Treinishb86cda92013-07-29 11:22:23 -0400168 if admin:
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300169 self.creds_client.assign_user_role(user, project, self.admin_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400170 role_assigned = True
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100171 if (self.identity_version == 'v3' and
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400172 self.identity_admin_domain_scope):
Andrea Frittoli (andreaf)4bee2e72015-09-22 13:06:18 +0100173 self.creds_client.assign_user_role_on_domain(
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400174 user, self.identity_admin_role)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500175 # Add roles specified in config file
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400176 for conf_role in self.extra_roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000177 self.creds_client.assign_user_role(user, project, conf_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400178 role_assigned = True
Matthew Treinish976e8df2014-12-19 14:21:54 -0500179 # Add roles requested by caller
180 if roles:
181 for role in roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000182 self.creds_client.assign_user_role(user, project, role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400183 role_assigned = True
184 # NOTE(mtreinish) For a user to have access to a project with v3 auth
185 # it must beassigned a role on the project. So we need to ensure that
186 # our newly created user has a role on the newly created project.
187 if self.identity_version == 'v3' and not role_assigned:
Adam Youngb226f8e2016-06-25 21:41:36 -0400188 try:
189 self.creds_client.create_user_role('Member')
190 except lib_exc.Conflict:
191 LOG.warning('Member role already exists, ignoring conflict.')
Matthew Treinish32f98a42015-07-14 19:58:46 -0400192 self.creds_client.assign_user_role(user, project, 'Member')
193
LingxianKong9c713d22015-06-09 15:19:55 +0800194 creds = self.creds_client.get_credentials(user, project, user_password)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400195 return cred_provider.TestResources(creds)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500196
197 def _create_network_resources(self, tenant_id):
edannon6cc6fbc2016-05-03 11:56:12 +0300198 """The function creates network resources in the given tenant.
199
200 The function checks if network_resources class member is empty,
201 In case it is, it will create a network, a subnet and a router for
202 the tenant according to the given tenant id parameter.
203 Otherwise it will create a network resource according
204 to the values from network_resources dict.
205
206 :param tenant_id: The tenant id to create resources for.
207 :type tenant_id: str
208 :raises: InvalidConfiguration, Exception
209 :returns: network resources(network,subnet,router)
210 :rtype: tuple
211 """
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500212 network = None
213 subnet = None
214 router = None
Matthew Treinish9f756a02014-01-15 10:26:07 -0500215 # Make sure settings
216 if self.network_resources:
217 if self.network_resources['router']:
218 if (not self.network_resources['subnet'] or
219 not self.network_resources['network']):
220 raise exceptions.InvalidConfiguration(
221 'A router requires a subnet and network')
222 elif self.network_resources['subnet']:
223 if not self.network_resources['network']:
224 raise exceptions.InvalidConfiguration(
225 'A subnet requires a network')
226 elif self.network_resources['dhcp']:
227 raise exceptions.InvalidConfiguration('DHCP requires a subnet')
228
Masayuki Igawa259c1132013-10-31 17:48:44 +0900229 data_utils.rand_name_root = data_utils.rand_name(self.name)
Matthew Treinish9f756a02014-01-15 10:26:07 -0500230 if not self.network_resources or self.network_resources['network']:
231 network_name = data_utils.rand_name_root + "-network"
232 network = self._create_network(network_name, tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500233 try:
Matthew Treinish9f756a02014-01-15 10:26:07 -0500234 if not self.network_resources or self.network_resources['subnet']:
235 subnet_name = data_utils.rand_name_root + "-subnet"
236 subnet = self._create_subnet(subnet_name, tenant_id,
237 network['id'])
238 if not self.network_resources or self.network_resources['router']:
239 router_name = data_utils.rand_name_root + "-router"
240 router = self._create_router(router_name, tenant_id)
241 self._add_router_interface(router['id'], subnet['id'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500242 except Exception:
Andrea Frittoli (andreaf)d9a18b02016-02-29 15:27:34 +0000243 try:
244 if router:
245 self._clear_isolated_router(router['id'], router['name'])
246 if subnet:
247 self._clear_isolated_subnet(subnet['id'], subnet['name'])
248 if network:
249 self._clear_isolated_network(network['id'],
250 network['name'])
251 except Exception as cleanup_exception:
252 msg = "There was an exception trying to setup network " \
253 "resources for tenant %s, and this error happened " \
254 "trying to clean them up: %s"
255 LOG.warning(msg % (tenant_id, cleanup_exception))
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500256 raise
257 return network, subnet, router
258
259 def _create_network(self, name, tenant_id):
John Warren94d8faf2015-09-15 12:22:24 -0400260 resp_body = self.networks_admin_client.create_network(
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100261 name=name, tenant_id=tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500262 return resp_body['network']
263
264 def _create_subnet(self, subnet_name, tenant_id, network_id):
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400265 base_cidr = netaddr.IPNetwork(self.project_network_cidr)
266 mask_bits = self.project_network_mask_bits
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500267 for subnet_cidr in base_cidr.subnet(mask_bits):
268 try:
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100269 if self.network_resources:
John Warren3961acd2015-10-02 14:38:53 -0400270 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100271 create_subnet(
272 network_id=network_id, cidr=str(subnet_cidr),
273 name=subnet_name,
274 tenant_id=tenant_id,
275 enable_dhcp=self.network_resources['dhcp'],
276 ip_version=4)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500277 else:
John Warren3961acd2015-10-02 14:38:53 -0400278 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100279 create_subnet(network_id=network_id,
280 cidr=str(subnet_cidr),
281 name=subnet_name,
282 tenant_id=tenant_id,
283 ip_version=4)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500284 break
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900285 except lib_exc.BadRequest as e:
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500286 if 'overlaps with another subnet' not in str(e):
287 raise
288 else:
David Kranzd4210412014-11-21 08:37:45 -0500289 message = 'Available CIDR for subnet creation could not be found'
290 raise Exception(message)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500291 return resp_body['subnet']
292
293 def _create_router(self, router_name, tenant_id):
294 external_net_id = dict(
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400295 network_id=self.public_network_id)
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000296 resp_body = self.routers_admin_client.create_router(
Ken'ichi Ohmichi6665c972016-02-24 13:09:09 -0800297 name=router_name,
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100298 external_gateway_info=external_net_id,
299 tenant_id=tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500300 return resp_body['router']
301
302 def _add_router_interface(self, router_id, subnet_id):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000303 self.routers_admin_client.add_router_interface(router_id,
piyush11078694aca952015-12-17 12:54:44 +0530304 subnet_id=subnet_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500305
Andrea Frittoli9612e812014-03-13 10:57:26 +0000306 def get_credentials(self, credential_type):
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700307 if self._creds.get(str(credential_type)):
308 credentials = self._creds[str(credential_type)]
Matthew Treinishb86cda92013-07-29 11:22:23 -0400309 else:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500310 if credential_type in ['primary', 'alt', 'admin']:
311 is_admin = (credential_type == 'admin')
312 credentials = self._create_creds(admin=is_admin)
313 else:
314 credentials = self._create_creds(roles=credential_type)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700315 self._creds[str(credential_type)] = credentials
Andrea Frittolifc315902014-03-20 09:21:44 +0000316 # Maintained until tests are ported
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700317 LOG.info("Acquired dynamic creds:\n credentials: %s"
Andrea Frittolifc315902014-03-20 09:21:44 +0000318 % credentials)
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400319 if (self.neutron_available and
320 self.create_networks):
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500321 network, subnet, router = self._create_network_resources(
Andrea Frittolifc315902014-03-20 09:21:44 +0000322 credentials.tenant_id)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400323 credentials.set_resources(network=network, subnet=subnet,
324 router=router)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500325 LOG.info("Created isolated network resources for : \n"
Andrea Frittolifc315902014-03-20 09:21:44 +0000326 + " credentials: %s" % credentials)
Andrea Frittoli9612e812014-03-13 10:57:26 +0000327 return credentials
Matthew Treinishb86cda92013-07-29 11:22:23 -0400328
Andrea Frittoli9612e812014-03-13 10:57:26 +0000329 def get_primary_creds(self):
330 return self.get_credentials('primary')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400331
Andrea Frittoli9612e812014-03-13 10:57:26 +0000332 def get_admin_creds(self):
333 return self.get_credentials('admin')
Andrea Frittolifc315902014-03-20 09:21:44 +0000334
Andrea Frittoli9612e812014-03-13 10:57:26 +0000335 def get_alt_creds(self):
336 return self.get_credentials('alt')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400337
Matthew Treinish976e8df2014-12-19 14:21:54 -0500338 def get_creds_by_roles(self, roles, force_new=False):
339 roles = list(set(roles))
340 # The roles list as a str will become the index as the dict key for
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700341 # the created credentials set in the dynamic_creds dict.
342 exist_creds = self._creds.get(str(roles))
Matthew Treinish976e8df2014-12-19 14:21:54 -0500343 # If force_new flag is True 2 cred sets with the same roles are needed
344 # handle this by creating a separate index for old one to store it
345 # separately for cleanup
346 if exist_creds and force_new:
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700347 new_index = str(roles) + '-' + str(len(self._creds))
348 self._creds[new_index] = exist_creds
349 del self._creds[str(roles)]
Matthew Treinish976e8df2014-12-19 14:21:54 -0500350 return self.get_credentials(roles)
351
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500352 def _clear_isolated_router(self, router_id, router_name):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000353 client = self.routers_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500354 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000355 client.delete_router(router_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900356 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000357 LOG.warning('router with name: %s not found for delete' %
358 router_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500359
360 def _clear_isolated_subnet(self, subnet_id, subnet_name):
John Warren3961acd2015-10-02 14:38:53 -0400361 client = self.subnets_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500362 try:
John Warren3961acd2015-10-02 14:38:53 -0400363 client.delete_subnet(subnet_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900364 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000365 LOG.warning('subnet with name: %s not found for delete' %
366 subnet_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500367
368 def _clear_isolated_network(self, network_id, network_name):
John Warren94d8faf2015-09-15 12:22:24 -0400369 net_client = self.networks_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500370 try:
371 net_client.delete_network(network_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900372 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000373 LOG.warning('network with name: %s not found for delete' %
374 network_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500375
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100376 def _cleanup_default_secgroup(self, tenant):
John Warrenf9606e92015-12-10 12:12:42 -0500377 nsg_client = self.security_groups_admin_client
378 resp_body = nsg_client.list_security_groups(tenant_id=tenant,
David Kranz34e88122014-12-11 15:24:05 -0500379 name="default")
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100380 secgroups_to_delete = resp_body['security_groups']
381 for secgroup in secgroups_to_delete:
382 try:
John Warrenf9606e92015-12-10 12:12:42 -0500383 nsg_client.delete_security_group(secgroup['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900384 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000385 LOG.warning('Security group %s, id %s not found for clean-up' %
386 (secgroup['name'], secgroup['id']))
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100387
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500388 def _clear_isolated_net_resources(self):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000389 client = self.routers_admin_client
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700390 for cred in self._creds:
391 creds = self._creds.get(cred)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400392 if (not creds or not any([creds.router, creds.network,
393 creds.subnet])):
394 continue
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800395 LOG.debug("Clearing network: %(network)s, "
Matthew Treinishfe094ea2014-12-09 01:19:27 +0000396 "subnet: %(subnet)s, router: %(router)s",
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400397 {'network': creds.network, 'subnet': creds.subnet,
398 'router': creds.router})
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800399 if (not self.network_resources or
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400400 (self.network_resources.get('router') and creds.subnet)):
Matthew Treinish9f756a02014-01-15 10:26:07 -0500401 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000402 client.remove_router_interface(
piyush11078694aca952015-12-17 12:54:44 +0530403 creds.router['id'],
404 subnet_id=creds.subnet['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900405 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000406 LOG.warning('router with name: %s not found for delete' %
407 creds.router['name'])
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400408 self._clear_isolated_router(creds.router['id'],
409 creds.router['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800410 if (not self.network_resources or
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800411 self.network_resources.get('subnet')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400412 self._clear_isolated_subnet(creds.subnet['id'],
413 creds.subnet['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800414 if (not self.network_resources or
415 self.network_resources.get('network')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400416 self._clear_isolated_network(creds.network['id'],
417 creds.network['name'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500418
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700419 def clear_creds(self):
420 if not self._creds:
Matthew Treinishb86cda92013-07-29 11:22:23 -0400421 return
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500422 self._clear_isolated_net_resources()
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700423 for creds in six.itervalues(self._creds):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400424 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000425 self.creds_client.delete_user(creds.user_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900426 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000427 LOG.warning("user with name: %s not found for delete" %
428 creds.username)
zhufl2344ea62016-06-01 14:44:00 +0800429 # NOTE(zhufl): Only when neutron's security_group ext is
430 # enabled, _cleanup_default_secgroup will not raise error. But
431 # here cannot use test.is_extension_enabled for it will cause
432 # "circular dependency". So here just use try...except to
433 # ensure tenant deletion without big changes.
Matthew Treinishb86cda92013-07-29 11:22:23 -0400434 try:
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400435 if self.neutron_available:
Andrea Frittolic3280152015-02-26 12:42:34 +0000436 self._cleanup_default_secgroup(creds.tenant_id)
zhufl2344ea62016-06-01 14:44:00 +0800437 except lib_exc.NotFound:
438 LOG.warning("failed to cleanup tenant %s's secgroup" %
439 creds.tenant_name)
440 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000441 self.creds_client.delete_project(creds.tenant_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900442 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000443 LOG.warning("tenant with name: %s not found for delete" %
444 creds.tenant_name)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700445 self._creds = {}
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100446
447 def is_multi_user(self):
448 return True
Yair Fried76488d72014-10-21 10:13:19 +0300449
450 def is_multi_tenant(self):
451 return True
Matthew Treinish4a596932015-03-06 20:37:01 -0500452
453 def is_role_available(self, role):
454 return True