Skip to content

Commit 52a12e7

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add project options to security group rule create"
2 parents ef68f23 + a5a9cae commit 52a12e7

4 files changed

Lines changed: 74 additions & 3 deletions

File tree

doc/source/command-objects/security-group-rule.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Create a new security group rule
1818
[--dst-port <port-range>]
1919
[--ingress | --egress]
2020
[--ethertype <ethertype>]
21+
[--project <project> [--project-domain <project-domain>]]
2122
<group>
2223
2324
.. option:: --proto <proto>
@@ -56,6 +57,19 @@ Create a new security group rule
5657
5758
*Network version 2 only*
5859
60+
.. option:: --project <project>
61+
62+
Owner's project (name or ID)
63+
64+
*Network version 2 only*
65+
66+
.. option:: --project-domain <project-domain>
67+
68+
Domain the project belongs to (name or ID).
69+
This can be used in case collisions between project names exist.
70+
71+
*Network version 2 only*
72+
5973
.. describe:: <group>
6074
6175
Create rule in this security group (name or ID)

openstackclient/network/v2/security_group_rule.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from openstackclient.common import exceptions
2424
from openstackclient.common import parseractions
2525
from openstackclient.common import utils
26+
from openstackclient.identity import common as identity_common
2627
from openstackclient.network import common
2728
from openstackclient.network import utils as network_utils
2829

@@ -120,6 +121,12 @@ def update_parser_network(self, parser):
120121
help='Ethertype of network traffic '
121122
'(IPv4, IPv6; default: IPv4)',
122123
)
124+
parser.add_argument(
125+
'--project',
126+
metavar='<project>',
127+
help="Owner's project (name or ID)"
128+
)
129+
identity_common.add_project_domain_option_to_parser(parser)
123130
return parser
124131

125132
def take_action_network(self, client, parsed_args):
@@ -159,6 +166,14 @@ def take_action_network(self, client, parsed_args):
159166
elif attrs['ethertype'] == 'IPv4':
160167
attrs['remote_ip_prefix'] = '0.0.0.0/0'
161168
attrs['security_group_id'] = security_group_id
169+
if parsed_args.project is not None:
170+
identity_client = self.app.client_manager.identity
171+
project_id = identity_common.find_project(
172+
identity_client,
173+
parsed_args.project,
174+
parsed_args.project_domain,
175+
).id
176+
attrs['tenant_id'] = project_id
162177

163178
# Create and show the security group rule.
164179
obj = client.create_security_group_rule(**attrs)

openstackclient/tests/network/v2/test_security_group_rule.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from openstackclient.network.v2 import security_group_rule
1919
from openstackclient.tests.compute.v2 import fakes as compute_fakes
2020
from openstackclient.tests import fakes
21+
from openstackclient.tests.identity.v3 import fakes as identity_fakes
2122
from openstackclient.tests.network.v2 import fakes as network_fakes
2223
from openstackclient.tests import utils as tests_utils
2324

@@ -89,6 +90,30 @@ def setUp(self):
8990
self.network.find_security_group = mock.Mock(
9091
return_value=self._security_group)
9192

93+
# Set identity client v3. And get a shortcut to Identity client.
94+
identity_client = identity_fakes.FakeIdentityv3Client(
95+
endpoint=fakes.AUTH_URL,
96+
token=fakes.AUTH_TOKEN,
97+
)
98+
self.app.client_manager.identity = identity_client
99+
self.identity = self.app.client_manager.identity
100+
101+
# Get a shortcut to the ProjectManager Mock
102+
self.projects_mock = self.identity.projects
103+
self.projects_mock.get.return_value = fakes.FakeResource(
104+
None,
105+
copy.deepcopy(identity_fakes.PROJECT),
106+
loaded=True,
107+
)
108+
109+
# Get a shortcut to the DomainManager Mock
110+
self.domains_mock = self.identity.domains
111+
self.domains_mock.get.return_value = fakes.FakeResource(
112+
None,
113+
copy.deepcopy(identity_fakes.DOMAIN),
114+
loaded=True,
115+
)
116+
92117
# Get the command object to test
93118
self.cmd = security_group_rule.CreateSecurityGroupRule(
94119
self.app, self.namespace)
@@ -231,13 +256,17 @@ def test_create_network_options(self):
231256
'--dst-port', str(self._security_group_rule.port_range_min),
232257
'--egress',
233258
'--ethertype', self._security_group_rule.ethertype,
259+
'--project', identity_fakes.project_name,
260+
'--project-domain', identity_fakes.domain_name,
234261
self._security_group.id,
235262
]
236263
verifylist = [
237264
('dst_port', (self._security_group_rule.port_range_min,
238265
self._security_group_rule.port_range_max)),
239266
('egress', True),
240267
('ethertype', self._security_group_rule.ethertype),
268+
('project', identity_fakes.project_name),
269+
('project_domain', identity_fakes.domain_name),
241270
('group', self._security_group.id),
242271
]
243272
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -251,6 +280,7 @@ def test_create_network_options(self):
251280
'port_range_min': self._security_group_rule.port_range_min,
252281
'protocol': self._security_group_rule.protocol,
253282
'security_group_id': self._security_group.id,
283+
'tenant_id': identity_fakes.project_id,
254284
})
255285
self.assertEqual(tuple(self.expected_columns), columns)
256286
self.assertEqual(self.expected_data, data)
@@ -307,6 +337,17 @@ def test_create_bad_protocol(self):
307337
self.assertRaises(tests_utils.ParserException,
308338
self.check_parser, self.cmd, arglist, [])
309339

340+
def test_create_network_options(self):
341+
arglist = [
342+
'--ingress',
343+
'--ethertype', 'IPv4',
344+
'--project', identity_fakes.project_name,
345+
'--project-domain', identity_fakes.domain_name,
346+
self._security_group.id,
347+
]
348+
self.assertRaises(tests_utils.ParserException,
349+
self.check_parser, self.cmd, arglist, [])
350+
310351
def test_create_default_rule(self):
311352
expected_columns, expected_data = self._setup_security_group_rule()
312353
dst_port = str(self._security_group_rule.from_port) + ':' + \
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
features:
3-
- Add ``--ingress``, ``--egress``, and ``--ethertype`` options to the
4-
``security group rule create`` command for Network v2 only. These
5-
options enable ``egress`` and ``IPv6`` security group rules.
3+
- Add ``--ingress``, ``--egress``, ``--ethertype``, ``--project``
4+
and ``--project-domain`` options to the ``security group rule create``
5+
command for Network v2 only. These options enable ``egress`` and
6+
``IPv6`` security group rules along with setting the project.
67
[Bug `1519512 <https://bugs.launchpad.net/bugs/1519512>`_]

0 commit comments

Comments
 (0)