Add project options to security group create
Add the --project and --project-domain options to the 'os security group create' command. These options are for Network v2 only. Change-Id: I9e1667080a1a49389d51ade2e76a08b08a09870b Closes-Bug: #1519511 Implements: blueprint neutron-client
This commit is contained in:
		| @@ -14,12 +14,26 @@ Create a new security group | ||||
|  | ||||
|     os security group create | ||||
|         [--description <description>] | ||||
|         [--project <project> [--project-domain <project-domain>]] | ||||
|         <name> | ||||
|  | ||||
| .. option:: --description <description> | ||||
|  | ||||
|     Security group description | ||||
|  | ||||
| .. option:: --project <project> | ||||
|  | ||||
|     Owner's project (name or ID) | ||||
|  | ||||
|     *Network version 2 only* | ||||
|  | ||||
| .. option:: --project-domain <project-domain> | ||||
|  | ||||
|     Domain the project belongs to (name or ID). | ||||
|     This can be used in case collisions between project names exist. | ||||
|  | ||||
|     *Network version 2 only* | ||||
|  | ||||
| .. describe:: <name> | ||||
|  | ||||
|     New security group name | ||||
|   | ||||
| @@ -17,6 +17,7 @@ import argparse | ||||
| import six | ||||
|  | ||||
| from openstackclient.common import utils | ||||
| from openstackclient.identity import common as identity_common | ||||
| from openstackclient.network import common | ||||
| from openstackclient.network import utils as network_utils | ||||
|  | ||||
| @@ -107,6 +108,15 @@ class CreateSecurityGroup(common.NetworkAndComputeShowOne): | ||||
|         ) | ||||
|         return parser | ||||
|  | ||||
|     def update_parser_network(self, 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 _get_description(self, parsed_args): | ||||
|         if parsed_args.description is not None: | ||||
|             return parsed_args.description | ||||
| @@ -114,9 +124,20 @@ class CreateSecurityGroup(common.NetworkAndComputeShowOne): | ||||
|             return parsed_args.name | ||||
|  | ||||
|     def take_action_network(self, client, parsed_args): | ||||
|         # Build the create attributes. | ||||
|         attrs = {} | ||||
|         attrs['name'] = parsed_args.name | ||||
|         attrs['description'] = self._get_description(parsed_args) | ||||
|         if parsed_args.project is not None: | ||||
|             identity_client = self.app.client_manager.identity | ||||
|             project_id = identity_common.find_project( | ||||
|                 identity_client, | ||||
|                 parsed_args.project, | ||||
|                 parsed_args.project_domain, | ||||
|             ).id | ||||
|             attrs['tenant_id'] = project_id | ||||
|  | ||||
|         # Create the security group and display the results. | ||||
|         obj = client.create_security_group(**attrs) | ||||
|         display_columns, property_columns = _get_columns(obj) | ||||
|         data = utils.get_item_properties( | ||||
|   | ||||
| @@ -11,10 +11,13 @@ | ||||
| #   under the License. | ||||
| # | ||||
|  | ||||
| import copy | ||||
| import mock | ||||
|  | ||||
| from openstackclient.network.v2 import security_group | ||||
| from openstackclient.tests.compute.v2 import fakes as compute_fakes | ||||
| from openstackclient.tests import fakes | ||||
| from openstackclient.tests.identity.v3 import fakes as identity_fakes | ||||
| from openstackclient.tests.network.v2 import fakes as network_fakes | ||||
| from openstackclient.tests import utils as tests_utils | ||||
|  | ||||
| @@ -65,6 +68,30 @@ class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): | ||||
|         self.network.create_security_group = mock.Mock( | ||||
|             return_value=self._security_group) | ||||
|  | ||||
|         # Set identity client v3. And get a shortcut to Identity client. | ||||
|         identity_client = identity_fakes.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.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.DOMAIN), | ||||
|             loaded=True, | ||||
|         ) | ||||
|  | ||||
|         # Get the command object to test | ||||
|         self.cmd = security_group.CreateSecurityGroup(self.app, self.namespace) | ||||
|  | ||||
| @@ -93,11 +120,15 @@ class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): | ||||
|     def test_create_all_options(self): | ||||
|         arglist = [ | ||||
|             '--description', self._security_group.description, | ||||
|             '--project', identity_fakes.project_name, | ||||
|             '--project-domain', identity_fakes.domain_name, | ||||
|             self._security_group.name, | ||||
|         ] | ||||
|         verifylist = [ | ||||
|             ('description', self._security_group.description), | ||||
|             ('name', self._security_group.name), | ||||
|             ('project', identity_fakes.project_name), | ||||
|             ('project_domain', identity_fakes.domain_name), | ||||
|         ] | ||||
|         parsed_args = self.check_parser(self.cmd, arglist, verifylist) | ||||
|  | ||||
| @@ -106,6 +137,7 @@ class TestCreateSecurityGroupNetwork(TestSecurityGroupNetwork): | ||||
|         self.network.create_security_group.assert_called_once_with(**{ | ||||
|             'description': self._security_group.description, | ||||
|             'name': self._security_group.name, | ||||
|             'tenant_id': identity_fakes.project_id, | ||||
|         }) | ||||
|         self.assertEqual(tuple(self.columns), columns) | ||||
|         self.assertEqual(self.data, data) | ||||
| @@ -147,6 +179,15 @@ class TestCreateSecurityGroupCompute(TestSecurityGroupCompute): | ||||
|         self.assertRaises(tests_utils.ParserException, | ||||
|                           self.check_parser, self.cmd, [], []) | ||||
|  | ||||
|     def test_create_network_options(self): | ||||
|         arglist = [ | ||||
|             '--project', identity_fakes.project_name, | ||||
|             '--project-domain', identity_fakes.domain_name, | ||||
|             self._security_group.name, | ||||
|         ] | ||||
|         self.assertRaises(tests_utils.ParserException, | ||||
|                           self.check_parser, self.cmd, arglist, []) | ||||
|  | ||||
|     def test_create_min_options(self): | ||||
|         arglist = [ | ||||
|             self._security_group.name, | ||||
|   | ||||
							
								
								
									
										5
									
								
								releasenotes/notes/bug-1519511-65d8d21dde31e5e2.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								releasenotes/notes/bug-1519511-65d8d21dde31e5e2.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | ||||
| --- | ||||
| features: | ||||
|   - Add ``--project`` and ``--project-domain`` options to the | ||||
|     ``security group create`` command for Network v2. | ||||
|     [Bug `1519511 <https://bugs.launchpad.net/bugs/1519511>`_] | ||||
		Reference in New Issue
	
	Block a user
	 Richard Theis
					Richard Theis