blob: c9b9db12748fc40af9279f3248e4768aef00ff19 [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 config
24from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050025from tempest.lib import exceptions as lib_exc
Matthew Treinishb86cda92013-07-29 11:22:23 -040026
Sean Dague86bd8422013-12-20 09:56:44 -050027CONF = config.CONF
Matthew Treinishb86cda92013-07-29 11:22:23 -040028LOG = logging.getLogger(__name__)
29
30
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070031class DynamicCredentialProvider(cred_provider.CredentialProvider):
Matthew Treinishb86cda92013-07-29 11:22:23 -040032
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +010033 def __init__(self, identity_version, name=None, network_resources=None,
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010034 credentials_domain=None, admin_role=None, admin_creds=None):
35 """Creates credentials dynamically for tests
36
37 A credential provider that, based on an initial set of
38 admin credentials, creates new credentials on the fly for
39 tests to use and then discard.
40
41 :param str identity_version: identity API version to use `v2` or `v3`
42 :param str admin_role: name of the admin role added to admin users
43 :param str name: names of dynamic resources include this parameter
44 when specified
45 :param str credentials_domain: name of the domain where the users
46 are created. If not defined, the project
47 domain from admin_credentials is used
48 :param dict network_resources: network resources to be created for
49 the created credentials
50 :param Credentials admin_creds: initial admin credentials
51 """
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070052 super(DynamicCredentialProvider, self).__init__(
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010053 identity_version=identity_version, admin_role=admin_role,
54 name=name, credentials_domain=credentials_domain,
55 network_resources=network_resources)
56 self.network_resources = network_resources
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070057 self._creds = {}
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050058 self.ports = []
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010059 self.default_admin_creds = admin_creds
Yaroslav Lobankov47a93ab2016-02-07 16:32:49 -060060 (self.identity_admin_client,
61 self.tenants_admin_client,
Daniel Mellado82c83a52015-12-09 15:16:49 +000062 self.users_admin_client,
Daniel Mellado7aea5342016-02-09 09:10:12 +000063 self.roles_admin_client,
Daniel Mellado91a26b62016-02-11 11:13:04 +000064 self.domains_admin_client,
John Warren3961acd2015-10-02 14:38:53 -040065 self.networks_admin_client,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +000066 self.routers_admin_client,
John Warren49c0fe52015-10-22 12:35:54 -040067 self.subnets_admin_client,
John Warrenf9606e92015-12-10 12:12:42 -050068 self.ports_admin_client,
69 self.security_groups_admin_client) = self._get_admin_clients()
John Warren3961acd2015-10-02 14:38:53 -040070 # Domain where isolated credentials are provisioned (v3 only).
Andrea Frittolic3280152015-02-26 12:42:34 +000071 # Use that of the admin account is None is configured.
72 self.creds_domain_name = None
73 if self.identity_version == 'v3':
74 self.creds_domain_name = (
David Kranz87fc7e92015-07-28 14:05:20 -040075 self.default_admin_creds.project_domain_name or
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +010076 self.credentials_domain)
Jamie Lennox15350172015-08-17 10:54:25 +100077 self.creds_client = cred_client.get_creds_client(
Daniel Melladob04da902015-11-20 17:43:12 +010078 self.identity_admin_client,
79 self.tenants_admin_client,
Daniel Mellado82c83a52015-12-09 15:16:49 +000080 self.users_admin_client,
Daniel Mellado7aea5342016-02-09 09:10:12 +000081 self.roles_admin_client,
Daniel Mellado91a26b62016-02-11 11:13:04 +000082 self.domains_admin_client,
Daniel Melladob04da902015-11-20 17:43:12 +010083 self.creds_domain_name)
Matthew Treinishb86cda92013-07-29 11:22:23 -040084
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050085 def _get_admin_clients(self):
Ken'ichi Ohmichicb67d2d2015-11-19 08:23:22 +000086 """Returns a tuple with instances of the following admin clients
87
88 (in this order):
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050089 identity
90 network
Matthew Treinishb86cda92013-07-29 11:22:23 -040091 """
Andrea Frittolic3280152015-02-26 12:42:34 +000092 os = clients.Manager(self.default_admin_creds)
93 if self.identity_version == 'v2':
Daniel Mellado7aea5342016-02-09 09:10:12 +000094 return (os.identity_client, os.tenants_client, os.users_client,
Ken'ichi Ohmichi43e7fcf2016-04-04 11:59:13 -070095 os.roles_client, None,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +000096 os.networks_client, os.routers_client, os.subnets_client,
97 os.ports_client, os.security_groups_client)
Andrea Frittolic3280152015-02-26 12:42:34 +000098 else:
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +010099 # We use a dedicated client manager for identity client in case we
100 # need a different token scope for them.
101 scope = 'domain' if CONF.identity.admin_domain_scope else 'project'
102 identity_os = clients.Manager(self.default_admin_creds,
103 scope=scope)
104 return (identity_os.identity_v3_client,
105 identity_os.projects_client,
106 identity_os.users_v3_client, identity_os.roles_v3_client,
107 identity_os.domains_client,
Ken'ichi Ohmichi43e7fcf2016-04-04 11:59:13 -0700108 os.networks_client, os.routers_client,
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000109 os.subnets_client, os.ports_client,
110 os.security_groups_client)
Matthew Treinishb86cda92013-07-29 11:22:23 -0400111
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300112 def _create_creds(self, admin=False, roles=None):
113 """Create credentials with random name.
Sean Dague6969b902014-01-28 06:48:37 -0500114
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300115 Creates project and user. When admin flag is True create user
116 with admin role. Assign user with additional roles (for example
117 _member_) and roles requested by caller.
Sean Dague6969b902014-01-28 06:48:37 -0500118
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300119 :param admin: Flag if to assign to the user admin role
120 :type admin: bool
121 :param roles: Roles to assign for the user
122 :type roles: list
123 :return: Readonly Credentials with network resources
Sean Dague6969b902014-01-28 06:48:37 -0500124 """
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300125 root = self.name
Sean Dague6969b902014-01-28 06:48:37 -0500126
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300127 project_name = data_utils.rand_name(root)
Andrea Frittolic3280152015-02-26 12:42:34 +0000128 project_desc = project_name + "-desc"
129 project = self.creds_client.create_project(
130 name=project_name, description=project_desc)
Sean Dague6969b902014-01-28 06:48:37 -0500131
Andrea Frittoli (andreaf)ef9b01f2016-06-02 10:25:25 +0100132 # NOTE(andreaf) User and project can be distinguished from the context,
133 # having the same ID in both makes it easier to match them and debug.
134 username = project_name
LingxianKong9c713d22015-06-09 15:19:55 +0800135 user_password = data_utils.rand_password()
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300136 email = data_utils.rand_name(root) + "@example.com"
Andrea Frittolic3280152015-02-26 12:42:34 +0000137 user = self.creds_client.create_user(
LingxianKong9c713d22015-06-09 15:19:55 +0800138 username, user_password, project, email)
John Warren56317e02015-08-12 20:48:32 +0000139 if 'user' in user:
140 user = user['user']
Matthew Treinish32f98a42015-07-14 19:58:46 -0400141 role_assigned = False
Matthew Treinishb86cda92013-07-29 11:22:23 -0400142 if admin:
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300143 self.creds_client.assign_user_role(user, project, self.admin_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400144 role_assigned = True
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100145 if (self.identity_version == 'v3' and
146 CONF.identity.admin_domain_scope):
Andrea Frittoli (andreaf)4bee2e72015-09-22 13:06:18 +0100147 self.creds_client.assign_user_role_on_domain(
148 user, CONF.identity.admin_role)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500149 # Add roles specified in config file
150 for conf_role in CONF.auth.tempest_roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000151 self.creds_client.assign_user_role(user, project, conf_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400152 role_assigned = True
Matthew Treinish976e8df2014-12-19 14:21:54 -0500153 # Add roles requested by caller
154 if roles:
155 for role in roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000156 self.creds_client.assign_user_role(user, project, role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400157 role_assigned = True
158 # NOTE(mtreinish) For a user to have access to a project with v3 auth
159 # it must beassigned a role on the project. So we need to ensure that
160 # our newly created user has a role on the newly created project.
161 if self.identity_version == 'v3' and not role_assigned:
Adam Youngb226f8e2016-06-25 21:41:36 -0400162 try:
163 self.creds_client.create_user_role('Member')
164 except lib_exc.Conflict:
165 LOG.warning('Member role already exists, ignoring conflict.')
Matthew Treinish32f98a42015-07-14 19:58:46 -0400166 self.creds_client.assign_user_role(user, project, 'Member')
167
LingxianKong9c713d22015-06-09 15:19:55 +0800168 creds = self.creds_client.get_credentials(user, project, user_password)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400169 return cred_provider.TestResources(creds)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500170
171 def _create_network_resources(self, tenant_id):
edannon6cc6fbc2016-05-03 11:56:12 +0300172 """The function creates network resources in the given tenant.
173
174 The function checks if network_resources class member is empty,
175 In case it is, it will create a network, a subnet and a router for
176 the tenant according to the given tenant id parameter.
177 Otherwise it will create a network resource according
178 to the values from network_resources dict.
179
180 :param tenant_id: The tenant id to create resources for.
181 :type tenant_id: str
182 :raises: InvalidConfiguration, Exception
183 :returns: network resources(network,subnet,router)
184 :rtype: tuple
185 """
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500186 network = None
187 subnet = None
188 router = None
Matthew Treinish9f756a02014-01-15 10:26:07 -0500189 # Make sure settings
190 if self.network_resources:
191 if self.network_resources['router']:
192 if (not self.network_resources['subnet'] or
193 not self.network_resources['network']):
194 raise exceptions.InvalidConfiguration(
195 'A router requires a subnet and network')
196 elif self.network_resources['subnet']:
197 if not self.network_resources['network']:
198 raise exceptions.InvalidConfiguration(
199 'A subnet requires a network')
200 elif self.network_resources['dhcp']:
201 raise exceptions.InvalidConfiguration('DHCP requires a subnet')
202
Masayuki Igawa259c1132013-10-31 17:48:44 +0900203 data_utils.rand_name_root = data_utils.rand_name(self.name)
Matthew Treinish9f756a02014-01-15 10:26:07 -0500204 if not self.network_resources or self.network_resources['network']:
205 network_name = data_utils.rand_name_root + "-network"
206 network = self._create_network(network_name, tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500207 try:
Matthew Treinish9f756a02014-01-15 10:26:07 -0500208 if not self.network_resources or self.network_resources['subnet']:
209 subnet_name = data_utils.rand_name_root + "-subnet"
210 subnet = self._create_subnet(subnet_name, tenant_id,
211 network['id'])
212 if not self.network_resources or self.network_resources['router']:
213 router_name = data_utils.rand_name_root + "-router"
214 router = self._create_router(router_name, tenant_id)
215 self._add_router_interface(router['id'], subnet['id'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500216 except Exception:
Andrea Frittoli (andreaf)d9a18b02016-02-29 15:27:34 +0000217 try:
218 if router:
219 self._clear_isolated_router(router['id'], router['name'])
220 if subnet:
221 self._clear_isolated_subnet(subnet['id'], subnet['name'])
222 if network:
223 self._clear_isolated_network(network['id'],
224 network['name'])
225 except Exception as cleanup_exception:
226 msg = "There was an exception trying to setup network " \
227 "resources for tenant %s, and this error happened " \
228 "trying to clean them up: %s"
229 LOG.warning(msg % (tenant_id, cleanup_exception))
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500230 raise
231 return network, subnet, router
232
233 def _create_network(self, name, tenant_id):
John Warren94d8faf2015-09-15 12:22:24 -0400234 resp_body = self.networks_admin_client.create_network(
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100235 name=name, tenant_id=tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500236 return resp_body['network']
237
238 def _create_subnet(self, subnet_name, tenant_id, network_id):
Sean Dagueed6e5862016-04-04 10:49:13 -0400239 base_cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)
240 mask_bits = CONF.network.project_network_mask_bits
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500241 for subnet_cidr in base_cidr.subnet(mask_bits):
242 try:
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100243 if self.network_resources:
John Warren3961acd2015-10-02 14:38:53 -0400244 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100245 create_subnet(
246 network_id=network_id, cidr=str(subnet_cidr),
247 name=subnet_name,
248 tenant_id=tenant_id,
249 enable_dhcp=self.network_resources['dhcp'],
250 ip_version=4)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500251 else:
John Warren3961acd2015-10-02 14:38:53 -0400252 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100253 create_subnet(network_id=network_id,
254 cidr=str(subnet_cidr),
255 name=subnet_name,
256 tenant_id=tenant_id,
257 ip_version=4)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500258 break
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900259 except lib_exc.BadRequest as e:
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500260 if 'overlaps with another subnet' not in str(e):
261 raise
262 else:
David Kranzd4210412014-11-21 08:37:45 -0500263 message = 'Available CIDR for subnet creation could not be found'
264 raise Exception(message)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500265 return resp_body['subnet']
266
267 def _create_router(self, router_name, tenant_id):
268 external_net_id = dict(
Sean Dague86bd8422013-12-20 09:56:44 -0500269 network_id=CONF.network.public_network_id)
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000270 resp_body = self.routers_admin_client.create_router(
Ken'ichi Ohmichi6665c972016-02-24 13:09:09 -0800271 name=router_name,
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100272 external_gateway_info=external_net_id,
273 tenant_id=tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500274 return resp_body['router']
275
276 def _add_router_interface(self, router_id, subnet_id):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000277 self.routers_admin_client.add_router_interface(router_id,
piyush11078694aca952015-12-17 12:54:44 +0530278 subnet_id=subnet_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500279
Andrea Frittoli9612e812014-03-13 10:57:26 +0000280 def get_credentials(self, credential_type):
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700281 if self._creds.get(str(credential_type)):
282 credentials = self._creds[str(credential_type)]
Matthew Treinishb86cda92013-07-29 11:22:23 -0400283 else:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500284 if credential_type in ['primary', 'alt', 'admin']:
285 is_admin = (credential_type == 'admin')
286 credentials = self._create_creds(admin=is_admin)
287 else:
288 credentials = self._create_creds(roles=credential_type)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700289 self._creds[str(credential_type)] = credentials
Andrea Frittolifc315902014-03-20 09:21:44 +0000290 # Maintained until tests are ported
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700291 LOG.info("Acquired dynamic creds:\n credentials: %s"
Andrea Frittolifc315902014-03-20 09:21:44 +0000292 % credentials)
Adam Gandelman85395e72014-07-29 18:34:33 -0700293 if (CONF.service_available.neutron and
Matthew Treinish2219d382015-04-24 10:33:04 -0400294 not CONF.baremetal.driver_enabled and
295 CONF.auth.create_isolated_networks):
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500296 network, subnet, router = self._create_network_resources(
Andrea Frittolifc315902014-03-20 09:21:44 +0000297 credentials.tenant_id)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400298 credentials.set_resources(network=network, subnet=subnet,
299 router=router)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500300 LOG.info("Created isolated network resources for : \n"
Andrea Frittolifc315902014-03-20 09:21:44 +0000301 + " credentials: %s" % credentials)
Andrea Frittoli9612e812014-03-13 10:57:26 +0000302 return credentials
Matthew Treinishb86cda92013-07-29 11:22:23 -0400303
Andrea Frittoli9612e812014-03-13 10:57:26 +0000304 def get_primary_creds(self):
305 return self.get_credentials('primary')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400306
Andrea Frittoli9612e812014-03-13 10:57:26 +0000307 def get_admin_creds(self):
308 return self.get_credentials('admin')
Andrea Frittolifc315902014-03-20 09:21:44 +0000309
Andrea Frittoli9612e812014-03-13 10:57:26 +0000310 def get_alt_creds(self):
311 return self.get_credentials('alt')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400312
Matthew Treinish976e8df2014-12-19 14:21:54 -0500313 def get_creds_by_roles(self, roles, force_new=False):
314 roles = list(set(roles))
315 # The roles list as a str will become the index as the dict key for
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700316 # the created credentials set in the dynamic_creds dict.
317 exist_creds = self._creds.get(str(roles))
Matthew Treinish976e8df2014-12-19 14:21:54 -0500318 # If force_new flag is True 2 cred sets with the same roles are needed
319 # handle this by creating a separate index for old one to store it
320 # separately for cleanup
321 if exist_creds and force_new:
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700322 new_index = str(roles) + '-' + str(len(self._creds))
323 self._creds[new_index] = exist_creds
324 del self._creds[str(roles)]
Matthew Treinish976e8df2014-12-19 14:21:54 -0500325 return self.get_credentials(roles)
326
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500327 def _clear_isolated_router(self, router_id, router_name):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000328 client = self.routers_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500329 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000330 client.delete_router(router_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900331 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000332 LOG.warning('router with name: %s not found for delete' %
333 router_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500334
335 def _clear_isolated_subnet(self, subnet_id, subnet_name):
John Warren3961acd2015-10-02 14:38:53 -0400336 client = self.subnets_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500337 try:
John Warren3961acd2015-10-02 14:38:53 -0400338 client.delete_subnet(subnet_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900339 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000340 LOG.warning('subnet with name: %s not found for delete' %
341 subnet_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500342
343 def _clear_isolated_network(self, network_id, network_name):
John Warren94d8faf2015-09-15 12:22:24 -0400344 net_client = self.networks_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500345 try:
346 net_client.delete_network(network_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900347 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000348 LOG.warning('network with name: %s not found for delete' %
349 network_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500350
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100351 def _cleanup_default_secgroup(self, tenant):
John Warrenf9606e92015-12-10 12:12:42 -0500352 nsg_client = self.security_groups_admin_client
353 resp_body = nsg_client.list_security_groups(tenant_id=tenant,
David Kranz34e88122014-12-11 15:24:05 -0500354 name="default")
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100355 secgroups_to_delete = resp_body['security_groups']
356 for secgroup in secgroups_to_delete:
357 try:
John Warrenf9606e92015-12-10 12:12:42 -0500358 nsg_client.delete_security_group(secgroup['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900359 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000360 LOG.warning('Security group %s, id %s not found for clean-up' %
361 (secgroup['name'], secgroup['id']))
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100362
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500363 def _clear_isolated_net_resources(self):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000364 client = self.routers_admin_client
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700365 for cred in self._creds:
366 creds = self._creds.get(cred)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400367 if (not creds or not any([creds.router, creds.network,
368 creds.subnet])):
369 continue
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800370 LOG.debug("Clearing network: %(network)s, "
Matthew Treinishfe094ea2014-12-09 01:19:27 +0000371 "subnet: %(subnet)s, router: %(router)s",
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400372 {'network': creds.network, 'subnet': creds.subnet,
373 'router': creds.router})
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800374 if (not self.network_resources or
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400375 (self.network_resources.get('router') and creds.subnet)):
Matthew Treinish9f756a02014-01-15 10:26:07 -0500376 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000377 client.remove_router_interface(
piyush11078694aca952015-12-17 12:54:44 +0530378 creds.router['id'],
379 subnet_id=creds.subnet['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900380 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000381 LOG.warning('router with name: %s not found for delete' %
382 creds.router['name'])
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400383 self._clear_isolated_router(creds.router['id'],
384 creds.router['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800385 if (not self.network_resources or
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800386 self.network_resources.get('subnet')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400387 self._clear_isolated_subnet(creds.subnet['id'],
388 creds.subnet['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800389 if (not self.network_resources or
390 self.network_resources.get('network')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400391 self._clear_isolated_network(creds.network['id'],
392 creds.network['name'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500393
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700394 def clear_creds(self):
395 if not self._creds:
Matthew Treinishb86cda92013-07-29 11:22:23 -0400396 return
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500397 self._clear_isolated_net_resources()
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700398 for creds in six.itervalues(self._creds):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400399 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000400 self.creds_client.delete_user(creds.user_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900401 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000402 LOG.warning("user with name: %s not found for delete" %
403 creds.username)
Matthew Treinishb86cda92013-07-29 11:22:23 -0400404 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000405 if CONF.service_available.neutron:
406 self._cleanup_default_secgroup(creds.tenant_id)
407 self.creds_client.delete_project(creds.tenant_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900408 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000409 LOG.warning("tenant with name: %s not found for delete" %
410 creds.tenant_name)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700411 self._creds = {}
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100412
413 def is_multi_user(self):
414 return True
Yair Fried76488d72014-10-21 10:13:19 +0300415
416 def is_multi_tenant(self):
417 return True
Matthew Treinish4a596932015-03-06 20:37:01 -0500418
419 def is_role_available(self, role):
420 return True