blob: 5f0685ee99d6b8d4c9f163541a64ebd48549c283 [file] [log] [blame]
Rohan Kanade9ce97df2013-12-10 18:59:35 +05301# 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
13import copy
14from oslo_log import log as logging
15
Matthew Treinishbe855fd2015-04-16 13:10:49 -040016from tempest import exceptions
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050017from tempest.lib.common.utils import misc as misc_utils
Rohan Kanade9ce97df2013-12-10 18:59:35 +053018
Rohan Kanade9ce97df2013-12-10 18:59:35 +053019LOG = logging.getLogger(__name__)
20
21
Matthew Treinishf83f35c2015-04-10 11:59:11 -040022def 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 Ohmichia6287072015-07-02 02:43:15 +000026 :param NetworksClient compute_networks_client: The network client
Matthew Treinishf83f35c2015-04-10 11:59:11 -040027 object to use for making the network lists api request
Matthew Treinishbe855fd2015-04-16 13:10:49 -040028 :return: The full dictionary for the network in question
Matthew Treinishf83f35c2015-04-10 11:59:11 -040029 :rtype: dict
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090030 :raises InvalidTestResource: If the name provided is invalid, the networks
Matthew Treinishbe855fd2015-04-16 13:10:49 -040031 list returns a 404, there are no found networks, or the found network
32 is invalid
Matthew Treinishf83f35c2015-04-10 11:59:11 -040033 """
34 caller = misc_utils.find_test_caller()
Matthew Treinishbe855fd2015-04-16 13:10:49 -040035
Matthew Treinishf83f35c2015-04-10 11:59:11 -040036 if not name:
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090037 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishbe855fd2015-04-16 13:10:49 -040038
ghanshyamf0f7cfc2015-08-24 16:21:18 +090039 networks = compute_networks_client.list_networks()['networks']
Ken'ichi Ohmichi46f574e2015-06-15 04:53:55 +000040 networks = [n for n in networks if n['label'] == name]
Matthew Treinishbe855fd2015-04-16 13:10:49 -040041
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)
zhangguoqing6c096642016-01-04 06:17:21 +000051 LOG.warning(msg)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090052 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishf83f35c2015-04-10 11:59:11 -040053 else:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040054 msg = "Network with name: %s not found" % name
55 if caller:
56 msg = '(%s) %s' % (caller, msg)
zhangguoqing6c096642016-01-04 06:17:21 +000057 LOG.warning(msg)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090058 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishbe855fd2015-04-16 13:10:49 -040059 # 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)
zhangguoqing6c096642016-01-04 06:17:21 +000067 LOG.warning(msg)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090068 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishbe855fd2015-04-16 13:10:49 -040069 network['name'] = name
Matthew Treinishf83f35c2015-04-10 11:59:11 -040070 return network
71
72
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090073def get_tenant_network(creds_provider, compute_networks_client,
74 shared_network_name):
Rohan Kanade9ce97df2013-12-10 18:59:35 +053075 """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)940f8c62015-10-30 16:39:24 +090083 :param shared_network_name: name of the shared network to be used if no
84 tenant network is available in the creds provider
lei zhangdd552b22015-11-25 20:41:48 +080085 :returns: a dict with 'id' and 'name' of the network
Rohan Kanade9ce97df2013-12-10 18:59:35 +053086 """
Matthew Treinisha47c3ed2015-04-10 11:49:47 -040087 caller = misc_utils.find_test_caller()
Matthew Treinishf83f35c2015-04-10 11:59:11 -040088 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)940f8c62015-10-30 16:39:24 +090091 if shared_network_name:
Matthew Treinisha47c3ed2015-04-10 11:49:47 -040092 msg = ('No valid network provided or created, defaulting to '
93 'fixed_network_name')
94 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040095 msg = '(%s) %s' % (caller, msg)
96 LOG.debug(msg)
97 try:
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090098 network = get_network_from_name(shared_network_name,
Matthew Treinishbe855fd2015-04-16 13:10:49 -040099 compute_networks_client)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +0900100 except exceptions.InvalidTestResource:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400101 network = {}
Matthew Treinisha47c3ed2015-04-10 11:49:47 -0400102 msg = ('Found network %s available for tenant' % network)
103 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400104 msg = '(%s) %s' % (caller, msg)
105 LOG.info(msg)
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530106 return network
107
108
109def 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 Treinisha47c3ed2015-04-10 11:49:47 -0400121 if 'id' in network.keys():
122 params.update({"networks": [{'uuid': network['id']}]})
123 else:
zhangguoqing6c096642016-01-04 06:17:21 +0000124 LOG.warning('The provided network dict: %s was invalid and did '
125 'not contain an id' % network)
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530126 return params