diff --git a/doc/source/command-objects/subnet-pool.rst b/doc/source/command-objects/subnet-pool.rst index 658616d5bc..722f92997d 100644 --- a/doc/source/command-objects/subnet-pool.rst +++ b/doc/source/command-objects/subnet-pool.rst @@ -4,6 +4,43 @@ subnet pool Network v2 +subnet pool create +------------------ + +Create subnet pool + +.. program:: subnet pool create + .. code:: bash + + os subnet pool create + [--pool-prefix [...]] + [--default-prefix-length ] + [--min-prefix-length ] + [--max-prefix-length ] + + +.. option:: --pool-prefix + + Set subnet pool prefixes (in CIDR notation). + Repeat this option to set multiple prefixes. + +.. option:: --default-prefix-length + + Set subnet pool default prefix length + +.. option:: --min-prefix-length + + Set subnet pool minimum prefix length + +.. option:: --max-prefix-length + + Set subnet pool maximum prefix length + +.. _subnet_pool_create-name: + .. describe:: + + Name of the new subnet pool + subnet pool delete ------------------ diff --git a/openstackclient/network/v2/subnet_pool.py b/openstackclient/network/v2/subnet_pool.py index 19cd46c986..44f30207a1 100644 --- a/openstackclient/network/v2/subnet_pool.py +++ b/openstackclient/network/v2/subnet_pool.py @@ -77,6 +77,29 @@ def _add_prefix_options(parser): ) +class CreateSubnetPool(command.ShowOne): + """Create subnet pool""" + + def get_parser(self, prog_name): + parser = super(CreateSubnetPool, self).get_parser(prog_name) + parser.add_argument( + 'name', + metavar="", + help='Name of the new subnet pool' + ) + _add_prefix_options(parser) + + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + attrs = _get_attrs(parsed_args) + obj = client.create_subnet_pool(**attrs) + columns = _get_columns(obj) + data = utils.get_item_properties(obj, columns, formatters=_formatters) + return (columns, data) + + class DeleteSubnetPool(command.Command): """Delete subnet pool""" diff --git a/openstackclient/tests/network/v2/fakes.py b/openstackclient/tests/network/v2/fakes.py index 9e6bf97f65..26213b1f12 100644 --- a/openstackclient/tests/network/v2/fakes.py +++ b/openstackclient/tests/network/v2/fakes.py @@ -734,15 +734,15 @@ class FakeSubnetPool(object): 'id': 'subnet-pool-id-' + uuid.uuid4().hex, 'name': 'subnet-pool-name-' + uuid.uuid4().hex, 'prefixes': ['10.0.0.0/24', '10.1.0.0/24'], - 'default_prefixlen': 8, + 'default_prefixlen': '8', 'address_scope_id': 'address-scope-id-' + uuid.uuid4().hex, 'tenant_id': 'project-id-' + uuid.uuid4().hex, 'is_default': False, 'shared': False, - 'max_prefixlen': 32, - 'min_prefixlen': 8, + 'max_prefixlen': '32', + 'min_prefixlen': '8', 'default_quota': None, - 'ip_version': 4, + 'ip_version': '4', } # Overwrite default attributes. diff --git a/openstackclient/tests/network/v2/test_subnet_pool.py b/openstackclient/tests/network/v2/test_subnet_pool.py index 9c372ac0fd..99994681d5 100644 --- a/openstackclient/tests/network/v2/test_subnet_pool.py +++ b/openstackclient/tests/network/v2/test_subnet_pool.py @@ -30,6 +30,117 @@ class TestSubnetPool(network_fakes.TestNetworkV2): self.network = self.app.client_manager.network +class TestCreateSubnetPool(TestSubnetPool): + + # The new subnet pool to create. + _subnet_pool = network_fakes.FakeSubnetPool.create_one_subnet_pool() + + columns = ( + 'address_scope_id', + 'default_prefixlen', + 'default_quota', + 'id', + 'ip_version', + 'is_default', + 'max_prefixlen', + 'min_prefixlen', + 'name', + 'prefixes', + 'project_id', + 'shared', + ) + data = ( + _subnet_pool.address_scope_id, + _subnet_pool.default_prefixlen, + _subnet_pool.default_quota, + _subnet_pool.id, + _subnet_pool.ip_version, + _subnet_pool.is_default, + _subnet_pool.max_prefixlen, + _subnet_pool.min_prefixlen, + _subnet_pool.name, + utils.format_list(_subnet_pool.prefixes), + _subnet_pool.project_id, + _subnet_pool.shared, + ) + + def setUp(self): + super(TestCreateSubnetPool, self).setUp() + + self.network.create_subnet_pool = mock.Mock( + return_value=self._subnet_pool) + + # Get the command object to test + self.cmd = subnet_pool.CreateSubnetPool(self.app, self.namespace) + + def test_create_no_options(self): + arglist = [] + verifylist = [] + + # Missing required args should bail here + self.assertRaises(tests_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_create_default_options(self): + arglist = [ + '--pool-prefix', '10.0.10.0/24', + self._subnet_pool.name, + ] + verifylist = [ + ('prefixes', ['10.0.10.0/24']), + ('name', self._subnet_pool.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = (self.cmd.take_action(parsed_args)) + + self.network.create_subnet_pool.assert_called_with(**{ + 'prefixes': ['10.0.10.0/24'], + 'name': self._subnet_pool.name, + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + + def test_create_prefixlen_options(self): + arglist = [ + '--default-prefix-length', self._subnet_pool.default_prefixlen, + '--max-prefix-length', self._subnet_pool.max_prefixlen, + '--min-prefix-length', self._subnet_pool.min_prefixlen, + self._subnet_pool.name, + ] + verifylist = [ + ('default_prefix_length', self._subnet_pool.default_prefixlen), + ('max_prefix_length', self._subnet_pool.max_prefixlen), + ('min_prefix_length', self._subnet_pool.min_prefixlen), + ('name', self._subnet_pool.name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = (self.cmd.take_action(parsed_args)) + + self.network.create_subnet_pool.assert_called_with(**{ + 'default_prefix_length': self._subnet_pool.default_prefixlen, + 'max_prefix_length': self._subnet_pool.max_prefixlen, + 'min_prefix_length': self._subnet_pool.min_prefixlen, + 'name': self._subnet_pool.name, + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + + def test_create_len_negative(self): + arglist = [ + self._subnet_pool.name, + '--min-prefix-length', '-16', + ] + verifylist = [ + ('subnet_pool', self._subnet_pool.name), + ('min_prefix_length', '-16'), + ] + + self.assertRaises(argparse.ArgumentTypeError, self.check_parser, + self.cmd, arglist, verifylist) + + class TestDeleteSubnetPool(TestSubnetPool): # The subnet pool to delete. diff --git a/setup.cfg b/setup.cfg index da360269b1..5a1e32c850 100644 --- a/setup.cfg +++ b/setup.cfg @@ -355,6 +355,7 @@ openstack.network.v2 = subnet_list = openstackclient.network.v2.subnet:ListSubnet subnet_show = openstackclient.network.v2.subnet:ShowSubnet + subnet_pool_create = openstackclient.network.v2.subnet_pool:CreateSubnetPool subnet_pool_delete = openstackclient.network.v2.subnet_pool:DeleteSubnetPool subnet_pool_list = openstackclient.network.v2.subnet_pool:ListSubnetPool subnet_pool_set = openstackclient.network.v2.subnet_pool:SetSubnetPool