]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
fix incorrect handling of duplicate network name, add exception for duplicate network...
authorDan Wendlandt <dan@nicira.com>
Mon, 1 Aug 2011 17:11:16 +0000 (10:11 -0700)
committerDan Wendlandt <dan@nicira.com>
Mon, 1 Aug 2011 17:11:16 +0000 (10:11 -0700)
quantum/api/faults.py
quantum/api/networks.py
quantum/common/exceptions.py
quantum/db/api.py
quantum/plugins/SamplePlugin.py
tests/unit/test_api.py

index ef4d50bc6b27c59279257afb22927ebdb64bbe17..52b36109d7d2dcc3f99a8e8f5616ada74f9a339e 100644 (file)
@@ -31,6 +31,7 @@ class Fault(webob.exc.HTTPException):
             401: "unauthorized",
             420: "networkNotFound",
             421: "networkInUse",
+            422: "networkNameExists",
             430: "portNotFound",
             431: "requestedStateInvalid",
             432: "portInUse",
@@ -90,6 +91,21 @@ class NetworkInUse(webob.exc.HTTPClientError):
     title = 'Network in Use'
     explanation = ('Unable to remove the network: attachments still plugged.')
 
+class NetworkNameExists(webob.exc.HTTPClientError):
+    """
+    subclass of :class:`~HTTPClientError`
+
+    This indicates that the server could not set the network name to the
+    specified value because another network for the same tenant already has
+    that name.
+
+    code: 422, title: Network Name Exists
+    """
+    code = 422
+    title = 'Network Name Exists'
+    explanation = ('Unable to set network name: tenant already has network' \
+                        ' with same name.')
+
 
 class PortNotFound(webob.exc.HTTPClientError):
     """
index 9afc09c24e32fb555a0c30a4c887088f101187b6..27709eb5941f641ce9d2923681628e9d55e15b81 100644 (file)
@@ -107,12 +107,15 @@ class Controller(common.QuantumController):
                                            self._network_ops_param_list)
         except exc.HTTPError as e:
             return faults.Fault(e)
-        network = self._plugin.\
+        try:
+            network = self._plugin.\
                        create_network(tenant_id,
                                       request_params['net-name'])
-        builder = networks_view.get_view_builder(request)
-        result = builder.build(network)
-        return dict(networks=result)
+            builder = networks_view.get_view_builder(request)
+            result = builder.build(network)
+            return dict(networks=result)
+        except exception.NetworkNameExists as e:
+            return faults.Fault(faults.NetworkNameExists(e))
 
     def update(self, request, tenant_id, id):
         """ Updates the name for the network with the given id """
@@ -128,6 +131,8 @@ class Controller(common.QuantumController):
             return exc.HTTPAccepted()
         except exception.NetworkNotFound as e:
             return faults.Fault(faults.NetworkNotFound(e))
+        except exception.NetworkNameExists as e:
+            return faults.Fault(faults.NetworkNameExists(e))
 
     def delete(self, request, tenant_id, id):
         """ Destroys the network with the given id """
index 4daf31762b9138d697dc02086e58422f9c1db8f6..b9705fb75ac91468cad6d50e0968eb26728920bf 100644 (file)
@@ -107,6 +107,11 @@ class AlreadyAttached(QuantumException):
                 "%(port_id)s for network %(net_id)s. The attachment is " \
                 "already plugged into port %(att_port_id)s")
 
+class NetworkNameExists(QuantumException):
+    message = _("Unable to set network name to %(net_name). " \
+                "Network with id %(net_id) already has this name for " \
+                "tenant %(tenant_id)")
+
 
 class Duplicate(Error):
     pass
index 0c42bda3df9bd0f76950fa08b9411359790e2b91..5711a47ddeb47b61ea13bbfdcb2e48dc19934fad 100644 (file)
@@ -76,21 +76,28 @@ def unregister_models():
     BASE.metadata.drop_all(_ENGINE)
 
 
-def network_create(tenant_id, name):
+def _check_duplicate_net_name(tenant_id, net_name):
     session = get_session()
-    net = None
     try:
         net = session.query(models.Network).\
-          filter_by(tenant_id=tenant_id, name=name).\
+          filter_by(tenant_id=tenant_id, name=net_name).\
           one()
-        raise Exception("Network with name %(name)s already " \
-                        "exists for tenant %(tenant_id)s" % locals())
+        raise q_exc.NetworkNameExists(tenant_id=tenant_id,
+                        net_name=net_name, net_id=net.uuid)
     except exc.NoResultFound:
-        with session.begin():
-            net = models.Network(tenant_id, name)
-            session.add(net)
-            session.flush()
-    return net
+        # this is the "normal" path, as API spec specifies
+        # that net-names are unique within a tenant
+        pass
+
+def network_create(tenant_id, name):
+    session = get_session()
+
+    _check_duplicate_net_name(tenant_id, name)
+    with session.begin():
+        net = models.Network(tenant_id, name)
+        session.add(net)
+        session.flush()
+        return net
 
 
 def network_list(tenant_id):
@@ -112,20 +119,12 @@ def network_get(net_id):
 
 def network_rename(net_id, tenant_id, new_name):
     session = get_session()
-    try:
-        res = session.query(models.Network).\
-          filter_by(tenant_id=tenant_id, name=new_name).\
-          one()
-        if not res:
-            raise exc.NetworkNotFound(net_id=net_id)
-    except exc.NoResultFound:
-        net = network_get(net_id)
-        net.name = new_name
-        session.merge(net)
-        session.flush()
-        return net
-    raise Exception("A network with name \"%s\" already exists" % new_name)
-
+    net = network_get(net_id)
+    _check_duplicate_net_name(tenant_id, new_name)
+    net.name = new_name
+    session.merge(net)
+    session.flush()
+    return net
 
 def network_destroy(net_id):
     session = get_session()
index f4aefac0a084d223ddef3e9320231363087fed41..9b155a2c14894ef749cd65db4272f4c708ab198e 100644 (file)
@@ -322,10 +322,7 @@ class FakePlugin(object):
         Virtual Network.
         """
         LOG.debug("FakePlugin.rename_network() called")
