Skip to content

Commit 785caf5

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add source security group support to create rule"
2 parents 67090b7 + 566388a commit 785caf5

3 files changed

Lines changed: 56 additions & 18 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Create a new security group rule
1414
1515
os security group rule create
1616
[--proto <proto>]
17-
[--src-ip <ip-address>]
17+
[--src-ip <ip-address> | --src-group <group>]
1818
[--dst-port <port-range>]
1919
<group>
2020
@@ -24,7 +24,11 @@ Create a new security group rule
2424

2525
.. option:: --src-ip <ip-address>
2626

27-
Source IP (may use CIDR notation; default: 0.0.0.0/0)
27+
Source IP address block (may use CIDR notation; default: 0.0.0.0/0)
28+
29+
.. option:: --src-group <group>
30+
31+
Source security group (ID only)
2832

2933
.. option:: --dst-port <port-range>
3034

openstackclient/compute/v2/security_group.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,18 @@ def get_parser(self, prog_name):
111111
default="tcp",
112112
help="IP protocol (icmp, tcp, udp; default: tcp)",
113113
)
114-
parser.add_argument(
114+
source_group = parser.add_mutually_exclusive_group()
115+
source_group.add_argument(
115116
"--src-ip",
116117
metavar="<ip-address>",
117118
default="0.0.0.0/0",
118-
help="Source IP (may use CIDR notation; default: 0.0.0.0/0)",
119+
help="Source IP address block (may use CIDR notation; default: "
120+
"0.0.0.0/0)",
121+
)
122+
source_group.add_argument(
123+
"--src-group",
124+
metavar="<group>",
125+
help="Source security group (ID only)",
119126
)
120127
parser.add_argument(
121128
"--dst-port",
@@ -145,6 +152,7 @@ def take_action(self, parsed_args):
145152
from_port,
146153
to_port,
147154
parsed_args.src_ip,
155+
parsed_args.src_group,
148156
)
149157

150158
info = _xform_security_group_rule(data._info)

openstackclient/tests/compute/v2/test_security_group_rule.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
from openstackclient.tests.compute.v2 import fakes as compute_fakes
1919
from openstackclient.tests import fakes
2020
from openstackclient.tests.identity.v2_0 import fakes as identity_fakes
21+
from openstackclient.tests import utils
2122

2223

2324
security_group_id = '11'
2425
security_group_name = 'wide-open'
2526
security_group_description = 'nothing but net'
2627

2728
security_group_rule_id = '1'
29+
security_group_rule_cidr = '0.0.0.0/0'
2830

2931
SECURITY_GROUP = {
3032
'id': security_group_id,
@@ -37,7 +39,7 @@
3739
'id': security_group_rule_id,
3840
'group': {},
3941
'ip_protocol': 'tcp',
40-
'ip_range': '0.0.0.0/0',
42+
'ip_range': {'cidr': security_group_rule_cidr},
4143
'parent_group_id': security_group_id,
4244
'from_port': 0,
4345
'to_port': 0,
@@ -47,7 +49,7 @@
4749
'id': security_group_rule_id,
4850
'group': {},
4951
'ip_protocol': 'icmp',
50-
'ip_range': '0.0.0.0/0',
52+
'ip_range': {'cidr': security_group_rule_cidr},
5153
'parent_group_id': security_group_id,
5254
'from_port': -1,
5355
'to_port': -1,
@@ -115,7 +117,8 @@ def test_security_group_rule_create_no_options(self):
115117
'tcp',
116118
0,
117119
0,
118-
'0.0.0.0/0',
120+
security_group_rule_cidr,
121+
None,
119122
)
120123

121124
collist = (
@@ -131,7 +134,7 @@ def test_security_group_rule_create_no_options(self):
131134
{},
132135
security_group_rule_id,
133136
'tcp',
134-
'',
137+
security_group_rule_cidr,
135138
security_group_id,
136139
'0:0',
137140
)
@@ -166,7 +169,8 @@ def test_security_group_rule_create_ftp(self):
166169
'tcp',
167170
20,
168171
21,
169-
'0.0.0.0/0',
172+
security_group_rule_cidr,
173+
None,
170174
)
171175

