Add --project to "subnet pool create"
This patch adds --project and --project-domain options to "subnet pool create" command. Change-Id: I2fe006013a194861299a9c77234a7cf988a8dad8 Partial-Bug: #1544586
This commit is contained in:
		| @@ -17,6 +17,7 @@ Create subnet pool | ||||
|         [--default-prefix-length <default-prefix-length>] | ||||
|         [--min-prefix-length <min-prefix-length>] | ||||
|         [--max-prefix-length <max-prefix-length>] | ||||
|         [--project <project> [--project-domain <project-domain>]] | ||||
|         <name> | ||||
|  | ||||
| .. option:: --pool-prefix <pool-prefix> | ||||
| @@ -36,6 +37,15 @@ Create subnet pool | ||||
|  | ||||
|     Set subnet pool maximum prefix length | ||||
|  | ||||
| .. option:: --project <project> | ||||
|  | ||||
|     Owner's project (name or ID) | ||||
|  | ||||
| .. option:: --project-domain <project-domain> | ||||
|  | ||||
|     Domain the project belongs to (name or ID). This can be used in case | ||||
|     collisions between project names exist. | ||||
|  | ||||
| .. _subnet_pool_create-name: | ||||
|       .. describe:: <name> | ||||
|  | ||||
|   | ||||
| @@ -17,6 +17,7 @@ from openstackclient.common import command | ||||
| from openstackclient.common import exceptions | ||||
| from openstackclient.common import parseractions | ||||
| from openstackclient.common import utils | ||||
| from openstackclient.identity import common as identity_common | ||||
|  | ||||
|  | ||||
| def _get_columns(item): | ||||
| @@ -32,7 +33,7 @@ _formatters = { | ||||
| } | ||||
|  | ||||
|  | ||||
| def _get_attrs(parsed_args): | ||||
| def _get_attrs(client_manager, parsed_args): | ||||
|     attrs = {} | ||||
|     if parsed_args.name is not None: | ||||
|         attrs['name'] = str(parsed_args.name) | ||||
| @@ -45,6 +46,16 @@ def _get_attrs(parsed_args): | ||||
|     if parsed_args.max_prefix_length is not None: | ||||
|         attrs['max_prefixlen'] = parsed_args.max_prefix_length | ||||
|  | ||||
|     # "subnet pool set" command doesn't support setting project. | ||||
|     if 'project' in parsed_args and parsed_args.project is not None: | ||||
|         identity_client = client_manager.identity | ||||
|         project_id = identity_common.find_project( | ||||
|             identity_client, | ||||
|             parsed_args.project, | ||||
|             parsed_args.project_domain, | ||||
|         ).id | ||||
|         attrs['tenant_id'] = project_id | ||||
|  | ||||
|     return attrs | ||||
|  | ||||
|  | ||||
| @@ -84,16 +95,22 @@ class CreateSubnetPool(command.ShowOne): | ||||
|         parser = super(CreateSubnetPool, self).get_parser(prog_name) | ||||
|         parser.add_argument( | ||||
|             'name', | ||||
|             metavar="<name>", | ||||
|             metavar='<name>', | ||||
|             help='Name of the new subnet pool' | ||||
|         ) | ||||
|         _add_prefix_options(parser) | ||||
|         parser.add_argument( | ||||
|             '--project', | ||||
|             metavar='<project>', | ||||
|             help="Owner's project (name or ID)", | ||||
|         ) | ||||
|         identity_common.add_project_domain_option_to_parser(parser) | ||||
|  | ||||
|         return parser | ||||
|  | ||||
|     def take_action(self, parsed_args): | ||||
|         client = self.app.client_manager.network | ||||
|         attrs = _get_attrs(parsed_args) | ||||
|         attrs = _get_attrs(self.app.client_manager, parsed_args) | ||||
|         obj = client.create_subnet_pool(**attrs) | ||||
|         columns = _get_columns(obj) | ||||
|         data = utils.get_item_properties(obj, columns, formatters=_formatters) | ||||
| @@ -192,7 +209,7 @@ class SetSubnetPool(command.Command): | ||||
|         obj = client.find_subnet_pool(parsed_args.subnet_pool, | ||||
|                                       ignore_missing=False) | ||||
|  | ||||
|         attrs = _get_attrs(parsed_args) | ||||
|         attrs = _get_attrs(self.app.client_manager, parsed_args) | ||||
|         if attrs == {}: | ||||
|             msg = "Nothing specified to be set" | ||||
|             raise exceptions.CommandError(msg) | ||||
|   | ||||
| @@ -12,11 +12,14 @@ | ||||
| # | ||||
|  | ||||
| import argparse | ||||
| import copy | ||||
| import mock | ||||
|  | ||||
| from openstackclient.common import exceptions | ||||
| from openstackclient.common import utils | ||||
| from openstackclient.network.v2 import subnet_pool | ||||
| from openstackclient.tests import fakes | ||||
| from openstackclient.tests.identity.v3 import fakes as identity_fakes_v3 | ||||
| from openstackclient.tests.network.v2 import fakes as network_fakes | ||||
| from openstackclient.tests import utils as tests_utils | ||||
|  | ||||
| @@ -73,6 +76,30 @@ class TestCreateSubnetPool(TestSubnetPool): | ||||
|         # Get the command object to test | ||||
|         self.cmd = subnet_pool.CreateSubnetPool(self.app, self.namespace) | ||||
|  | ||||
|         # Set identity client. And get a shortcut to Identity client. | ||||
|         identity_client = identity_fakes_v3.FakeIdentityv3Client( | ||||
|             endpoint=fakes.AUTH_URL, | ||||
|             token=fakes.AUTH_TOKEN, | ||||
|         ) | ||||
|         self.app.client_manager.identity = identity_client | ||||
|         self.identity = self.app.client_manager.identity | ||||
|  | ||||
|         # Get a shortcut to the ProjectManager Mock | ||||
|         self.projects_mock = self.identity.projects | ||||
|         self.projects_mock.get.return_value = fakes.FakeResource( | ||||
|             None, | ||||
|             copy.deepcopy(identity_fakes_v3.PROJECT), | ||||
|             loaded=True, | ||||
|         ) | ||||
|  | ||||
|         # Get a shortcut to the DomainManager Mock | ||||
|         self.domains_mock = self.identity.domains | ||||
|         self.domains_mock.get.return_value = fakes.FakeResource( | ||||
|             None, | ||||
|             copy.deepcopy(identity_fakes_v3.DOMAIN), | ||||
|             loaded=True, | ||||
|         ) | ||||
|  | ||||
|     def test_create_no_options(self): | ||||
|         arglist = [] | ||||
|         verifylist = [] | ||||
| @@ -140,6 +167,31 @@ class TestCreateSubnetPool(TestSubnetPool): | ||||
|         self.assertRaises(argparse.ArgumentTypeError, self.check_parser, | ||||
|                           self.cmd, arglist, verifylist) | ||||
|  | ||||
|     def test_create_project_domain(self): | ||||
|         arglist = [ | ||||
|             '--pool-prefix', '10.0.10.0/24', | ||||
|             "--project", identity_fakes_v3.project_name, | ||||
|             "--project-domain", identity_fakes_v3.domain_name, | ||||
|             self._subnet_pool.name, | ||||
|         ] | ||||
|         verifylist = [ | ||||
|             ('prefixes', ['10.0.10.0/24']), | ||||
|             ('project', identity_fakes_v3.project_name), | ||||
|             ('project_domain', identity_fakes_v3.domain_name), | ||||
|             ('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_once_with(**{ | ||||
|             'prefixes': ['10.0.10.0/24'], | ||||
|             'tenant_id': identity_fakes_v3.project_id, | ||||
|             'name': self._subnet_pool.name, | ||||
|         }) | ||||
|         self.assertEqual(self.columns, columns) | ||||
|         self.assertEqual(self.data, data) | ||||
|  | ||||
|  | ||||
| class TestDeleteSubnetPool(TestSubnetPool): | ||||
|  | ||||
|   | ||||
| @@ -2,3 +2,6 @@ | ||||
| features: | ||||
|   - Add ``subnet pool create`` command. | ||||
|     [Bug `1544586 <https://bugs.launchpad.net/python-openstackclient/+bug/1544586>`_] | ||||
|   - Command ``subnet pool create`` now supports ``--project`` and | ||||
|     ``--project-domain`` options. | ||||
|     [Bug `1544586 <https://bugs.launchpad.net/python-openstackclient/+bug/1544586>`_] | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Tang Chen
					Tang Chen