From 861446ba206ca60dbd4741ea712cdd066900eb62 Mon Sep 17 00:00:00 2001 From: Trygve Vea Date: Tue, 21 Nov 2017 19:58:18 +0100 Subject: [PATCH] Implement availability_zone_hints for networks and routers. Adds an optional parameter to the create_network and create_router-methods, for use with availability zone-scheduling of network agents. Change-Id: Ifb93a10415dc676f5cc56b5315f2dff24fc395b8 --- openstack/cloud/openstackcloud.py | 33 ++++++++++++++- openstack/tests/unit/cloud/test_network.py | 41 ++++++++++++++++++ openstack/tests/unit/cloud/test_router.py | 42 +++++++++++++++++++ ...ility_zone_extension-675c2460ebb50a09.yaml | 8 ++++ 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/neutron_availability_zone_extension-675c2460ebb50a09.yaml diff --git a/openstack/cloud/openstackcloud.py b/openstack/cloud/openstackcloud.py index 9a59156b5..d90da7ab2 100644 --- a/openstack/cloud/openstackcloud.py +++ b/openstack/cloud/openstackcloud.py @@ -24,6 +24,10 @@ import os import six import threading import time +# import types so that we can reference ListType in sphinx param declarations. +# We can't just use list, because sphinx gets confused by +# openstack.resource.Resource.list and openstack.resource2.Resource.list +import types # noqa import warnings import dogpile.cache @@ -3389,7 +3393,8 @@ class OpenStackCloud(_normalize.Normalizer): return True def create_network(self, name, shared=False, admin_state_up=True, - external=False, provider=None, project_id=None): + external=False, provider=None, project_id=None, + availability_zone_hints=None): """Create a network. :param string name: Name of the network being created. @@ -3401,6 +3406,8 @@ class OpenStackCloud(_normalize.Normalizer): { 'network_type': 'vlan', 'segmentation_id': 'vlan1' } :param string project_id: Specify the project ID this network will be created on (admin-only). + :param types.ListType availability_zone_hints: A list of availability + zone hints. :returns: The network object. :raises: OpenStackCloudException on operation error. @@ -3416,6 +3423,16 @@ class OpenStackCloud(_normalize.Normalizer): if project_id is not None: network['tenant_id'] = project_id + if availability_zone_hints is not None: + if not isinstance(availability_zone_hints, list): + raise OpenStackCloudException( + "Parameter 'availability_zone_hints' must be a list") + if not self._has_neutron_extension('network_availability_zone'): + raise OpenStackCloudUnavailableExtension( + 'network_availability_zone extension is not available on ' + 'target cloud') + network['availability_zone_hints'] = availability_zone_hints + if provider: if not isinstance(provider, dict): raise OpenStackCloudException( @@ -4251,7 +4268,8 @@ class OpenStackCloud(_normalize.Normalizer): def create_router(self, name=None, admin_state_up=True, ext_gateway_net_id=None, enable_snat=None, - ext_fixed_ips=None, project_id=None): + ext_fixed_ips=None, project_id=None, + availability_zone_hints=None): """Create a logical router. :param string name: The router name. @@ -4269,6 +4287,8 @@ class OpenStackCloud(_normalize.Normalizer): } ] :param string project_id: Project ID for the router. + :param types.ListType availability_zone_hints: + A list of availability zone hints. :returns: The router object. :raises: OpenStackCloudException on operation error. @@ -4285,6 +4305,15 @@ class OpenStackCloud(_normalize.Normalizer): ) if ext_gw_info: router['external_gateway_info'] = ext_gw_info + if availability_zone_hints is not None: + if not isinstance(availability_zone_hints, list): + raise OpenStackCloudException( + "Parameter 'availability_zone_hints' must be a list") + if not self._has_neutron_extension('router_availability_zone'): + raise OpenStackCloudUnavailableExtension( + 'router_availability_zone extension is not available on ' + 'target cloud') + router['availability_zone_hints'] = availability_zone_hints data = self._network_client.post( "/routers.json", json={"router": router}, diff --git a/openstack/tests/unit/cloud/test_network.py b/openstack/tests/unit/cloud/test_network.py index 9a88501cb..825ab235a 100644 --- a/openstack/tests/unit/cloud/test_network.py +++ b/openstack/tests/unit/cloud/test_network.py @@ -13,6 +13,7 @@ import copy import testtools +import openstack import openstack.cloud from openstack.tests.unit import base @@ -47,6 +48,16 @@ class TestNetwork(base.RequestsMockTestCase): 'mtu': 0 } + network_availability_zone_extension = { + "alias": "network_availability_zone", + "updated": "2015-01-01T10:00:00-00:00", + "description": "Availability zone support for router.", + "links": [], + "name": "Network Availability Zone" + } + + enabled_neutron_extensions = [network_availability_zone_extension] + def test_list_networks(self): net1 = {'id': '1', 'name': 'net1'} net2 = {'id': '2', 'name': 'net2'} @@ -151,6 +162,27 @@ class TestNetwork(base.RequestsMockTestCase): self.assertEqual(mock_new_network_rep, network) self.assert_calls() + def test_create_network_with_availability_zone_hints(self): + self.register_uris([ + dict(method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions.json']), + json={'extensions': self.enabled_neutron_extensions}), + dict(method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'networks.json']), + json={'network': self.mock_new_network_rep}, + validate=dict( + json={'network': { + 'admin_state_up': True, + 'name': 'netname', + 'availability_zone_hints': ['nova']}})) + ]) + network = self.cloud.create_network("netname", + availability_zone_hints=['nova']) + self.assertEqual(self.mock_new_network_rep, network) + self.assert_calls() + def test_create_network_provider_ignored_value(self): provider_opts = {'physical_network': 'mynet', 'network_type': 'vlan', @@ -180,6 +212,15 @@ class TestNetwork(base.RequestsMockTestCase): self.assertEqual(mock_new_network_rep, network) self.assert_calls() + def test_create_network_wrong_availability_zone_hints_type(self): + azh_opts = "invalid" + with testtools.ExpectedException( + openstack.cloud.OpenStackCloudException, + "Parameter 'availability_zone_hints' must be a list" + ): + self.cloud.create_network("netname", + availability_zone_hints=azh_opts) + def test_create_network_provider_wrong_type(self): provider_opts = "invalid" with testtools.ExpectedException( diff --git a/openstack/tests/unit/cloud/test_router.py b/openstack/tests/unit/cloud/test_router.py index 2b3a74143..48747d925 100644 --- a/openstack/tests/unit/cloud/test_router.py +++ b/openstack/tests/unit/cloud/test_router.py @@ -14,6 +14,7 @@ # limitations under the License. import copy +import testtools from openstack.cloud import exc from openstack.tests.unit import base @@ -52,6 +53,16 @@ class TestRouter(base.RequestsMockTestCase): 'request_ids': ['req-f1b0b1b4-ae51-4ef9-b371-0cc3c3402cf7'] } + router_availability_zone_extension = { + "alias": "router_availability_zone", + "updated": "2015-01-01T10:00:00-00:00", + "description": "Availability zone support for router.", + "links": [], + "name": "Router Availability Zone" + } + + enabled_neutron_extensions = [router_availability_zone_extension] + def test_get_router(self): self.register_uris([ dict(method='GET', @@ -112,6 +123,27 @@ class TestRouter(base.RequestsMockTestCase): project_id=new_router_tenant_id) self.assert_calls() + def test_create_router_with_availability_zone_hints(self): + self.register_uris([ + dict(method='GET', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'extensions.json']), + json={'extensions': self.enabled_neutron_extensions}), + dict(method='POST', + uri=self.get_mock_url( + 'network', 'public', append=['v2.0', 'routers.json']), + json={'router': self.mock_router_rep}, + validate=dict( + json={'router': { + 'name': self.router_name, + 'admin_state_up': True, + 'availability_zone_hints': ['nova']}})) + ]) + self.cloud.create_router( + name=self.router_name, admin_state_up=True, + availability_zone_hints=['nova']) + self.assert_calls() + def test_create_router_with_enable_snat_True(self): """Do not send enable_snat when same as neutron default.""" self.register_uris([ @@ -145,6 +177,16 @@ class TestRouter(base.RequestsMockTestCase): name=self.router_name, admin_state_up=True, enable_snat=False) self.assert_calls() + def test_create_router_wrong_availability_zone_hints_type(self): + azh_opts = "invalid" + with testtools.ExpectedException( + exc.OpenStackCloudException, + "Parameter 'availability_zone_hints' must be a list" + ): + self.cloud.create_router( + name=self.router_name, admin_state_up=True, + availability_zone_hints=azh_opts) + def test_add_router_interface(self): self.register_uris([ dict(method='PUT', diff --git a/releasenotes/notes/neutron_availability_zone_extension-675c2460ebb50a09.yaml b/releasenotes/notes/neutron_availability_zone_extension-675c2460ebb50a09.yaml new file mode 100644 index 000000000..058f40bbc --- /dev/null +++ b/releasenotes/notes/neutron_availability_zone_extension-675c2460ebb50a09.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + ``availability_zone_hints`` now accepted for ``create_network()`` when + ``network_availability_zone`` extension is enabled on target cloud. + - | + ``availability_zone_hints`` now accepted for ``create_router()`` when + ``router_availability_zone`` extension is enabled on target cloud.