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