-        try:
-            db.network_rename(net_id, tenant_id, new_name)
-        except:
-            raise exc.NetworkNotFound(net_id=net_id)
+        db.network_rename(net_id, tenant_id, new_name)
         net = self._get_network(tenant_id, net_id)
         return net
 
index d17e6ed87fad0c4add107a6b1c6adc959644e2a7..c52f6cefa75e165a48fba145b542fa697d65ce6a 100644 (file)
@@ -152,6 +152,19 @@ class APITest(unittest.TestCase):
                          network_data['network'])
         LOG.debug("_test_rename_network - format:%s - END", format)
 
+    def _test_rename_network_duplicate(self, format):
+        LOG.debug("_test_rename_network_duplicate - format:%s - START", format)
+        content_type = "application/%s" % format
+        network_id1 = self._create_network(format, name="net1")
+        network_id2 = self._create_network(format, name="net2")
+        update_network_req = testlib.update_network_request(self.tenant_id,
+                                                            network_id2,
+                                                            "net1",
+                                                            format)
+        update_network_res = update_network_req.get_response(self.api)
+        self.assertEqual(update_network_res.status_int, 422)
+        LOG.debug("_test_rename_network_duplicate - format:%s - END", format)
+
     def _test_rename_network_badrequest(self, format):
         LOG.debug("_test_rename_network_badrequest - format:%s - START",
                   format)
@@ -693,6 +706,12 @@ class APITest(unittest.TestCase):
     def test_rename_network_xml(self):
         self._test_rename_network('xml')
 
+    def test_rename_network_duplicate_json(self):
+        self._test_rename_network_duplicate('json')
+
+    def test_rename_network_duplicate_xml(self):
+        self._test_rename_network_duplicate('xml')
+
     def test_rename_network_badrequest_json(self):
         self._test_rename_network_badrequest('json')