blob: 632a87687c1e3b8f868bfbc78f34eba9b5b11106 [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
Matthew Treinish3787e4c2016-10-07 21:25:33 -040020from tempest.lib.common import cred_client
Matthew Treinish00ab6be2016-10-07 16:29:18 -040021from tempest.lib.common import cred_provider
Matthew Treinish0650aed2016-10-07 16:36:46 -040022from tempest.lib.common.utils import data_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050023from tempest.lib import exceptions as lib_exc
Matthew Treinishb86cda92013-07-29 11:22:23 -040024
25LOG = logging.getLogger(__name__)
26
27
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070028class DynamicCredentialProvider(cred_provider.CredentialProvider):
Matthew Treinishb86cda92013-07-29 11:22:23 -040029
Andrea Frittoli (andreaf)1eb04962015-10-09 14:48:06 +010030 def __init__(self, identity_version, name=None, network_resources=None,
Matthew Treinish75abbcf2016-10-07 16:19:12 -040031 credentials_domain=None, admin_role=None, admin_creds=None,
32 identity_admin_domain_scope=False,
33 identity_admin_role='admin', extra_roles=None,
34 neutron_available=False, create_networks=True,
35 project_network_cidr=None, project_network_mask_bits=None,
Matthew Treinish0650aed2016-10-07 16:36:46 -040036 public_network_id=None, resource_prefix=None):
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010037 """Creates credentials dynamically for tests
38
39 A credential provider that, based on an initial set of
40 admin credentials, creates new credentials on the fly for
41 tests to use and then discard.
42
43 :param str identity_version: identity API version to use `v2` or `v3`
44 :param str admin_role: name of the admin role added to admin users
45 :param str name: names of dynamic resources include this parameter
46 when specified
47 :param str credentials_domain: name of the domain where the users
48 are created. If not defined, the project
49 domain from admin_credentials is used
50 :param dict network_resources: network resources to be created for
51 the created credentials
52 :param Credentials admin_creds: initial admin credentials
Matthew Treinish75abbcf2016-10-07 16:19:12 -040053 :param bool identity_admin_domain_scope: Set to true if admin should be
54 scoped to the domain. By
55 default this is False and the
56 admin role is scoped to the
57 project.
58 :param str identity_admin_role: The role name to use for admin
59 :param list extra_roles: A list of strings for extra roles that should
60 be assigned to all created users
61 :param bool neutron_available: Whether we are running in an environemnt
62 with neutron
63 :param bool create_networks: Whether dynamic project networks should be
64 created or not
65 :param project_network_cidr: The CIDR to use for created project
66 networks
67 :param project_network_mask_bits: The network mask bits to use for
68 created project networks
69 :param public_network_id: The id for the public network to use
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010070 """
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070071 super(DynamicCredentialProvider, self).__init__(
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010072 identity_version=identity_version, admin_role=admin_role,
73 name=name, credentials_domain=credentials_domain,
74 network_resources=network_resources)
75 self.network_resources = network_resources
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -070076 self._creds = {}
Miguel Lavalleb8fabc52013-08-23 11:19:57 -050077 self.ports = []
Matthew Treinish0650aed2016-10-07 16:36:46 -040078 self.resource_prefix = resource_prefix or ''
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
Matthew Treinish0650aed2016-10-07 16:36:46 -0400155 project_name = data_utils.rand_name(root, prefix=self.resource_prefix)
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()
Matthew Treinish0650aed2016-10-07 16:36:46 -0400164 email = data_utils.rand_name(
165 root, prefix=self.resource_prefix) + "@example.com"
Andrea Frittolic3280152015-02-26 12:42:34 +0000166 user = self.creds_client.create_user(
LingxianKong9c713d22015-06-09 15:19:55 +0800167 username, user_password, project, email)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400168 role_assigned = False
Matthew Treinishb86cda92013-07-29 11:22:23 -0400169 if admin:
Genadi Chereshnya88ea9ab2016-05-15 14:47:07 +0300170 self.creds_client.assign_user_role(user, project, self.admin_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400171 role_assigned = True
Andrea Frittoli (andreaf)100d18d2016-05-05 23:34:52 +0100172 if (self.identity_version == 'v3' and
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400173 self.identity_admin_domain_scope):
Andrea Frittoli (andreaf)4bee2e72015-09-22 13:06:18 +0100174 self.creds_client.assign_user_role_on_domain(
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400175 user, self.identity_admin_role)
Matthew Treinish976e8df2014-12-19 14:21:54 -0500176 # Add roles specified in config file
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400177 for conf_role in self.extra_roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000178 self.creds_client.assign_user_role(user, project, conf_role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400179 role_assigned = True
Matthew Treinish976e8df2014-12-19 14:21:54 -0500180 # Add roles requested by caller
181 if roles:
182 for role in roles:
Andrea Frittolic3280152015-02-26 12:42:34 +0000183 self.creds_client.assign_user_role(user, project, role)
Matthew Treinish32f98a42015-07-14 19:58:46 -0400184 role_assigned = True
185 # NOTE(mtreinish) For a user to have access to a project with v3 auth
186 # it must beassigned a role on the project. So we need to ensure that
187 # our newly created user has a role on the newly created project.
188 if self.identity_version == 'v3' and not role_assigned:
Adam Youngb226f8e2016-06-25 21:41:36 -0400189 try:
190 self.creds_client.create_user_role('Member')
191 except lib_exc.Conflict:
192 LOG.warning('Member role already exists, ignoring conflict.')
Matthew Treinish32f98a42015-07-14 19:58:46 -0400193 self.creds_client.assign_user_role(user, project, 'Member')
194
LingxianKong9c713d22015-06-09 15:19:55 +0800195 creds = self.creds_client.get_credentials(user, project, user_password)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400196 return cred_provider.TestResources(creds)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500197
198 def _create_network_resources(self, tenant_id):
edannon6cc6fbc2016-05-03 11:56:12 +0300199 """The function creates network resources in the given tenant.
200
201 The function checks if network_resources class member is empty,
202 In case it is, it will create a network, a subnet and a router for
203 the tenant according to the given tenant id parameter.
204 Otherwise it will create a network resource according
205 to the values from network_resources dict.
206
207 :param tenant_id: The tenant id to create resources for.
208 :type tenant_id: str
209 :raises: InvalidConfiguration, Exception
210 :returns: network resources(network,subnet,router)
211 :rtype: tuple
212 """
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500213 network = None
214 subnet = None
215 router = None
Matthew Treinish9f756a02014-01-15 10:26:07 -0500216 # Make sure settings
217 if self.network_resources:
218 if self.network_resources['router']:
219 if (not self.network_resources['subnet'] or
220 not self.network_resources['network']):
Matthew Treinish4217a702016-10-07 17:27:11 -0400221 raise lib_exc.InvalidConfiguration(
Matthew Treinish9f756a02014-01-15 10:26:07 -0500222 'A router requires a subnet and network')
223 elif self.network_resources['subnet']:
224 if not self.network_resources['network']:
Matthew Treinish4217a702016-10-07 17:27:11 -0400225 raise lib_exc.InvalidConfiguration(
Matthew Treinish9f756a02014-01-15 10:26:07 -0500226 'A subnet requires a network')
227 elif self.network_resources['dhcp']:
Matthew Treinish4217a702016-10-07 17:27:11 -0400228 raise lib_exc.InvalidConfiguration('DHCP requires a subnet')
Matthew Treinish9f756a02014-01-15 10:26:07 -0500229
Matthew Treinish0650aed2016-10-07 16:36:46 -0400230 rand_name_root = data_utils.rand_name(
231 self.name, prefix=self.resource_prefix)
Matthew Treinish9f756a02014-01-15 10:26:07 -0500232 if not self.network_resources or self.network_resources['network']:
Matthew Treinish0650aed2016-10-07 16:36:46 -0400233 network_name = rand_name_root + "-network"
Matthew Treinish9f756a02014-01-15 10:26:07 -0500234 network = self._create_network(network_name, tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500235 try:
Matthew Treinish9f756a02014-01-15 10:26:07 -0500236 if not self.network_resources or self.network_resources['subnet']:
Matthew Treinish0650aed2016-10-07 16:36:46 -0400237 subnet_name = rand_name_root + "-subnet"
Matthew Treinish9f756a02014-01-15 10:26:07 -0500238 subnet = self._create_subnet(subnet_name, tenant_id,
239 network['id'])
240 if not self.network_resources or self.network_resources['router']:
Matthew Treinish0650aed2016-10-07 16:36:46 -0400241 router_name = rand_name_root + "-router"
Matthew Treinish9f756a02014-01-15 10:26:07 -0500242 router = self._create_router(router_name, tenant_id)
243 self._add_router_interface(router['id'], subnet['id'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500244 except Exception:
Andrea Frittoli (andreaf)d9a18b02016-02-29 15:27:34 +0000245 try:
246 if router:
247 self._clear_isolated_router(router['id'], router['name'])
248 if subnet:
249 self._clear_isolated_subnet(subnet['id'], subnet['name'])
250 if network:
251 self._clear_isolated_network(network['id'],
252 network['name'])
253 except Exception as cleanup_exception:
254 msg = "There was an exception trying to setup network " \
255 "resources for tenant %s, and this error happened " \
256 "trying to clean them up: %s"
Jordan Pittier525ec712016-12-07 17:51:26 +0100257 LOG.warning(msg, tenant_id, cleanup_exception)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500258 raise
259 return network, subnet, router
260
261 def _create_network(self, name, tenant_id):
John Warren94d8faf2015-09-15 12:22:24 -0400262 resp_body = self.networks_admin_client.create_network(
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100263 name=name, tenant_id=tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500264 return resp_body['network']
265
266 def _create_subnet(self, subnet_name, tenant_id, network_id):
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400267 base_cidr = netaddr.IPNetwork(self.project_network_cidr)
268 mask_bits = self.project_network_mask_bits
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500269 for subnet_cidr in base_cidr.subnet(mask_bits):
270 try:
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100271 if self.network_resources:
John Warren3961acd2015-10-02 14:38:53 -0400272 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100273 create_subnet(
274 network_id=network_id, cidr=str(subnet_cidr),
275 name=subnet_name,
276 tenant_id=tenant_id,
277 enable_dhcp=self.network_resources['dhcp'],
278 ip_version=4)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500279 else:
John Warren3961acd2015-10-02 14:38:53 -0400280 resp_body = self.subnets_admin_client.\
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100281 create_subnet(network_id=network_id,
282 cidr=str(subnet_cidr),
283 name=subnet_name,
284 tenant_id=tenant_id,
285 ip_version=4)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500286 break
Masayuki Igawa4b29e472015-02-16 10:41:54 +0900287 except lib_exc.BadRequest as e:
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500288 if 'overlaps with another subnet' not in str(e):
289 raise
290 else:
David Kranzd4210412014-11-21 08:37:45 -0500291 message = 'Available CIDR for subnet creation could not be found'
292 raise Exception(message)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500293 return resp_body['subnet']
294
295 def _create_router(self, router_name, tenant_id):
296 external_net_id = dict(
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400297 network_id=self.public_network_id)
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000298 resp_body = self.routers_admin_client.create_router(
Ken'ichi Ohmichi6665c972016-02-24 13:09:09 -0800299 name=router_name,
Andrea Frittoliae9aca02014-09-25 11:43:11 +0100300 external_gateway_info=external_net_id,
301 tenant_id=tenant_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500302 return resp_body['router']
303
304 def _add_router_interface(self, router_id, subnet_id):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000305 self.routers_admin_client.add_router_interface(router_id,
piyush11078694aca952015-12-17 12:54:44 +0530306 subnet_id=subnet_id)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500307
Andrea Frittoli9612e812014-03-13 10:57:26 +0000308 def get_credentials(self, credential_type):
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700309 if self._creds.get(str(credential_type)):
310 credentials = self._creds[str(credential_type)]
Matthew Treinishb86cda92013-07-29 11:22:23 -0400311 else:
Matthew Treinish976e8df2014-12-19 14:21:54 -0500312 if credential_type in ['primary', 'alt', 'admin']:
313 is_admin = (credential_type == 'admin')
314 credentials = self._create_creds(admin=is_admin)
315 else:
316 credentials = self._create_creds(roles=credential_type)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700317 self._creds[str(credential_type)] = credentials
Andrea Frittolifc315902014-03-20 09:21:44 +0000318 # Maintained until tests are ported
Jordan Pittier525ec712016-12-07 17:51:26 +0100319 LOG.info("Acquired dynamic creds:\n credentials: %s", credentials)
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400320 if (self.neutron_available and
321 self.create_networks):
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500322 network, subnet, router = self._create_network_resources(
Andrea Frittolifc315902014-03-20 09:21:44 +0000323 credentials.tenant_id)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400324 credentials.set_resources(network=network, subnet=subnet,
325 router=router)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500326 LOG.info("Created isolated network resources for : \n"
Jordan Pittier525ec712016-12-07 17:51:26 +0100327 + " credentials: %s", credentials)
Andrea Frittoli9612e812014-03-13 10:57:26 +0000328 return credentials
Matthew Treinishb86cda92013-07-29 11:22:23 -0400329
Andrea Frittoli9612e812014-03-13 10:57:26 +0000330 def get_primary_creds(self):
331 return self.get_credentials('primary')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400332
Andrea Frittoli9612e812014-03-13 10:57:26 +0000333 def get_admin_creds(self):
334 return self.get_credentials('admin')
Andrea Frittolifc315902014-03-20 09:21:44 +0000335
Andrea Frittoli9612e812014-03-13 10:57:26 +0000336 def get_alt_creds(self):
337 return self.get_credentials('alt')
Matthew Treinishb86cda92013-07-29 11:22:23 -0400338
Matthew Treinish976e8df2014-12-19 14:21:54 -0500339 def get_creds_by_roles(self, roles, force_new=False):
340 roles = list(set(roles))
341 # The roles list as a str will become the index as the dict key for
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700342 # the created credentials set in the dynamic_creds dict.
343 exist_creds = self._creds.get(str(roles))
Matthew Treinish976e8df2014-12-19 14:21:54 -0500344 # If force_new flag is True 2 cred sets with the same roles are needed
345 # handle this by creating a separate index for old one to store it
346 # separately for cleanup
347 if exist_creds and force_new:
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700348 new_index = str(roles) + '-' + str(len(self._creds))
349 self._creds[new_index] = exist_creds
350 del self._creds[str(roles)]
Matthew Treinish976e8df2014-12-19 14:21:54 -0500351 return self.get_credentials(roles)
352
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500353 def _clear_isolated_router(self, router_id, router_name):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000354 client = self.routers_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500355 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000356 client.delete_router(router_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900357 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100358 LOG.warning('router with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000359 router_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500360
361 def _clear_isolated_subnet(self, subnet_id, subnet_name):
John Warren3961acd2015-10-02 14:38:53 -0400362 client = self.subnets_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500363 try:
John Warren3961acd2015-10-02 14:38:53 -0400364 client.delete_subnet(subnet_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900365 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100366 LOG.warning('subnet with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000367 subnet_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500368
369 def _clear_isolated_network(self, network_id, network_name):
John Warren94d8faf2015-09-15 12:22:24 -0400370 net_client = self.networks_admin_client
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500371 try:
372 net_client.delete_network(network_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900373 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100374 LOG.warning('network with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000375 network_name)
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500376
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100377 def _cleanup_default_secgroup(self, tenant):
John Warrenf9606e92015-12-10 12:12:42 -0500378 nsg_client = self.security_groups_admin_client
379 resp_body = nsg_client.list_security_groups(tenant_id=tenant,
David Kranz34e88122014-12-11 15:24:05 -0500380 name="default")
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100381 secgroups_to_delete = resp_body['security_groups']
382 for secgroup in secgroups_to_delete:
383 try:
John Warrenf9606e92015-12-10 12:12:42 -0500384 nsg_client.delete_security_group(secgroup['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900385 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100386 LOG.warning('Security group %s, id %s not found for clean-up',
387 secgroup['name'], secgroup['id'])
Ala Rezmerita846eb7c2014-03-10 09:06:03 +0100388
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500389 def _clear_isolated_net_resources(self):
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000390 client = self.routers_admin_client
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700391 for cred in self._creds:
392 creds = self._creds.get(cred)
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400393 if (not creds or not any([creds.router, creds.network,
394 creds.subnet])):
395 continue
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800396 LOG.debug("Clearing network: %(network)s, "
Matthew Treinishfe094ea2014-12-09 01:19:27 +0000397 "subnet: %(subnet)s, router: %(router)s",
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400398 {'network': creds.network, 'subnet': creds.subnet,
399 'router': creds.router})
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800400 if (not self.network_resources or
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400401 (self.network_resources.get('router') and creds.subnet)):
Matthew Treinish9f756a02014-01-15 10:26:07 -0500402 try:
Ken'ichi Ohmichie35f4722015-12-22 04:57:11 +0000403 client.remove_router_interface(
piyush11078694aca952015-12-17 12:54:44 +0530404 creds.router['id'],
405 subnet_id=creds.subnet['id'])
Masayuki Igawabfa07602015-01-20 18:47:17 +0900406 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100407 LOG.warning('router with name: %s not found for delete',
zhangguoqing6c096642016-01-04 06:17:21 +0000408 creds.router['name'])
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400409 self._clear_isolated_router(creds.router['id'],
410 creds.router['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800411 if (not self.network_resources or
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800412 self.network_resources.get('subnet')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400413 self._clear_isolated_subnet(creds.subnet['id'],
414 creds.subnet['name'])
Salvatore Orlandocf996c62014-01-30 09:15:18 -0800415 if (not self.network_resources or
416 self.network_resources.get('network')):
Andrea Frittoli (andreaf)9540dfd2015-03-25 17:06:50 -0400417 self._clear_isolated_network(creds.network['id'],
418 creds.network['name'])
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500419
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700420 def clear_creds(self):
421 if not self._creds:
Matthew Treinishb86cda92013-07-29 11:22:23 -0400422 return
Miguel Lavalleb8fabc52013-08-23 11:19:57 -0500423 self._clear_isolated_net_resources()
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700424 for creds in six.itervalues(self._creds):
Matthew Treinishb86cda92013-07-29 11:22:23 -0400425 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000426 self.creds_client.delete_user(creds.user_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900427 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100428 LOG.warning("user with name: %s not found for delete",
zhangguoqing6c096642016-01-04 06:17:21 +0000429 creds.username)
zhufl2344ea62016-06-01 14:44:00 +0800430 # NOTE(zhufl): Only when neutron's security_group ext is
431 # enabled, _cleanup_default_secgroup will not raise error. But
432 # here cannot use test.is_extension_enabled for it will cause
433 # "circular dependency". So here just use try...except to
434 # ensure tenant deletion without big changes.
Matthew Treinishb86cda92013-07-29 11:22:23 -0400435 try:
Matthew Treinish75abbcf2016-10-07 16:19:12 -0400436 if self.neutron_available:
Andrea Frittolic3280152015-02-26 12:42:34 +0000437 self._cleanup_default_secgroup(creds.tenant_id)
zhufl2344ea62016-06-01 14:44:00 +0800438 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100439 LOG.warning("failed to cleanup tenant %s's secgroup",
zhufl2344ea62016-06-01 14:44:00 +0800440 creds.tenant_name)
441 try:
Andrea Frittolic3280152015-02-26 12:42:34 +0000442 self.creds_client.delete_project(creds.tenant_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900443 except lib_exc.NotFound:
Jordan Pittier525ec712016-12-07 17:51:26 +0100444 LOG.warning("tenant with name: %s not found for delete",
zhangguoqing6c096642016-01-04 06:17:21 +0000445 creds.tenant_name)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700446 self._creds = {}
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100447
448 def is_multi_user(self):
449 return True
Yair Fried76488d72014-10-21 10:13:19 +0300450
451 def is_multi_tenant(self):
452 return True
Matthew Treinish4a596932015-03-06 20:37:01 -0500453
454 def is_role_available(self, role):
455 return True