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