From d7fb1f33cca07403944a4a4324dd19e28cc023a9 Mon Sep 17 00:00:00 2001 From: jakedahn Date: Mon, 9 Jan 2012 14:49:12 -0800 Subject: [PATCH] Implementing Floating Ip Pools. See vishy's nova branch: https://review.openstack.org/#change,2892 Change-Id: I6a5bec55edd21f659674f478282e65fd6b1b0b1e --- README.rst | 2 ++ novaclient/v1_1/client.py | 2 ++ novaclient/v1_1/floating_ip_pools.py | 34 ++++++++++++++++++++++++++++ novaclient/v1_1/floating_ips.py | 5 ++-- novaclient/v1_1/shell.py | 14 ++++++++++-- tests/v1_1/fakes.py | 14 ++++++++++++ tests/v1_1/test_floating_ip_pools.py | 31 +++++++++++++++++++++++++ tests/v1_1/test_floating_ips.py | 23 +++++++++++++++++++ 8 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 novaclient/v1_1/floating_ip_pools.py create mode 100644 tests/v1_1/test_floating_ip_pools.py diff --git a/README.rst b/README.rst index a26bded89..407ac52ed 100644 --- a/README.rst +++ b/README.rst @@ -88,6 +88,8 @@ You'll find complete documentation on the shell by running floating-ip-create Allocate a floating IP to the current tenant. floating-ip-delete De-allocate a floating IP from the current tenant. floating-ip-list List allocated floating IPs for the current tenant. + floating-ip-pool-list + List all floating ip pools. help Display help about this program or one of its subcommands. image-create Create a new image by taking a snapshot of a running diff --git a/novaclient/v1_1/client.py b/novaclient/v1_1/client.py index fad14cb0d..f6c165aa4 100644 --- a/novaclient/v1_1/client.py +++ b/novaclient/v1_1/client.py @@ -2,6 +2,7 @@ from novaclient import client from novaclient.v1_1 import flavors from novaclient.v1_1 import floating_ip_dns from novaclient.v1_1 import floating_ips +from novaclient.v1_1 import floating_ip_pools from novaclient.v1_1 import images from novaclient.v1_1 import keypairs from novaclient.v1_1 import limits @@ -46,6 +47,7 @@ class Client(object): # extensions self.floating_ips = floating_ips.FloatingIPManager(self) self.floating_ip_dns = floating_ip_dns.FloatingIPDNSManager(self) + self.floating_ip_pools = floating_ip_pools.FloatingIPPoolManager(self) self.volumes = volumes.VolumeManager(self) self.volume_snapshots = volume_snapshots.SnapshotManager(self) self.keypairs = keypairs.KeypairManager(self) diff --git a/novaclient/v1_1/floating_ip_pools.py b/novaclient/v1_1/floating_ip_pools.py new file mode 100644 index 000000000..c6b678a17 --- /dev/null +++ b/novaclient/v1_1/floating_ip_pools.py @@ -0,0 +1,34 @@ +# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. +# +# All Rights Reserved. +# +# 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. + +import urllib + +from novaclient import base + + +class FloatingIPPool(base.Resource): + def __repr__(self): + return "" % self.name + + +class FloatingIPPoolManager(base.ManagerWithFind): + resource_class = FloatingIPPool + + def list(self): + """ + Retrieve a list of all floating ip pools. + """ + return self._list('/os-floating-ip-pools', 'floating_ip_pools') diff --git a/novaclient/v1_1/floating_ips.py b/novaclient/v1_1/floating_ips.py index eb16cf6a7..c76467474 100644 --- a/novaclient/v1_1/floating_ips.py +++ b/novaclient/v1_1/floating_ips.py @@ -1,3 +1,4 @@ +# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. # Copyright 2011 OpenStack LLC. # All Rights Reserved. # @@ -33,11 +34,11 @@ class FloatingIPManager(base.ManagerWithFind): """ return self._list("/os-floating-ips", "floating_ips") - def create(self): + def create(self, pool=None): """ Create (allocate) a floating ip for a tenant """ - return self._create("/os-floating-ips", {}, "floating_ip") + return self._create("/os-floating-ips", {'pool': pool}, "floating_ip") def delete(self, floating_ip): """ diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index 14a21a271..23e1b4e6c 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -963,7 +963,7 @@ def do_volume_snapshot_delete(cs, args): def _print_floating_ip_list(floating_ips): - utils.print_list(floating_ips, ['Ip', 'Instance Id', 'Fixed Ip']) + utils.print_list(floating_ips, ['Ip', 'Instance Id', 'Fixed Ip', 'Pool']) @utils.arg('server', metavar='', help='Name or ID of server.') @@ -982,9 +982,14 @@ def do_remove_floating_ip(cs, args): server.remove_floating_ip(args.address) +@utils.arg('pool', + metavar='', + help='Name of Floating IP Pool. (Optional)', + nargs='?', + default=None) def do_floating_ip_create(cs, args): """Allocate a floating IP for the current tenant.""" - _print_floating_ip_list([cs.floating_ips.create()]) + _print_floating_ip_list([cs.floating_ips.create(pool=args.pool)]) @utils.arg('address', metavar='
', help='IP of Floating Ip.') @@ -1002,6 +1007,11 @@ def do_floating_ip_list(cs, args): _print_floating_ip_list(cs.floating_ips.list()) +def do_floating_ip_pool_list(cs, args): + """List all floating ip pools.""" + utils.print_list(cs.floating_ip_pools.list(), ['name']) + + def _print_dns_list(dns_entries): utils.print_list(dns_entries, ['ip', 'zone', 'name']) diff --git a/tests/v1_1/fakes.py b/tests/v1_1/fakes.py index 43997ce0f..4f0085d00 100644 --- a/tests/v1_1/fakes.py +++ b/tests/v1_1/fakes.py @@ -1,3 +1,4 @@ +# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. # Copyright 2011 OpenStack, LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -352,6 +353,9 @@ class FakeHTTPClient(base_client.HTTPClient): # Floating ips # + def get_os_floating_ip_pools(self): + return (200, {'floating_ip_pools': [{'name': 'foo', 'name': 'bar'}]}) + def get_os_floating_ips(self, **kw): return (200, {'floating_ips': [ {'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1'}, @@ -366,6 +370,16 @@ class FakeHTTPClient(base_client.HTTPClient): def post_os_floating_ips(self, body, **kw): return (202, self.get_os_floating_ips_1()[1]) + def post_os_floating_ips(self, body): + if body.get('pool'): + return (200, {'floating_ip': + {'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1', + 'pool': 'nova'}}) + else: + return (200, {'floating_ip': + {'id': 1, 'fixed_ip': '10.0.0.1', 'ip': '11.0.0.1', + 'pool': None}}) + def delete_os_floating_ips_1(self, **kw): return (204, None) diff --git a/tests/v1_1/test_floating_ip_pools.py b/tests/v1_1/test_floating_ip_pools.py new file mode 100644 index 000000000..6091b2888 --- /dev/null +++ b/tests/v1_1/test_floating_ip_pools.py @@ -0,0 +1,31 @@ +# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. +# +# All Rights Reserved. +# +# 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 novaclient.v1_1 import floating_ip_pools +from tests import utils +from tests.v1_1 import fakes + + +cs = fakes.FakeClient() + + +class TestFloatingIPPools(utils.TestCase): + + def test_list_floating_ips(self): + fl = cs.floating_ip_pools.list() + cs.assert_called('GET', '/os-floating-ip-pools') + [self.assertTrue(isinstance(f, floating_ip_pools.FloatingIPPool)) + for f in fl] diff --git a/tests/v1_1/test_floating_ips.py b/tests/v1_1/test_floating_ips.py index c1ffb2487..f2895c6db 100644 --- a/tests/v1_1/test_floating_ips.py +++ b/tests/v1_1/test_floating_ips.py @@ -1,3 +1,19 @@ +# Copyright (c) 2011 X.commerce, a business unit of eBay Inc. +# +# All Rights Reserved. +# +# 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 novaclient.v1_1 import floating_ips from tests import utils from tests.v1_1 import fakes @@ -25,4 +41,11 @@ class FloatingIPsTest(utils.TestCase): def test_create_floating_ip(self): fl = cs.floating_ips.create() cs.assert_called('POST', '/os-floating-ips') + self.assertEqual(fl.pool, None) + self.assertTrue(isinstance(fl, floating_ips.FloatingIP)) + + def test_create_floating_ip_with_pool(self): + fl = cs.floating_ips.create('foo') + cs.assert_called('POST', '/os-floating-ips') + self.assertEqual(fl.pool, 'nova') self.assertTrue(isinstance(fl, floating_ips.FloatingIP))