Merge "Fix Python client library for Neutron"
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
import netaddr
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from oslo.utils import encodeutils
|
from oslo.utils import encodeutils
|
||||||
@@ -171,3 +172,11 @@ def add_boolean_argument(parser, name, **kwargs):
|
|||||||
choices=['True', 'true', 'False', 'false'],
|
choices=['True', 'true', 'False', 'false'],
|
||||||
default=default,
|
default=default,
|
||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid_cidr(cidr):
|
||||||
|
try:
|
||||||
|
netaddr.IPNetwork(cidr)
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|||||||
@@ -97,10 +97,25 @@ def run_command(cmd, cmd_parser, sub_argv):
|
|||||||
_argv = sub_argv[:index]
|
_argv = sub_argv[:index]
|
||||||
values_specs = sub_argv[index:]
|
values_specs = sub_argv[index:]
|
||||||
known_args, _values_specs = cmd_parser.parse_known_args(_argv)
|
known_args, _values_specs = cmd_parser.parse_known_args(_argv)
|
||||||
|
if(isinstance(cmd, subnet.CreateSubnet) and not known_args.cidr):
|
||||||
|
cidr = get_first_valid_cidr(_values_specs)
|
||||||
|
if cidr:
|
||||||
|
known_args.cidr = cidr
|
||||||
|
_values_specs.remove(cidr)
|
||||||
cmd.values_specs = (index == -1 and _values_specs or values_specs)
|
cmd.values_specs = (index == -1 and _values_specs or values_specs)
|
||||||
return cmd.run(known_args)
|
return cmd.run(known_args)
|
||||||
|
|
||||||
|
|
||||||
|
def get_first_valid_cidr(value_specs):
|
||||||
|
# Bug 1442771, argparse does not allow optional positional parameter
|
||||||
|
# to be separated from previous positional parameter.
|
||||||
|
# When cidr was separated from network, the value will not be able
|
||||||
|
# to be parsed into known_args, but saved to _values_specs instead.
|
||||||
|
for value in value_specs:
|
||||||
|
if utils.is_valid_cidr(value):
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
def env(*_vars, **kwargs):
|
def env(*_vars, **kwargs):
|
||||||
"""Search for the first defined of possibly many env vars.
|
"""Search for the first defined of possibly many env vars.
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class CLITestV20SubnetJSON(test_cli20.CLITestV20Base):
|
|||||||
name = 'myname'
|
name = 'myname'
|
||||||
myid = 'myid'
|
myid = 'myid'
|
||||||
netid = 'netid'
|
netid = 'netid'
|
||||||
cidr = 'cidrvalue'
|
cidr = '10.10.10.0/24'
|
||||||
gateway = 'gatewayvalue'
|
gateway = 'gatewayvalue'
|
||||||
args = ['--gateway', gateway, netid, cidr]
|
args = ['--gateway', gateway, netid, cidr]
|
||||||
position_names = ['ip_version', 'network_id', 'cidr', 'gateway_ip']
|
position_names = ['ip_version', 'network_id', 'cidr', 'gateway_ip']
|
||||||
@@ -42,6 +42,22 @@ class CLITestV20SubnetJSON(test_cli20.CLITestV20Base):
|
|||||||
self._test_create_resource(resource, cmd, name, myid, args,
|
self._test_create_resource(resource, cmd, name, myid, args,
|
||||||
position_names, position_values)
|
position_names, position_values)
|
||||||
|
|
||||||
|
def test_create_subnet_network_cidr_seperated(self):
|
||||||
|
# For positional value, network_id and cidr can be separated.
|
||||||
|
"""Create subnet: --gateway gateway netid cidr."""
|
||||||
|
resource = 'subnet'
|
||||||
|
cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None)
|
||||||
|
name = 'myname'
|
||||||
|
myid = 'myid'
|
||||||
|
netid = 'netid'
|
||||||
|
cidr = '10.10.10.0/24'
|
||||||
|
gateway = 'gatewayvalue'
|
||||||
|
args = [netid, '--gateway', gateway, cidr]
|
||||||
|
position_names = ['ip_version', 'network_id', 'cidr', 'gateway_ip']
|
||||||
|
position_values = [4, netid, cidr, gateway]
|
||||||
|
self._test_create_resource(resource, cmd, name, myid, args,
|
||||||
|
position_names, position_values)
|
||||||
|
|
||||||
def test_create_subnet_with_no_gateway(self):
|
def test_create_subnet_with_no_gateway(self):
|
||||||
"""Create subnet: --no-gateway netid cidr."""
|
"""Create subnet: --no-gateway netid cidr."""
|
||||||
resource = 'subnet'
|
resource = 'subnet'
|
||||||
|
|||||||
@@ -102,6 +102,11 @@ class TestUtils(testtools.TestCase):
|
|||||||
act = utils.get_item_properties(item, fields, formatters=formatters)
|
act = utils.get_item_properties(item, fields, formatters=formatters)
|
||||||
self.assertEqual(('test_name', 'test_id', 'test', 'pass'), act)
|
self.assertEqual(('test_name', 'test_id', 'test', 'pass'), act)
|
||||||
|
|
||||||
|
def test_is_cidr(self):
|
||||||
|
self.assertTrue(utils.is_valid_cidr('10.10.10.0/24'))
|
||||||
|
self.assertFalse(utils.is_valid_cidr('10.10.10..0/24'))
|
||||||
|
self.assertFalse(utils.is_valid_cidr('wrong_cidr_format'))
|
||||||
|
|
||||||
|
|
||||||
class ImportClassTestCase(testtools.TestCase):
|
class ImportClassTestCase(testtools.TestCase):
|
||||||
def test_get_client_class_invalid_version(self):
|
def test_get_client_class_invalid_version(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user