Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
| 13 | import copy |
| 14 | from oslo_log import log as logging |
| 15 | |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 16 | from tempest_lib.common.utils import misc as misc_utils |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 17 | from tempest_lib import exceptions as lib_exc |
| 18 | |
| 19 | from tempest import config |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 20 | from tempest import exceptions |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 21 | |
| 22 | CONF = config.CONF |
| 23 | |
| 24 | LOG = logging.getLogger(__name__) |
| 25 | |
| 26 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 27 | def get_network_from_name(name, compute_networks_client): |
| 28 | """Get a full network dict from just a network name |
| 29 | |
| 30 | :param str name: the name of the network to use |
| 31 | :param NetworksClientJSON compute_networks_client: The network client |
| 32 | object to use for making the network lists api request |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 33 | :return: The full dictionary for the network in question |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 34 | :rtype: dict |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 35 | :raises InvalidConfiguration: If the name provided is invalid, the networks |
| 36 | list returns a 404, there are no found networks, or the found network |
| 37 | is invalid |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 38 | """ |
| 39 | caller = misc_utils.find_test_caller() |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 40 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 41 | if not name: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 42 | raise exceptions.InvalidConfiguration() |
| 43 | |
| 44 | try: |
| 45 | networks = compute_networks_client.list_networks(name=name) |
| 46 | except lib_exc.NotFound: |
| 47 | # In case of nova network, if the fixed_network_name is not |
| 48 | # owned by the tenant, and the network client is not an admin |
| 49 | # one, list_networks will not find it |
| 50 | msg = ('Unable to find network %s. ' |
| 51 | 'Starting instance without specifying a network.' % |
| 52 | name) |
| 53 | if caller: |
| 54 | msg = '(%s) %s' % (caller, msg) |
| 55 | LOG.info(msg) |
| 56 | raise exceptions.InvalidConfiguration() |
| 57 | |
| 58 | # Check that a network exists, else raise an InvalidConfigurationException |
| 59 | if len(networks) == 1: |
| 60 | network = sorted(networks)[0] |
| 61 | elif len(networks) > 1: |
| 62 | msg = ("Network with name: %s had multiple matching networks in the " |
| 63 | "list response: %s\n Unable to specify a single network" % ( |
| 64 | name, networks)) |
| 65 | if caller: |
| 66 | msg = '(%s) %s' % (caller, msg) |
| 67 | LOG.warn(msg) |
| 68 | raise exceptions.InvalidConfiguration() |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 69 | else: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 70 | msg = "Network with name: %s not found" % name |
| 71 | if caller: |
| 72 | msg = '(%s) %s' % (caller, msg) |
| 73 | LOG.warn(msg) |
| 74 | raise exceptions.InvalidConfiguration() |
| 75 | # To be consistent between neutron and nova network always use name even |
| 76 | # if label is used in the api response. If neither is present than then |
| 77 | # the returned network is invalid. |
| 78 | name = network.get('name') or network.get('label') |
| 79 | if not name: |
| 80 | msg = "Network found from list doesn't contain a valid name or label" |
| 81 | if caller: |
| 82 | msg = '(%s) %s' % (caller, msg) |
| 83 | LOG.warn(msg) |
| 84 | raise exceptions.InvalidConfiguration() |
| 85 | network['name'] = name |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 86 | return network |
| 87 | |
| 88 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 89 | def get_tenant_network(creds_provider, compute_networks_client): |
| 90 | """Get a network usable by the primary tenant |
| 91 | |
| 92 | :param creds_provider: instance of credential provider |
| 93 | :param compute_networks_client: compute network client. We want to have the |
| 94 | compute network client so we can have use a common approach for both |
| 95 | neutron and nova-network cases. If this is not an admin network |
| 96 | client, set_network_kwargs might fail in case fixed_network_name |
| 97 | is the network to be used, and it's not visible to the tenant |
| 98 | :return a dict with 'id' and 'name' of the network |
| 99 | """ |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 100 | caller = misc_utils.find_test_caller() |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 101 | fixed_network_name = CONF.compute.fixed_network_name |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 102 | net_creds = creds_provider.get_primary_creds() |
| 103 | network = getattr(net_creds, 'network', None) |
| 104 | if not network or not network.get('name'): |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 105 | if fixed_network_name: |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 106 | msg = ('No valid network provided or created, defaulting to ' |
| 107 | 'fixed_network_name') |
| 108 | if caller: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 109 | msg = '(%s) %s' % (caller, msg) |
| 110 | LOG.debug(msg) |
| 111 | try: |
| 112 | network = get_network_from_name(fixed_network_name, |
| 113 | compute_networks_client) |
| 114 | except exceptions.InvalidConfiguration: |
| 115 | network = {} |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 116 | msg = ('Found network %s available for tenant' % network) |
| 117 | if caller: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 118 | msg = '(%s) %s' % (caller, msg) |
| 119 | LOG.info(msg) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 120 | return network |
| 121 | |
| 122 | |
| 123 | def set_networks_kwarg(network, kwargs=None): |
| 124 | """Set 'networks' kwargs for a server create if missing |
| 125 | |
| 126 | :param network: dict of network to be used with 'id' and 'name' |
| 127 | :param kwargs: server create kwargs to be enhanced |
| 128 | :return: new dict of kwargs updated to include networks |
| 129 | """ |
| 130 | params = copy.copy(kwargs) or {} |
| 131 | if kwargs and 'networks' in kwargs: |
| 132 | return params |
| 133 | |
| 134 | if network: |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 135 | if 'id' in network.keys(): |
| 136 | params.update({"networks": [{'uuid': network['id']}]}) |
| 137 | else: |
| 138 | LOG.warn('The provided network dict: %s was invalid and did not ' |
| 139 | ' contain an id' % network) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 140 | return params |