Adds prefixlen to the request body when creating subnets

The python-openstack client uses the OpenStack SDK to interface with
Neutron, which strips the prefix length from the client request when
creating a subnet from a subnet pool. This patch adds support for
prefixlen when creating a subnet.

Partial-Bug: 1754062

Change-Id: I87762c55ea7b24eefbd91fc159cf57a5566ce5f4
This commit is contained in:
James Denton 2018-03-07 13:25:31 -05:00 committed by James Denton
parent 92889d9f24
commit b668add5a9
4 changed files with 139 additions and 1 deletions

View File

@ -8185,7 +8185,7 @@ class _OpenStackCloudMixin(_normalize.Normalizer):
gateway_ip=None, disable_gateway_ip=False,
dns_nameservers=None, host_routes=None,
ipv6_ra_mode=None, ipv6_address_mode=None,
use_default_subnetpool=False, **kwargs):
prefixlen=None, use_default_subnetpool=False, **kwargs):
"""Create a subnet on a specified network.
:param string network_name_or_id:
@ -8247,6 +8247,8 @@ class _OpenStackCloudMixin(_normalize.Normalizer):
:param string ipv6_address_mode:
IPv6 address mode. Valid values are: 'dhcpv6-stateful',
'dhcpv6-stateless', or 'slaac'.
:param string prefixlen:
The prefix length to use for subnet allocation from a subnet pool.
:param bool use_default_subnetpool:
Use the default subnetpool for ``ip_version`` to obtain a CIDR. It
is required to pass ``None`` to the ``cidr`` argument when enabling
@ -8317,6 +8319,8 @@ class _OpenStackCloudMixin(_normalize.Normalizer):
subnet['ipv6_ra_mode'] = ipv6_ra_mode
if ipv6_address_mode:
subnet['ipv6_address_mode'] = ipv6_address_mode
if prefixlen:
subnet['prefixlen'] = prefixlen
if use_default_subnetpool:
subnet['use_default_subnetpool'] = True

View File

@ -69,6 +69,8 @@ class Subnet(resource.Resource, resource.TagMixin):
name = resource.Body('name')
#: The ID of the attached network.
network_id = resource.Body('network_id')
#: The prefix length to use for subnet allocation from a subnet pool
prefix_length = resource.Body('prefixlen')
#: The ID of the project this subnet is associated with.
project_id = resource.Body('tenant_id')
#: Revision number of the subnet. *Type: int*

View File

@ -0,0 +1,80 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from openstack.network.v2 import network
from openstack.network.v2 import subnet
from openstack.network.v2 import subnet_pool
from openstack.tests.functional import base
class TestSubnetFromSubnetPool(base.BaseFunctionalTest):
IPV4 = 4
CIDR = "10.100.0.0/28"
MINIMUM_PREFIX_LENGTH = 8
DEFAULT_PREFIX_LENGTH = 24
MAXIMUM_PREFIX_LENGTH = 32
SUBNET_PREFIX_LENGTH = 28
IP_VERSION = 4
PREFIXES = ['10.100.0.0/24']
NET_ID = None
SUB_ID = None
SUB_POOL_ID = None
def setUp(self):
super(TestSubnetFromSubnetPool, self).setUp()
self.NET_NAME = self.getUniqueString()
self.SUB_NAME = self.getUniqueString()
self.SUB_POOL_NAME = self.getUniqueString()
sub_pool = self.conn.network.create_subnet_pool(
name=self.SUB_POOL_NAME,
min_prefixlen=self.MINIMUM_PREFIX_LENGTH,
default_prefixlen=self.DEFAULT_PREFIX_LENGTH,
max_prefixlen=self.MAXIMUM_PREFIX_LENGTH,
prefixes=self.PREFIXES)
self.assertIsInstance(sub_pool, subnet_pool.SubnetPool)
self.assertEqual(self.SUB_POOL_NAME, sub_pool.name)
self.SUB_POOL_ID = sub_pool.id
net = self.conn.network.create_network(name=self.NET_NAME)
self.assertIsInstance(net, network.Network)
self.assertEqual(self.NET_NAME, net.name)
self.NET_ID = net.id
sub = self.conn.network.create_subnet(
name=self.SUB_NAME,
ip_version=self.IPV4,
network_id=self.NET_ID,
prefixlen=self.SUBNET_PREFIX_LENGTH,
subnetpool_id=self.SUB_POOL_ID)
self.assertIsInstance(sub, subnet.Subnet)
self.assertEqual(self.SUB_NAME, sub.name)
self.SUB_ID = sub.id
def tearDown(self):
sot = self.conn.network.delete_subnet(self.SUB_ID)
self.assertIsNone(sot)
sot = self.conn.network.delete_network(
self.NET_ID, ignore_missing=False)
self.assertIsNone(sot)
sot = self.conn.network.delete_subnet_pool(self.SUB_POOL_ID)
self.assertIsNone(sot)
super(TestSubnetFromSubnetPool, self).tearDown()
def test_get(self):
sot = self.conn.network.get_subnet(self.SUB_ID)
self.assertEqual(self.SUB_NAME, sot.name)
self.assertEqual(self.SUB_ID, sot.id)
self.assertEqual(self.CIDR, sot.cidr)
self.assertEqual(self.IPV4, sot.ip_version)
self.assertEqual("10.100.0.1", sot.gateway_ip)
self.assertTrue(sot.is_dhcp_enabled)

View File

@ -26,6 +26,8 @@ class TestSubnet(base.TestCase):
subnet_name = 'subnet_name'
subnet_id = '1f1696eb-7f47-47f6-835c-4889bff88604'
subnet_cidr = '192.168.199.0/24'
subnetpool_cidr = '172.16.0.0/28'
prefix_length = 28
mock_network_rep = {
'id': '881d1bb7-a663-44c0-8f9f-ee2765b74486',
@ -57,6 +59,13 @@ class TestSubnet(base.TestCase):
'tags': []
}
mock_subnetpool_rep = {
'id': 'f49a1319-423a-4ee6-ba54-1d95a4f6cc68',
'prefixes': [
'172.16.0.0/16'
]
}
def test_get_subnet(self):
self.register_uris([
dict(method='GET',
@ -263,6 +272,49 @@ class TestSubnet(base.TestCase):
self.network_name, self.subnet_cidr)
self.assert_calls()
def test_create_subnet_from_subnetpool_with_prefixlen(self):
pool = [{'start': '172.16.0.2', 'end': '172.16.0.15'}]
id = '143296eb-7f47-4755-835c-488123475604'
gateway = '172.16.0.1'
dns = ['8.8.8.8']
routes = [{"destination": "0.0.0.0/0", "nexthop": "123.456.78.9"}]
mock_subnet_rep = copy.copy(self.mock_subnet_rep)
mock_subnet_rep['allocation_pools'] = pool
mock_subnet_rep['dns_nameservers'] = dns
mock_subnet_rep['host_routes'] = routes
mock_subnet_rep['gateway_ip'] = gateway
mock_subnet_rep['subnetpool_id'] = self.mock_subnetpool_rep['id']
mock_subnet_rep['cidr'] = self.subnetpool_cidr
mock_subnet_rep['id'] = id
self.register_uris([
dict(method='GET',
uri=self.get_mock_url(
'network', 'public', append=['v2.0', 'networks.json']),
json={'networks': [self.mock_network_rep]}),
dict(method='POST',
uri=self.get_mock_url(
'network', 'public', append=['v2.0', 'subnets.json']),
json={'subnet': mock_subnet_rep},
validate=dict(
json={'subnet': {
'enable_dhcp': False,
'ip_version': 4,
'network_id': self.mock_network_rep['id'],
'allocation_pools': pool,
'dns_nameservers': dns,
'use_default_subnetpool': True,
'prefixlen': self.prefix_length,
'host_routes': routes}}))
])
subnet = self.cloud.create_subnet(self.network_name,
allocation_pools=pool,
dns_nameservers=dns,
use_default_subnetpool=True,
prefixlen=self.prefix_length,
host_routes=routes)
self.assertDictEqual(mock_subnet_rep, subnet)
self.assert_calls()
def test_delete_subnet(self):
self.register_uris([
dict(method='GET',