blob: 56cd3319ec4b7e43b88b822ba401aa8245d4b775 [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 Treinisha47c3ed2015-04-10 11:49:47 -040016from tempest_lib.common.utils import misc as misc_utils
Rohan Kanade9ce97df2013-12-10 18:59:35 +053017
Matthew Treinishbe855fd2015-04-16 13:10:49 -040018from tempest import exceptions
Rohan Kanade9ce97df2013-12-10 18:59:35 +053019
Rohan Kanade9ce97df2013-12-10 18:59:35 +053020LOG = logging.getLogger(__name__)
21
22
Matthew Treinishf83f35c2015-04-10 11:59:11 -040023def get_network_from_name(name, compute_networks_client):
24 """Get a full network dict from just a network name
25
26 :param str name: the name of the network to use
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000027 :param NetworksClient compute_networks_client: The network client
Matthew Treinishf83f35c2015-04-10 11:59:11 -040028 object to use for making the network lists api request
Matthew Treinishbe855fd2015-04-16 13:10:49 -040029 :return: The full dictionary for the network in question
Matthew Treinishf83f35c2015-04-10 11:59:11 -040030 :rtype: dict
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090031 :raises InvalidTestResource: If the name provided is invalid, the networks
Matthew Treinishbe855fd2015-04-16 13:10:49 -040032 list returns a 404, there are no found networks, or the found network
33 is invalid
Matthew Treinishf83f35c2015-04-10 11:59:11 -040034 """
35 caller = misc_utils.find_test_caller()
Matthew Treinishbe855fd2015-04-16 13:10:49 -040036
Matthew Treinishf83f35c2015-04-10 11:59:11 -040037 if not name:
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090038 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishbe855fd2015-04-16 13:10:49 -040039
ghanshyamf0f7cfc2015-08-24 16:21:18 +090040 networks = compute_networks_client.list_networks()['networks']
Ken'ichi Ohmichi46f574e2015-06-15 04:53:55 +000041 networks = [n for n in networks if n['label'] == name]
Matthew Treinishbe855fd2015-04-16 13:10:49 -040042
43 # Check that a network exists, else raise an InvalidConfigurationException
44 if len(networks) == 1:
45 network = sorted(networks)[0]
46 elif len(networks) > 1:
47 msg = ("Network with name: %s had multiple matching networks in the "
48 "list response: %s\n Unable to specify a single network" % (
49 name, networks))
50 if caller:
51 msg = '(%s) %s' % (caller, msg)
52 LOG.warn(msg)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090053 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishf83f35c2015-04-10 11:59:11 -040054 else:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040055 msg = "Network with name: %s not found" % name
56 if caller:
57 msg = '(%s) %s' % (caller, msg)
58 LOG.warn(msg)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090059 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishbe855fd2015-04-16 13:10:49 -040060 # To be consistent between neutron and nova network always use name even
61 # if label is used in the api response. If neither is present than then
62 # the returned network is invalid.
63 name = network.get('name') or network.get('label')
64 if not name:
65 msg = "Network found from list doesn't contain a valid name or label"
66 if caller:
67 msg = '(%s) %s' % (caller, msg)
68 LOG.warn(msg)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090069 raise exceptions.InvalidTestResource(type='network', name=name)
Matthew Treinishbe855fd2015-04-16 13:10:49 -040070 network['name'] = name
Matthew Treinishf83f35c2015-04-10 11:59:11 -040071 return network
72
73
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090074def get_tenant_network(creds_provider, compute_networks_client,
75 shared_network_name):
Rohan Kanade9ce97df2013-12-10 18:59:35 +053076 """Get a network usable by the primary tenant
77
78 :param creds_provider: instance of credential provider
79 :param compute_networks_client: compute network client. We want to have the
80 compute network client so we can have use a common approach for both
81 neutron and nova-network cases. If this is not an admin network
82 client, set_network_kwargs might fail in case fixed_network_name
83 is the network to be used, and it's not visible to the tenant
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090084 :param shared_network_name: name of the shared network to be used if no
85 tenant network is available in the creds provider
lei zhangdd552b22015-11-25 20:41:48 +080086 :returns: a dict with 'id' and 'name' of the network
Rohan Kanade9ce97df2013-12-10 18:59:35 +053087 """
Matthew Treinisha47c3ed2015-04-10 11:49:47 -040088 caller = misc_utils.find_test_caller()
Matthew Treinishf83f35c2015-04-10 11:59:11 -040089 net_creds = creds_provider.get_primary_creds()
90 network = getattr(net_creds, 'network', None)
91 if not network or not network.get('name'):
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090092 if shared_network_name:
Matthew Treinisha47c3ed2015-04-10 11:49:47 -040093 msg = ('No valid network provided or created, defaulting to '
94 'fixed_network_name')
95 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040096 msg = '(%s) %s' % (caller, msg)
97 LOG.debug(msg)
98 try:
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +090099 network = get_network_from_name(shared_network_name,
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400100 compute_networks_client)
Andrea Frittoli (andreaf)940f8c62015-10-30 16:39:24 +0900101 except exceptions.InvalidTestResource:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400102 network = {}
Matthew Treinisha47c3ed2015-04-10 11:49:47 -0400103 msg = ('Found network %s available for tenant' % network)
104 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400105 msg = '(%s) %s' % (caller, msg)
106 LOG.info(msg)
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530107 return network
108
109
110def set_networks_kwarg(network, kwargs=None):
111 """Set 'networks' kwargs for a server create if missing
112
113 :param network: dict of network to be used with 'id' and 'name'
114 :param kwargs: server create kwargs to be enhanced
115 :return: new dict of kwargs updated to include networks
116 """
117 params = copy.copy(kwargs) or {}
118 if kwargs and 'networks' in kwargs:
119 return params
120
121 if network:
Matthew Treinisha47c3ed2015-04-10 11:49:47 -0400122 if 'id' in network.keys():
123 params.update({"networks": [{'uuid': network['id']}]})
124 else:
125 LOG.warn('The provided network dict: %s was invalid and did not '
126 ' contain an id' % network)
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530127 return params