blob: 9ec0ec6f6abe60d9da3782cc41009b8a6bae31fd [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
18from tempest import config
Matthew Treinishbe855fd2015-04-16 13:10:49 -040019from tempest import exceptions
Rohan Kanade9ce97df2013-12-10 18:59:35 +053020
21CONF = config.CONF
22
23LOG = logging.getLogger(__name__)
24
25
Matthew Treinishf83f35c2015-04-10 11:59:11 -040026def 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 Ohmichia6287072015-07-02 02:43:15 +000030 :param NetworksClient compute_networks_client: The network client
Matthew Treinishf83f35c2015-04-10 11:59:11 -040031 object to use for making the network lists api request
Matthew Treinishbe855fd2015-04-16 13:10:49 -040032 :return: The full dictionary for the network in question
Matthew Treinishf83f35c2015-04-10 11:59:11 -040033 :rtype: dict
Matthew Treinishbe855fd2015-04-16 13:10:49 -040034 :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 Treinishf83f35c2015-04-10 11:59:11 -040037 """
38 caller = misc_utils.find_test_caller()
Matthew Treinishbe855fd2015-04-16 13:10:49 -040039
Matthew Treinishf83f35c2015-04-10 11:59:11 -040040 if not name:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040041 raise exceptions.InvalidConfiguration()
42
Ken'ichi Ohmichi46f574e2015-06-15 04:53:55 +000043 networks = compute_networks_client.list_networks()
44 networks = [n for n in networks if n['label'] == name]
Matthew Treinishbe855fd2015-04-16 13:10:49 -040045
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 Treinishf83f35c2015-04-10 11:59:11 -040057 else:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040058 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 Treinishf83f35c2015-04-10 11:59:11 -040074 return network
75
76
Rohan Kanade9ce97df2013-12-10 18:59:35 +053077def 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 Treinisha47c3ed2015-04-10 11:49:47 -040088 caller = misc_utils.find_test_caller()
Rohan Kanade9ce97df2013-12-10 18:59:35 +053089 fixed_network_name = CONF.compute.fixed_network_name
Matthew Treinishf83f35c2015-04-10 11:59:11 -040090 net_creds = creds_provider.get_primary_creds()
91 network = getattr(net_creds, 'network', None)
92 if not network or not network.get('name'):
Rohan Kanade9ce97df2013-12-10 18:59:35 +053093 if fixed_network_name:
Matthew Treinisha47c3ed2015-04-10 11:49:47 -040094 msg = ('No valid network provided or created, defaulting to '
95 'fixed_network_name')
96 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040097 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 Treinisha47c3ed2015-04-10 11:49:47 -0400104 msg = ('Found network %s available for tenant' % network)
105 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400106 msg = '(%s) %s' % (caller, msg)
107 LOG.info(msg)
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530108 return network
109
110
111def 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 Treinisha47c3ed2015-04-10 11:49:47 -0400123 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 Kanade9ce97df2013-12-10 18:59:35 +0530128 return params