Updated Subnet.allocated_ips to include only IPAddress with _deallocated != 1.
This commit is contained in:
parent
5b245b2c58
commit
0ad7b8b0f3
@ -191,7 +191,11 @@ class Subnet(BASEV2, CreatedAt, HasId, HasTenant, IsHazTags):
|
||||
last_ip = sa.Column(custom_types.INET())
|
||||
ip_version = sa.Column(sa.Integer())
|
||||
|
||||
allocated_ips = orm.relationship(IPAddress, backref="subnet")
|
||||
allocated_ips = orm.relationship(IPAddress,
|
||||
primaryjoin=
|
||||
'and_(Subnet.id==IPAddress.subnet_id, '
|
||||
'IPAddress._deallocated!=1)',
|
||||
backref="subnet")
|
||||
routes = orm.relationship(Route, backref='subnet', cascade='delete')
|
||||
|
||||
|
||||
|
@ -65,7 +65,7 @@ class QuarkIpam(object):
|
||||
return deallocated_mac
|
||||
|
||||
ranges = session.query(models.MacAddressRange,
|
||||
sql_func.count(models.MacAddress).
|
||||
sql_func.count(models.MacAddress.address).
|
||||
label("count")).\
|
||||
outerjoin(models.MacAddress).\
|
||||
group_by(models.MacAddressRange).\
|
||||
@ -128,6 +128,7 @@ class QuarkIpam(object):
|
||||
address["version"] = subnet["ip_version"]
|
||||
address["network_id"] = net_id
|
||||
address["tenant_id"] = subnet["tenant_id"]
|
||||
address["_deallocated"] = 0
|
||||
|
||||
if address:
|
||||
address["port_id"] = port_id
|
||||
|
@ -50,7 +50,8 @@ quark_opts = [
|
||||
]
|
||||
|
||||
CONF.register_opts(quark_opts, "QUARK")
|
||||
CONF.set_override('api_extensions_path', ":".join(extensions.__path__))
|
||||
if 'api_extensions_path' in CONF:
|
||||
CONF.set_override('api_extensions_path', ":".join(extensions.__path__))
|
||||
|
||||
#NOTE(mdietz): hacking this for now because disallowing subnets on a network
|
||||
# create is absurd. Might be useful to implement the ability
|
||||
@ -327,7 +328,7 @@ class Plugin(quantum_plugin_base_v2.QuantumPluginBaseV2):
|
||||
subnets = []
|
||||
if network["network"].get("subnets"):
|
||||
subnets = network["network"].pop("subnets")
|
||||
new_net = models.Network(id=net_uuid)
|
||||
new_net = models.Network(id=net_uuid, tenant_id=context.tenant_id)
|
||||
new_net.update(network["network"])
|
||||
|
||||
for sub in subnets:
|
||||
@ -501,6 +502,7 @@ class Plugin(quantum_plugin_base_v2.QuantumPluginBaseV2):
|
||||
new_port["backend_key"] = backend_port["uuid"]
|
||||
new_port["addresses"] = [addresses]
|
||||
new_port["mac_address"] = mac["address"]
|
||||
new_port["tenant_id"] = context.tenant_id
|
||||
|
||||
session.add(new_port)
|
||||
new_port["mac_address"] = str(netaddr.EUI(new_port["mac_address"],
|
||||
@ -641,8 +643,9 @@ class Plugin(quantum_plugin_base_v2.QuantumPluginBaseV2):
|
||||
raise exceptions.NetworkNotFound(net_id=id)
|
||||
|
||||
backend_key = port["backend_key"]
|
||||
mac_address = netaddr.EUI(port["mac_address"]).value
|
||||
self.ipam_driver.deallocate_mac_address(session,
|
||||
port["mac_address"],)
|
||||
mac_address,)
|
||||
self.ipam_driver.deallocate_ip_address(session, id,
|
||||
ipam_reuse_after=self.ipam_reuse_after)
|
||||
session.delete(port)
|
||||
|
6
quark/tests/test_base.py
Normal file
6
quark/tests/test_base.py
Normal file
@ -0,0 +1,6 @@
|
||||
import unittest2
|
||||
|
||||
|
||||
class TestBase(unittest2.TestCase):
|
||||
'''Class to decide which unit test class to inherit from uniformly.'''
|
||||
pass
|
51
quark/tests/test_db_models.py
Normal file
51
quark/tests/test_db_models.py
Normal file
@ -0,0 +1,51 @@
|
||||
from collections import namedtuple
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from quantum.db import api as db_api
|
||||
|
||||
from quark.db import models
|
||||
import quark.plugin
|
||||
|
||||
import test_base
|
||||
|
||||
|
||||
class TestSubnets(test_base.TestBase):
|
||||
def setUp(self):
|
||||
db_api._ENGINE = create_engine('sqlite://')
|
||||
db_api.register_models()
|
||||
self.session = db_api.get_session()
|
||||
self.plugin = quark.plugin.Plugin()
|
||||
|
||||
def test_allocated_ips_only(self):
|
||||
MockContext = namedtuple('MockContext', ['tenant_id', 'session'])
|
||||
context = MockContext('0', self.session)
|
||||
|
||||
# 1. Create network
|
||||
network = {'network': {'name': 'test'}}
|
||||
response = self.plugin.create_network(context, network)
|
||||
network_id = response['id']
|
||||
|
||||
# 2. Create subnet
|
||||
subnet = {'subnet': {'cidr': '192.168.10.1/24',
|
||||
'network_id': network_id}}
|
||||
self.plugin.create_subnet(context, subnet)
|
||||
|
||||
# 3. Create M.A.R.
|
||||
mac_range = {'mac_address_range': {'cidr': '01:23:45/24'}}
|
||||
self.plugin.create_mac_address_range(context, mac_range)
|
||||
|
||||
# 4. Create port
|
||||
port = {'port': {'network_id': network_id,
|
||||
'device_id': ''}}
|
||||
response = self.plugin.create_port(context, port)
|
||||
|
||||
q = self.session.query(models.Subnet).outerjoin(models.IPAddress)
|
||||
self.assertEqual(len(q.first().allocated_ips),
|
||||
1)
|
||||
|
||||
# 5. Delete port.
|
||||
self.plugin.delete_port(context, response['id'])
|
||||
|
||||
q = self.session.query(models.Subnet).outerjoin(models.IPAddress)
|
||||
self.assertEqual(len(q.first().allocated_ips),
|
||||
0)
|
Loading…
x
Reference in New Issue
Block a user