Skip to content

Commit 246f60a

Browse files
committed
Add --enable/disable-port-security option to port set and port create
This patch adds the currently missing options `--enable-port-security` and `--disable-port-security` in the `os port set` and `os port create` commands. Partially-Implements: blueprint network-commands-options Change-Id: I4dc11cdf32bf482a5937f5464fe8a3b418644ec3
1 parent e07b0e0 commit 246f60a

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

doc/source/command-objects/port.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Create new port
2828
[--mac-address <mac-address>]
2929
[--security-group <security-group> | --no-security-group]
3030
[--project <project> [--project-domain <project-domain>]]
31+
[--enable-port-security | --disable-port-security]
3132
<name>
3233
3334
.. option:: --network <network>
@@ -94,6 +95,14 @@ Create new port
9495
Domain the project belongs to (name or ID).
9596
This can be used in case collisions between project names exist.
9697
98+
.. option:: --enable-port-security
99+
100+
Enable port security for this port (Default)
101+
102+
.. option:: --disable-port-security
103+
104+
Disable port security for this port
105+
97106
.. _port_create-name:
98107
.. describe:: <name>
99108
@@ -171,6 +180,7 @@ Set port properties
171180
[--name <name>]
172181
[--security-group <security-group>]
173182
[--no-security-group]
183+
[--enable-port-security | --disable-port-security]
174184
<port>
175185
176186
.. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address>
@@ -236,6 +246,14 @@ Set port properties
236246
237247
Clear existing security groups associated with this port
238248
249+
.. option:: --enable-port-security
250+
251+
Enable port security for this port
252+
253+
.. option:: --disable-port-security
254+
255+
Disable port security for this port
256+
239257
.. _port_set-port:
240258
.. describe:: <port>
241259

openstackclient/network/v2/port.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ def _get_attrs(client_manager, parsed_args):
146146
).id
147147
attrs['tenant_id'] = project_id
148148

149+
if parsed_args.disable_port_security:
150+
attrs['port_security_enabled'] = False
151+
152+
if parsed_args.enable_port_security:
153+
attrs['port_security_enabled'] = True
154+
149155
return attrs
150156

151157

@@ -297,6 +303,17 @@ def get_parser(self, prog_name):
297303
action='store_true',
298304
help=_("Associate no security groups with this port")
299305
)
306+
port_security = parser.add_mutually_exclusive_group()
307+
port_security.add_argument(
308+
'--enable-port-security',
309+
action='store_true',
310+
help=_("Enable port security for this port (Default)")
311+
)
312+
port_security.add_argument(
313+
'--disable-port-security',
314+
action='store_true',
315+
help=_("Disable port security for this port")
316+
)
300317

301318
return parser
302319

@@ -512,6 +529,17 @@ def get_parser(self, prog_name):
512529
action='store_true',
513530
help=_("Clear existing security groups associated with this port")
514531
)
532+
port_security = parser.add_mutually_exclusive_group()
533+
port_security.add_argument(
534+
'--enable-port-security',
535+
action='store_true',
536+
help=_("Enable port security for this port")
537+
)
538+
port_security.add_argument(
539+
'--disable-port-security',
540+
action='store_true',
541+
help=_("Disable port security for this port")
542+
)
515543

516544
return parser
517545

openstackclient/tests/unit/network/v2/test_port.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,54 @@ def test_create_with_no_secuirty_groups(self):
315315
self.assertEqual(ref_columns, columns)
316316
self.assertEqual(ref_data, data)
317317

318+
def test_create_port_security_enabled(self):
319+
arglist = [
320+
'--network', self._port.network_id,
321+
'--enable-port-security',
322+
'test-port',
323+
]
324+
verifylist = [
325+
('network', self._port.network_id,),
326+
('enable', True),
327+
('enable_port_security', True),
328+
('name', 'test-port'),
329+
]
330+
331+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
332+
333+
self.cmd.take_action(parsed_args)
334+
335+
self.network.create_port.assert_called_once_with(**{
336+
'admin_state_up': True,
337+
'network_id': self._port.network_id,
338+
'port_security_enabled': True,
339+
'name': 'test-port',
340+
})
341+
342+
def test_create_port_security_disabled(self):
343+
arglist = [
344+
'--network', self._port.network_id,
345+
'--disable-port-security',
346+
'test-port',
347+
]
348+
verifylist = [
349+
('network', self._port.network_id,),
350+
('enable', True),
351+
('disable_port_security', True),
352+
('name', 'test-port'),
353+
]
354+
355+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
356+
357+
self.cmd.take_action(parsed_args)
358+
359+
self.network.create_port.assert_called_once_with(**{
360+
'admin_state_up': True,
361+
'network_id': self._port.network_id,
362+
'port_security_enabled': False,
363+
'name': 'test-port',
364+
})
365+
318366

319367
class TestDeletePort(TestPort):
320368

@@ -868,6 +916,42 @@ def test_overwrite_security_group(self):
868916
self.network.update_port.assert_called_once_with(_testport, **attrs)
869917
self.assertIsNone(result)
870918

919+
def test_port_security_enabled(self):
920+
arglist = [
921+
'--enable-port-security',
922+
self._port.id,
923+
]
924+
verifylist = [
925+
('enable_port_security', True),
926+
('port', self._port.id,)
927+
]
928+
929+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
930+
931+
self.cmd.take_action(parsed_args)
932+
933+
self.network.update_port.assert_called_once_with(self._port, **{
934+
'port_security_enabled': True,
935+
})
936+
937+
def test_port_security_disabled(self):
938+
arglist = [
939+
'--disable-port-security',
940+
self._port.id,
941+
]
942+
verifylist = [
943+
('disable_port_security', True),
944+
('port', self._port.id,)
945+
]
946+
947+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
948+
949+
self.cmd.take_action(parsed_args)
950+
951+
self.network.update_port.assert_called_once_with(self._port, **{
952+
'port_security_enabled': False,
953+
})
954+
871955

872956
class TestShowPort(TestPort):
873957

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Added ``--enable-port-security`` and ``--disable-port-security``
5+
options to ``port set`` and ``port create`` commands.
6+
[Blueprint :oscbp:`network-commands-options`]

0 commit comments

Comments
 (0)