From b10621f5f85cccde3d159afddb78398544d4c32e Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Thu, 16 Jun 2011 17:30:36 +0400 Subject: First implementation of FloatingIpController --- nova/db/api.py | 3 +++ nova/db/sqlalchemy/api.py | 23 +++++++++++++++++++++++ nova/network/api.py | 4 ++++ 3 files changed, 30 insertions(+) (limited to 'nova') diff --git a/nova/db/api.py b/nova/db/api.py index 4e0aa60a2..8348d90ae 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -223,6 +223,9 @@ def certificate_update(context, certificate_id, values): ################### +def floating_ip_get(context, floating_ip_id): + return IMPL.floating_ip_get(context, floating_ip_id) + def floating_ip_allocate_address(context, host, project_id): """Allocate free floating ip and return the address. diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 73870d2f3..c3b517492 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -428,6 +428,29 @@ def certificate_update(context, certificate_id, values): ################### +@require_context +def floating_ip_get(context, ip_id): + session = get_session() + result = None + if is_admin_context(context): + result = session.query(models.FloatingIp).\ + options(joinedload('fixed_ip')).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(id=ip_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() + elif is_user_context(context): + result = session.query(models.FloatingIp).\ + options(joinedload('fixed_ip')).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(project_id=context.project_id).\ + filter_by(id=ip_id).\ + filter_by(deleted=False).\ + first() + if not result: + raise exception.FloatingIpNotFound() + + return result @require_context diff --git a/nova/network/api.py b/nova/network/api.py index e2eacdf42..e5c4d6718 100644 --- a/nova/network/api.py +++ b/nova/network/api.py @@ -34,6 +34,10 @@ LOG = logging.getLogger('nova.network') class API(base.Base): """API for interacting with the network manager.""" + def get(self, context, id): + rv = self.db.floating_ip_get(context) + return dict(rv.iteritems()) + def allocate_floating_ip(self, context): if quota.allowed_floating_ips(context, 1) < 1: LOG.warn(_('Quota exceeeded for %s, tried to allocate ' -- cgit From 25009974df913c3e5c071b53a6004ae35e37d26b Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Thu, 16 Jun 2011 17:37:45 +0400 Subject: First implementation of FloatingIpController --- nova/api/openstack/contrib/floating_ips.py | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 nova/api/openstack/contrib/floating_ips.py (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py new file mode 100644 index 000000000..b67b8b4ec --- /dev/null +++ b/nova/api/openstack/contrib/floating_ips.py @@ -0,0 +1,54 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# Copyright 2011 Grid Dynamics +# +# 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 webob import exc + +from nova import exception +from nova import network +from nova.api.openstack import faults + +def _translate_floating_ip_detail_view(context, floating_ip): + #TODO(enugaev) implement view + return None + +class FloatingIPController(object): + """The Volumes API controller for the OpenStack API.""" + + _serialization_metadata = { + 'application/xml': { + "attributes": { + "floating_ip": [ + "id", + "ip", + "instance_id", + "fixed_ip", + ]}}} + + def __init__(self): + self.network_api = network.API() + super(FloatingIPController, self).__init__() + + def show(self, req, id): + """Return data about the given volume.""" + context = req.environ['nova.context'] + + try: + floating_ip = self.network_api.get(context, id) + except exception.NotFound: + return faults.Fault(exc.HTTPNotFound()) + + return {'volume': _translate_floating_ip_detail_view(context, + floating_ip)} \ No newline at end of file -- cgit From a1e310aaa9f0ef829e2857c524be140541f3a13d Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Thu, 16 Jun 2011 20:31:09 +0400 Subject: floating_ips extension is loading to api now --- nova/api/openstack/contrib/floating_ips.py | 45 ++++++++++++++++++++++++++++-- nova/network/api.py | 5 ++++ 2 files changed, 48 insertions(+), 2 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index b67b8b4ec..6c08f52e5 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -19,11 +19,16 @@ from webob import exc from nova import exception from nova import network from nova.api.openstack import faults +from nova.api.openstack import extensions def _translate_floating_ip_detail_view(context, floating_ip): #TODO(enugaev) implement view return None +def _translate_floating_ips_view(context, floating_ips): + #TODO(adiantum) implement view + return [] + class FloatingIPController(object): """The Volumes API controller for the OpenStack API.""" @@ -50,5 +55,41 @@ class FloatingIPController(object): except exception.NotFound: return faults.Fault(exc.HTTPNotFound()) - return {'volume': _translate_floating_ip_detail_view(context, - floating_ip)} \ No newline at end of file + return {'floating_ips': _translate_floating_ip_detail_view(context, + floating_ip)} + + def index(self, req): + context = req.environ['nova.context'] + + floating_ips = self.network_api.list(context) + + return {'floating_ips' : _translate_floating_ips_view(context, + floating_ips)} + + +class Floating_ips(extensions.ExtensionDescriptor): + def get_name(self): + return "Floating_ips" + + def get_alias(self): + return "FLOATING_IPS" + + def get_description(self): + return "Floating IPs support" + + def get_namespace(self): + return "http://docs.openstack.org/ext/floating_ips/api/v1.1" + + def get_updated(self): + return "2011-06-16T00:00:00+00:00" + + def get_resources(self): + resources = [] + + res = extensions.ResourceExtension('floating_ips', + FloatingIPController(), + collection_actions={}) + resources.append(res) + + return resources + diff --git a/nova/network/api.py b/nova/network/api.py index e5c4d6718..001c5a6f6 100644 --- a/nova/network/api.py +++ b/nova/network/api.py @@ -38,6 +38,11 @@ class API(base.Base): rv = self.db.floating_ip_get(context) return dict(rv.iteritems()) + def list(self, context): + ips = self.db.floating_ip_get_all_by_project(context, + context.project_id) + return ips + def allocate_floating_ip(self, context): if quota.allowed_floating_ips(context, 1) < 1: LOG.warn(_('Quota exceeeded for %s, tried to allocate ' -- cgit From 971efd1b5568c324c91e826fc347c49ceea3790c Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Fri, 17 Jun 2011 17:27:06 +0400 Subject: stub api methods --- nova/api/openstack/contrib/floating_ips.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index 6c08f52e5..f7a939ddd 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -66,6 +66,25 @@ class FloatingIPController(object): return {'floating_ips' : _translate_floating_ips_view(context, floating_ips)} + def create(self, req, body): + context = req.environ['nova.context'] + + return {'allocate': None} + + def delete(self,req, id): + context = req.environ['nova.context'] + + return {'release': None } + + def associate(self, req, id, body): + context = req.environ['nova.context'] + + return {'associate': None} + + def disassociate(self, req, id, body): + context = req.environ['nova.context'] + + return {'disassociate': None} class Floating_ips(extensions.ExtensionDescriptor): def get_name(self): @@ -88,7 +107,9 @@ class Floating_ips(extensions.ExtensionDescriptor): res = extensions.ResourceExtension('floating_ips', FloatingIPController(), - collection_actions={}) + member_actions={ + 'associate' : 'POST', + 'disassociate' : 'POST'}) resources.append(res) return resources -- cgit From b7684e0d36010050ce8254bbbf4573a6a624fa69 Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Thu, 23 Jun 2011 04:28:42 +0400 Subject: allocate and release implementation --- nova/api/openstack/contrib/floating_ips.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index f7a939ddd..9bad6806c 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -18,6 +18,7 @@ from webob import exc from nova import exception from nova import network +from nova import rpc from nova.api.openstack import faults from nova.api.openstack import extensions @@ -69,12 +70,27 @@ class FloatingIPController(object): def create(self, req, body): context = req.environ['nova.context'] - return {'allocate': None} + try: + ip = self.network_api.allocate_floating_ip(context) + except rpc.RemoteError as ex: + if ex.exc_type == 'NoMoreAddresses': + raise exception.NoMoreFloatingIps() + else: + raise + + return {'allocated': ip} def delete(self,req, id): context = req.environ['nova.context'] - return {'release': None } + if id.isdigit(): + ip = self.network_api.get(id) + else: + ip = id + + self.network_api.release_floating_ip(context, address=ip) + + return {'released': ip } def associate(self, req, id, body): context = req.environ['nova.context'] -- cgit From c2216547d0c55e32a4f8203129f4604f4ba004c7 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Fri, 24 Jun 2011 00:39:37 +0400 Subject: Implemented view and added tests --- nova/api/openstack/contrib/floating_ips.py | 57 +++++++++++++--------- nova/network/api.py | 2 +- nova/tests/api/openstack/contrib/__init__.py | 15 ++++++ .../api/openstack/contrib/test_floating_ips.py | 47 ++++++++++++++++++ 4 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 nova/tests/api/openstack/contrib/__init__.py create mode 100644 nova/tests/api/openstack/contrib/test_floating_ips.py (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index 9bad6806c..dc048869c 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -22,13 +22,25 @@ from nova import rpc from nova.api.openstack import faults from nova.api.openstack import extensions -def _translate_floating_ip_detail_view(context, floating_ip): - #TODO(enugaev) implement view - return None -def _translate_floating_ips_view(context, floating_ips): - #TODO(adiantum) implement view - return [] +def _translate_floating_ip_view(floating_ip): + result = {'id': floating_ip['id'], + 'ip': floating_ip['address']} + if 'fixed_ip' in floating_ip: + result['fixed_ip'] = floating_ip['fixed_ip']['address'] + else: + result['fixed_ip'] = None + if 'instance' in floating_ip: + result['instance_id'] = floating_ip['instance']['id'] + else: + result['instance_id'] = None + return {'floating_ip': result} + + +def _translate_floating_ips_view(floating_ips): + return {'floating_ips': [_translate_floating_ip_view(floating_ip) + for floating_ip in floating_ips]} + class FloatingIPController(object): """The Volumes API controller for the OpenStack API.""" @@ -48,7 +60,7 @@ class FloatingIPController(object): super(FloatingIPController, self).__init__() def show(self, req, id): - """Return data about the given volume.""" + """Return data about the given floating ip.""" context = req.environ['nova.context'] try: @@ -56,16 +68,14 @@ class FloatingIPController(object): except exception.NotFound: return faults.Fault(exc.HTTPNotFound()) - return {'floating_ips': _translate_floating_ip_detail_view(context, - floating_ip)} + return _translate_floating_ip_view(floating_ip) def index(self, req): context = req.environ['nova.context'] floating_ips = self.network_api.list(context) - return {'floating_ips' : _translate_floating_ips_view(context, - floating_ips)} + return _translate_floating_ips_view(floating_ips) def create(self, req, body): context = req.environ['nova.context'] @@ -80,17 +90,13 @@ class FloatingIPController(object): return {'allocated': ip} - def delete(self,req, id): + def delete(self, req, id): context = req.environ['nova.context'] - - if id.isdigit(): - ip = self.network_api.get(id) - else: - ip = id - + + ip = self._get_ip_by_id(context, id) self.network_api.release_floating_ip(context, address=ip) - return {'released': ip } + return {'released': ip} def associate(self, req, id, body): context = req.environ['nova.context'] @@ -102,6 +108,14 @@ class FloatingIPController(object): return {'disassociate': None} + def _get_ip_by_id(self, context, value): + """Checks that value is id and then returns its address. + If value is ip then return value.""" + if value.isdigit(): + return self.network_api.get(context, value)['address'] + return value + + class Floating_ips(extensions.ExtensionDescriptor): def get_name(self): return "Floating_ips" @@ -124,9 +138,8 @@ class Floating_ips(extensions.ExtensionDescriptor): res = extensions.ResourceExtension('floating_ips', FloatingIPController(), member_actions={ - 'associate' : 'POST', - 'disassociate' : 'POST'}) + 'associate': 'POST', + 'disassociate': 'POST'}) resources.append(res) return resources - diff --git a/nova/network/api.py b/nova/network/api.py index 001c5a6f6..a9a7ba552 100644 --- a/nova/network/api.py +++ b/nova/network/api.py @@ -35,7 +35,7 @@ class API(base.Base): """API for interacting with the network manager.""" def get(self, context, id): - rv = self.db.floating_ip_get(context) + rv = self.db.floating_ip_get(context, id) return dict(rv.iteritems()) def list(self, context): diff --git a/nova/tests/api/openstack/contrib/__init__.py b/nova/tests/api/openstack/contrib/__init__.py new file mode 100644 index 000000000..848908a95 --- /dev/null +++ b/nova/tests/api/openstack/contrib/__init__.py @@ -0,0 +1,15 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC +# +# 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. diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py new file mode 100644 index 000000000..9e079c9b0 --- /dev/null +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -0,0 +1,47 @@ +# Copyright 2011 Eldar Nugaev +# 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 nova import context +from nova import db +from nova import test +from nova.api.openstack.contrib.floating_ips import FloatingIPController +from nova.api.openstack.contrib.floating_ips import \ + _translate_floating_ip_view + + +class FloatingIpTest(test.TestCase): + address = "10.10.10.10" + + def _create_floating_ip(self): + """Create a volume object.""" + host = "fake_host" + return db.floating_ip_create(self.context, + {'address': self.address, + 'host': host}) + + def setUp(self): + super(FloatingIpTest, self).setUp() + self.floating_ips = FloatingIPController() + self.context = context.get_admin_context() + + def test_translate_floating_ip_view(self): + floating_ip_address = self._create_floating_ip() + floating_ip = db.floating_ip_get_by_address(self.context, + floating_ip_address) + view = _translate_floating_ip_view(floating_ip) + self.assertTrue('floating_ip' in view) + self.assertTrue(view['floating_ip']['id']) + self.assertEqual(view['floating_ip']['ip'], self.address) + self.assertEqual(view['floating_ip']['fixed_ip'], None) + self.assertEqual(view['floating_ip']['instance_id'], None) -- cgit From c5745c0cb61bb6ab375a1e52d5e203a7a0a76366 Mon Sep 17 00:00:00 2001 From: Kirill Shileev Date: Fri, 24 Jun 2011 20:59:32 +0400 Subject: associate diassociate untested, first attept to test --- nova/api/openstack/contrib/floating_ips.py | 27 +++++++++++++---- .../api/openstack/contrib/test_floating_ips.py | 34 +++++++++++++++++----- 2 files changed, 48 insertions(+), 13 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index dc048869c..23864c352 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -43,7 +43,7 @@ def _translate_floating_ips_view(floating_ips): class FloatingIPController(object): - """The Volumes API controller for the OpenStack API.""" + """The Floating IPs API controller for the OpenStack API.""" _serialization_metadata = { 'application/xml': { @@ -98,15 +98,32 @@ class FloatingIPController(object): return {'released': ip} - def associate(self, req, id, body): + def associate(self, req, id_ip, body): + """ /floating_ips/ip or id/associate fixed ip in body """ context = req.environ['nova.context'] + floating_ip = self._get_ip_by_id(context, id_ip) - return {'associate': None} + fixed_ip = body['associate_address']['fixed_ip'] - def disassociate(self, req, id, body): + try: + self.network_api.associate_floating_ip(context, floating_ip, fixed_ip) + except rpc.RemoteError: + raise + + return {'associated': [floating_ip, fixed_ip]} + + def disassociate(self, req, id_ip, body): + """ POST /floating_ips/{ip | ip_id}/disassociate """ context = req.environ['nova.context'] - return {'disassociate': None} + floating_ip = self._get_ip_by_id(context, id_ip) + + try: + self.network_api.disassociate_floating_ip(context, floating_ip) + except rpc.RemoteError: + raise + + return {'disassociated': floating_ip} def _get_ip_by_id(self, context, value): """Checks that value is id and then returns its address. diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 9e079c9b0..4ac1cc270 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -15,24 +15,27 @@ from nova import context from nova import db from nova import test +import webob from nova.api.openstack.contrib.floating_ips import FloatingIPController from nova.api.openstack.contrib.floating_ips import \ _translate_floating_ip_view class FloatingIpTest(test.TestCase): - address = "10.10.10.10" + floating_ip_address = "10.10.10.10" + fixed_ip_address = '100.100.100.100' + + def _create_fixed_ip(self): + """Create a fixed ip object. Returns address as string""" + return db.fixed_ip_create(self.context, {'address': self.floating_ip_address}) def _create_floating_ip(self): - """Create a volume object.""" - host = "fake_host" - return db.floating_ip_create(self.context, - {'address': self.address, - 'host': host}) + """Create a floating ip object. Returns address as string""" + return db.floating_ip_create(self.context, {'address': self.floating_ip_address, }) def setUp(self): super(FloatingIpTest, self).setUp() - self.floating_ips = FloatingIPController() + self.controller = FloatingIPController() self.context = context.get_admin_context() def test_translate_floating_ip_view(self): @@ -42,6 +45,21 @@ class FloatingIpTest(test.TestCase): view = _translate_floating_ip_view(floating_ip) self.assertTrue('floating_ip' in view) self.assertTrue(view['floating_ip']['id']) - self.assertEqual(view['floating_ip']['ip'], self.address) + self.assertEqual(view['floating_ip']['ip'], self.floating_ip_address) self.assertEqual(view['floating_ip']['fixed_ip'], None) self.assertEqual(view['floating_ip']['instance_id'], None) + + def test_associate_by_address(self): + fixed_ip_address = self._create_fixed_ip() + floating_ip_address = self._create_floating_ip() + floating_ip = db.floating_ip_get_by_address(self.context, floating_ip_address) + + self.assertEqual(floating_ip['address'], self.floating_ip_address) + self.assertEqual(floating_ip['fixed_ip_id'], None) + body = {'associate_address': {'fixed_ip': self.fixed_ip_address}} + + req = webob.Request.blank('/v1.1/floating_ips//associate') + raise Exception(req.__dict__) + #self.controller.associate(req, self.floating_ip_address, body) + #self.assertEqual(floating_ip['fixed_ip'], self.fixed_ip_address) + -- cgit From 4c3993ebcee2cf1abe24a9065822c88bbcb0df55 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Fri, 24 Jun 2011 23:31:21 +0400 Subject: add stubs for flating api os api testing --- .../api/openstack/contrib/test_floating_ips.py | 43 +++++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'nova') diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 0faeaeb39..21b8fdec3 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -15,14 +15,33 @@ from nova import context from nova import db from nova import test +from nova import network +from nova.tests.api.openstack import fakes import stubout import webob -from nova.api.openstack.contrib.floating_ips import FloatingIPController from nova.api.openstack.contrib.floating_ips import \ _translate_floating_ip_view +def network_api_get(): + pass + +def network_api_list(): + pass + +def network_api_allocate(): + pass + +def network_api_release(): + pass + +def network_api_associate(): + pass + +def network_api_disassociate(): + pass + class FloatingIpTest(test.TestCase): floating_ip_address = "10.10.10.10" @@ -40,10 +59,30 @@ class FloatingIpTest(test.TestCase): def setUp(self): super(FloatingIpTest, self).setUp() - self.controller = FloatingIPController() self.stubs = stubout.StubOutForTesting() + fakes.FakeAuthManager.reset_fake_data() + fakes.FakeAuthDatabase.data = {} + fakes.stub_out_networking(self.stubs) + fakes.stub_out_rate_limiting(self.stubs) + fakes.stub_out_auth(self.stubs) + self.stubs.Set(network.api, "get", + network_api_get) + self.stubs.Set(network.api, "list", + network_api_list) + self.stubs.Set(network.api, "allocate_floating_ip", + network_api_allocate) + self.stubs.Set(network.api, "release_floating_ip", + network_api_release) + self.stubs.Set(network.api, "associate_floating_ip", + network_api_associate) + self.stubs.Set(network.api, "disassociate_floating_ip", + network_api_disassociate) self.context = context.get_admin_context() + def tearDown(self): + self.stubs.UnsetAll() + super(FloatingIpTest, self).tearDown() + def test_translate_floating_ip_view(self): floating_ip_address = self._create_floating_ip() floating_ip = db.floating_ip_get_by_address(self.context, -- cgit From 09d439cd74290a6b2532376afc94d2c8e23cdda6 Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Fri, 24 Jun 2011 23:55:18 +0400 Subject: stub tests --- nova/api/openstack/contrib/floating_ips.py | 1 + .../api/openstack/contrib/test_floating_ips.py | 47 ++++++++++++---------- 2 files changed, 27 insertions(+), 21 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index 23864c352..924b504a8 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -2,6 +2,7 @@ # Copyright 2011 OpenStack LLC. # Copyright 2011 Grid Dynamics +# Copyright 2011 Eldar Nugaev, Kirill Shileev, Ilya Alekseyev # # 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 diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 21b8fdec3..31b571eb9 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -15,12 +15,16 @@ from nova import context from nova import db from nova import test +<<<<<<< TREE +from nova.api.openstack.contrib.floating_ips import FloatingIPController +======= from nova import network from nova.tests.api.openstack import fakes import stubout import webob +>>>>>>> MERGE-SOURCE from nova.api.openstack.contrib.floating_ips import \ _translate_floating_ip_view @@ -44,18 +48,14 @@ def network_api_disassociate(): class FloatingIpTest(test.TestCase): - floating_ip_address = "10.10.10.10" - fixed_ip_address = '100.100.100.100' - - def _create_fixed_ip(self): - """Create a fixed ip object. Returns address as string""" - return db.fixed_ip_create(self.context, - {'address': self.fixed_ip_address}) + address = "10.10.10.10" def _create_floating_ip(self): - """Create a floating ip object. Returns address as string""" + """Create a volume object.""" + host = "fake_host" return db.floating_ip_create(self.context, - {'address': self.floating_ip_address}) + {'address': self.address, + 'host': host}) def setUp(self): super(FloatingIpTest, self).setUp() @@ -90,22 +90,27 @@ class FloatingIpTest(test.TestCase): view = _translate_floating_ip_view(floating_ip) self.assertTrue('floating_ip' in view) self.assertTrue(view['floating_ip']['id']) - self.assertEqual(view['floating_ip']['ip'], self.floating_ip_address) + self.assertEqual(view['floating_ip']['ip'], self.address) self.assertEqual(view['floating_ip']['fixed_ip'], None) self.assertEqual(view['floating_ip']['instance_id'], None) + def test_translate_floating_ips_view(self): + pass - def test_associate_by_address(self): - fixed_ip_address = self._create_fixed_ip() - floating_ip_address = self._create_floating_ip() - floating_ip = db.floating_ip_get_by_address(self.context, floating_ip_address) + def test_floating_ips_list(self): + pass + + def test_floating_ip_show(self): + pass + + def test_floating_ip_allocate(self): + pass - self.assertEqual(floating_ip['address'], self.floating_ip_address) - self.assertEqual(floating_ip['fixed_ip_id'], None) - body = {'associate_address': {'fixed_ip': self.fixed_ip_address}} + def test_floating_ip_release(self): + pass - req = webob.Request.blank('/v1.1/floating_ips//associate') - raise Exception(req.__dict__) - #self.controller.associate(req, self.floating_ip_address, body) - #self.assertEqual(floating_ip['fixed_ip'], self.fixed_ip_address) + def test_floating_ip_associate(self): + pass + def test_floating_ip_disassociate(self): + pass \ No newline at end of file -- cgit From 153621b9f3a4480b544de5ccd2a96bf4d63adbc9 Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Fri, 24 Jun 2011 23:57:10 +0400 Subject: conflict resolved --- nova/tests/api/openstack/contrib/test_floating_ips.py | 4 ---- 1 file changed, 4 deletions(-) (limited to 'nova') diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 31b571eb9..5dca0b5ea 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -15,16 +15,12 @@ from nova import context from nova import db from nova import test -<<<<<<< TREE -from nova.api.openstack.contrib.floating_ips import FloatingIPController -======= from nova import network from nova.tests.api.openstack import fakes import stubout import webob ->>>>>>> MERGE-SOURCE from nova.api.openstack.contrib.floating_ips import \ _translate_floating_ip_view -- cgit From ee82eb5916cd87ee984d00a07759d7c7648c6976 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Sat, 25 Jun 2011 00:30:22 +0400 Subject: fix tests for extensions --- .../api/openstack/contrib/test_floating_ips.py | 33 ++++++++++++++-------- nova/tests/api/openstack/fakes.py | 10 ++----- 2 files changed, 24 insertions(+), 19 deletions(-) (limited to 'nova') diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 5dca0b5ea..bc3c20bf2 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -12,20 +12,23 @@ # 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 json +import stubout +import webob + from nova import context from nova import db from nova import test from nova import network from nova.tests.api.openstack import fakes -import stubout -import webob - from nova.api.openstack.contrib.floating_ips import \ _translate_floating_ip_view -def network_api_get(): - pass +def network_api_get(self, context, id): + return {'id': 1, + 'address': '10.10.10.10'} def network_api_list(): pass @@ -61,17 +64,17 @@ class FloatingIpTest(test.TestCase): fakes.stub_out_networking(self.stubs) fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_auth(self.stubs) - self.stubs.Set(network.api, "get", + self.stubs.Set(network.api.API, "get", network_api_get) - self.stubs.Set(network.api, "list", + self.stubs.Set(network.api.API, "list", network_api_list) - self.stubs.Set(network.api, "allocate_floating_ip", + self.stubs.Set(network.api.API, "allocate_floating_ip", network_api_allocate) - self.stubs.Set(network.api, "release_floating_ip", + self.stubs.Set(network.api.API, "release_floating_ip", network_api_release) - self.stubs.Set(network.api, "associate_floating_ip", + self.stubs.Set(network.api.API, "associate_floating_ip", network_api_associate) - self.stubs.Set(network.api, "disassociate_floating_ip", + self.stubs.Set(network.api.API, "disassociate_floating_ip", network_api_disassociate) self.context = context.get_admin_context() @@ -97,7 +100,13 @@ class FloatingIpTest(test.TestCase): pass def test_floating_ip_show(self): - pass + req = webob.Request.blank('/v1.1/floating_ips/1') + res = req.get_response(fakes.wsgi_app()) + res_dict = json.loads(res.body) + self.assertEqual(res_dict['floating_ip']['id'], 1) + self.assertEqual(res_dict['floating_ip']['ip'], '10.10.10.10') + self.assertEqual(res_dict['floating_ip']['fixed_ip'], None) + self.assertEqual(res_dict['floating_ip']['instance_id'], None) def test_floating_ip_allocate(self): pass diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index a10fb7433..a818c1330 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -16,7 +16,6 @@ # under the License. import copy -import json import random import string @@ -29,19 +28,15 @@ from glance.common import exception as glance_exc from nova import context from nova import exception as exc -from nova import flags from nova import utils import nova.api.openstack.auth from nova.api import openstack from nova.api.openstack import auth +from nova.api.openstack import extensions from nova.api.openstack import versions from nova.api.openstack import limits from nova.auth.manager import User, Project import nova.image.fake -from nova.image import glance -from nova.image import local -from nova.image import service -from nova.tests import fake_flags from nova.wsgi import Router @@ -83,7 +78,8 @@ def wsgi_app(inner_app10=None, inner_app11=None): api10 = openstack.FaultWrapper(auth.AuthMiddleware( limits.RateLimitingMiddleware(inner_app10))) api11 = openstack.FaultWrapper(auth.AuthMiddleware( - limits.RateLimitingMiddleware(inner_app11))) + limits.RateLimitingMiddleware( + extensions.ExtensionMiddleware(inner_app11)))) mapper['/v1.0'] = api10 mapper['/v1.1'] = api11 mapper['/'] = openstack.FaultWrapper(versions.Versions()) -- cgit From cbd0622ffbd021d404270be8b35b3e4839dd0ea0 Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Sat, 25 Jun 2011 00:33:40 +0400 Subject: some tests --- nova/tests/api/openstack/contrib/test_floating_ips.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'nova') diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 5dca0b5ea..55bd8c1a1 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -20,6 +20,7 @@ from nova.tests.api.openstack import fakes import stubout import webob +import json from nova.api.openstack.contrib.floating_ips import \ _translate_floating_ip_view @@ -30,8 +31,8 @@ def network_api_get(): def network_api_list(): pass -def network_api_allocate(): - pass +def network_api_allocate(context): + return '10.10.10.10' def network_api_release(): pass @@ -47,7 +48,7 @@ class FloatingIpTest(test.TestCase): address = "10.10.10.10" def _create_floating_ip(self): - """Create a volume object.""" + """Create a floating ip object.""" host = "fake_host" return db.floating_ip_create(self.context, {'address': self.address, @@ -100,7 +101,13 @@ class FloatingIpTest(test.TestCase): pass def test_floating_ip_allocate(self): - pass + req = webob.Request.blank('/v1.1/floating_ips') + req.method = 'POST' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + ip = json.loads(res.body)['allocated'] + expected = '10.10.10.10' + self.assertEqual(ip, expected) def test_floating_ip_release(self): pass -- cgit From 91cf150ac2acdc742f56cc03a67dbee833525bce Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Sat, 25 Jun 2011 01:47:25 +0400 Subject: implement list test --- .../api/openstack/contrib/test_floating_ips.py | 37 ++++++++++++++++------ 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'nova') diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index bc3c20bf2..494569a97 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -23,25 +23,34 @@ from nova import test from nova import network from nova.tests.api.openstack import fakes -from nova.api.openstack.contrib.floating_ips import \ +from nova.api.openstack.contrib.floating_ips import\ _translate_floating_ip_view def network_api_get(self, context, id): - return {'id': 1, - 'address': '10.10.10.10'} + return {'id': id, 'address': '10.10.10.10'} + + +def network_api_list(self, context): + return [{'id': 1, + 'address': '10.10.10.10', + 'instance': {'id': 11}, + 'fixed_ip': {'address': '10.0.0.1'}}, + {'id': 2, + 'address': '10.10.10.11'}] -def network_api_list(): - pass def network_api_allocate(): pass + def network_api_release(): pass + def network_api_associate(): pass + def network_api_disassociate(): pass @@ -93,12 +102,20 @@ class FloatingIpTest(test.TestCase): self.assertEqual(view['floating_ip']['fixed_ip'], None) self.assertEqual(view['floating_ip']['instance_id'], None) - def test_translate_floating_ips_view(self): - pass - def test_floating_ips_list(self): - pass - + req = webob.Request.blank('/v1.1/floating_ips') + res = req.get_response(fakes.wsgi_app()) + res_dict = json.loads(res.body) + response = {'floating_ips': [{'floating_ip': {'instance_id': 11, + 'ip': '10.10.10.10', + 'fixed_ip': '10.0.0.1', + 'id': 1}}, + {'floating_ip': {'instance_id': None, + 'ip': '10.10.10.11', + 'fixed_ip': None, + 'id': 2}}]} + self.assertEqual(res_dict, response) + def test_floating_ip_show(self): req = webob.Request.blank('/v1.1/floating_ips/1') res = req.get_response(fakes.wsgi_app()) -- cgit From a770864d308242bfcfa8dadb210595785d8fa71f Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Sat, 25 Jun 2011 02:42:27 +0400 Subject: tests --- nova/api/openstack/contrib/floating_ips.py | 14 +++++++---- .../api/openstack/contrib/test_floating_ips.py | 29 ++++++++++++++-------- 2 files changed, 27 insertions(+), 16 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index c6bc85c61..95502a5c5 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -104,10 +104,10 @@ class FloatingIPController(object): "id": ip['id'], "floating_ip": ip['address']}} - def associate(self, req, id_ip, body): + def associate(self, req, id, body): """ /floating_ips/{id}/associate fixed ip in body """ context = req.environ['nova.context'] - floating_ip = self._get_ip_by_id(context, id_ip) + floating_ip = self._get_ip_by_id(context, id) fixed_ip = body['associate_address']['fixed_ip'] @@ -117,13 +117,17 @@ class FloatingIPController(object): except rpc.RemoteError: raise - return {'associated': [floating_ip, fixed_ip]} + return {'associated': + { + "floating_ip_id": id, + "floating_ip": floating_ip, + "fixed_ip": fixed_ip}} - def disassociate(self, req, id_ip, body): + def disassociate(self, req, id, body): """ POST /floating_ips/{id}/disassociate """ context = req.environ['nova.context'] - floating_ip = self._get_ip_by_id(context, id_ip) + floating_ip = self._get_ip_by_id(context, id) try: self.network_api.disassociate_floating_ip(context, floating_ip) diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index efdfdcf74..1f2012ec7 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -28,7 +28,7 @@ from nova.api.openstack.contrib.floating_ips import FloatingIPController from nova.api.openstack.contrib.floating_ips import _translate_floating_ip_view def network_api_get(self, context, id): - return {'id': id, 'address': '10.10.10.10'} + return {'id': 1, 'address': '10.10.10.10'} def network_api_list(self, context): @@ -47,7 +47,7 @@ def network_api_allocate(self, context): def network_api_release(self, context, address): pass -def network_api_associate(): +def network_api_associate(self, context,floating_ip, fixed_ip): pass @@ -97,12 +97,6 @@ class FloatingIpTest(test.TestCase): self._delete_floating_ip() super(FloatingIpTest, self).tearDown() - def test_get_ip_by_id(self): - ip = self.controller._get_ip_by_id(self.context, '10.10.10.10') - self.assertEqual(ip, '10.10.10.10') - ip = self.controller._get_ip_by_id(self.context, '1') - self.assertEqual(ip, '10.10.10.10') - def test_translate_floating_ip_view(self): floating_ip_address = self._create_floating_ip() floating_ip = db.floating_ip_get_by_address(self.context, @@ -154,15 +148,28 @@ class FloatingIpTest(test.TestCase): req.method = 'DELETE' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) - ip = json.loads(res.body)['released'] + actual = json.loads(res.body)['released'] expected = { "id": 1, "floating_ip": '10.10.10.10' } - self.assertEqual(ip, expected) + self.assertEqual(actual, expected) def test_floating_ip_associate(self): - pass + body = dict(associate_address=dict(fixed_ip='1.2.3.4')) + req = webob.Request.blank('/v1.1/floating_ips/1/associate') + req.method = 'POST' + req.body = json.dumps(body) + req.headers["content-type"] = "application/json" + + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + actual = json.loads(res.body)['associated'] + expected = { + "floating_ip_id": '1', + "floating_ip": "10.10.10.10", + "fixed_ip": "1.2.3.4"} + self.assertEqual(actual, expected) def test_floating_ip_disassociate(self): pass \ No newline at end of file -- cgit From 5e4d90b33ddb993294232eea168a768486ba0bf4 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Sat, 25 Jun 2011 03:05:09 +0400 Subject: added disassociate method to tests --- nova/api/openstack/contrib/floating_ips.py | 12 ++++++---- nova/db/api.py | 2 ++ nova/db/sqlalchemy/api.py | 2 ++ nova/exception.py | 2 ++ .../api/openstack/contrib/test_floating_ips.py | 28 ++++++++++++++++------ 5 files changed, 34 insertions(+), 12 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index 95502a5c5..467b46712 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -91,7 +91,7 @@ class FloatingIPController(object): raise return {'allocated': { - "id" : ip['id'], + "id": ip['id'], "floating_ip": ip['address']}} def delete(self, req, id): @@ -126,15 +126,17 @@ class FloatingIPController(object): def disassociate(self, req, id, body): """ POST /floating_ips/{id}/disassociate """ context = req.environ['nova.context'] - - floating_ip = self._get_ip_by_id(context, id) + floating_ip = self.network_api.get(context, id) + address = floating_ip['address'] + fixed_ip = floating_ip['fixed_ip']['address'] try: - self.network_api.disassociate_floating_ip(context, floating_ip) + self.network_api.disassociate_floating_ip(context, address) except rpc.RemoteError: raise - return {'disassociated': floating_ip} + return {'disassociated': {'floating_ip': address, + 'fixed_ip': fixed_ip}} def _get_ip_by_id(self, context, value): """Checks that value is id and then returns its address.""" diff --git a/nova/db/api.py b/nova/db/api.py index 8d4b7c8b7..c6ba24478 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -291,10 +291,12 @@ def floating_ip_get_by_address(context, address): """Get a floating ip by address or raise if it doesn't exist.""" return IMPL.floating_ip_get_by_address(context, address) + def floating_ip_get_by_ip(context, ip): """Get a floating ip by floating address.""" return IMPL.floating_ip_get_by_ip(context, ip) + def floating_ip_update(context, address, values): """Update a floating ip by address or raise if it doesn't exist.""" return IMPL.floating_ip_update(context, address, values) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 7735ec8c2..87f112588 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -609,6 +609,7 @@ def floating_ip_get_by_address(context, address, session=None): return result + @require_context def floating_ip_get_by_ip(context, ip, session=None): if not session: @@ -624,6 +625,7 @@ def floating_ip_get_by_ip(context, ip, session=None): return result + @require_context def floating_ip_update(context, address, values): session = get_session() diff --git a/nova/exception.py b/nova/exception.py index 64abff9af..8230c2b26 100644 --- a/nova/exception.py +++ b/nova/exception.py @@ -359,9 +359,11 @@ class DatastoreNotFound(NotFound): class NoFixedIpsFoundForInstance(NotFound): message = _("Instance %(instance_id)s has zero fixed ips.") + class FloatingIpNotDefined(NotFound): message = _("Floating ip %(floating_ip)s not found") + class FloatingIpNotFound(NotFound): message = _("Floating ip not found for fixed address %(fixed_ip)s.") diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 1f2012ec7..4e26994dd 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -27,8 +27,10 @@ from nova.tests.api.openstack import fakes from nova.api.openstack.contrib.floating_ips import FloatingIPController from nova.api.openstack.contrib.floating_ips import _translate_floating_ip_view + def network_api_get(self, context, id): - return {'id': 1, 'address': '10.10.10.10'} + return {'id': 1, 'address': '10.10.10.10', + 'fixed_ip': {'address': '11.0.0.1'}} def network_api_list(self, context): @@ -47,11 +49,12 @@ def network_api_allocate(self, context): def network_api_release(self, context, address): pass -def network_api_associate(self, context,floating_ip, fixed_ip): + +def network_api_associate(self, context, floating_ip, fixed_ip): pass -def network_api_disassociate(): +def network_api_disassociate(self, context, floating_address): pass @@ -111,6 +114,7 @@ class FloatingIpTest(test.TestCase): def test_floating_ips_list(self): req = webob.Request.blank('/v1.1/floating_ips') res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) res_dict = json.loads(res.body) response = {'floating_ips': [{'floating_ip': {'instance_id': 11, 'ip': '10.10.10.10', @@ -121,14 +125,15 @@ class FloatingIpTest(test.TestCase): 'fixed_ip': None, 'id': 2}}]} self.assertEqual(res_dict, response) - + def test_floating_ip_show(self): req = webob.Request.blank('/v1.1/floating_ips/1') res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) res_dict = json.loads(res.body) self.assertEqual(res_dict['floating_ip']['id'], 1) self.assertEqual(res_dict['floating_ip']['ip'], '10.10.10.10') - self.assertEqual(res_dict['floating_ip']['fixed_ip'], None) + self.assertEqual(res_dict['floating_ip']['fixed_ip'], '11.0.0.1') self.assertEqual(res_dict['floating_ip']['instance_id'], None) def test_floating_ip_allocate(self): @@ -161,7 +166,7 @@ class FloatingIpTest(test.TestCase): req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" - + res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) actual = json.loads(res.body)['associated'] @@ -172,4 +177,13 @@ class FloatingIpTest(test.TestCase): self.assertEqual(actual, expected) def test_floating_ip_disassociate(self): - pass \ No newline at end of file + req = webob.Request.blank('/v1.1/floating_ips/1/disassociate') + req.method = 'POST' + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, 200) + ip = json.loads(res.body)['disassociated'] + expected = { + "floating_ip": '10.10.10.10', + "fixed_ip": '11.0.0.1' + } + self.assertEqual(ip, expected) -- cgit From ef1f4d33fa5763ea602c2fc1098a4b230b86e82b Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Mon, 27 Jun 2011 16:33:01 +0400 Subject: review issues fixed --- nova/api/openstack/contrib/floating_ips.py | 20 ++++++++++---------- nova/db/sqlalchemy/api.py | 8 ++++---- nova/exception.py | 4 ++-- nova/network/api.py | 4 ++-- .../tests/api/openstack/contrib/test_floating_ips.py | 12 ++++++------ 5 files changed, 24 insertions(+), 24 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index 467b46712..ca192f501 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -65,7 +65,7 @@ class FloatingIPController(object): context = req.environ['nova.context'] try: - floating_ip = self.network_api.get(context, id) + floating_ip = self.network_api.get_floating_ip(context, id) except exception.NotFound: return faults.Fault(exc.HTTPNotFound()) @@ -74,7 +74,7 @@ class FloatingIPController(object): def index(self, req): context = req.environ['nova.context'] - floating_ips = self.network_api.list(context) + floating_ips = self.network_api.list_floating_ips(context) return _translate_floating_ips_view(floating_ips) @@ -97,7 +97,7 @@ class FloatingIPController(object): def delete(self, req, id): context = req.environ['nova.context'] - ip = self.network_api.get(context, id) + ip = self.network_api.get_floating_ip(context, id) self.network_api.release_floating_ip(context, address=ip) return {'released': { @@ -126,7 +126,7 @@ class FloatingIPController(object): def disassociate(self, req, id, body): """ POST /floating_ips/{id}/disassociate """ context = req.environ['nova.context'] - floating_ip = self.network_api.get(context, id) + floating_ip = self.network_api.get_floating_ip(context, id) address = floating_ip['address'] fixed_ip = floating_ip['fixed_ip']['address'] @@ -140,7 +140,7 @@ class FloatingIPController(object): def _get_ip_by_id(self, context, value): """Checks that value is id and then returns its address.""" - return self.network_api.get(context, value)['address'] + return self.network_api.get_floating_ip(context, value)['address'] class Floating_ips(extensions.ExtensionDescriptor): @@ -148,7 +148,7 @@ class Floating_ips(extensions.ExtensionDescriptor): return "Floating_ips" def get_alias(self): - return "FLOATING_IPS" + return "os-floating-ips" def get_description(self): return "Floating IPs support" @@ -163,10 +163,10 @@ class Floating_ips(extensions.ExtensionDescriptor): resources = [] res = extensions.ResourceExtension('floating_ips', - FloatingIPController(), - member_actions={ - 'associate': 'POST', - 'disassociate': 'POST'}) + FloatingIPController(), + member_actions={ + 'associate': 'POST', + 'disassociate': 'POST'}) resources.append(res) return resources diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 426ea5127..55badb9e0 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -448,7 +448,7 @@ def floating_ip_get(context, ip_id): filter_by(deleted=False).\ first() if not result: - raise exception.FloatingIpNotFound() + raise exception.FloatingIpNotFoundForFixedAddress() return result @@ -605,7 +605,7 @@ def floating_ip_get_by_address(context, address, session=None): filter_by(deleted=can_read_deleted(context)).\ first() if not result: - raise exception.FloatingIpNotFound(fixed_ip=address) + raise exception.FloatingIpNotFoundForFixedAddress(fixed_ip=address) return result @@ -621,7 +621,7 @@ def floating_ip_get_by_ip(context, ip, session=None): first() if not result: - raise exception.FloatingIpNotDefined(floating_ip=ip) + raise exception.FloatingIpNotFound(floating_ip=ip) return result @@ -761,7 +761,7 @@ def fixed_ip_get_by_address(context, address, session=None): options(joinedload('instance')).\ first() if not result: - raise exception.FloatingIpNotFound(fixed_ip=address) + raise exception.FloatingIpNotFoundForFixedAddress(fixed_ip=address) if is_user_context(context): authorize_project_context(context, result.instance.project_id) diff --git a/nova/exception.py b/nova/exception.py index 8230c2b26..8a63a79f2 100644 --- a/nova/exception.py +++ b/nova/exception.py @@ -360,11 +360,11 @@ class NoFixedIpsFoundForInstance(NotFound): message = _("Instance %(instance_id)s has zero fixed ips.") -class FloatingIpNotDefined(NotFound): +class FloatingIpNotFound(NotFound): message = _("Floating ip %(floating_ip)s not found") -class FloatingIpNotFound(NotFound): +class FloatingIpNotFoundForFixedAddress(NotFound): message = _("Floating ip not found for fixed address %(fixed_ip)s.") diff --git a/nova/network/api.py b/nova/network/api.py index f1c2dd1f6..dd3ed1709 100644 --- a/nova/network/api.py +++ b/nova/network/api.py @@ -34,7 +34,7 @@ LOG = logging.getLogger('nova.network') class API(base.Base): """API for interacting with the network manager.""" - def get(self, context, id): + def get_floating_ip(self, context, id): rv = self.db.floating_ip_get(context, id) return dict(rv.iteritems()) @@ -42,7 +42,7 @@ class API(base.Base): res = self.db.floating_ip_get_by_ip(context, address) return dict(res.iteritems()) - def list(self, context): + def list_floating_ips(self, context): ips = self.db.floating_ip_get_all_by_project(context, context.project_id) return ips diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 4e26994dd..4a74861bd 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -28,12 +28,12 @@ from nova.api.openstack.contrib.floating_ips import FloatingIPController from nova.api.openstack.contrib.floating_ips import _translate_floating_ip_view -def network_api_get(self, context, id): +def network_api_get_floating_ip(self, context, id): return {'id': 1, 'address': '10.10.10.10', 'fixed_ip': {'address': '11.0.0.1'}} -def network_api_list(self, context): +def network_api_list_floating_ips(self, context): return [{'id': 1, 'address': '10.10.10.10', 'instance': {'id': 11}, @@ -80,10 +80,10 @@ class FloatingIpTest(test.TestCase): fakes.stub_out_networking(self.stubs) fakes.stub_out_rate_limiting(self.stubs) fakes.stub_out_auth(self.stubs) - self.stubs.Set(network.api.API, "get", - network_api_get) - self.stubs.Set(network.api.API, "list", - network_api_list) + self.stubs.Set(network.api.API, "get_floating_ip", + network_api_get_floating_ip) + self.stubs.Set(network.api.API, "list_floating_ips", + network_api_list_floating_ips) self.stubs.Set(network.api.API, "allocate_floating_ip", network_api_allocate) self.stubs.Set(network.api.API, "release_floating_ip", -- cgit From 75dd73b1246904fd11bf9b566bf2319d3e6bada5 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Mon, 27 Jun 2011 17:05:35 +0400 Subject: fixed pep style --- nova/db/sqlalchemy/api.py | 6 +++--- nova/tests/api/openstack/contrib/test_floating_ips.py | 9 +++------ nova/tests/api/openstack/fakes.py | 2 -- 3 files changed, 6 insertions(+), 11 deletions(-) (limited to 'nova') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 55badb9e0..d20e27617 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -429,14 +429,14 @@ def certificate_update(context, certificate_id, values): ################### @require_context -def floating_ip_get(context, ip_id): +def floating_ip_get(context, id): session = get_session() result = None if is_admin_context(context): result = session.query(models.FloatingIp).\ options(joinedload('fixed_ip')).\ options(joinedload_all('fixed_ip.instance')).\ - filter_by(id=ip_id).\ + filter_by(id=id).\ filter_by(deleted=can_read_deleted(context)).\ first() elif is_user_context(context): @@ -444,7 +444,7 @@ def floating_ip_get(context, ip_id): options(joinedload('fixed_ip')).\ options(joinedload_all('fixed_ip.instance')).\ filter_by(project_id=context.project_id).\ - filter_by(id=ip_id).\ + filter_by(id=id).\ filter_by(deleted=False).\ first() if not result: diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index 4a74861bd..bf62925aa 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -144,8 +144,7 @@ class FloatingIpTest(test.TestCase): ip = json.loads(res.body)['allocated'] expected = { "id": 1, - "floating_ip": '10.10.10.10' - } + "floating_ip": '10.10.10.10'} self.assertEqual(ip, expected) def test_floating_ip_release(self): @@ -156,8 +155,7 @@ class FloatingIpTest(test.TestCase): actual = json.loads(res.body)['released'] expected = { "id": 1, - "floating_ip": '10.10.10.10' - } + "floating_ip": '10.10.10.10'} self.assertEqual(actual, expected) def test_floating_ip_associate(self): @@ -184,6 +182,5 @@ class FloatingIpTest(test.TestCase): ip = json.loads(res.body)['disassociated'] expected = { "floating_ip": '10.10.10.10', - "fixed_ip": '11.0.0.1' - } + "fixed_ip": '11.0.0.1'} self.assertEqual(ip, expected) diff --git a/nova/tests/api/openstack/fakes.py b/nova/tests/api/openstack/fakes.py index 2ecdd111f..eeb96cb12 100644 --- a/nova/tests/api/openstack/fakes.py +++ b/nova/tests/api/openstack/fakes.py @@ -37,11 +37,9 @@ from nova.api.openstack import versions from nova.api.openstack import limits from nova.auth.manager import User, Project import nova.image.fake - from nova.image import glance from nova.image import service from nova.tests import fake_flags - from nova.wsgi import Router -- cgit From 7c4f83bc8f119ff4486f913bd3e5ef7eff5b338f Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Mon, 27 Jun 2011 20:36:53 +0400 Subject: changed extension alias to os-floating-ips --- nova/api/openstack/contrib/floating_ips.py | 2 +- nova/tests/api/openstack/contrib/test_floating_ips.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'nova') diff --git a/nova/api/openstack/contrib/floating_ips.py b/nova/api/openstack/contrib/floating_ips.py index ca192f501..914ec5bfb 100644 --- a/nova/api/openstack/contrib/floating_ips.py +++ b/nova/api/openstack/contrib/floating_ips.py @@ -162,7 +162,7 @@ class Floating_ips(extensions.ExtensionDescriptor): def get_resources(self): resources = [] - res = extensions.ResourceExtension('floating_ips', + res = extensions.ResourceExtension('os-floating-ips', FloatingIPController(), member_actions={ 'associate': 'POST', diff --git a/nova/tests/api/openstack/contrib/test_floating_ips.py b/nova/tests/api/openstack/contrib/test_floating_ips.py index bf62925aa..de1eb2f53 100644 --- a/nova/tests/api/openstack/contrib/test_floating_ips.py +++ b/nova/tests/api/openstack/contrib/test_floating_ips.py @@ -112,7 +112,7 @@ class FloatingIpTest(test.TestCase): self.assertEqual(view['floating_ip']['instance_id'], None) def test_floating_ips_list(self): - req = webob.Request.blank('/v1.1/floating_ips') + req = webob.Request.blank('/v1.1/os-floating-ips') res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) res_dict = json.loads(res.body) @@ -127,7 +127,7 @@ class FloatingIpTest(test.TestCase): self.assertEqual(res_dict, response) def test_floating_ip_show(self): - req = webob.Request.blank('/v1.1/floating_ips/1') + req = webob.Request.blank('/v1.1/os-floating-ips/1') res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) res_dict = json.loads(res.body) @@ -137,7 +137,7 @@ class FloatingIpTest(test.TestCase): self.assertEqual(res_dict['floating_ip']['instance_id'], None) def test_floating_ip_allocate(self): - req = webob.Request.blank('/v1.1/floating_ips') + req = webob.Request.blank('/v1.1/os-floating-ips') req.method = 'POST' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) @@ -148,7 +148,7 @@ class FloatingIpTest(test.TestCase): self.assertEqual(ip, expected) def test_floating_ip_release(self): - req = webob.Request.blank('/v1.1/floating_ips/1') + req = webob.Request.blank('/v1.1/os-floating-ips/1') req.method = 'DELETE' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) @@ -160,7 +160,7 @@ class FloatingIpTest(test.TestCase): def test_floating_ip_associate(self): body = dict(associate_address=dict(fixed_ip='1.2.3.4')) - req = webob.Request.blank('/v1.1/floating_ips/1/associate') + req = webob.Request.blank('/v1.1/os-floating-ips/1/associate') req.method = 'POST' req.body = json.dumps(body) req.headers["content-type"] = "application/json" @@ -175,7 +175,7 @@ class FloatingIpTest(test.TestCase): self.assertEqual(actual, expected) def test_floating_ip_disassociate(self): - req = webob.Request.blank('/v1.1/floating_ips/1/disassociate') + req = webob.Request.blank('/v1.1/os-floating-ips/1/disassociate') req.method = 'POST' res = req.get_response(fakes.wsgi_app()) self.assertEqual(res.status_int, 200) -- cgit