From a7c76878da02da406c9ccbcd62cc40def1108faa Mon Sep 17 00:00:00 2001 From: Richard Theis Date: Fri, 11 Mar 2016 08:11:12 -0600 Subject: [PATCH] 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 --- doc/source/command-objects/security-group.rst | 14 +++++++ openstackclient/network/v2/security_group.py | 21 ++++++++++ .../tests/network/v2/test_security_group.py | 41 +++++++++++++++++++ .../notes/bug-1519511-65d8d21dde31e5e2.yaml | 5 +++ 4 files changed, 81 insertions(+) create mode 100644 releasenotes/notes/bug-1519511-65d8d21dde31e5e2.yaml diff --git a/doc/source/command-objects/security-group.rst b/doc/source/command-objects/security-group.rst index 22ea5cb78c..9fc4c987ba 100644 --- a/doc/source/command-objects/security-group.rst +++ b/doc/source/command-objects/security-group.rst @@ -14,12 +14,26 @@ Create a new security group os security group create [--description ] + [--project [--project-domain ]] .. option:: --description Security group description +.. option:: --project + + Owner's project (name or ID) + + *Network version 2 only* + +.. option:: --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:: New security group name diff --git a/openstackclient/network/v2/security_group.py b/openstackclient/network/v2/security_group.py index f8162477b8..92498144a5 100644 --- a/openstackclient/network/v2/security_group.py +++ b/openstackclient/network/v2/security_group.py @@ -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='', + 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( diff --git a/openstackclient/tests/network/v2/test_security_group.py b/openstackclient/tests/network/v2/test_security_group.py index 2d43d87218..dd6a3d416c 100644 --- a/openstackclient/tests/network/v2/test_security_group.py +++ b/openstackclient/tests/network/v2/test_security_group.py @@ -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, diff --git a/releasenotes/notes/bug-1519511-65d8d21dde31e5e2.yaml b/releasenotes/notes/bug-1519511-65d8d21dde31e5e2.yaml new file mode 100644 index 0000000000..8800db80d8 --- /dev/null +++ b/releasenotes/notes/bug-1519511-65d8d21dde31e5e2.yaml @@ -0,0 +1,5 @@ +--- +features: + - Add ``--project`` and ``--project-domain`` options to the + ``security group create`` command for Network v2. + [Bug `1519511 `_]