Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 1 | # 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 Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 15 | import netaddr |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 16 | from oslo_log import log as logging |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 17 | import six |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 18 | |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 19 | from tempest import clients |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 20 | from tempest.common import cred_client |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 21 | from tempest.common import cred_provider |
Andrea Frittoli (andreaf) | 8def7ca | 2015-05-13 14:24:19 +0100 | [diff] [blame] | 22 | from tempest.common.utils import data_utils |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 23 | from tempest import config |
| 24 | from tempest import exceptions |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 25 | from tempest.lib import exceptions as lib_exc |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 26 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 27 | CONF = config.CONF |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 28 | LOG = logging.getLogger(__name__) |
| 29 | |
| 30 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 31 | class DynamicCredentialProvider(cred_provider.CredentialProvider): |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 32 | |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 33 | def __init__(self, identity_version, name=None, network_resources=None, |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 34 | 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) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 52 | super(DynamicCredentialProvider, self).__init__( |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 53 | 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) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 57 | self._creds = {} |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 58 | self.ports = [] |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 59 | self.default_admin_creds = admin_creds |
Yaroslav Lobankov | 47a93ab | 2016-02-07 16:32:49 -0600 | [diff] [blame] | 60 | (self.identity_admin_client, |
| 61 | self.tenants_admin_client, |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 62 | self.users_admin_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 63 | self.roles_admin_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 64 | self.domains_admin_client, |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 65 | self.network_admin_client, |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 66 | self.networks_admin_client, |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 67 | self.routers_admin_client, |
John Warren | 49c0fe5 | 2015-10-22 12:35:54 -0400 | [diff] [blame] | 68 | self.subnets_admin_client, |
John Warren | f9606e9 | 2015-12-10 12:12:42 -0500 | [diff] [blame] | 69 | self.ports_admin_client, |
| 70 | self.security_groups_admin_client) = self._get_admin_clients() |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 71 | # Domain where isolated credentials are provisioned (v3 only). |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 72 | # Use that of the admin account is None is configured. |
| 73 | self.creds_domain_name = None |
| 74 | if self.identity_version == 'v3': |
| 75 | self.creds_domain_name = ( |
David Kranz | 87fc7e9 | 2015-07-28 14:05:20 -0400 | [diff] [blame] | 76 | self.default_admin_creds.project_domain_name or |
Andrea Frittoli (andreaf) | 1eb0496 | 2015-10-09 14:48:06 +0100 | [diff] [blame] | 77 | self.credentials_domain) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 78 | self.creds_client = cred_client.get_creds_client( |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 79 | self.identity_admin_client, |
| 80 | self.tenants_admin_client, |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 81 | self.users_admin_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 82 | self.roles_admin_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 83 | self.domains_admin_client, |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 84 | self.creds_domain_name) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 85 | |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 86 | def _get_admin_clients(self): |
Ken'ichi Ohmichi | cb67d2d | 2015-11-19 08:23:22 +0000 | [diff] [blame] | 87 | """Returns a tuple with instances of the following admin clients |
| 88 | |
| 89 | (in this order): |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 90 | identity |
| 91 | network |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 92 | """ |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 93 | os = clients.Manager(self.default_admin_creds) |
| 94 | if self.identity_version == 'v2': |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 95 | return (os.identity_client, os.tenants_client, os.users_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 96 | os.roles_client, None, os.network_client, |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 97 | os.networks_client, os.routers_client, os.subnets_client, |
| 98 | os.ports_client, os.security_groups_client) |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 99 | else: |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 100 | return (os.identity_v3_client, os.projects_client, |
Arx Cruz | 24bcb88 | 2016-02-10 15:20:16 +0100 | [diff] [blame] | 101 | os.users_v3_client, os.roles_v3_client, os.domains_client, |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 102 | os.network_client, os.networks_client, os.routers_client, |
| 103 | os.subnets_client, os.ports_client, |
| 104 | os.security_groups_client) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 105 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 106 | def _create_creds(self, suffix="", admin=False, roles=None): |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 107 | """Create random credentials under the following schema. |
| 108 | |
| 109 | If the name contains a '.' is the full class path of something, and |
| 110 | we don't really care. If it isn't, it's probably a meaningful name, |
| 111 | so use it. |
| 112 | |
| 113 | For logging purposes, -user and -tenant are long and redundant, |
| 114 | don't use them. The user# will be sufficient to figure it out. |
| 115 | """ |
| 116 | if '.' in self.name: |
| 117 | root = "" |
| 118 | else: |
| 119 | root = self.name |
| 120 | |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 121 | project_name = data_utils.rand_name(root) + suffix |
| 122 | project_desc = project_name + "-desc" |
| 123 | project = self.creds_client.create_project( |
| 124 | name=project_name, description=project_desc) |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 125 | |
| 126 | username = data_utils.rand_name(root) + suffix |
LingxianKong | 9c713d2 | 2015-06-09 15:19:55 +0800 | [diff] [blame] | 127 | user_password = data_utils.rand_password() |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 128 | email = data_utils.rand_name(root) + suffix + "@example.com" |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 129 | user = self.creds_client.create_user( |
LingxianKong | 9c713d2 | 2015-06-09 15:19:55 +0800 | [diff] [blame] | 130 | username, user_password, project, email) |
John Warren | 56317e0 | 2015-08-12 20:48:32 +0000 | [diff] [blame] | 131 | if 'user' in user: |
| 132 | user = user['user'] |
Matthew Treinish | 32f98a4 | 2015-07-14 19:58:46 -0400 | [diff] [blame] | 133 | role_assigned = False |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 134 | if admin: |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 135 | self.creds_client.assign_user_role(user, project, |
Andrea Frittoli (andreaf) | 29491a7 | 2015-10-13 11:24:17 +0100 | [diff] [blame] | 136 | self.admin_role) |
Matthew Treinish | 32f98a4 | 2015-07-14 19:58:46 -0400 | [diff] [blame] | 137 | role_assigned = True |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 138 | # Add roles specified in config file |
| 139 | for conf_role in CONF.auth.tempest_roles: |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 140 | self.creds_client.assign_user_role(user, project, conf_role) |
Matthew Treinish | 32f98a4 | 2015-07-14 19:58:46 -0400 | [diff] [blame] | 141 | role_assigned = True |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 142 | # Add roles requested by caller |
| 143 | if roles: |
| 144 | for role in roles: |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 145 | self.creds_client.assign_user_role(user, project, role) |
Matthew Treinish | 32f98a4 | 2015-07-14 19:58:46 -0400 | [diff] [blame] | 146 | role_assigned = True |
| 147 | # NOTE(mtreinish) For a user to have access to a project with v3 auth |
| 148 | # it must beassigned a role on the project. So we need to ensure that |
| 149 | # our newly created user has a role on the newly created project. |
| 150 | if self.identity_version == 'v3' and not role_assigned: |
| 151 | self.creds_client.create_user_role('Member') |
| 152 | self.creds_client.assign_user_role(user, project, 'Member') |
| 153 | |
LingxianKong | 9c713d2 | 2015-06-09 15:19:55 +0800 | [diff] [blame] | 154 | creds = self.creds_client.get_credentials(user, project, user_password) |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 155 | return cred_provider.TestResources(creds) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 156 | |
| 157 | def _create_network_resources(self, tenant_id): |
| 158 | network = None |
| 159 | subnet = None |
| 160 | router = None |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 161 | # Make sure settings |
| 162 | if self.network_resources: |
| 163 | if self.network_resources['router']: |
| 164 | if (not self.network_resources['subnet'] or |
| 165 | not self.network_resources['network']): |
| 166 | raise exceptions.InvalidConfiguration( |
| 167 | 'A router requires a subnet and network') |
| 168 | elif self.network_resources['subnet']: |
| 169 | if not self.network_resources['network']: |
| 170 | raise exceptions.InvalidConfiguration( |
| 171 | 'A subnet requires a network') |
| 172 | elif self.network_resources['dhcp']: |
| 173 | raise exceptions.InvalidConfiguration('DHCP requires a subnet') |
| 174 | |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 175 | data_utils.rand_name_root = data_utils.rand_name(self.name) |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 176 | if not self.network_resources or self.network_resources['network']: |
| 177 | network_name = data_utils.rand_name_root + "-network" |
| 178 | network = self._create_network(network_name, tenant_id) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 179 | try: |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 180 | if not self.network_resources or self.network_resources['subnet']: |
| 181 | subnet_name = data_utils.rand_name_root + "-subnet" |
| 182 | subnet = self._create_subnet(subnet_name, tenant_id, |
| 183 | network['id']) |
| 184 | if not self.network_resources or self.network_resources['router']: |
| 185 | router_name = data_utils.rand_name_root + "-router" |
| 186 | router = self._create_router(router_name, tenant_id) |
| 187 | self._add_router_interface(router['id'], subnet['id']) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 188 | except Exception: |
Andrea Frittoli (andreaf) | d9a18b0 | 2016-02-29 15:27:34 +0000 | [diff] [blame] | 189 | try: |
| 190 | if router: |
| 191 | self._clear_isolated_router(router['id'], router['name']) |
| 192 | if subnet: |
| 193 | self._clear_isolated_subnet(subnet['id'], subnet['name']) |
| 194 | if network: |
| 195 | self._clear_isolated_network(network['id'], |
| 196 | network['name']) |
| 197 | except Exception as cleanup_exception: |
| 198 | msg = "There was an exception trying to setup network " \ |
| 199 | "resources for tenant %s, and this error happened " \ |
| 200 | "trying to clean them up: %s" |
| 201 | LOG.warning(msg % (tenant_id, cleanup_exception)) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 202 | raise |
| 203 | return network, subnet, router |
| 204 | |
| 205 | def _create_network(self, name, tenant_id): |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 206 | resp_body = self.networks_admin_client.create_network( |
Andrea Frittoli | ae9aca0 | 2014-09-25 11:43:11 +0100 | [diff] [blame] | 207 | name=name, tenant_id=tenant_id) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 208 | return resp_body['network'] |
| 209 | |
| 210 | def _create_subnet(self, subnet_name, tenant_id, network_id): |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 211 | base_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr) |
| 212 | mask_bits = CONF.network.tenant_network_mask_bits |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 213 | for subnet_cidr in base_cidr.subnet(mask_bits): |
| 214 | try: |
Andrea Frittoli | ae9aca0 | 2014-09-25 11:43:11 +0100 | [diff] [blame] | 215 | if self.network_resources: |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 216 | resp_body = self.subnets_admin_client.\ |
Andrea Frittoli | ae9aca0 | 2014-09-25 11:43:11 +0100 | [diff] [blame] | 217 | create_subnet( |
| 218 | network_id=network_id, cidr=str(subnet_cidr), |
| 219 | name=subnet_name, |
| 220 | tenant_id=tenant_id, |
| 221 | enable_dhcp=self.network_resources['dhcp'], |
| 222 | ip_version=4) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 223 | else: |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 224 | resp_body = self.subnets_admin_client.\ |
Andrea Frittoli | ae9aca0 | 2014-09-25 11:43:11 +0100 | [diff] [blame] | 225 | create_subnet(network_id=network_id, |
| 226 | cidr=str(subnet_cidr), |
| 227 | name=subnet_name, |
| 228 | tenant_id=tenant_id, |
| 229 | ip_version=4) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 230 | break |
Masayuki Igawa | 4b29e47 | 2015-02-16 10:41:54 +0900 | [diff] [blame] | 231 | except lib_exc.BadRequest as e: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 232 | if 'overlaps with another subnet' not in str(e): |
| 233 | raise |
| 234 | else: |
David Kranz | d421041 | 2014-11-21 08:37:45 -0500 | [diff] [blame] | 235 | message = 'Available CIDR for subnet creation could not be found' |
| 236 | raise Exception(message) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 237 | return resp_body['subnet'] |
| 238 | |
| 239 | def _create_router(self, router_name, tenant_id): |
| 240 | external_net_id = dict( |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 241 | network_id=CONF.network.public_network_id) |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 242 | resp_body = self.routers_admin_client.create_router( |
Ken'ichi Ohmichi | 6665c97 | 2016-02-24 13:09:09 -0800 | [diff] [blame] | 243 | name=router_name, |
Andrea Frittoli | ae9aca0 | 2014-09-25 11:43:11 +0100 | [diff] [blame] | 244 | external_gateway_info=external_net_id, |
| 245 | tenant_id=tenant_id) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 246 | return resp_body['router'] |
| 247 | |
| 248 | def _add_router_interface(self, router_id, subnet_id): |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 249 | self.routers_admin_client.add_router_interface(router_id, |
piyush110786 | 94aca95 | 2015-12-17 12:54:44 +0530 | [diff] [blame] | 250 | subnet_id=subnet_id) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 251 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 252 | def get_credentials(self, credential_type): |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 253 | if self._creds.get(str(credential_type)): |
| 254 | credentials = self._creds[str(credential_type)] |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 255 | else: |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 256 | if credential_type in ['primary', 'alt', 'admin']: |
| 257 | is_admin = (credential_type == 'admin') |
| 258 | credentials = self._create_creds(admin=is_admin) |
| 259 | else: |
| 260 | credentials = self._create_creds(roles=credential_type) |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 261 | self._creds[str(credential_type)] = credentials |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 262 | # Maintained until tests are ported |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 263 | LOG.info("Acquired dynamic creds:\n credentials: %s" |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 264 | % credentials) |
Adam Gandelman | 85395e7 | 2014-07-29 18:34:33 -0700 | [diff] [blame] | 265 | if (CONF.service_available.neutron and |
Matthew Treinish | 2219d38 | 2015-04-24 10:33:04 -0400 | [diff] [blame] | 266 | not CONF.baremetal.driver_enabled and |
| 267 | CONF.auth.create_isolated_networks): |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 268 | network, subnet, router = self._create_network_resources( |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 269 | credentials.tenant_id) |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 270 | credentials.set_resources(network=network, subnet=subnet, |
| 271 | router=router) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 272 | LOG.info("Created isolated network resources for : \n" |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 273 | + " credentials: %s" % credentials) |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 274 | return credentials |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 275 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 276 | def get_primary_creds(self): |
| 277 | return self.get_credentials('primary') |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 278 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 279 | def get_admin_creds(self): |
| 280 | return self.get_credentials('admin') |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 281 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 282 | def get_alt_creds(self): |
| 283 | return self.get_credentials('alt') |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 284 | |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 285 | def get_creds_by_roles(self, roles, force_new=False): |
| 286 | roles = list(set(roles)) |
| 287 | # The roles list as a str will become the index as the dict key for |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 288 | # the created credentials set in the dynamic_creds dict. |
| 289 | exist_creds = self._creds.get(str(roles)) |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 290 | # If force_new flag is True 2 cred sets with the same roles are needed |
| 291 | # handle this by creating a separate index for old one to store it |
| 292 | # separately for cleanup |
| 293 | if exist_creds and force_new: |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 294 | new_index = str(roles) + '-' + str(len(self._creds)) |
| 295 | self._creds[new_index] = exist_creds |
| 296 | del self._creds[str(roles)] |
Matthew Treinish | 976e8df | 2014-12-19 14:21:54 -0500 | [diff] [blame] | 297 | return self.get_credentials(roles) |
| 298 | |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 299 | def _clear_isolated_router(self, router_id, router_name): |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 300 | client = self.routers_admin_client |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 301 | try: |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 302 | client.delete_router(router_id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 303 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 304 | LOG.warning('router with name: %s not found for delete' % |
| 305 | router_name) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 306 | |
| 307 | def _clear_isolated_subnet(self, subnet_id, subnet_name): |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 308 | client = self.subnets_admin_client |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 309 | try: |
John Warren | 3961acd | 2015-10-02 14:38:53 -0400 | [diff] [blame] | 310 | client.delete_subnet(subnet_id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 311 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 312 | LOG.warning('subnet with name: %s not found for delete' % |
| 313 | subnet_name) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 314 | |
| 315 | def _clear_isolated_network(self, network_id, network_name): |
John Warren | 94d8faf | 2015-09-15 12:22:24 -0400 | [diff] [blame] | 316 | net_client = self.networks_admin_client |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 317 | try: |
| 318 | net_client.delete_network(network_id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 319 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 320 | LOG.warning('network with name: %s not found for delete' % |
| 321 | network_name) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 322 | |
Ala Rezmerita | 846eb7c | 2014-03-10 09:06:03 +0100 | [diff] [blame] | 323 | def _cleanup_default_secgroup(self, tenant): |
John Warren | f9606e9 | 2015-12-10 12:12:42 -0500 | [diff] [blame] | 324 | nsg_client = self.security_groups_admin_client |
| 325 | resp_body = nsg_client.list_security_groups(tenant_id=tenant, |
David Kranz | 34e8812 | 2014-12-11 15:24:05 -0500 | [diff] [blame] | 326 | name="default") |
Ala Rezmerita | 846eb7c | 2014-03-10 09:06:03 +0100 | [diff] [blame] | 327 | secgroups_to_delete = resp_body['security_groups'] |
| 328 | for secgroup in secgroups_to_delete: |
| 329 | try: |
John Warren | f9606e9 | 2015-12-10 12:12:42 -0500 | [diff] [blame] | 330 | nsg_client.delete_security_group(secgroup['id']) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 331 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 332 | LOG.warning('Security group %s, id %s not found for clean-up' % |
| 333 | (secgroup['name'], secgroup['id'])) |
Ala Rezmerita | 846eb7c | 2014-03-10 09:06:03 +0100 | [diff] [blame] | 334 | |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 335 | def _clear_isolated_net_resources(self): |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 336 | client = self.routers_admin_client |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 337 | for cred in self._creds: |
| 338 | creds = self._creds.get(cred) |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 339 | if (not creds or not any([creds.router, creds.network, |
| 340 | creds.subnet])): |
| 341 | continue |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 342 | LOG.debug("Clearing network: %(network)s, " |
Matthew Treinish | fe094ea | 2014-12-09 01:19:27 +0000 | [diff] [blame] | 343 | "subnet: %(subnet)s, router: %(router)s", |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 344 | {'network': creds.network, 'subnet': creds.subnet, |
| 345 | 'router': creds.router}) |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 346 | if (not self.network_resources or |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 347 | (self.network_resources.get('router') and creds.subnet)): |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 348 | try: |
Ken'ichi Ohmichi | e35f472 | 2015-12-22 04:57:11 +0000 | [diff] [blame] | 349 | client.remove_router_interface( |
piyush110786 | 94aca95 | 2015-12-17 12:54:44 +0530 | [diff] [blame] | 350 | creds.router['id'], |
| 351 | subnet_id=creds.subnet['id']) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 352 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 353 | LOG.warning('router with name: %s not found for delete' % |
| 354 | creds.router['name']) |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 355 | self._clear_isolated_router(creds.router['id'], |
| 356 | creds.router['name']) |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 357 | if (not self.network_resources or |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 358 | self.network_resources.get('subnet')): |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 359 | self._clear_isolated_subnet(creds.subnet['id'], |
| 360 | creds.subnet['name']) |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 361 | if (not self.network_resources or |
| 362 | self.network_resources.get('network')): |
Andrea Frittoli (andreaf) | 9540dfd | 2015-03-25 17:06:50 -0400 | [diff] [blame] | 363 | self._clear_isolated_network(creds.network['id'], |
| 364 | creds.network['name']) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 365 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 366 | def clear_creds(self): |
| 367 | if not self._creds: |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 368 | return |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 369 | self._clear_isolated_net_resources() |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 370 | for creds in six.itervalues(self._creds): |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 371 | try: |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 372 | self.creds_client.delete_user(creds.user_id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 373 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 374 | LOG.warning("user with name: %s not found for delete" % |
| 375 | creds.username) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 376 | try: |
Andrea Frittoli | c328015 | 2015-02-26 12:42:34 +0000 | [diff] [blame] | 377 | if CONF.service_available.neutron: |
| 378 | self._cleanup_default_secgroup(creds.tenant_id) |
| 379 | self.creds_client.delete_project(creds.tenant_id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 380 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 381 | LOG.warning("tenant with name: %s not found for delete" % |
| 382 | creds.tenant_name) |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 383 | self._creds = {} |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 384 | |
| 385 | def is_multi_user(self): |
| 386 | return True |
Yair Fried | 76488d7 | 2014-10-21 10:13:19 +0300 | [diff] [blame] | 387 | |
| 388 | def is_multi_tenant(self): |
| 389 | return True |
Matthew Treinish | 4a59693 | 2015-03-06 20:37:01 -0500 | [diff] [blame] | 390 | |
| 391 | def is_role_available(self, role): |
| 392 | return True |