diff --git a/doc/v3/api_samples/os-floating-ip-pools/floatingippools-list-resp.json b/doc/v3/api_samples/os-floating-ip-pools/floatingippools-list-resp.json new file mode 100644 index 000000000000..7b648298758d --- /dev/null +++ b/doc/v3/api_samples/os-floating-ip-pools/floatingippools-list-resp.json @@ -0,0 +1,10 @@ +{ + "floating_ip_pools": [ + { + "name": "pool1" + }, + { + "name": "pool2" + } + ] +} \ No newline at end of file diff --git a/etc/nova/policy.json b/etc/nova/policy.json index 8880ee70e5f8..22810f423d69 100644 --- a/etc/nova/policy.json +++ b/etc/nova/policy.json @@ -152,6 +152,8 @@ "compute_extension:v3:flavor-manage": "rule:admin_api", "compute_extension:floating_ip_dns": "", "compute_extension:floating_ip_pools": "", + "compute_extension:v3:os-floating-ip-pools": "", + "compute_extension:v3:os-floating-ip-pools:discoverable": "", "compute_extension:floating_ips": "", "compute_extension:floating_ips_bulk": "rule:admin_api", "compute_extension:fping": "", diff --git a/nova/api/openstack/compute/plugins/v3/floating_ip_pools.py b/nova/api/openstack/compute/plugins/v3/floating_ip_pools.py new file mode 100644 index 000000000000..0af35b551e94 --- /dev/null +++ b/nova/api/openstack/compute/plugins/v3/floating_ip_pools.py @@ -0,0 +1,68 @@ +# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from nova.api.openstack import extensions +from nova import network + + +ALIAS = 'os-floating-ip-pools' +authorize = extensions.extension_authorizer('compute', 'v3:' + ALIAS) + + +def _translate_floating_ip_view(pool_name): + return { + 'name': pool_name, + } + + +def _translate_floating_ip_pools_view(pools): + return { + 'floating_ip_pools': [_translate_floating_ip_view(pool_name) + for pool_name in pools] + } + + +class FloatingIPPoolsController(object): + """The Floating IP Pool API controller for the OpenStack API.""" + + def __init__(self): + self.network_api = network.API() + super(FloatingIPPoolsController, self).__init__() + + @extensions.expected_errors(()) + def index(self, req): + """Return a list of pools.""" + context = req.environ['nova.context'] + authorize(context) + pools = self.network_api.get_floating_ip_pools(context) + return _translate_floating_ip_pools_view(pools) + + +class FloatingIpPools(extensions.V3APIExtensionBase): + """Floating IPs support.""" + + name = "FloatingIpPools" + alias = ALIAS + version = 1 + + def get_resources(self): + resource = [extensions.ResourceExtension(ALIAS, + FloatingIPPoolsController())] + return resource + + def get_controller_extensions(self): + """It's an abstract function V3APIExtensionBase and the extension + will not be loaded without it. + """ + return [] diff --git a/nova/tests/api/openstack/compute/contrib/test_floating_ip_pools.py b/nova/tests/api/openstack/compute/contrib/test_floating_ip_pools.py index da0e62bcf1d1..1b24fe5fdf5b 100644 --- a/nova/tests/api/openstack/compute/contrib/test_floating_ip_pools.py +++ b/nova/tests/api/openstack/compute/contrib/test_floating_ip_pools.py @@ -15,7 +15,9 @@ from lxml import etree -from nova.api.openstack.compute.contrib import floating_ip_pools +from nova.api.openstack.compute.contrib import floating_ip_pools as fipp_v2 +from nova.api.openstack.compute.plugins.v3 import floating_ip_pools as\ + fipp_v21 from nova import context from nova import network from nova import test @@ -26,18 +28,21 @@ def fake_get_floating_ip_pools(self, context): return ['nova', 'other'] -class FloatingIpPoolTest(test.NoDBTestCase): +class FloatingIpPoolTestV21(test.NoDBTestCase): + floating_ip_pools = fipp_v21 + url = '/v2/fake/os-floating-ip-pools' + def setUp(self): - super(FloatingIpPoolTest, self).setUp() + super(FloatingIpPoolTestV21, self).setUp() self.stubs.Set(network.api.API, "get_floating_ip_pools", fake_get_floating_ip_pools) self.context = context.RequestContext('fake', 'fake') - self.controller = floating_ip_pools.FloatingIPPoolsController() + self.controller = self.floating_ip_pools.FloatingIPPoolsController() def test_translate_floating_ip_pools_view(self): pools = fake_get_floating_ip_pools(None, self.context) - view = floating_ip_pools._translate_floating_ip_pools_view(pools) + view = self.floating_ip_pools._translate_floating_ip_pools_view(pools) self.assertIn('floating_ip_pools', view) self.assertEqual(view['floating_ip_pools'][0]['name'], pools[0]) @@ -45,7 +50,7 @@ class FloatingIpPoolTest(test.NoDBTestCase): pools[1]) def test_floating_ips_pools_list(self): - req = fakes.HTTPRequest.blank('/v2/fake/os-floating-ip-pools') + req = fakes.HTTPRequest.blank(self.url) res_dict = self.controller.index(req) pools = fake_get_floating_ip_pools(None, self.context) @@ -53,9 +58,15 @@ class FloatingIpPoolTest(test.NoDBTestCase): self.assertEqual(res_dict, response) -class FloatingIpPoolSerializerTest(test.NoDBTestCase): +class FloatingIpPoolTestV2(FloatingIpPoolTestV21): + floating_ip_pools = fipp_v2 + + +class FloatingIpPoolSerializerTestV2(test.NoDBTestCase): + floating_ip_pools = fipp_v2 + def test_index_serializer(self): - serializer = floating_ip_pools.FloatingIPPoolsTemplate() + serializer = self.floating_ip_pools.FloatingIPPoolsTemplate() text = serializer.serialize(dict( floating_ip_pools=[ dict(name='nova'), diff --git a/nova/tests/fake_policy.py b/nova/tests/fake_policy.py index b59affb0086d..15c531a3ffde 100644 --- a/nova/tests/fake_policy.py +++ b/nova/tests/fake_policy.py @@ -212,6 +212,7 @@ policy_data = """ "compute_extension:v3:flavors:discoverable": "", "compute_extension:floating_ip_dns": "", "compute_extension:floating_ip_pools": "", + "compute_extension:v3:os-floating-ip-pools": "", "compute_extension:floating_ips": "", "compute_extension:floating_ips_bulk": "", "compute_extension:fping": "", diff --git a/nova/tests/integrated/v3/api_samples/os-floating-ip-pools/floatingippools-list-resp.json.tpl b/nova/tests/integrated/v3/api_samples/os-floating-ip-pools/floatingippools-list-resp.json.tpl new file mode 100644 index 000000000000..607109d70d34 --- /dev/null +++ b/nova/tests/integrated/v3/api_samples/os-floating-ip-pools/floatingippools-list-resp.json.tpl @@ -0,0 +1,10 @@ +{ + "floating_ip_pools": [ + { + "name": "%(pool1)s" + }, + { + "name": "%(pool2)s" + } + ] +} diff --git a/nova/tests/integrated/v3/test_floating_ip_pools.py b/nova/tests/integrated/v3/test_floating_ip_pools.py new file mode 100644 index 000000000000..bea1123cb9e4 --- /dev/null +++ b/nova/tests/integrated/v3/test_floating_ip_pools.py @@ -0,0 +1,35 @@ +# Copyright 2014 IBM Corp. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from nova.network import api as network_api +from nova.tests.integrated.v3 import api_sample_base + + +class FloatingIPPoolsSampleTests(api_sample_base.ApiSampleTestBaseV3): + extension_name = "os-floating-ip-pools" + + def test_list_floatingippools(self): + pool_list = ["pool1", "pool2"] + + def fake_get_floating_ip_pools(self, context): + return pool_list + + self.stubs.Set(network_api.API, "get_floating_ip_pools", + fake_get_floating_ip_pools) + response = self._do_get('os-floating-ip-pools') + subs = { + 'pool1': pool_list[0], + 'pool2': pool_list[1] + } + self._verify_response('floatingippools-list-resp', subs, response, 200) diff --git a/setup.cfg b/setup.cfg index 138857b3e5bf..7d18ad394bf6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -84,6 +84,7 @@ nova.api.v3.extensions = flavor_access = nova.api.openstack.compute.plugins.v3.flavor_access:FlavorAccess flavor_rxtx = nova.api.openstack.compute.plugins.v3.flavor_rxtx:FlavorRxtx flavor_manage = nova.api.openstack.compute.plugins.v3.flavor_manage:FlavorManage + floating_ip_pools = nova.api.openstack.compute.plugins.v3.floating_ip_pools:FloatingIpPools fping = nova.api.openstack.compute.plugins.v3.fping:Fping hide_server_addresses = nova.api.openstack.compute.plugins.v3.hide_server_addresses:HideServerAddresses hosts = nova.api.openstack.compute.plugins.v3.hosts:Hosts