| 1 |
# Copyright 2013, 2016 Red Hat, Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import json |
| 17 |
|
| 18 |
from config_tempest.constants import LOG |
| 19 |
from config_tempest.services.base import VersionedService |
| 20 |
|
| 21 |
|
| 22 |
class NetworkService(VersionedService): |
| 23 |
def set_extensions(self): |
| 24 |
body = self.do_get(self.service_url + '/v2.0/extensions.json') |
| 25 |
body = json.loads(body) |
| 26 |
self.extensions = list(map(lambda x: x['alias'], body['extensions'])) |
| 27 |
|
| 28 |
def set_versions(self): |
| 29 |
super(NetworkService, self).set_versions(top_level=False) |
| 30 |
|
| 31 |
def create_tempest_networks(self, conf, network): |
| 32 |
LOG.info("Setting up network") |
| 33 |
self.client = self.client.networks |
| 34 |
self.create_tempest_networks_neutron(conf, network) |
| 35 |
|
| 36 |
def create_tempest_networks_neutron(self, conf, public_network): |
| 37 |
self._public_network = public_network |
| 38 |
self._public_network_name = None |
| 39 |
self._public_network_id = None |
| 40 |
# if user supplied the network we should use |
| 41 |
if public_network: |
| 42 |
self._supplied_network() |
| 43 |
# no network id provided, try to auto discover a public network |
| 44 |
else: |
| 45 |
self._discover_network() |
| 46 |
if self._public_network_id is not None: |
| 47 |
conf.set('network', 'public_network_id', self._public_network_id) |
| 48 |
if self._public_network_name is not None: |
| 49 |
conf.set('network', 'floating_network_name', |
| 50 |
self._public_network_name) |
| 51 |
|
| 52 |
def get_service_extension_key(self): |
| 53 |
return 'api_extensions' |
| 54 |
|
| 55 |
def _supplied_network(self): |
| 56 |
LOG.info("Looking for existing network: %s", |
| 57 |
self._public_network) |
| 58 |
given_net = self._public_network |
| 59 |
# check if network exists |
| 60 |
network_list = self.client.list_networks() |
| 61 |
for network in network_list['networks']: |
| 62 |
if network['id'] == given_net or network['name'] == given_net: |
| 63 |
self._public_network_name = network['name'] |
| 64 |
self._public_network_id = network['id'] |
| 65 |
break |
| 66 |
else: |
| 67 |
raise ValueError('provided network: {0} was not found.' |
| 68 |
''.format(self._public_network)) |
| 69 |
|
| 70 |
def _discover_network(self): |
| 71 |
LOG.info("No network supplied, trying auto discover for an external " |
| 72 |
"network while prioritizing the one called public, if not " |
| 73 |
"found, the network discovered last will be used.") |
| 74 |
network_list = self.client.list_networks() |
| 75 |
|
| 76 |
for network in network_list['networks']: |
| 77 |
if network['router:external'] and network['subnets']: |
| 78 |
if network['status'] != 'ACTIVE': |
| 79 |
continue |
| 80 |
self._public_network_id = network['id'] |
| 81 |
self._public_network_name = network['name'] |
| 82 |
# usually the external network we use is called 'public' |
| 83 |
if network['name'] == 'public': |
| 84 |
# we found a network called public, end the loop |
| 85 |
break |
| 86 |
# Couldn't find an existing external network |
| 87 |
if not self._public_network_name: |
| 88 |
LOG.error("No external networks found. " |
| 89 |
"Please note that any test that relies on external " |
| 90 |
"connectivity would most likely fail.") |
| 91 |
return |
| 92 |
LOG.info("Setting %s as the public network for tempest", |
| 93 |
self._public_network_id) |
| 94 |
|
| 95 |
@staticmethod |
| 96 |
def get_service_type(): |
| 97 |
return ['network'] |
| 98 |
|
| 99 |
@staticmethod |
| 100 |
def get_codename(): |
| 101 |
return 'neutron' |