Raise an exception for identical associations

When a request asks for creating a network association that
already exists, we have to catch the DB exception and raise an
exception that respond an error code 400.
Change-Id: Iafcaa6b016bc5f39f5ee1d64ef7ff627efa74183
Closes-Bug: 1512714
This commit is contained in:
Mathieu Rohon
2015年12月03日 13:19:18 +00:00
parent 2a4ca063d2
commit bd6471dbbc

View File

@@ -18,9 +18,10 @@ from neutron.db import model_base
from neutron.db import models_v2
from neutron.i18n import _LI
from neutron.i18n import _LW
from oslo_db import exception as db_exc
from oslo_log import log
from oslo_utils import uuidutils
import sqlalchemy as sa
@@ -197,13 +198,21 @@ class BGPVPNPluginDb(common_db_mixin.CommonDbMixin):
bgpvpn_id=bgpvpn_id)
def create_net_assoc(self, context, bgpvpn_id, net_assoc):
with context.session.begin(subtransactions=True):
net_assoc_db = BGPVPNNetAssociation(
tenant_id=net_assoc['tenant_id'],
bgpvpn_id=bgpvpn_id,
network_id=net_assoc['network_id'])
context.session.add(net_assoc_db)
return self._make_net_assoc_dict(net_assoc_db)
try:
with context.session.begin(subtransactions=True):
net_assoc_db = BGPVPNNetAssociation(
tenant_id=net_assoc['tenant_id'],
bgpvpn_id=bgpvpn_id,
network_id=net_assoc['network_id'])
context.session.add(net_assoc_db)
return self._make_net_assoc_dict(net_assoc_db)
except db_exc.DBDuplicateEntry:
LOG.warning(_LW("network %(net_id)s is already associated to "
"BGPVPN %(bgpvpn_id)s"),
{'net_id': net_assoc['network_id'],
'bgpvpn_id': bgpvpn_id})
raise bgpvpn_ext.BGPVPNNetAssocAlreadyExists(
bgpvpn_id=bgpvpn_id, net_id=net_assoc['network_id'])
def get_net_assoc(self, context, assoc_id, bgpvpn_id, fields=None):
net_assoc_db = self._get_net_assoc(context, assoc_id, bgpvpn_id)

View File

@@ -62,6 +62,11 @@ class BGPVPNRDNotSupported(n_exc.BadRequest):
"route distinguisher")
class BGPVPNNetAssocAlreadyExists(n_exc.BadRequest):
message = _("network %(net_id)s is already associated to "
"BGPVPN %(bgpvpn_id)s")
def _validate_rt_list(data, valid_values=None):
if not isinstance(data, list):
msg = _("'%s' is not a list") % data

View File

@@ -16,6 +16,8 @@
from neutron import context
from networking_bgpvpn.neutron.db.bgpvpn_db import BGPVPNPluginDb
from networking_bgpvpn.neutron.extensions.bgpvpn \
import BGPVPNNetAssocAlreadyExists
from networking_bgpvpn.neutron.extensions.bgpvpn import BGPVPNNetAssocNotFound
from networking_bgpvpn.neutron.extensions.bgpvpn import BGPVPNNotFound
from networking_bgpvpn.tests.unit.services import test_plugin
@@ -159,6 +161,20 @@ class BgpvpnDBTestCase(test_plugin.BgpvpnTestCaseMixin):
bgpvpn = self.plugin_db.get_bgpvpn(self.ctx, id)
self.assertEqual([], bgpvpn['networks'])
def test_db_associate_twice(self):
with self.network() as net, self.bgpvpn() as bgpvpn:
net_id = net['network']['id']
id = bgpvpn['bgpvpn']['id']
with self.assoc_net(id, net_id=net_id):
self.assoc_net(id,
net_id=net_id,
do_disassociate=False)
self.assertRaises(BGPVPNNetAssocAlreadyExists,
self.plugin_db.create_net_assoc,
self.ctx,
id, {'tenant_id': self._tenant_id,
'network_id': net_id})
def test_db_find_bgpvpn_for_net(self):
with self.network() as net:
net_id = net['network']['id']
Reference in New Issue
openstack/networking-bgpvpn
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.

The note is not visible to the blocked user.