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. Conflicts: neutron/db/db_base_plugin_common.py Closes-Bug: #1831811 Change-Id: I8ae478a03ceedc6c3b1ae1d40081b5e5158813e6 (cherry picked from commit af773557323580c7b07bd41dd1f524e81530c939)
This commit is contained in:
parent
a725ef6dbe
commit
4ecfdb6fd0
@ -15,6 +15,8 @@
|
||||
|
||||
import functools
|
||||
|
||||
import netaddr
|
||||
|
||||
from neutron_lib.api.definitions import network as net_def
|
||||
from neutron_lib.api.definitions import port as port_def
|
||||
from neutron_lib.api.definitions import subnet as subnet_def
|
||||
@ -277,6 +279,10 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
|
||||
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']]})
|
||||
return subnet_obj.Subnet.get_objects(context, _pager=pager,
|
||||
validate_filters=False,
|
||||
**filters)
|
||||
|
@ -5127,6 +5127,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):
|
||||
with self.network() as network:
|
||||
with self.subnet(network=network,
|
||||
|
Loading…
x
Reference in New Issue
Block a user