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 | |
| 18 | from tempest import config |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 19 | from tempest import exceptions |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 20 | |
| 21 | CONF = config.CONF |
| 22 | |
| 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 26 | def get_network_from_name(name, compute_networks_client): |
| 27 | """Get a full network dict from just a network name |
| 28 | |
| 29 | :param str name: the name of the network to use |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 30 | :param NetworksClient compute_networks_client: The network client |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 31 | object to use for making the network lists api request |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 32 | :return: The full dictionary for the network in question |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 33 | :rtype: dict |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 34 | :raises InvalidConfiguration: If the name provided is invalid, the networks |
| 35 | list returns a 404, there are no found networks, or the found network |
| 36 | is invalid |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 37 | """ |
| 38 | caller = misc_utils.find_test_caller() |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 39 | |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 40 | if not name: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 41 | raise exceptions.InvalidConfiguration() |
| 42 | |
Ken'ichi Ohmichi | 46f574e | 2015-06-15 04:53:55 +0000 | [diff] [blame] | 43 | networks = compute_networks_client.list_networks() |
| 44 | networks = [n for n in networks if n['label'] == name] |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 45 | |
| 46 | # Check that a network exists, else raise an InvalidConfigurationException |
| 47 | if len(networks) == 1: |
| 48 | network = sorted(networks)[0] |
| 49 | elif len(networks) > 1: |
| 50 | msg = ("Network with name: %s had multiple matching networks in the " |
| 51 | "list response: %s\n Unable to specify a single network" % ( |
| 52 | name, networks)) |
| 53 | if caller: |
| 54 | msg = '(%s) %s' % (caller, msg) |
| 55 | LOG.warn(msg) |
| 56 | raise exceptions.InvalidConfiguration() |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 57 | else: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 58 | msg = "Network with name: %s not found" % name |
| 59 | if caller: |
| 60 | msg = '(%s) %s' % (caller, msg) |
| 61 | LOG.warn(msg) |
| 62 | raise exceptions.InvalidConfiguration() |
| 63 | # To be consistent between neutron and nova network always use name even |
| 64 | # if label is used in the api response. If neither is present than then |
| 65 | # the returned network is invalid. |
| 66 | name = network.get('name') or network.get('label') |
| 67 | if not name: |
| 68 | msg = "Network found from list doesn't contain a valid name or label" |
| 69 | if caller: |
| 70 | msg = '(%s) %s' % (caller, msg) |
| 71 | LOG.warn(msg) |
| 72 | raise exceptions.InvalidConfiguration() |
| 73 | network['name'] = name |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 74 | return network |
| 75 | |
| 76 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 77 | def get_tenant_network(creds_provider, compute_networks_client): |
| 78 | """Get a network usable by the primary tenant |
| 79 | |
| 80 | :param creds_provider: instance of credential provider |
| 81 | :param compute_networks_client: compute network client. We want to have the |
| 82 | compute network client so we can have use a common approach for both |
| 83 | neutron and nova-network cases. If this is not an admin network |
| 84 | client, set_network_kwargs might fail in case fixed_network_name |
| 85 | is the network to be used, and it's not visible to the tenant |
| 86 | :return a dict with 'id' and 'name' of the network |
| 87 | """ |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 88 | caller = misc_utils.find_test_caller() |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 89 | fixed_network_name = CONF.compute.fixed_network_name |
Matthew Treinish | f83f35c | 2015-04-10 11:59:11 -0400 | [diff] [blame] | 90 | net_creds = creds_provider.get_primary_creds() |
| 91 | network = getattr(net_creds, 'network', None) |
| 92 | if not network or not network.get('name'): |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 93 | if fixed_network_name: |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 94 | msg = ('No valid network provided or created, defaulting to ' |
| 95 | 'fixed_network_name') |
| 96 | if caller: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 97 | msg = '(%s) %s' % (caller, msg) |
| 98 | LOG.debug(msg) |
| 99 | try: |
| 100 | network = get_network_from_name(fixed_network_name, |
| 101 | compute_networks_client) |
| 102 | except exceptions.InvalidConfiguration: |
| 103 | network = {} |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 104 | msg = ('Found network %s available for tenant' % network) |
| 105 | if caller: |
Matthew Treinish | be855fd | 2015-04-16 13:10:49 -0400 | [diff] [blame] | 106 | msg = '(%s) %s' % (caller, msg) |
| 107 | LOG.info(msg) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 108 | return network |
| 109 | |
| 110 | |
| 111 | def set_networks_kwarg(network, kwargs=None): |
| 112 | """Set 'networks' kwargs for a server create if missing |
| 113 | |
| 114 | :param network: dict of network to be used with 'id' and 'name' |
| 115 | :param kwargs: server create kwargs to be enhanced |
| 116 | :return: new dict of kwargs updated to include networks |
| 117 | """ |
| 118 | params = copy.copy(kwargs) or {} |
| 119 | if kwargs and 'networks' in kwargs: |
| 120 | return params |
| 121 | |
| 122 | if network: |
Matthew Treinish | a47c3ed | 2015-04-10 11:49:47 -0400 | [diff] [blame] | 123 | if 'id' in network.keys(): |
| 124 | params.update({"networks": [{'uuid': network['id']}]}) |
| 125 | else: |
| 126 | LOG.warn('The provided network dict: %s was invalid and did not ' |
| 127 | ' contain an id' % network) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 128 | return params |