<s> from io import StringIO <EOL> from pydoc import pager <EOL> from kamaki . cli import command <EOL> from kamaki . cli . cmdtree import CommandTree <EOL> from kamaki . cli . errors import CLIInvalidArgument , raiseCLIError <EOL> from kamaki . clients . cyclades import ( <EOL> CycladesNetworkClient , ClientError , CycladesComputeClient ) <EOL> from kamaki . cli . argument import ( <EOL> FlagArgument , ValueArgument , RepeatableArgument , IntArgument , <EOL> StatusArgument ) <EOL> from kamaki . cli . cmds import ( <EOL> CommandInit , OptionalOutput , NameFilter , IDFilter , errors , client_log ) <EOL> from kamaki . cli . cmds import Wait <EOL> network_cmds = CommandTree ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> port_cmds = CommandTree ( '<STR_LIT:port>' , '<STR_LIT>' ) <EOL> subnet_cmds = CommandTree ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ip_cmds = CommandTree ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> namespaces = [ network_cmds , port_cmds , subnet_cmds , ip_cmds ] <EOL> port_states = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class _PortWait ( Wait ) : <EOL> def wait_while ( self , port_id , current_status , timeout = <NUM_LIT> ) : <EOL> super ( _PortWait , self ) . wait ( <EOL> '<STR_LIT>' , port_id , self . client . wait_port_while , current_status , <EOL> timeout = timeout ) <EOL> def wait_until ( self , port_id , target_status , timeout = <NUM_LIT> ) : <EOL> super ( _PortWait , self ) . wait ( <EOL> '<STR_LIT>' , port_id , self . client . wait_port_until , target_status , <EOL> timeout = timeout , msg = '<STR_LIT>' ) <EOL> class _NetworkInit ( CommandInit ) : <EOL> @ errors . Generic . all <EOL> @ client_log <EOL> def _run ( self ) : <EOL> self . client = self . get_client ( CycladesNetworkClient , '<STR_LIT>' ) <EOL> def _filter_by_user_id ( self , nets ) : <EOL> return [ net for net in nets if net [ '<STR_LIT>' ] == self [ '<STR_LIT>' ] ] if ( <EOL> self [ '<STR_LIT>' ] ) else nets <EOL> def _get_compute_client ( self ) : <EOL> compute = getattr ( self , '<STR_LIT>' , None ) <EOL> if not compute : <EOL> compute = self . get_client ( CycladesComputeClient , '<STR_LIT>' ) <EOL> self . _compute_client = compute <EOL> return compute <EOL> @ errors . Cyclades . network_id <EOL> def _network_exists ( self , network_id ) : <EOL> self . client . get_network_details ( network_id ) <EOL> @ errors . Cyclades . server_id <EOL> def _server_exists ( self , server_id ) : <EOL> compute_client = self . _get_compute_client ( ) <EOL> compute_client . get_server_details ( server_id ) <EOL> def _ip_exists ( self , ip , network_id , error ) : <EOL> for ip_item in self . client . list_floatingips ( ) : <EOL> if ip_item [ '<STR_LIT>' ] == ip : <EOL> if network_id and ip_item [ '<STR_LIT>' ] != network_id : <EOL> raiseCLIError ( error , details = [ <EOL> '<STR_LIT>' % ( <EOL> ip , network_id ) , <EOL> '<STR_LIT>' % ip , <EOL> '<STR_LIT>' % ip_item [ '<STR_LIT:id>' ] ] ) <EOL> return <EOL> raiseCLIError ( error , details = [ <EOL> '<STR_LIT>' % ip ] + errors . Cyclades . about_ips ) <EOL> def main ( self ) : <EOL> self . _run ( ) <EOL> @ command ( network_cmds ) <EOL> class network_list ( _NetworkInit , OptionalOutput , NameFilter , IDFilter ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> detail = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> more = FlagArgument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> user_id = ValueArgument ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> nets = self . client . list_networks ( detail = True ) <EOL> nets = self . _filter_by_user_id ( nets ) <EOL> nets = self . _filter_by_name ( nets ) <EOL> nets = self . _filter_by_id ( nets ) <EOL> if not self [ '<STR_LIT>' ] : <EOL> nets = [ dict ( <EOL> id = n [ '<STR_LIT:id>' ] , <EOL> name = n [ '<STR_LIT:name>' ] , <EOL> public = '<STR_LIT>' % ( '<STR_LIT>' if ( <EOL> n . get ( '<STR_LIT>' , None ) ) else '<STR_LIT>' ) ) for n in nets ] <EOL> kwargs = dict ( title = ( '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ) ) <EOL> else : <EOL> kwargs = dict ( ) <EOL> if self [ '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT>' ] = StringIO ( ) <EOL> kwargs [ '<STR_LIT:title>' ] = ( ) <EOL> self . print_ ( nets , ** kwargs ) <EOL> if self [ '<STR_LIT>' ] : <EOL> pager ( kwargs [ '<STR_LIT>' ] . getvalue ( ) ) <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ) <EOL> @ command ( network_cmds ) <EOL> class network_info ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . network_id <EOL> def _run ( self , network_id ) : <EOL> net = self . client . get_network_details ( network_id ) <EOL> self . print_ ( net , self . print_dict ) <EOL> def main ( self , network_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( network_id = network_id ) <EOL> class NetworkTypeArgument ( ValueArgument ) : <EOL> types = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ property <EOL> def value ( self ) : <EOL> return getattr ( self , '<STR_LIT>' , self . types [ <NUM_LIT:0> ] ) <EOL> @ value . setter <EOL> def value ( self , new_value ) : <EOL> if new_value and new_value . upper ( ) in self . types : <EOL> self . _value = new_value . upper ( ) <EOL> elif new_value : <EOL> raise CLIInvalidArgument ( <EOL> '<STR_LIT>' % new_value , details = [ <EOL> '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( self . types ) , ] ) <EOL> @ command ( network_cmds ) <EOL> class network_create ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> shared = FlagArgument ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> project_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> network_type = NetworkTypeArgument ( <EOL> '<STR_LIT>' % ( '<STR_LIT:U+002CU+0020>' . join ( NetworkTypeArgument . types ) ) , <EOL> '<STR_LIT>' ) <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> try : <EOL> net = self . client . create_network ( <EOL> self [ '<STR_LIT>' ] , <EOL> name = self [ '<STR_LIT:name>' ] , <EOL> shared = self [ '<STR_LIT>' ] , <EOL> project_id = self [ '<STR_LIT>' ] ) <EOL> except ClientError as ce : <EOL> if self [ '<STR_LIT>' ] and ce . status in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _project_id_exists ( project_id = self [ '<STR_LIT>' ] ) <EOL> raise <EOL> self . print_ ( net , self . print_dict ) <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ) <EOL> @ command ( network_cmds ) <EOL> class network_reassign ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> project_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> required = ( '<STR_LIT>' , ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . network_permissions <EOL> @ errors . Cyclades . network_id <EOL> def _run ( self , network_id ) : <EOL> try : <EOL> self . client . reassign_network ( network_id , self [ '<STR_LIT>' ] ) <EOL> except ClientError as ce : <EOL> if ce . status in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _project_id_exists ( project_id = self [ '<STR_LIT>' ] ) <EOL> raise <EOL> def main ( self , network_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( network_id = network_id ) <EOL> @ command ( network_cmds ) <EOL> class network_delete ( _NetworkInit ) : <EOL> """<STR_LIT>""" <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . network_permissions <EOL> @ errors . Cyclades . network_in_use <EOL> @ errors . Cyclades . network_id <EOL> def _run ( self , network_id ) : <EOL> self . client . delete_network ( network_id ) <EOL> def main ( self , network_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( network_id = network_id ) <EOL> @ command ( network_cmds ) <EOL> class network_modify ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( new_name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> required = [ '<STR_LIT>' , ] <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . network_permissions <EOL> @ errors . Cyclades . network_id <EOL> def _run ( self , network_id ) : <EOL> r = self . client . update_network ( network_id , name = self [ '<STR_LIT>' ] ) <EOL> self . print_ ( r , self . print_dict ) <EOL> def main ( self , network_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( network_id = network_id ) <EOL> @ command ( subnet_cmds ) <EOL> class subnet_list ( _NetworkInit , OptionalOutput , NameFilter , IDFilter ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> detail = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> more = FlagArgument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> nets = self . client . list_subnets ( ) <EOL> nets = self . _filter_by_name ( nets ) <EOL> nets = self . _filter_by_id ( nets ) <EOL> if not self [ '<STR_LIT>' ] : <EOL> nets = [ dict ( <EOL> id = n [ '<STR_LIT:id>' ] , <EOL> name = n [ '<STR_LIT:name>' ] , <EOL> net = '<STR_LIT>' % n [ '<STR_LIT>' ] ) for n in nets ] <EOL> kwargs = dict ( title = ( '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ) ) <EOL> else : <EOL> kwargs = dict ( ) <EOL> if self [ '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT>' ] = StringIO ( ) <EOL> kwargs [ '<STR_LIT:title>' ] = ( ) <EOL> self . print_ ( nets , ** kwargs ) <EOL> if self [ '<STR_LIT>' ] : <EOL> pager ( '<STR_LIT:%s>' % kwargs [ '<STR_LIT>' ] . getvalue ( ) ) <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ) <EOL> @ command ( subnet_cmds ) <EOL> class subnet_info ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . subnet_id <EOL> def _run ( self , subnet_id ) : <EOL> net = self . client . get_subnet_details ( subnet_id ) <EOL> self . print_ ( net , self . print_dict ) <EOL> def main ( self , subnet_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( subnet_id = subnet_id ) <EOL> class AllocationPoolArgument ( RepeatableArgument ) : <EOL> @ property <EOL> def value ( self ) : <EOL> return super ( AllocationPoolArgument , self ) . value or [ ] <EOL> @ value . setter <EOL> def value ( self , new_pools ) : <EOL> if not new_pools : <EOL> return <EOL> new_list = [ ] <EOL> for pool in new_pools : <EOL> start , comma , end = pool . partition ( '<STR_LIT:U+002C>' ) <EOL> if not ( start and comma and end ) : <EOL> raise CLIInvalidArgument ( <EOL> '<STR_LIT>' % pool , details = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> new_list . append ( dict ( start = start , end = end ) ) <EOL> self . _value = new_list <EOL> @ command ( subnet_cmds ) <EOL> class subnet_create ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> allocation_pools = AllocationPoolArgument ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> gateway = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> no_gateway = FlagArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> subnet_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ipv6 = FlagArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> enable_dhcp = FlagArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> network_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> cidr = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> required = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> gateway = '<STR_LIT>' if self [ '<STR_LIT>' ] else self [ '<STR_LIT>' ] <EOL> try : <EOL> net = self . client . create_subnet ( <EOL> self [ '<STR_LIT>' ] , self [ '<STR_LIT>' ] , <EOL> self [ '<STR_LIT:name>' ] , self [ '<STR_LIT>' ] , gateway , <EOL> self [ '<STR_LIT>' ] , self [ '<STR_LIT>' ] , self [ '<STR_LIT>' ] ) <EOL> except ClientError as ce : <EOL> if ce . status in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _network_exists ( network_id = self [ '<STR_LIT>' ] ) <EOL> raise <EOL> self . print_ ( net , self . print_dict ) <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> if self [ '<STR_LIT>' ] and self [ '<STR_LIT>' ] : <EOL> raise CLIInvalidArgument ( '<STR_LIT>' , details = [ <EOL> '<STR_LIT>' % ( <EOL> self . arguments [ '<STR_LIT>' ] . lvalue , <EOL> self . arguments [ '<STR_LIT>' ] . lvalue ) ] ) <EOL> self . _run ( ) <EOL> @ command ( subnet_cmds ) <EOL> class subnet_modify ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> new_name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> required = [ '<STR_LIT>' ] <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . subnet_permissions <EOL> @ errors . Cyclades . subnet_id <EOL> def _run ( self , subnet_id ) : <EOL> r = self . client . update_subnet ( subnet_id , name = self [ '<STR_LIT>' ] ) <EOL> self . print_ ( r , self . print_dict ) <EOL> def main ( self , subnet_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( subnet_id = subnet_id ) <EOL> @ command ( port_cmds ) <EOL> class port_list ( _NetworkInit , OptionalOutput , NameFilter , IDFilter ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> detail = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> more = FlagArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> user_id = ValueArgument ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> ports = self . client . list_ports ( ) <EOL> ports = self . _filter_by_user_id ( ports ) <EOL> ports = self . _filter_by_name ( ports ) <EOL> ports = self . _filter_by_id ( ports ) <EOL> if not self [ '<STR_LIT>' ] : <EOL> ports = [ dict ( id = p [ '<STR_LIT:id>' ] , name = p [ '<STR_LIT:name>' ] ) for p in ports ] <EOL> kwargs = dict ( ) <EOL> if self [ '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT>' ] = StringIO ( ) <EOL> kwargs [ '<STR_LIT:title>' ] = ( ) <EOL> self . print_ ( ports , ** kwargs ) <EOL> if self [ '<STR_LIT>' ] : <EOL> pager ( kwargs [ '<STR_LIT>' ] . getvalue ( ) ) <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ) <EOL> @ command ( port_cmds ) <EOL> class port_info ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . port_id <EOL> def _run ( self , port_id ) : <EOL> port = self . client . get_port_details ( port_id ) <EOL> self . print_ ( port , self . print_dict ) <EOL> def main ( self , port_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( port_id = port_id ) <EOL> @ command ( port_cmds ) <EOL> class port_delete ( _NetworkInit , _PortWait ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> wait = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . port_id <EOL> def _run ( self , port_id ) : <EOL> if self [ '<STR_LIT>' ] : <EOL> status = self . client . get_port_details ( port_id ) [ '<STR_LIT:status>' ] <EOL> self . client . delete_port ( port_id ) <EOL> if self [ '<STR_LIT>' ] : <EOL> try : <EOL> self . wait_while ( port_id , status ) <EOL> except ClientError as ce : <EOL> if ce . status not in ( <NUM_LIT> , ) : <EOL> raise <EOL> self . error ( '<STR_LIT>' % port_id ) <EOL> def main ( self , port_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( port_id = port_id ) <EOL> @ command ( port_cmds ) <EOL> class port_modify ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( new_name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> required = [ '<STR_LIT>' , ] <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . port_id <EOL> def _run ( self , port_id ) : <EOL> r = self . client . get_port_details ( port_id ) <EOL> r = self . client . update_port ( <EOL> port_id , r [ '<STR_LIT>' ] , name = self [ '<STR_LIT>' ] ) <EOL> self . print_ ( r , self . print_dict ) <EOL> def main ( self , port_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( port_id = port_id ) <EOL> class _port_create ( _NetworkInit , OptionalOutput , _PortWait ) : <EOL> @ errors . Cyclades . subnet_id <EOL> def _subnet_exists ( self , subnet_id ) : <EOL> self . client . get_subnet_details ( subnet_id ) <EOL> def connect ( self , network_id , device_id ) : <EOL> subnet_id , ip = self [ '<STR_LIT>' ] , self [ '<STR_LIT>' ] <EOL> fixed_ips = [ dict ( ip_address = ip ) ] if ( ip ) else None <EOL> if fixed_ips and subnet_id : <EOL> fixed_ips [ <NUM_LIT:0> ] [ '<STR_LIT>' ] = subnet_id <EOL> try : <EOL> r = self . client . create_port ( <EOL> network_id , device_id , <EOL> name = self [ '<STR_LIT:name>' ] , <EOL> security_groups = self [ '<STR_LIT>' ] , <EOL> fixed_ips = fixed_ips ) <EOL> except ClientError as ce : <EOL> if ce . status in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _network_exists ( network_id = network_id ) <EOL> self . _server_exists ( server_id = device_id ) <EOL> if subnet_id : <EOL> self . _subnet_exists ( subnet_id = subnet_id ) <EOL> if self [ '<STR_LIT>' ] : <EOL> self . _ip_exists ( ip = ip , network_id = network_id , error = ce ) <EOL> raise <EOL> if self [ '<STR_LIT>' ] : <EOL> self . wait_while ( r [ '<STR_LIT:id>' ] , r [ '<STR_LIT:status>' ] ) <EOL> r = self . client . get_port_details ( r [ '<STR_LIT:id>' ] ) <EOL> self . print_ ( [ r ] ) <EOL> @ command ( port_cmds ) <EOL> class port_create ( _port_create ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> security_group_id = RepeatableArgument ( <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> subnet_id = ValueArgument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ip_address = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> network_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> device_id = ValueArgument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> wait = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ) <EOL> required = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> self . connect ( self [ '<STR_LIT>' ] , self [ '<STR_LIT>' ] ) <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ) <EOL> @ command ( port_cmds ) <EOL> class port_wait ( _NetworkInit , _PortWait ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> timeout = IntArgument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , default = <NUM_LIT> ) , <EOL> status = StatusArgument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> valid_states = port_states ) , <EOL> status_w = StatusArgument ( <EOL> '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( port_states ) , '<STR_LIT>' , <EOL> valid_states = port_states ) , <EOL> status_u = StatusArgument ( <EOL> '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( port_states ) , <EOL> '<STR_LIT>' , <EOL> valid_states = port_states ) , <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self , port_id ) : <EOL> r = self . client . get_port_details ( port_id ) <EOL> if self [ '<STR_LIT>' ] : <EOL> if r [ '<STR_LIT:status>' ] . lower ( ) == self [ '<STR_LIT>' ] . lower ( ) : <EOL> self . error ( '<STR_LIT>' % ( port_id , r [ '<STR_LIT:status>' ] ) ) <EOL> else : <EOL> self . wait_until ( <EOL> port_id , self [ '<STR_LIT>' ] , timeout = self [ '<STR_LIT>' ] ) <EOL> else : <EOL> status_w = self [ '<STR_LIT>' ] or self [ '<STR_LIT:status>' ] or '<STR_LIT>' <EOL> if r [ '<STR_LIT:status>' ] . lower ( ) == status_w . lower ( ) : <EOL> self . wait_while ( port_id , status_w , timeout = self [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . error ( '<STR_LIT>' % ( port_id , r [ '<STR_LIT:status>' ] ) ) <EOL> def main ( self , port_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> status_args = [ self [ '<STR_LIT:status>' ] , self [ '<STR_LIT>' ] , self [ '<STR_LIT>' ] ] <EOL> if len ( [ x for x in status_args if x ] ) > <NUM_LIT:1> : <EOL> raise CLIInvalidArgument ( <EOL> '<STR_LIT>' , importance = <NUM_LIT:2> , details = [ <EOL> '<STR_LIT>' % ( <EOL> self . arguments [ '<STR_LIT:status>' ] . lvalue , <EOL> self . arguments [ '<STR_LIT>' ] . lvalue , <EOL> self . arguments [ '<STR_LIT>' ] . lvalue ) ] ) <EOL> if self [ '<STR_LIT:status>' ] : <EOL> self . error ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> self . arguments [ '<STR_LIT:status>' ] . lvalue , <EOL> self . arguments [ '<STR_LIT>' ] . lvalue ) ) <EOL> self . _run ( port_id = port_id ) <EOL> @ command ( ip_cmds ) <EOL> class ip_list ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> self . print_ ( self . client . list_floatingips ( ) ) <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ) <EOL> @ command ( ip_cmds ) <EOL> class ip_info ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . ip_id <EOL> def _run ( self , ip_id ) : <EOL> self . print_ ( self . client . get_floatingip_details ( ip_id ) , self . print_dict ) <EOL> def main ( self , ip_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ip_id = ip_id ) <EOL> @ command ( ip_cmds ) <EOL> class ip_create ( _NetworkInit , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> network_id = ValueArgument ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ip_address = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> project_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self ) : <EOL> try : <EOL> self . print_ ( <EOL> self . client . create_floatingip ( <EOL> self [ '<STR_LIT>' ] , <EOL> floating_ip_address = self [ '<STR_LIT>' ] , <EOL> project_id = self [ '<STR_LIT>' ] ) , <EOL> self . print_dict ) <EOL> except ClientError as ce : <EOL> if ce . status in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> network_id , ip = self [ '<STR_LIT>' ] , self [ '<STR_LIT>' ] <EOL> self . _network_exists ( network_id = network_id ) <EOL> if ip : <EOL> self . _ip_exists ( ip , network_id , ce ) <EOL> if self [ '<STR_LIT>' ] and ce . status in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _project_id_exists ( project_id = self [ '<STR_LIT>' ] ) <EOL> raise <EOL> def main ( self ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ) <EOL> @ command ( ip_cmds ) <EOL> class ip_reassign ( _NetworkInit ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> project_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> required = ( '<STR_LIT>' , ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self , ip ) : <EOL> try : <EOL> self . client . reassign_floating_ip ( ip , self [ '<STR_LIT>' ] ) <EOL> except ClientError as ce : <EOL> if ce . status in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _ip_exists ( ip = ip , network_id = None , error = ce ) <EOL> raise <EOL> def main ( self , IP ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ip = IP ) <EOL> @ command ( ip_cmds ) <EOL> class ip_delete ( _NetworkInit ) : <EOL> """<STR_LIT>""" <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . ip_id <EOL> def _run ( self , ip_id ) : <EOL> self . client . delete_floatingip ( ip_id ) <EOL> def main ( self , ip_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ip_id = ip_id ) <EOL> @ command ( ip_cmds ) <EOL> class ip_attach ( _port_create ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> security_group_id = RepeatableArgument ( <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> subnet_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> wait = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> server_id = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> required = ( '<STR_LIT>' , ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self , ip_or_ip_id ) : <EOL> netid = None <EOL> for ip in self . client . list_floatingips ( ) : <EOL> if ip_or_ip_id in ( ip [ '<STR_LIT>' ] , ip [ '<STR_LIT:id>' ] ) : <EOL> netid = ip [ '<STR_LIT>' ] <EOL> iparg = ValueArgument ( parsed_name = '<STR_LIT>' ) <EOL> iparg . value = ip [ '<STR_LIT>' ] <EOL> self . arguments [ '<STR_LIT>' ] = iparg <EOL> break <EOL> if netid : <EOL> server_id = self [ '<STR_LIT>' ] <EOL> self . error ( '<STR_LIT>' % ( <EOL> ip_or_ip_id , server_id ) ) <EOL> try : <EOL> self . connect ( netid , server_id ) <EOL> except ClientError as ce : <EOL> self . error ( '<STR_LIT>' % ( <EOL> netid , server_id ) ) <EOL> if ce . status in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _server_exists ( server_id = server_id ) <EOL> self . _network_exists ( network_id = netid ) <EOL> raise <EOL> else : <EOL> raiseCLIError ( <EOL> '<STR_LIT>' % ip_or_ip_id , <EOL> details = errors . Cyclades . about_ips ) <EOL> def main ( self , ip_or_ip_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ip_or_ip_id = ip_or_ip_id ) <EOL> @ command ( ip_cmds ) <EOL> class ip_detach ( _NetworkInit , _PortWait , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> wait = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> def _run ( self , ip_or_ip_id ) : <EOL> for ip in self . client . list_floatingips ( ) : <EOL> if ip_or_ip_id in ( ip [ '<STR_LIT>' ] , ip [ '<STR_LIT:id>' ] ) : <EOL> if not ip [ '<STR_LIT>' ] : <EOL> raiseCLIError ( '<STR_LIT>' % ip_or_ip_id ) <EOL> self . error ( '<STR_LIT>' % ip [ '<STR_LIT>' ] ) <EOL> self . client . delete_port ( ip [ '<STR_LIT>' ] ) <EOL> if self [ '<STR_LIT>' ] : <EOL> port_status = self . client . get_port_details ( ip [ '<STR_LIT>' ] ) [ <EOL> '<STR_LIT:status>' ] <EOL> try : <EOL> self . wait_while ( ip [ '<STR_LIT>' ] , port_status ) <EOL> except ClientError as ce : <EOL> if ce . status not in ( <NUM_LIT> , ) : <EOL> raise <EOL> self . error ( '<STR_LIT>' % ip [ '<STR_LIT>' ] ) <EOL> return <EOL> raiseCLIError ( '<STR_LIT>' % ip_or_ip_id ) <EOL> def main ( self , ip_or_ip_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> self . _run ( ip_or_ip_id ) <EOL> @ command ( network_cmds ) <EOL> class network_connect ( _port_create ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> name = ValueArgument ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> security_group_id = RepeatableArgument ( <EOL> '<STR_LIT>' , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> subnet_id = ValueArgument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ip_address = ValueArgument ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> wait = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> device_id = RepeatableArgument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> ) <EOL> required = ( '<STR_LIT>' , ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . network_id <EOL> def _run ( self , network_id , server_id ) : <EOL> self . error ( '<STR_LIT>' % ( <EOL> network_id , server_id ) ) <EOL> try : <EOL> self . connect ( network_id , server_id ) <EOL> except ClientError as ce : <EOL> if ce . status in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> self . _server_exists ( server_id = server_id ) <EOL> raise <EOL> def main ( self , network_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> for sid in self [ '<STR_LIT>' ] : <EOL> self . _run ( network_id = network_id , server_id = sid ) <EOL> @ command ( network_cmds ) <EOL> class network_disconnect ( _NetworkInit , _PortWait , OptionalOutput ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( <EOL> wait = FlagArgument ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> device_id = RepeatableArgument ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> ) <EOL> required = ( '<STR_LIT>' , ) <EOL> @ errors . Cyclades . server_id <EOL> def _get_vm ( self , server_id ) : <EOL> return self . _get_compute_client ( ) . get_server_details ( server_id ) <EOL> @ errors . Generic . all <EOL> @ errors . Cyclades . connection <EOL> @ errors . Cyclades . network_id <EOL> def _run ( self , network_id , server_id ) : <EOL> vm = self . _get_vm ( server_id = server_id ) <EOL> ports = [ port for port in vm [ '<STR_LIT>' ] if ( <EOL> port [ '<STR_LIT>' ] in ( network_id , ) ) ] <EOL> if not ports : <EOL> raiseCLIError ( '<STR_LIT>' % ( <EOL> server_id , network_id ) , importance = <NUM_LIT:2> , details = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' % server_id ] ) <EOL> for port in ports : <EOL> if self [ '<STR_LIT>' ] : <EOL> port [ '<STR_LIT:status>' ] = self . client . get_port_details ( port [ '<STR_LIT:id>' ] ) [ <EOL> '<STR_LIT:status>' ] <EOL> self . client . delete_port ( port [ '<STR_LIT:id>' ] ) <EOL> self . error ( '<STR_LIT>' % ( <EOL> port [ '<STR_LIT:id>' ] , network_id , server_id ) ) <EOL> if self [ '<STR_LIT>' ] : <EOL> try : <EOL> self . wait_while ( port [ '<STR_LIT:id>' ] , port [ '<STR_LIT:status>' ] ) <EOL> except ClientError as ce : <EOL> if ce . status not in ( <NUM_LIT> , ) : <EOL> raise <EOL> self . error ( '<STR_LIT>' % port [ '<STR_LIT:id>' ] ) <EOL> def main ( self , network_id ) : <EOL> super ( self . __class__ , self ) . _run ( ) <EOL> for sid in self [ '<STR_LIT>' ] : <EOL> self . _run ( network_id = network_id , server_id = sid ) </s>
