Rajaram/Vinkesh | AllocatedIps index now does not show deallocated IPs
This commit is contained in:
parent
6fb912a4f6
commit
80bd741881
@ -28,13 +28,17 @@ from melange.db.sqlalchemy import mappers
|
||||
from melange.db.sqlalchemy import session
|
||||
|
||||
|
||||
def list(query):
|
||||
return query.all()
|
||||
|
||||
|
||||
def find_all_by(model, **conditions):
|
||||
return _query_by(model, **conditions).all()
|
||||
return _query_by(model, **conditions)
|
||||
|
||||
|
||||
def find_all_by_limit(model, conditions, limit, marker=None,
|
||||
def find_all_by_limit(query_func, model, conditions, limit, marker=None,
|
||||
marker_column=None):
|
||||
return _limits(model, conditions, limit, marker, marker_column).all()
|
||||
return _limits(query_func, model, conditions, limit, marker, marker_column)
|
||||
|
||||
|
||||
def find_by(model, **kwargs):
|
||||
@ -59,8 +63,8 @@ def delete(model):
|
||||
db_session.flush()
|
||||
|
||||
|
||||
def delete_all(model, **conditions):
|
||||
_query_by(model, **conditions).delete()
|
||||
def delete_all(query_func, model, **conditions):
|
||||
query_func(model, **conditions).delete()
|
||||
|
||||
|
||||
def update(model, values):
|
||||
@ -68,8 +72,8 @@ def update(model, values):
|
||||
model[k] = v
|
||||
|
||||
|
||||
def update_all(model, conditions, values):
|
||||
_query_by(model, **conditions).update(values)
|
||||
def update_all(query_func, model, conditions, values):
|
||||
query_func(model, **conditions).update(values)
|
||||
|
||||
|
||||
def find_inside_globals_for(local_address_id, **kwargs):
|
||||
@ -78,7 +82,8 @@ def find_inside_globals_for(local_address_id, **kwargs):
|
||||
marker = kwargs.pop('marker', None)
|
||||
|
||||
kwargs["inside_local_address_id"] = local_address_id
|
||||
query = _limits(mappers.IpNat, kwargs, limit, marker, marker_column)
|
||||
query = _limits(find_all_by, mappers.IpNat, kwargs, limit, marker,
|
||||
marker_column)
|
||||
return [nat.inside_global_address for nat in query]
|
||||
|
||||
|
||||
@ -88,7 +93,8 @@ def find_inside_locals_for(global_address_id, **kwargs):
|
||||
marker = kwargs.pop('marker', None)
|
||||
|
||||
kwargs["inside_global_address_id"] = global_address_id
|
||||
query = _limits(mappers.IpNat, kwargs, limit, marker, marker_column)
|
||||
query = _limits(find_all_by, mappers.IpNat, kwargs, limit, marker,
|
||||
marker_column)
|
||||
return [nat.inside_local_address for nat in query]
|
||||
|
||||
|
||||
@ -167,6 +173,12 @@ def find_all_ips_in_network(network_id, **conditions):
|
||||
filter(ipam.models.IpBlock.network_id == network_id)
|
||||
|
||||
|
||||
def find_all_allocated_ips(model, **conditions):
|
||||
return _query_by(ipam.models.IpAddress, **conditions).\
|
||||
filter(or_(ipam.models.IpAddress.marked_for_deallocation == None,
|
||||
ipam.models.IpAddress.marked_for_deallocation == False))
|
||||
|
||||
|
||||
def configure_db(options):
|
||||
session.configure_db(options)
|
||||
|
||||
@ -202,9 +214,9 @@ def _query_by(cls, **conditions):
|
||||
return query
|
||||
|
||||
|
||||
def _limits(cls, conditions, limit, marker, marker_column=None):
|
||||
query = _query_by(cls, **conditions)
|
||||
marker_column = marker_column or cls.id
|
||||
def _limits(query_func, model, conditions, limit, marker, marker_column=None):
|
||||
query = query_func(model, **conditions)
|
||||
marker_column = marker_column or model.id
|
||||
if marker is not None:
|
||||
query = query.filter(marker_column > marker)
|
||||
return query.order_by(marker_column).limit(limit)
|
||||
|
@ -20,7 +20,6 @@
|
||||
import datetime
|
||||
import logging
|
||||
import netaddr
|
||||
import sys
|
||||
|
||||
from melange import ipv6
|
||||
from melange.common import config
|
||||
@ -39,28 +38,31 @@ class Query(object):
|
||||
Using this class makes the models independent of sqlalchemy
|
||||
|
||||
"""
|
||||
def __init__(self, model, **conditions):
|
||||
def __init__(self, model, query_func=None, **conditions):
|
||||
self._query_func = query_func or db_api.find_all_by
|
||||
self._model = model
|
||||
self._conditions = conditions
|
||||
|
||||
def all(self):
|
||||
return db_api.find_all_by(self._model, **self._conditions)
|
||||
return db_api.list(self._query_func(self._model, **self._conditions))
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.all())
|
||||
|
||||
def update(self, **values):
|
||||
db_api.update_all(self._model, self._conditions, values)
|
||||
db_api.update_all(self._query_func, self._model, self._conditions,
|
||||
values)
|
||||
|
||||
def delete(self):
|
||||
db_api.delete_all(self._model, **self._conditions)
|
||||
db_api.delete_all(self._query_func, self._model, **self._conditions)
|
||||
|
||||
def limit(self, limit=200, marker=None, marker_column=None):
|
||||
return db_api.find_all_by_limit(self._model,
|
||||
self._conditions,
|
||||
limit=limit,
|
||||
marker=marker,
|
||||
marker_column=marker_column)
|
||||
return db_api.list(db_api.find_all_by_limit(self._query_func,
|
||||
self._model,
|
||||
self._conditions,
|
||||
limit=limit,
|
||||
marker=marker,
|
||||
marker_column=marker_column))
|
||||
|
||||
def paginated_collection(self, limit=200, marker=None, marker_column=None):
|
||||
collection = self.limit(int(limit) + 1, marker, marker_column)
|
||||
@ -558,6 +560,11 @@ class IpAddress(ModelBase):
|
||||
def find_all_by_network(cls, network_id, **conditions):
|
||||
return db_api.find_all_ips_in_network(network_id, **conditions)
|
||||
|
||||
@classmethod
|
||||
def find_all_allocated_ips(cls, **conditions):
|
||||
return Query(cls, query_func=db_api.find_all_allocated_ips,
|
||||
**conditions)
|
||||
|
||||
def _before_save(self):
|
||||
self.address = self._formatted(self.address)
|
||||
|
||||
|
@ -156,7 +156,7 @@ class AllocatedIpAddressesController(BaseController):
|
||||
filter_conditions = utils.filter_dict(request.params, 'used_by_device')
|
||||
if tenant_id is not None:
|
||||
filter_conditions['used_by_tenant'] = tenant_id
|
||||
ips = models.IpAddress.find_all(**filter_conditions)
|
||||
ips = models.IpAddress.find_all_allocated_ips(**filter_conditions)
|
||||
return self._paginated_response('ip_addresses', ips, request)
|
||||
|
||||
|
||||
|
@ -949,6 +949,20 @@ class TestIpAddress(tests.BaseTest):
|
||||
models.IpAddress.find,
|
||||
123)
|
||||
|
||||
def test_find_all_allocated_ips(self):
|
||||
block = factory_models.IpBlockFactory(tenant_id="1")
|
||||
|
||||
ip1 = block.allocate_ip()
|
||||
ip2 = block.allocate_ip()
|
||||
ip3 = block.allocate_ip()
|
||||
ip4 = block.allocate_ip(used_by_tenant="2")
|
||||
|
||||
ip2.deallocate()
|
||||
|
||||
allocated_ips = models.IpAddress.find_all_allocated_ips(
|
||||
used_by_tenant="1")
|
||||
self.assertModelsEqual(allocated_ips, [ip1, ip3])
|
||||
|
||||
def test_delete_ip_address(self):
|
||||
block = factory_models.PrivateIpBlockFactory(cidr="10.0.0.1/8")
|
||||
ip = factory_models.IpAddressFactory(ip_block_id=block.id,
|
||||
|
@ -665,6 +665,18 @@ class TestAllocatedIpAddressController(BaseTestController):
|
||||
self.assertItemsEqual(response.json['ip_addresses'],
|
||||
_data([tnt1_device1_ip1, tnt1_device1_ip2]))
|
||||
|
||||
def test_index_doesnt_return_soft_deallocated_ips(self):
|
||||
block = factory_models.IpBlockFactory(tenant_id="1")
|
||||
|
||||
ip1 = block.allocate_ip()
|
||||
ip2 = block.allocate_ip()
|
||||
ip3 = block.allocate_ip()
|
||||
|
||||
ip2.deallocate()
|
||||
response = self.app.get("/ipam/tenants/1/allocated_ip_addresses")
|
||||
|
||||
self.assertItemsEqual(response.json['ip_addresses'], _data([ip1, ip3]))
|
||||
|
||||
|
||||
class TestInsideGlobalsController(BaseTestController):
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user