172176
collist = (
@@ -182,7 +186,7 @@ def test_security_group_rule_create_ftp(self):
182186
{},
183187
security_group_rule_id,
184188
'tcp',
185-
'',
189+
security_group_rule_cidr,
186190
security_group_id,
187191
'20:21',
188192
)
@@ -192,6 +196,7 @@ def test_security_group_rule_create_ssh(self):
192196
sg_rule = copy.deepcopy(SECURITY_GROUP_RULE)
193197
sg_rule['from_port'] = 22
194198
sg_rule['to_port'] = 22
199+
sg_rule['group'] = {'name': security_group_name}
195200
self.sg_rules_mock.create.return_value = FakeSecurityGroupRuleResource(
196201
None,
197202
sg_rule,
@@ -201,10 +206,12 @@ def test_security_group_rule_create_ssh(self):
201206
arglist = [
202207
security_group_name,
203208
'--dst-port', '22',
209+
'--src-group', security_group_id,
204210
]
205211
verifylist = [
206212
('group', security_group_name),
207213
('dst_port', (22, 22)),
214+
('src_group', security_group_id),
208215
]
209216
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
210217

@@ -217,7 +224,8 @@ def test_security_group_rule_create_ssh(self):
217224
'tcp',
218225
22,
219226
22,
220-
'0.0.0.0/0',
227+
security_group_rule_cidr,
228+
security_group_id,
221229
)
222230

223231
collist = (
@@ -230,10 +238,10 @@ def test_security_group_rule_create_ssh(self):
230238
)
231239
self.assertEqual(collist, columns)
232240
datalist = (
233-
{},
241+
{'name': security_group_name},
234242
security_group_rule_id,
235243
'tcp',
236-
'',
244+
security_group_rule_cidr,
237245
security_group_id,
238246
'22:22',
239247
)
@@ -267,7 +275,8 @@ def test_security_group_rule_create_udp(self):
267275
'udp',
268276
0,
269277
0,
270-
'0.0.0.0/0',
278+
security_group_rule_cidr,
279+
None,
271280
)
272281

273282
collist = (
@@ -283,26 +292,31 @@ def test_security_group_rule_create_udp(self):
283292
{},
284293
security_group_rule_id,
285294
'udp',
286-
'',
295+
security_group_rule_cidr,
287296
security_group_id,
288297
'0:0',
289298
)
290299
self.assertEqual(datalist, data)
291300

292301
def test_security_group_rule_create_icmp(self):
302+
sg_rule_cidr = '10.0.2.0/24'
303+
sg_rule = copy.deepcopy(SECURITY_GROUP_RULE_ICMP)
304+
sg_rule['ip_range'] = {'cidr': sg_rule_cidr}
293305
self.sg_rules_mock.create.return_value = FakeSecurityGroupRuleResource(
294306
None,
295-
copy.deepcopy(SECURITY_GROUP_RULE_ICMP),
307+
sg_rule,
296308
loaded=True,
297309
)
298310

299311
arglist = [
300312
security_group_name,
301313
'--proto', 'ICMP',
314+
'--src-ip', sg_rule_cidr,
302315
]
303316
verifylist = [
304317
('group', security_group_name),
305318
('proto', 'ICMP'),
319+
('src_ip', sg_rule_cidr)
306320
]
307321
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
308322

@@ -315,7 +329,8 @@ def test_security_group_rule_create_icmp(self):
315329
'ICMP',
316330
-1,
317331
-1,
318-
'0.0.0.0/0',
332+
sg_rule_cidr,
333+
None,
319334
)
320335

321336
collist = (
@@ -331,8 +346,19 @@ def test_security_group_rule_create_icmp(self):
331346
{},
332347
security_group_rule_id,
333348
'icmp',
334-
'',
349+
sg_rule_cidr,
335350
security_group_id,
336351
'',
337352
)
338353
self.assertEqual(datalist, data)
354+
355+
def test_security_group_rule_create_src_invalid(self):
356+
arglist = [
357+
security_group_name,
358+
'--proto', 'ICMP',
359+
'--src-ip', security_group_rule_cidr,
360+
'--src-group', security_group_id,
361+
]
362+
363+
self.assertRaises(utils.ParserException,
364+
self.check_parser, self.cmd, arglist, [])

0 commit comments

Comments
 (0)