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 |
| 16 | |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 17 | from tempest import auth |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 18 | from tempest import clients |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 19 | from tempest.common import cred_provider |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 20 | from tempest.common.utils import data_utils |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 21 | from tempest import config |
| 22 | from tempest import exceptions |
| 23 | from tempest.openstack.common import log as logging |
| 24 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 25 | CONF = config.CONF |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 26 | LOG = logging.getLogger(__name__) |
| 27 | |
| 28 | |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 29 | class IsolatedCreds(cred_provider.CredentialProvider): |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 30 | |
| 31 | def __init__(self, name, tempest_client=True, interface='json', |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 32 | password='pass', network_resources=None): |
Marc Koderer | d2690fe | 2014-07-16 14:17:47 +0200 | [diff] [blame] | 33 | super(IsolatedCreds, self).__init__(name, tempest_client, interface, |
| 34 | password, network_resources) |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 35 | self.network_resources = network_resources |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 36 | self.isolated_creds = {} |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 37 | self.isolated_net_resources = {} |
| 38 | self.ports = [] |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 39 | self.tempest_client = tempest_client |
| 40 | self.interface = interface |
| 41 | self.password = password |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 42 | self.identity_admin_client, self.network_admin_client = ( |
| 43 | self._get_admin_clients()) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 44 | |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 45 | def _get_admin_clients(self): |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 46 | """ |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 47 | Returns a tuple with instances of the following admin clients (in this |
| 48 | order): |
| 49 | identity |
| 50 | network |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 51 | """ |
| 52 | if self.tempest_client: |
| 53 | os = clients.AdminManager(interface=self.interface) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 54 | else: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 55 | os = clients.OfficialClientManager( |
| 56 | auth.get_default_credentials('identity_admin') |
| 57 | ) |
| 58 | return os.identity_client, os.network_client |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 59 | |
| 60 | def _create_tenant(self, name, description): |
| 61 | if self.tempest_client: |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 62 | _, tenant = self.identity_admin_client.create_tenant( |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 63 | name=name, description=description) |
| 64 | else: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 65 | tenant = self.identity_admin_client.tenants.create( |
| 66 | name, |
| 67 | description=description) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 68 | return tenant |
| 69 | |
| 70 | def _get_tenant_by_name(self, name): |
| 71 | if self.tempest_client: |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 72 | _, tenant = self.identity_admin_client.get_tenant_by_name(name) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 73 | else: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 74 | tenants = self.identity_admin_client.tenants.list() |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 75 | for ten in tenants: |
| 76 | if ten['name'] == name: |
| 77 | tenant = ten |
DennyZhang | 1eb4046 | 2013-09-26 14:18:30 -0500 | [diff] [blame] | 78 | break |
| 79 | else: |
| 80 | raise exceptions.NotFound('No such tenant') |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 81 | return tenant |
| 82 | |
| 83 | def _create_user(self, username, password, tenant, email): |
| 84 | if self.tempest_client: |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 85 | _, user = self.identity_admin_client.create_user(username, |
| 86 | password, |
| 87 | tenant['id'], |
| 88 | email) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 89 | else: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 90 | user = self.identity_admin_client.users.create(username, password, |
| 91 | email, |
| 92 | tenant_id=tenant.id) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 93 | return user |
| 94 | |
| 95 | def _get_user(self, tenant, username): |
| 96 | if self.tempest_client: |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 97 | _, user = self.identity_admin_client.get_user_by_username( |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 98 | tenant['id'], |
| 99 | username) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 100 | else: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 101 | user = self.identity_admin_client.users.get(username) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 102 | return user |
| 103 | |
| 104 | def _list_roles(self): |
| 105 | if self.tempest_client: |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 106 | _, roles = self.identity_admin_client.list_roles() |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 107 | else: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 108 | roles = self.identity_admin_client.roles.list() |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 109 | return roles |
| 110 | |
Andrey Pavlov | af1fb70 | 2014-05-29 17:08:10 +0400 | [diff] [blame] | 111 | def _assign_user_role(self, tenant, user, role_name): |
| 112 | role = None |
| 113 | try: |
| 114 | roles = self._list_roles() |
| 115 | if self.tempest_client: |
| 116 | role = next(r for r in roles if r['name'] == role_name) |
| 117 | else: |
| 118 | role = next(r for r in roles if r.name == role_name) |
| 119 | except StopIteration: |
| 120 | msg = 'No "%s" role found' % role_name |
| 121 | raise exceptions.NotFound(msg) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 122 | if self.tempest_client: |
Andrey Pavlov | af1fb70 | 2014-05-29 17:08:10 +0400 | [diff] [blame] | 123 | self.identity_admin_client.assign_user_role(tenant['id'], |
| 124 | user['id'], role['id']) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 125 | else: |
Andrey Pavlov | af1fb70 | 2014-05-29 17:08:10 +0400 | [diff] [blame] | 126 | self.identity_admin_client.roles.add_user_role(user.id, role.id, |
| 127 | tenant.id) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 128 | |
| 129 | def _delete_user(self, user): |
| 130 | if self.tempest_client: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 131 | self.identity_admin_client.delete_user(user) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 132 | else: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 133 | self.identity_admin_client.users.delete(user) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 134 | |
| 135 | def _delete_tenant(self, tenant): |
| 136 | if self.tempest_client: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 137 | self.identity_admin_client.delete_tenant(tenant) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 138 | else: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 139 | self.identity_admin_client.tenants.delete(tenant) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 140 | |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 141 | def _create_creds(self, suffix="", admin=False): |
| 142 | """Create random credentials under the following schema. |
| 143 | |
| 144 | If the name contains a '.' is the full class path of something, and |
| 145 | we don't really care. If it isn't, it's probably a meaningful name, |
| 146 | so use it. |
| 147 | |
| 148 | For logging purposes, -user and -tenant are long and redundant, |
| 149 | don't use them. The user# will be sufficient to figure it out. |
| 150 | """ |
| 151 | if '.' in self.name: |
| 152 | root = "" |
| 153 | else: |
| 154 | root = self.name |
| 155 | |
| 156 | tenant_name = data_utils.rand_name(root) + suffix |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 157 | tenant_desc = tenant_name + "-desc" |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 158 | tenant = self._create_tenant(name=tenant_name, |
| 159 | description=tenant_desc) |
Sean Dague | 6969b90 | 2014-01-28 06:48:37 -0500 | [diff] [blame] | 160 | |
| 161 | username = data_utils.rand_name(root) + suffix |
| 162 | email = data_utils.rand_name(root) + suffix + "@example.com" |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 163 | user = self._create_user(username, self.password, |
| 164 | tenant, email) |
Andrey Pavlov | af1fb70 | 2014-05-29 17:08:10 +0400 | [diff] [blame] | 165 | # NOTE(andrey-mp): user needs this role to create containers in swift |
| 166 | swift_operator_role = CONF.object_storage.operator_role |
| 167 | self._assign_user_role(tenant, user, swift_operator_role) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 168 | if admin: |
Andrey Pavlov | af1fb70 | 2014-05-29 17:08:10 +0400 | [diff] [blame] | 169 | self._assign_user_role(tenant, user, CONF.identity.admin_role) |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 170 | return self._get_credentials(user, tenant) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 171 | |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 172 | def _get_credentials(self, user, tenant): |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 173 | if self.tempest_client: |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 174 | user_get = user.get |
| 175 | tenant_get = tenant.get |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 176 | else: |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 177 | user_get = user.__dict__.get |
| 178 | tenant_get = tenant.__dict__.get |
| 179 | return auth.get_credentials( |
| 180 | username=user_get('name'), user_id=user_get('id'), |
| 181 | tenant_name=tenant_get('name'), tenant_id=tenant_get('id'), |
| 182 | password=self.password) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 183 | |
| 184 | def _create_network_resources(self, tenant_id): |
| 185 | network = None |
| 186 | subnet = None |
| 187 | router = None |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 188 | # Make sure settings |
| 189 | if self.network_resources: |
| 190 | if self.network_resources['router']: |
| 191 | if (not self.network_resources['subnet'] or |
| 192 | not self.network_resources['network']): |
| 193 | raise exceptions.InvalidConfiguration( |
| 194 | 'A router requires a subnet and network') |
| 195 | elif self.network_resources['subnet']: |
| 196 | if not self.network_resources['network']: |
| 197 | raise exceptions.InvalidConfiguration( |
| 198 | 'A subnet requires a network') |
| 199 | elif self.network_resources['dhcp']: |
| 200 | raise exceptions.InvalidConfiguration('DHCP requires a subnet') |
| 201 | |
Masayuki Igawa | 259c113 | 2013-10-31 17:48:44 +0900 | [diff] [blame] | 202 | data_utils.rand_name_root = data_utils.rand_name(self.name) |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 203 | if not self.network_resources or self.network_resources['network']: |
| 204 | network_name = data_utils.rand_name_root + "-network" |
| 205 | network = self._create_network(network_name, tenant_id) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 206 | try: |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 207 | if not self.network_resources or self.network_resources['subnet']: |
| 208 | subnet_name = data_utils.rand_name_root + "-subnet" |
| 209 | subnet = self._create_subnet(subnet_name, tenant_id, |
| 210 | network['id']) |
| 211 | if not self.network_resources or self.network_resources['router']: |
| 212 | router_name = data_utils.rand_name_root + "-router" |
| 213 | router = self._create_router(router_name, tenant_id) |
| 214 | self._add_router_interface(router['id'], subnet['id']) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 215 | except Exception: |
| 216 | if router: |
| 217 | self._clear_isolated_router(router['id'], router['name']) |
| 218 | if subnet: |
| 219 | self._clear_isolated_subnet(subnet['id'], subnet['name']) |
| 220 | if network: |
| 221 | self._clear_isolated_network(network['id'], network['name']) |
| 222 | raise |
| 223 | return network, subnet, router |
| 224 | |
| 225 | def _create_network(self, name, tenant_id): |
| 226 | if self.tempest_client: |
| 227 | resp, resp_body = self.network_admin_client.create_network( |
Eugene Nikanorov | e9d255a | 2013-12-18 16:31:42 +0400 | [diff] [blame] | 228 | name=name, tenant_id=tenant_id) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 229 | else: |
| 230 | body = {'network': {'tenant_id': tenant_id, 'name': name}} |
| 231 | resp_body = self.network_admin_client.create_network(body) |
| 232 | return resp_body['network'] |
| 233 | |
| 234 | def _create_subnet(self, subnet_name, tenant_id, network_id): |
| 235 | if not self.tempest_client: |
| 236 | body = {'subnet': {'name': subnet_name, 'tenant_id': tenant_id, |
| 237 | 'network_id': network_id, 'ip_version': 4}} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 238 | if self.network_resources: |
| 239 | body['enable_dhcp'] = self.network_resources['dhcp'] |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 240 | base_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr) |
| 241 | mask_bits = CONF.network.tenant_network_mask_bits |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 242 | for subnet_cidr in base_cidr.subnet(mask_bits): |
| 243 | try: |
| 244 | if self.tempest_client: |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 245 | if self.network_resources: |
| 246 | resp, resp_body = self.network_admin_client.\ |
| 247 | create_subnet( |
Eugene Nikanorov | e9d255a | 2013-12-18 16:31:42 +0400 | [diff] [blame] | 248 | network_id=network_id, cidr=str(subnet_cidr), |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 249 | name=subnet_name, |
| 250 | tenant_id=tenant_id, |
Eugene Nikanorov | e9d255a | 2013-12-18 16:31:42 +0400 | [diff] [blame] | 251 | enable_dhcp=self.network_resources['dhcp'], |
| 252 | ip_version=4) |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 253 | else: |
| 254 | resp, resp_body = self.network_admin_client.\ |
Eugene Nikanorov | e9d255a | 2013-12-18 16:31:42 +0400 | [diff] [blame] | 255 | create_subnet(network_id=network_id, |
| 256 | cidr=str(subnet_cidr), |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 257 | name=subnet_name, |
Eugene Nikanorov | e9d255a | 2013-12-18 16:31:42 +0400 | [diff] [blame] | 258 | tenant_id=tenant_id, |
| 259 | ip_version=4) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 260 | else: |
| 261 | body['subnet']['cidr'] = str(subnet_cidr) |
| 262 | resp_body = self.network_admin_client.create_subnet(body) |
| 263 | break |
| 264 | except exceptions.BadRequest as e: |
| 265 | if 'overlaps with another subnet' not in str(e): |
| 266 | raise |
| 267 | else: |
| 268 | e = exceptions.BuildErrorException() |
| 269 | e.message = 'Available CIDR for subnet creation could not be found' |
| 270 | raise e |
| 271 | return resp_body['subnet'] |
| 272 | |
| 273 | def _create_router(self, router_name, tenant_id): |
| 274 | external_net_id = dict( |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 275 | network_id=CONF.network.public_network_id) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 276 | if self.tempest_client: |
| 277 | resp, resp_body = self.network_admin_client.create_router( |
| 278 | router_name, |
| 279 | external_gateway_info=external_net_id, |
| 280 | tenant_id=tenant_id) |
| 281 | else: |
| 282 | body = {'router': {'name': router_name, 'tenant_id': tenant_id, |
| 283 | 'external_gateway_info': external_net_id, |
| 284 | 'admin_state_up': True}} |
| 285 | resp_body = self.network_admin_client.create_router(body) |
| 286 | return resp_body['router'] |
| 287 | |
| 288 | def _add_router_interface(self, router_id, subnet_id): |
| 289 | if self.tempest_client: |
| 290 | self.network_admin_client.add_router_interface_with_subnet_id( |
| 291 | router_id, subnet_id) |
| 292 | else: |
| 293 | body = {'subnet_id': subnet_id} |
| 294 | self.network_admin_client.add_interface_router(router_id, body) |
| 295 | |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 296 | def get_primary_network(self): |
| 297 | return self.isolated_net_resources.get('primary')[0] |
| 298 | |
| 299 | def get_primary_subnet(self): |
| 300 | return self.isolated_net_resources.get('primary')[1] |
| 301 | |
| 302 | def get_primary_router(self): |
| 303 | return self.isolated_net_resources.get('primary')[2] |
| 304 | |
| 305 | def get_admin_network(self): |
| 306 | return self.isolated_net_resources.get('admin')[0] |
| 307 | |
| 308 | def get_admin_subnet(self): |
| 309 | return self.isolated_net_resources.get('admin')[1] |
| 310 | |
| 311 | def get_admin_router(self): |
| 312 | return self.isolated_net_resources.get('admin')[2] |
| 313 | |
| 314 | def get_alt_network(self): |
| 315 | return self.isolated_net_resources.get('alt')[0] |
| 316 | |
| 317 | def get_alt_subnet(self): |
| 318 | return self.isolated_net_resources.get('alt')[1] |
| 319 | |
| 320 | def get_alt_router(self): |
| 321 | return self.isolated_net_resources.get('alt')[2] |
| 322 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 323 | def get_credentials(self, credential_type): |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 324 | if self.isolated_creds.get(credential_type): |
| 325 | credentials = self.isolated_creds[credential_type] |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 326 | else: |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 327 | is_admin = (credential_type == 'admin') |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 328 | credentials = self._create_creds(admin=is_admin) |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 329 | self.isolated_creds[credential_type] = credentials |
| 330 | # Maintained until tests are ported |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 331 | LOG.info("Acquired isolated creds:\n credentials: %s" |
| 332 | % credentials) |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 333 | if CONF.service_available.neutron: |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 334 | network, subnet, router = self._create_network_resources( |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 335 | credentials.tenant_id) |
| 336 | self.isolated_net_resources[credential_type] = ( |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 337 | network, subnet, router,) |
| 338 | LOG.info("Created isolated network resources for : \n" |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 339 | + " credentials: %s" % credentials) |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 340 | return credentials |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 341 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 342 | def get_primary_creds(self): |
| 343 | return self.get_credentials('primary') |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 344 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 345 | def get_admin_creds(self): |
| 346 | return self.get_credentials('admin') |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 347 | |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 348 | def get_alt_creds(self): |
| 349 | return self.get_credentials('alt') |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 350 | |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 351 | def _clear_isolated_router(self, router_id, router_name): |
| 352 | net_client = self.network_admin_client |
| 353 | try: |
| 354 | net_client.delete_router(router_id) |
| 355 | except exceptions.NotFound: |
| 356 | LOG.warn('router with name: %s not found for delete' % |
| 357 | router_name) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 358 | |
| 359 | def _clear_isolated_subnet(self, subnet_id, subnet_name): |
| 360 | net_client = self.network_admin_client |
| 361 | try: |
| 362 | net_client.delete_subnet(subnet_id) |
| 363 | except exceptions.NotFound: |
| 364 | LOG.warn('subnet with name: %s not found for delete' % |
| 365 | subnet_name) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 366 | |
| 367 | def _clear_isolated_network(self, network_id, network_name): |
| 368 | net_client = self.network_admin_client |
| 369 | try: |
| 370 | net_client.delete_network(network_id) |
| 371 | except exceptions.NotFound: |
| 372 | LOG.warn('network with name: %s not found for delete' % |
| 373 | network_name) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 374 | |
| 375 | def _cleanup_ports(self, network_id): |
| 376 | # TODO(mlavalle) This method will be removed once patch |
| 377 | # https://review.openstack.org/#/c/46563/ merges in Neutron |
| 378 | if not self.ports: |
| 379 | if self.tempest_client: |
| 380 | resp, resp_body = self.network_admin_client.list_ports() |
| 381 | else: |
| 382 | resp_body = self.network_admin_client.list_ports() |
| 383 | self.ports = resp_body['ports'] |
| 384 | ports_to_delete = [ |
armando-migliaccio | 96a86ae | 2013-11-21 17:26:16 -0800 | [diff] [blame] | 385 | port |
| 386 | for port in self.ports |
| 387 | if (port['network_id'] == network_id and |
shihanzhang | c3d73c8 | 2014-02-18 10:27:02 +0800 | [diff] [blame] | 388 | port['device_owner'] != 'network:router_interface' and |
| 389 | port['device_owner'] != 'network:dhcp') |
armando-migliaccio | 96a86ae | 2013-11-21 17:26:16 -0800 | [diff] [blame] | 390 | ] |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 391 | for port in ports_to_delete: |
| 392 | try: |
| 393 | LOG.info('Cleaning up port id %s, name %s' % |
| 394 | (port['id'], port['name'])) |
| 395 | self.network_admin_client.delete_port(port['id']) |
| 396 | except exceptions.NotFound: |
| 397 | LOG.warn('Port id: %s, name %s not found for clean-up' % |
| 398 | (port['id'], port['name'])) |
| 399 | |
| 400 | def _clear_isolated_net_resources(self): |
| 401 | net_client = self.network_admin_client |
| 402 | for cred in self.isolated_net_resources: |
| 403 | network, subnet, router = self.isolated_net_resources.get(cred) |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 404 | LOG.debug("Clearing network: %(network)s, " |
| 405 | "subnet: %(subnet)s, router: %(router)s", |
| 406 | {'network': network, 'subnet': subnet, 'router': router}) |
| 407 | if (not self.network_resources or |
| 408 | self.network_resources.get('router')): |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 409 | try: |
| 410 | if self.tempest_client: |
| 411 | net_client.remove_router_interface_with_subnet_id( |
| 412 | router['id'], subnet['id']) |
| 413 | else: |
| 414 | body = {'subnet_id': subnet['id']} |
| 415 | net_client.remove_interface_router(router['id'], body) |
| 416 | except exceptions.NotFound: |
| 417 | LOG.warn('router with name: %s not found for delete' % |
| 418 | router['name']) |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 419 | self._clear_isolated_router(router['id'], router['name']) |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 420 | if (not self.network_resources or |
| 421 | self.network_resources.get('network')): |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 422 | # TODO(mlavalle) This method call will be removed once patch |
| 423 | # https://review.openstack.org/#/c/46563/ merges in Neutron |
| 424 | self._cleanup_ports(network['id']) |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 425 | if (not self.network_resources or |
| 426 | self.network_resources.get('subnet')): |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 427 | self._clear_isolated_subnet(subnet['id'], subnet['name']) |
Salvatore Orlando | cf996c6 | 2014-01-30 09:15:18 -0800 | [diff] [blame] | 428 | if (not self.network_resources or |
| 429 | self.network_resources.get('network')): |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 430 | self._clear_isolated_network(network['id'], network['name']) |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 431 | |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 432 | def clear_isolated_creds(self): |
| 433 | if not self.isolated_creds: |
| 434 | return |
Miguel Lavalle | b8fabc5 | 2013-08-23 11:19:57 -0500 | [diff] [blame] | 435 | self._clear_isolated_net_resources() |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 436 | for creds in self.isolated_creds.itervalues(): |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 437 | try: |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 438 | self._delete_user(creds.user_id) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 439 | except exceptions.NotFound: |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 440 | LOG.warn("user with name: %s not found for delete" % |
| 441 | creds.username) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 442 | try: |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 443 | self._delete_tenant(creds.tenant_id) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 444 | except exceptions.NotFound: |
Andrea Frittoli | fc31590 | 2014-03-20 09:21:44 +0000 | [diff] [blame] | 445 | LOG.warn("tenant with name: %s not found for delete" % |
| 446 | creds.tenant_name) |