]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Validate protocol when creating VIP.
authorRyan O'Hara <rohara@redhat.com>
Tue, 12 Mar 2013 18:01:23 +0000 (13:01 -0500)
committerRyan O'Hara <rohara@redhat.com>
Mon, 18 Mar 2013 18:07:29 +0000 (13:07 -0500)
Fixes: bug #1123114
When creating a VIP, check that the protocol matches the protocol
for the associated pool. If not, raise an exception.

Change-Id: Iba318eda935ccc89dbe8244e00f36ebdfcce65e0

quantum/db/loadbalancer/loadbalancer_db.py
quantum/extensions/loadbalancer.py
quantum/tests/unit/db/loadbalancer/test_db_loadbalancer.py

index 1d60a72fe72cf59470fb14d27c35eb06761dc12c..a2557f30858fe1c8ea4507cbdf1f3f08f7293800 100644 (file)
@@ -357,11 +357,16 @@ class LoadBalancerPluginDb(LoadBalancerPluginBase):
         tenant_id = self._get_tenant_id_for_create(context, v)
 
         with context.session.begin(subtransactions=True):
-            # validate that the pool has same tenant
             if v['pool_id']:
                 pool = self._get_resource(context, Pool, v['pool_id'])
+                # validate that the pool has same tenant
                 if pool['tenant_id'] != tenant_id:
                     raise q_exc.NotAuthorized()
+                # validate that the pool has same protocol
+                if pool['protocol'] != v['protocol']:
+                    raise loadbalancer.ProtocolMismatch(
+                        vip_proto=v['protocol'],
+                        pool_proto=pool['protocol'])
             else:
                 pool = None
 
@@ -426,6 +431,11 @@ class LoadBalancerPluginDb(LoadBalancerPluginBase):
                     # check that the pool matches the tenant_id
                     if new_pool['tenant_id'] != vip_db['tenant_id']:
                         raise q_exc.NotAuthorized()
+                    # validate that the pool has same protocol
+                    if new_pool['protocol'] != vip_db['protocol']:
+                        raise loadbalancer.ProtocolMismatch(
+                            vip_proto=vip_db['protocol'],
+                            pool_proto=new_pool['protocol'])
 
                     if vip_db['pool_id']:
                         old_pool = self._get_resource(
index a93a5f8398018ce66633ef20d6d785ca937f22b6..f984441bfdbbde211c327c27ceeb1469a68cbdee 100644 (file)
@@ -57,6 +57,11 @@ class PoolStatsNotFound(qexception.NotFound):
     message = _("Statistics of Pool %(pool_id)s could not be found")
 
 
+class ProtocolMismatch(qexception.BadRequest):
+    message = _("Protocol %(vip_proto)s does not match "
+                "pool protocol %(pool_proto)s")
+
+
 RESOURCE_ATTRIBUTE_MAP = {
     'vips': {
         'id': {'allow_post': False, 'allow_put': False,
index 622212e10773e61cf27b6bc49b24f41ce7cc5e78..5305d05414cbb189ca9180aa4f391eba1c063bfe 100644 (file)
@@ -342,6 +342,19 @@ class TestLoadBalancer(LoadBalancerPluginDbTestCase):
         with testtools.ExpectedException(webob.exc.HTTPClientError):
             self.test_create_vip(session_persistence=sp)
 
+    def test_create_vip_with_protocol_mismatch(self):
+        with self.pool(protocol='TCP') as pool:
+            with testtools.ExpectedException(webob.exc.HTTPClientError):
+                self.test_create_vip(pool=pool, protocol='HTTP')
+
+    def test_update_vip_with_protocol_mismatch(self):
+        with self.pool(protocol='TCP') as pool:
+            with self.vip(protocol='HTTP') as vip:
+                data = {'vip': {'pool_id': pool['pool']['id']}}
+                req = self.new_update_request('vips', data, vip['vip']['id'])
+                res = req.get_response(self.ext_api)
+                self.assertEqual(res.status_int, 400)
+
     def test_reset_session_persistence(self):
         name = 'vip4'
         session_persistence = {'type': "HTTP_COOKIE"}