Skip to content

Commit ab9cfc4

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add security groups options to "port create/set/unset""
2 parents bae09c3 + 66a04ab commit ab9cfc4

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)
@@ -159,6 +169,8 @@ Set port properties
159169
[--host <host-id>]
160170
[--enable | --disable]
161171
[--name <name>]
172+
[--security-group <security-group>]
173+
[--no-security-group]
162174
<port>
163175
164176
.. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address>
@@ -215,6 +227,15 @@ Set port properties
215227
216228
Set port name
217229
230+
.. option:: --security-group <security-group>
231+
232+
Security group to associate with this port (name or ID)
233+
(repeat option to set multiple security groups)
234+
235+
.. option:: --no-security-group
236+
237+
Clear existing security groups associated with this port
238+
218239
.. _port_set-port:
219240
.. describe:: <port>
220241
@@ -247,6 +268,7 @@ Unset port properties
247268
os port unset
248269
[--fixed-ip subnet=<subnet>,ip-address=<ip-address> [...]]
249270
[--binding-profile <binding-profile-key> [...]]
271+
[--security-group <security-group> [...]]
250272
<port>
251273
252274
.. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address>
@@ -260,6 +282,11 @@ Unset port properties
260282
Desired key which should be removed from binding-profile
261283
(repeat option to unset multiple binding:profile data)
262284
285+
.. option:: --security-group <security-group>
286+
287+
Security group which should be removed from this port (name or ID)
288+
(repeat option to unset multiple security groups)
289+
263290
.. _port_unset-port:
264291
.. describe:: <port>
265292

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)
@@ -474,6 +498,21 @@ def get_parser(self, prog_name):
474498
metavar="<port>",
475499
help=_("Port to modify (name or ID)")
476500
)
501+
parser.add_argument(
502+
'--security-group',
503+
metavar='<security-group>',
504+
action='append',
505+
dest='security_groups',
506+
help=_("Security group to associate with this port (name or ID) "
507+
"(repeat option to set multiple security groups)")
508+
)
509+
parser.add_argument(
510+
'--no-security-group',
511+
dest='no_security_group',
512+
action='store_true',
513+
help=_("Clear existing security groups associated with this port")
514+
)
515+
477516
return parser
478517

479518
def take_action(self, parsed_args):
@@ -501,6 +540,17 @@ def take_action(self, parsed_args):
501540
attrs['fixed_ips'] += [ip for ip in obj.fixed_ips if ip]
502541
elif parsed_args.no_fixed_ip:
503542
attrs['fixed_ips'] = []
543+
if parsed_args.security_groups and parsed_args.no_security_group:
544+
attrs['security_groups'] = [client.find_security_group(sg,
545+
ignore_missing=False).id
546+
for sg in parsed_args.security_groups]
547+
elif parsed_args.security_groups:
548+
attrs['security_groups'] = obj.security_groups
549+
for sg in parsed_args.security_groups:
550+
sg_id = client.find_security_group(sg, ignore_missing=False).id
551+
attrs['security_groups'].append(sg_id)
552+
elif parsed_args.no_security_group:
553+
attrs['security_groups'] = []
504554

505555
client.update_port(obj, **attrs)
506556

@@ -546,6 +596,15 @@ def get_parser(self, prog_name):
546596
action='append',
547597
help=_("Desired key which should be removed from binding:profile"
548598
"(repeat option to unset multiple binding:profile data)"))
599+
parser.add_argument(
600+
'--security-group',
601+
metavar='<security-group>',
602+
action='append',
603+
dest='security_groups',
604+
help=_("Security group which should be removed this port (name "
605+
"or ID) (repeat option to unset multiple security groups)")
606+
)
607+
549608
parser.add_argument(
550609
'port',
551610
metavar="<port>",
@@ -561,6 +620,7 @@ def take_action(self, parsed_args):
561620
# Unset* classes
562621
tmp_fixed_ips = copy.deepcopy(obj.fixed_ips)
563622
tmp_binding_profile = copy.deepcopy(obj.binding_profile)
623+
tmp_secgroups = copy.deepcopy(obj.security_groups)
564624
_prepare_fixed_ips(self.app.client_manager, parsed_args)
565625
attrs = {}
566626
if parsed_args.fixed_ip:
@@ -579,5 +639,16 @@ def take_action(self, parsed_args):
579639
msg = _("Port does not contain binding-profile %s") % key
580640
raise exceptions.CommandError(msg)
581641
attrs['binding:profile'] = tmp_binding_profile
642+
if parsed_args.security_groups:
643+
try:
644+
for sg in parsed_args.security_groups:
645+
sg_id = client.find_security_group(
646+
sg, ignore_missing=False).id
647+
tmp_secgroups.remove(sg_id)
648+
except ValueError:
649+
msg = _("Port does not contain security group %s") % sg
650+
raise exceptions.CommandError(msg)
651+
attrs['security_groups'] = tmp_secgroups
652+
582653
if attrs:
583654
client.update_port(obj, **attrs)

0 commit comments

Comments
 (0)