blob: 2c6e3349f77edb91276828a5d8e8aae11bd55b02 [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 +053017from tempest_lib import exceptions as lib_exc
18
19from tempest import config
Matthew Treinishbe855fd2015-04-16 13:10:49 -040020from tempest import exceptions
Rohan Kanade9ce97df2013-12-10 18:59:35 +053021
22CONF = config.CONF
23
24LOG = logging.getLogger(__name__)
25
26
Matthew Treinishf83f35c2015-04-10 11:59:11 -040027def get_network_from_name(name, compute_networks_client):
28 """Get a full network dict from just a network name
29
30 :param str name: the name of the network to use
31 :param NetworksClientJSON compute_networks_client: The network client
32 object to use for making the network lists api request
Matthew Treinishbe855fd2015-04-16 13:10:49 -040033 :return: The full dictionary for the network in question
Matthew Treinishf83f35c2015-04-10 11:59:11 -040034 :rtype: dict
Matthew Treinishbe855fd2015-04-16 13:10:49 -040035 :raises InvalidConfiguration: If the name provided is invalid, the networks
36 list returns a 404, there are no found networks, or the found network
37 is invalid
Matthew Treinishf83f35c2015-04-10 11:59:11 -040038 """
39 caller = misc_utils.find_test_caller()
Matthew Treinishbe855fd2015-04-16 13:10:49 -040040
Matthew Treinishf83f35c2015-04-10 11:59:11 -040041 if not name:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040042 raise exceptions.InvalidConfiguration()
43
44 try:
45 networks = compute_networks_client.list_networks(name=name)
46 except lib_exc.NotFound:
47 # In case of nova network, if the fixed_network_name is not
48 # owned by the tenant, and the network client is not an admin
49 # one, list_networks will not find it
50 msg = ('Unable to find network %s. '
51 'Starting instance without specifying a network.' %
52 name)
53 if caller:
54 msg = '(%s) %s' % (caller, msg)
55 LOG.info(msg)
56 raise exceptions.InvalidConfiguration()
57
58 # Check that a network exists, else raise an InvalidConfigurationException
59 if len(networks) == 1:
60 network = sorted(networks)[0]
61 elif len(networks) > 1:
62 msg = ("Network with name: %s had multiple matching networks in the "
63 "list response: %s\n Unable to specify a single network" % (
64 name, networks))
65 if caller:
66 msg = '(%s) %s' % (caller, msg)
67 LOG.warn(msg)
68 raise exceptions.InvalidConfiguration()
Matthew Treinishf83f35c2015-04-10 11:59:11 -040069 else:
Matthew Treinishbe855fd2015-04-16 13:10:49 -040070 msg = "Network with name: %s not found" % name
71 if caller:
72 msg = '(%s) %s' % (caller, msg)
73 LOG.warn(msg)
74 raise exceptions.InvalidConfiguration()
75 # To be consistent between neutron and nova network always use name even
76 # if label is used in the api response. If neither is present than then
77 # the returned network is invalid.
78 name = network.get('name') or network.get('label')
79 if not name:
80 msg = "Network found from list doesn't contain a valid name or label"
81 if caller:
82 msg = '(%s) %s' % (caller, msg)
83 LOG.warn(msg)
84 raise exceptions.InvalidConfiguration()
85 network['name'] = name
Matthew Treinishf83f35c2015-04-10 11:59:11 -040086 return network
87
88
Rohan Kanade9ce97df2013-12-10 18:59:35 +053089def get_tenant_network(creds_provider, compute_networks_client):
90 """Get a network usable by the primary tenant
91
92 :param creds_provider: instance of credential provider
93 :param compute_networks_client: compute network client. We want to have the
94 compute network client so we can have use a common approach for both
95 neutron and nova-network cases. If this is not an admin network
96 client, set_network_kwargs might fail in case fixed_network_name
97 is the network to be used, and it's not visible to the tenant
98 :return a dict with 'id' and 'name' of the network
99 """
Matthew Treinisha47c3ed2015-04-10 11:49:47 -0400100 caller = misc_utils.find_test_caller()
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530101 fixed_network_name = CONF.compute.fixed_network_name
Matthew Treinishf83f35c2015-04-10 11:59:11 -0400102 net_creds = creds_provider.get_primary_creds()
103 network = getattr(net_creds, 'network', None)
104 if not network or not network.get('name'):
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530105 if fixed_network_name:
Matthew Treinisha47c3ed2015-04-10 11:49:47 -0400106 msg = ('No valid network provided or created, defaulting to '
107 'fixed_network_name')
108 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400109 msg = '(%s) %s' % (caller, msg)
110 LOG.debug(msg)
111 try:
112 network = get_network_from_name(fixed_network_name,
113 compute_networks_client)
114 except exceptions.InvalidConfiguration:
115 network = {}
Matthew Treinisha47c3ed2015-04-10 11:49:47 -0400116 msg = ('Found network %s available for tenant' % network)
117 if caller:
Matthew Treinishbe855fd2015-04-16 13:10:49 -0400118 msg = '(%s) %s' % (caller, msg)
119 LOG.info(msg)
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530120 return network
121
122
123def set_networks_kwarg(network, kwargs=None):
124 """Set 'networks' kwargs for a server create if missing
125
126 :param network: dict of network to be used with 'id' and 'name'
127 :param kwargs: server create kwargs to be enhanced
128 :return: new dict of kwargs updated to include networks
129 """
130 params = copy.copy(kwargs) or {}
131 if kwargs and 'networks' in kwargs:
132 return params
133
134 if network:
Matthew Treinisha47c3ed2015-04-10 11:49:47 -0400135 if 'id' in network.keys():
136 params.update({"networks": [{'uuid': network['id']}]})
137 else:
138 LOG.warn('The provided network dict: %s was invalid and did not '
139 ' contain an id' % network)
Rohan Kanade9ce97df2013-12-10 18:59:35 +0530140 return params