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