Turn CIDR in query filter into proper subnet

Use netaddr.IPNetwork to convert CIDRs in a query
into proper subnets.

When creating subnets the CIDR is converted into
a proper subnet. i.e a subnet can be created with
cidr=10.0.0.11/24 in the create request. On create
the cidr is turned into a proper subnet resulting
in a subnet with cidr=10.0.0.0/24.

This change does the same to CIDRs when used as
query filters during a list subnet request, so
that the same value that was used to create the
subnet can be used to find the subnet.

Closes-Bug: #1831811
Change-Id: I8ae478a03ceedc6c3b1ae1d40081b5e5158813e6
This commit is contained in:
Harald Jensås 2019-06-06 03:06:17 +02:00
parent fc5235c49c
commit af77355732
2 changed files with 17 additions and 0 deletions

View File

@ -285,6 +285,10 @@ class DbBasePluginCommon(object):
page_reverse=False):
pager = base_obj.Pager(sorts, limit, page_reverse, marker)
filters = filters or {}
# turn the CIDRs into a proper subnets
if filters.get('cidr'):
filters.update(
{'cidr': [netaddr.IPNetwork(x).cidr for x in filters['cidr']]})
# TODO(ihrachys) remove explicit reader usage when subnet OVO switches
# to engine facade by default
with db_api.CONTEXT_READER.using(context):

View File

@ -5241,6 +5241,19 @@ class TestSubnetsV2(NeutronDbPluginV2TestCase):
self._test_list_resources('subnet', [],
query_params=query_params)
def test_list_subnets_filtering_by_cidr_used_on_create(self):
with self.network() as network:
with self.subnet(network=network,
gateway_ip='10.0.0.1',
cidr='10.0.0.11/24') as v1,\
self.subnet(network=network,
gateway_ip='10.0.1.1',
cidr='10.0.1.11/24') as v2:
subnets = (v1, v2)
query_params = ('cidr=10.0.0.11/24&cidr=10.0.1.11/24')
self._test_list_resources('subnet', subnets,
query_params=query_params)
def test_list_subnets_filtering_by_unknown_filter(self):
if self._skip_filter_validation:
self.skipTest("Plugin does not support filter validation")