@@ -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