Address problems with foreign keys with subnet and network deletion

This also fixes bug 1020879 and bug 1020847

Change-Id: Ib68f9357ed65f35e56d17577b83fabe8f96388cf
This commit is contained in:
Gary Kotton 2012-07-04 06:54:10 -04:00 committed by Dan Wendlandt
parent b99dde80e2
commit 36e64afd44
4 changed files with 39 additions and 9 deletions

View File

@ -704,9 +704,6 @@ class QuantumDbPluginV2(quantum_plugin_base_v2.QuantumPluginBaseV2):
allocated = allocated_qry.filter_by(subnet_id=id).all()
if allocated:
raise q_exc.SubnetInUse(subnet_id=id)
# Delete IP Allocations on subnet
range_qry = context.session.query(models_v2.IPAllocationRange)
range_qry.filter_by(subnet_id=id).delete()
context.session.delete(subnet)
def get_subnet(self, context, id, fields=None, verbose=None):

View File

@ -44,7 +44,8 @@ class IPAvailabilityRange(model_base.BASEV2):
"""
allocation_pool_id = sa.Column(sa.String(36),
sa.ForeignKey('ipallocationpools.id'),
sa.ForeignKey('ipallocationpools.id',
ondelete="CASCADE"),
nullable=True,
primary_key=True)
first_ip = sa.Column(sa.String(64), nullable=False, primary_key=True)
@ -57,7 +58,8 @@ class IPAvailabilityRange(model_base.BASEV2):
class IPAllocationPool(model_base.BASEV2, HasId):
"""Representation of an allocation pool in a Quantum subnet."""
subnet_id = sa.Column(sa.String(36), sa.ForeignKey('subnets.id'),
subnet_id = sa.Column(sa.String(36), sa.ForeignKey('subnets.id',
ondelete="CASCADE"),
nullable=True)
first_ip = sa.Column(sa.String(64), nullable=False)
last_ip = sa.Column(sa.String(64), nullable=False)
@ -72,12 +74,15 @@ class IPAllocationPool(model_base.BASEV2, HasId):
class IPAllocation(model_base.BASEV2):
"""Internal representation of allocated IP addresses in a Quantum subnet.
"""
port_id = sa.Column(sa.String(36), sa.ForeignKey('ports.id'),
port_id = sa.Column(sa.String(36), sa.ForeignKey('ports.id',
ondelete="CASCADE"),
nullable=False, primary_key=True)
ip_address = sa.Column(sa.String(64), nullable=False, primary_key=True)
subnet_id = sa.Column(sa.String(36), sa.ForeignKey('subnets.id'),
subnet_id = sa.Column(sa.String(36), sa.ForeignKey('subnets.id',
ondelete="CASCADE"),
nullable=False, primary_key=True)
network_id = sa.Column(sa.String(36), sa.ForeignKey("networks.id"),
network_id = sa.Column(sa.String(36), sa.ForeignKey("networks.id",
ondelete="CASCADE"),
nullable=False, primary_key=True)

View File

@ -49,6 +49,6 @@ class LinuxBridgePluginV2(db_base_plugin_v2.QuantumDbPluginV2):
def delete_network(self, context, id):
vlan_binding = cdb.get_vlan_binding(id)
cdb.release_vlanid(vlan_binding[const.VLANID])
cdb.release_vlanid(vlan_binding['vlan_id'])
cdb.remove_vlan_binding(id)
return super(LinuxBridgePluginV2, self).delete_network(context, id)

View File

@ -718,6 +718,34 @@ class TestSubnetsV2(QuantumDbPluginV2TestCase):
self._test_create_subnet(gateway_ip=gateway_ip,
cidr=cidr)
def test_delete_subnet(self):
gateway_ip = '10.0.0.1'
cidr = '10.0.0.0/24'
fmt = 'json'
# Create new network
res = self._create_network(fmt=fmt, name='net',
admin_status_up=True)
network = self.deserialize(fmt, res)
subnet = self._make_subnet(fmt, network, gateway_ip,
cidr, ip_version=4)
req = self.new_delete_request('subnets', subnet['subnet']['id'])
res = req.get_response(self.api)
self.assertEquals(res.status_int, 204)
def test_delete_network(self):
gateway_ip = '10.0.0.1'
cidr = '10.0.0.0/24'
fmt = 'json'
# Create new network
res = self._create_network(fmt=fmt, name='net',
admin_status_up=True)
network = self.deserialize(fmt, res)
subnet = self._make_subnet(fmt, network, gateway_ip,
cidr, ip_version=4)
req = self.new_delete_request('networks', network['network']['id'])
res = req.get_response(self.api)
self.assertEquals(res.status_int, 204)
def test_create_subnet_defaults(self):
gateway = '10.0.0.1'
cidr = '10.0.0.0/24'