Skip to content

Commit 66a04ab

Browse files
committed
Add security groups options to "port create/set/unset"
This patch adds '--security-group' and '--no-security-group' options to "port create", "port set" and "port unset" commands. Change-Id: Iff60d8f29227017b0a3966efca6cdecba69abcea Partial-Bug: #1612136 Partially-Implements: blueprint network-commands-options
1 parent 1ee9333 commit 66a04ab

4 files changed

Lines changed: 325 additions & 1 deletion

File tree

doc/source/command-objects/port.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Create new port
2626
[--host <host-id>]
2727
[--enable | --disable]
2828
[--mac-address <mac-address>]
29+
[--security-group <security-group> | --no-security-group]
2930
[--project <project> [--project-domain <project-domain>]]
3031
<name>
3132
@@ -75,6 +76,15 @@ Create new port
7576
7677
MAC address of this port
7778
79+
.. option:: --security-group <security-group>
80+
81+
Security group to associate with this port (name or ID)
82+
(repeat option to set multiple security groups)
83+
84+
.. option:: --no-security-group
85+
86+
Associate no security groups with this port
87+
7888
.. option:: --project <project>
7989
8090
Owner's project (name or ID)
@@ -154,6 +164,8 @@ Set port properties
154164
[--host <host-id>]
155165
[--enable | --disable]
156166
[--name <name>]
167+
[--security-group <security-group>]
168+
[--no-security-group]
157169
<port>
158170
159171
.. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address>
@@ -210,6 +222,15 @@ Set port properties
210222
211223
Set port name
212224
225+
.. option:: --security-group <security-group>
226+
227+
Security group to associate with this port (name or ID)
228+
(repeat option to set multiple security groups)
229+
230+
.. option:: --no-security-group
231+
232+
Clear existing security groups associated with this port
233+
213234
.. _port_set-port:
214235
.. describe:: <port>
215236
@@ -242,6 +263,7 @@ Unset port properties
242263
os port unset
243264
[--fixed-ip subnet=<subnet>,ip-address=<ip-address> [...]]
244265
[--binding-profile <binding-profile-key> [...]]
266+
[--security-group <security-group> [...]]
245267
<port>
246268
247269
.. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address>
@@ -255,6 +277,11 @@ Unset port properties
255277
Desired key which should be removed from binding-profile
256278
(repeat option to unset multiple binding:profile data)
257279
280+
.. option:: --security-group <security-group>
281+
282+
Security group which should be removed from this port (name or ID)
283+
(repeat option to unset multiple security groups)
284+
258285
.. _port_unset-port:
259286
.. describe:: <port>
260287

openstackclient/network/v2/port.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,23 @@ def get_parser(self, prog_name):
281281
help=_("Name of this port")
282282
)
283283
# TODO(singhj): Add support for extended options:
284-
# qos,security groups,dhcp, address pairs
284+
# qos,dhcp, address pairs
285+
secgroups = parser.add_mutually_exclusive_group()
286+
secgroups.add_argument(
287+
'--security-group',
288+
metavar='<security-group>',
289+
action='append',
290+
dest='security_groups',
291+
help=_("Security group to associate with this port (name or ID) "
292+
"(repeat option to set multiple security groups)")
293+
)
294+
secgroups.add_argument(
295+
'--no-security-group',
296+
dest='no_security_group',
297+
action='store_true',
298+
help=_("Associate no security groups with this port")
299+
)
300+
285301
return parser
286302

287303
def take_action(self, parsed_args):
@@ -291,6 +307,14 @@ def take_action(self, parsed_args):
291307
parsed_args.network = _network.id
292308
_prepare_fixed_ips(self.app.client_manager, parsed_args)
293309
attrs = _get_attrs(self.app.client_manager, parsed_args)
310+
311+
if parsed_args.security_groups:
312+
attrs['security_groups'] = [client.find_security_group(
313+
sg, ignore_missing=False).id
314+
for sg in parsed_args.security_groups]
315+
if parsed_args.no_security_group:
316+
attrs['security_groups'] = []
317+
294318
obj = client.create_port(**attrs)
295319
columns = _get_columns(obj)
296320
data = utils.get_item_properties(obj, columns, formatters=_formatters)
@@ -463,6 +487,21 @@ def get_parser(self, prog_name):
463487
metavar="<port>",
464488
help=_("Port to modify (name or ID)")
465489
)
490+
parser.add_argument(
491+
'--security-group',
492+
metavar='<security-group>',
493+
action='append',
494+
dest='security_groups',
495+
help=_("Security group to associate with this port (name or ID) "
496+
"(repeat option to set multiple security groups)")
497+
)
498+
parser.add_argument(
499+
'--no-security-group',
500+
dest='no_security_group',
501+
action='store_true',
502+
help=_("Clear existing security groups associated with this port")
503+
)
504+
466505
return parser
467506

468507
def take_action(self, parsed_args):
@@ -490,6 +529,17 @@ def take_action(self, parsed_args):
490529
attrs['fixed_ips'] += [ip for ip in obj.fixed_ips if ip]
491530
elif parsed_args.no_fixed_ip:
492531
attrs['fixed_ips'] = []
532+
if parsed_args.security_groups and parsed_args.no_security_group:
533+
attrs['security_groups'] = [client.find_security_group(sg,
534+
ignore_missing=False).id
535+
for sg in parsed_args.security_groups]
536+
elif parsed_args.security_groups:
537+
attrs['security_groups'] = obj.security_groups
538+
for sg in parsed_args.security_groups:
539+
sg_id = client.find_security_group(sg, ignore_missing=False).id
540+
attrs['security_groups'].append(sg_id)
541+
elif parsed_args.no_security_group:
542+
attrs['security_groups'] = []
493543

494544
client.update_port(obj, **attrs)
495545

@@ -535,6 +585,15 @@ def get_parser(self, prog_name):
535585
action='append',
536586
help=_("Desired key which should be removed from binding:profile"
537587
"(repeat option to unset multiple binding:profile data)"))
588+
parser.add_argument(
589+
'--security-group',
590+
metavar='<security-group>',
591+
action='append',
592+
dest='security_groups',
593+
help=_("Security group which should be removed this port (name "
594+
"or ID) (repeat option to unset multiple security groups)")
595+
)
596+
538597
parser.add_argument(
539598
'port',
540599
metavar="<port>",
@@ -550,6 +609,7 @@ def take_action(self, parsed_args):
550609
# Unset* classes
551610
tmp_fixed_ips = copy.deepcopy(obj.fixed_ips)
552611
tmp_binding_profile = copy.deepcopy(obj.binding_profile)
612+
tmp_secgroups = copy.deepcopy(obj.security_groups)
553613
_prepare_fixed_ips(self.app.client_manager, parsed_args)
554614
attrs = {}
555615
if parsed_args.fixed_ip:
@@ -568,5 +628,16 @@ def take_action(self, parsed_args):
568628
msg = _("Port does not contain binding-profile %s") % key
569629
raise exceptions.CommandError(msg)
570630
attrs['binding:profile'] = tmp_binding_profile
631+
if parsed_args.security_groups:
632+
try:
633+
for sg in parsed_args.security_groups:
634+
sg_id = client.find_security_group(
635+
sg, ignore_missing=False).id
636+
tmp_secgroups.remove(sg_id)
637+
except ValueError:
638+
msg = _("Port does not contain security group %s") % sg
639+
raise exceptions.CommandError(msg)
640+
attrs['security_groups'] = tmp_secgroups
641+
571642
if attrs:
572643
client.update_port(obj, **attrs)

0 commit comments

Comments
 (0)