<s> """<STR_LIT>""" <EOL> import argparse <EOL> import logging <EOL> from openstackclient . common import command <EOL> from openstackclient . common import exceptions <EOL> from openstackclient . common import parseractions <EOL> from openstackclient . common import utils <EOL> from openstackclient . i18n import _ <EOL> from openstackclient . identity import common as identity_common <EOL> LOG = logging . getLogger ( __name__ ) <EOL> def _format_admin_state ( state ) : <EOL> return '<STR_LIT>' if state else '<STR_LIT>' <EOL> _formatters = { <EOL> '<STR_LIT>' : _format_admin_state , <EOL> '<STR_LIT>' : utils . format_list_of_dicts , <EOL> '<STR_LIT>' : utils . format_dict , <EOL> '<STR_LIT>' : utils . format_dict , <EOL> '<STR_LIT>' : utils . format_list_of_dicts , <EOL> '<STR_LIT>' : utils . format_list_of_dicts , <EOL> '<STR_LIT>' : utils . format_list_of_dicts , <EOL> '<STR_LIT>' : utils . format_list , <EOL> } <EOL> def _get_columns ( item ) : <EOL> columns = list ( item . keys ( ) ) <EOL> if '<STR_LIT>' in columns : <EOL> columns . remove ( '<STR_LIT>' ) <EOL> columns . append ( '<STR_LIT>' ) <EOL> binding_columns = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> for binding_column in binding_columns : <EOL> if binding_column in columns : <EOL> columns . remove ( binding_column ) <EOL> columns . append ( binding_column . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> return tuple ( sorted ( columns ) ) <EOL> def _get_attrs ( client_manager , parsed_args ) : <EOL> attrs = { } <EOL> if parsed_args . device_id : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . device_id <EOL> LOG . warning ( _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> if parsed_args . host_id : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . host_id <EOL> LOG . warning ( _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) ) <EOL> if parsed_args . fixed_ip is not None : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . fixed_ip <EOL> if parsed_args . device : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . device <EOL> if parsed_args . device_owner is not None : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . device_owner <EOL> if parsed_args . enable : <EOL> attrs [ '<STR_LIT>' ] = True <EOL> if parsed_args . disable : <EOL> attrs [ '<STR_LIT>' ] = False <EOL> if parsed_args . binding_profile is not None : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . binding_profile <EOL> if parsed_args . vnic_type is not None : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . vnic_type <EOL> if parsed_args . host : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . host <EOL> if parsed_args . name is not None : <EOL> attrs [ '<STR_LIT:name>' ] = str ( parsed_args . name ) <EOL> if '<STR_LIT>' in parsed_args and parsed_args . mac_address is not None : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . mac_address <EOL> if '<STR_LIT>' in parsed_args and parsed_args . network is not None : <EOL> attrs [ '<STR_LIT>' ] = parsed_args . network <EOL> if '<STR_LIT>' in parsed_args and parsed_args . project is not None : <EOL> identity_client = client_manager . identity <EOL> project_id = identity_common . find_project ( <EOL> identity_client , <EOL> parsed_args . project , <EOL> parsed_args . project_domain , <EOL> ) . id <EOL> attrs [ '<STR_LIT>' ] = project_id <EOL> return attrs <EOL> def _prepare_fixed_ips ( client_manager , parsed_args ) : <EOL> """<STR_LIT>""" <EOL> client = client_manager . network <EOL> ips = [ ] <EOL> if parsed_args . fixed_ip : <EOL> for ip_spec in parsed_args . fixed_ip : <EOL> if '<STR_LIT>' in ip_spec : <EOL> subnet_name_id = ip_spec [ '<STR_LIT>' ] <EOL> if subnet_name_id : <EOL> _subnet = client . find_subnet ( subnet_name_id , <EOL> ignore_missing = False ) <EOL> ip_spec [ '<STR_LIT>' ] = _subnet . id <EOL> del ip_spec [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in ip_spec : <EOL> ip_spec [ '<STR_LIT>' ] = ip_spec [ '<STR_LIT>' ] <EOL> del ip_spec [ '<STR_LIT>' ] <EOL> ips . append ( ip_spec ) <EOL> if ips : <EOL> parsed_args . fixed_ip = ips <EOL> def _add_updatable_args ( parser ) : <EOL> device_group = parser . add_mutually_exclusive_group ( ) <EOL> device_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> device_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = argparse . SUPPRESS , <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> choices = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> host_group = parser . add_mutually_exclusive_group ( ) <EOL> host_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> host_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = argparse . SUPPRESS , <EOL> ) <EOL> class CreatePort ( command . ShowOne ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( CreatePort , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> required = True , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> _add_updatable_args ( parser ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> action = parseractions . MultiKeyValueAction , <EOL> optional_keys = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> action = parseractions . KeyValueAction , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> admin_group = parser . add_mutually_exclusive_group ( ) <EOL> admin_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = True , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> admin_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> identity_common . add_project_domain_option_to_parser ( parser ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:name>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> client = self . app . client_manager . network <EOL> _network = client . find_network ( parsed_args . network , <EOL> ignore_missing = False ) <EOL> parsed_args . network = _network . id <EOL> _prepare_fixed_ips ( self . app . client_manager , parsed_args ) <EOL> attrs = _get_attrs ( self . app . client_manager , parsed_args ) <EOL> obj = client . create_port ( ** attrs ) <EOL> columns = _get_columns ( obj ) <EOL> data = utils . get_item_properties ( obj , columns , formatters = _formatters ) <EOL> return columns , data <EOL> class DeletePort ( command . Command ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( DeletePort , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:port>' , <EOL> metavar = "<STR_LIT>" , <EOL> nargs = "<STR_LIT:+>" , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> client = self . app . client_manager . network <EOL> for port in parsed_args . port : <EOL> res = client . find_port ( port ) <EOL> client . delete_port ( res ) <EOL> class ListPort ( command . Lister ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( ListPort , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> client = self . app . client_manager . network <EOL> columns = ( <EOL> '<STR_LIT:id>' , <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> column_headers = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:Name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> filters = { } <EOL> if parsed_args . router : <EOL> _router = client . find_router ( parsed_args . router , <EOL> ignore_missing = False ) <EOL> filters = { '<STR_LIT>' : _router . id } <EOL> data = client . ports ( ** filters ) <EOL> return ( column_headers , <EOL> ( utils . get_item_properties ( <EOL> s , columns , <EOL> formatters = _formatters , <EOL> ) for s in data ) ) <EOL> class SetPort ( command . Command ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( SetPort , self ) . get_parser ( prog_name ) <EOL> _add_updatable_args ( parser ) <EOL> admin_group = parser . add_mutually_exclusive_group ( ) <EOL> admin_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> default = None , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> admin_group . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = "<STR_LIT>" , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> fixed_ip = parser . add_mutually_exclusive_group ( ) <EOL> fixed_ip . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> action = parseractions . MultiKeyValueAction , <EOL> optional_keys = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> fixed_ip . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> binding_profile = parser . add_mutually_exclusive_group ( ) <EOL> binding_profile . add_argument ( <EOL> '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> action = parseractions . KeyValueAction , <EOL> help = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> binding_profile . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:port>' , <EOL> metavar = "<STR_LIT>" , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> client = self . app . client_manager . network <EOL> _prepare_fixed_ips ( self . app . client_manager , parsed_args ) <EOL> attrs = _get_attrs ( self . app . client_manager , parsed_args ) <EOL> obj = client . find_port ( parsed_args . port , ignore_missing = False ) <EOL> if '<STR_LIT>' in attrs : <EOL> attrs [ '<STR_LIT>' ] . update ( obj . binding_profile ) <EOL> elif parsed_args . no_binding_profile : <EOL> attrs [ '<STR_LIT>' ] = { } <EOL> if '<STR_LIT>' in attrs : <EOL> attrs [ '<STR_LIT>' ] += [ ip for ip in obj . fixed_ips if ip ] <EOL> elif parsed_args . no_fixed_ip : <EOL> attrs [ '<STR_LIT>' ] = [ ] <EOL> if attrs == { } : <EOL> msg = "<STR_LIT>" <EOL> raise exceptions . CommandError ( msg ) <EOL> client . update_port ( obj , ** attrs ) <EOL> class ShowPort ( command . ShowOne ) : <EOL> """<STR_LIT>""" <EOL> def get_parser ( self , prog_name ) : <EOL> parser = super ( ShowPort , self ) . get_parser ( prog_name ) <EOL> parser . add_argument ( <EOL> '<STR_LIT:port>' , <EOL> metavar = "<STR_LIT>" , <EOL> help = _ ( "<STR_LIT>" ) <EOL> ) <EOL> return parser <EOL> def take_action ( self , parsed_args ) : <EOL> client = self . app . client_manager . network <EOL> obj = client . find_port ( parsed_args . port , ignore_missing = False ) <EOL> columns = _get_columns ( obj ) <EOL> data = utils . get_item_properties ( obj , columns , formatters = _formatters ) <EOL> return columns , data </s>
