
    h1                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r  SSKJr  SS	KrS
rSrSrSrSrSrSrSrSrSr " S S\R2                  5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S  S!\5      r S" r!S# r"S$ r#S% r$S& r%S' r&S( r'S) r(S* r)S+ r*S, r+S- r,g	).z8Code that's shared between multiple routers subcommands.    )absolute_import)division)unicode_literals)routers_utils)parser_errors)
exceptions)
console_ioNzWARNING: switching from custom advertisement mode to default will clear out any existing advertised groups/ranges from this {resource}.zS--add/remove-advertisement flags are not compatible with --set-advertisement flags.zHCannot specify custom advertisements for a {resource} with default mode.z6Advertised group {group} not found on this {resource}.z<Advertised IP range {ip_range} not found on this {resource}.zNCustom Learned Route IP address range {ip_range} not found on this {resource}.z4--ip-address and --mask-length must be set together.z-keyz-key-?   c                       \ rS rSrSrSrg)RouterError=   z7Error superclass for all router surface-related errors. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       >lib/googlecloudsdk/command_lib/compute/routers/router_utils.pyr   r   =   s    ?r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )PeerNotFoundErrorA   z<Raised when a peer is specified but not found in the router.c                 Z   > Xl         SR                  U5      n[        [        U ]  U5        g )Nzpeer `{0}` not found)nameformatsuperr   __init__selfr   msg	__class__s      r   r   PeerNotFoundError.__init__D   s(    I
 
'
'
-C	
T+C0r   r   r   r   r   r   r   r   r   __classcell__r"   s   @r   r   r   A   s    D1 1r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )InterfaceNotFoundErrorJ   zBRaised when an interface is specified but not found in the router.c                 Z   > Xl         SR                  U5      n[        [        U ]  U5        g )Nzinterface `{0}` not found)r   r   r   r)   r   r   s      r   r   InterfaceNotFoundError.__init__M   s(    I
%
,
,T
2C	
 $05r   r$   r%   r'   s   @r   r)   r)   J   s    J6 6r   r)   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )"RequireIpAddressAndMaskLengthErrorS   zERaised when ip-address or mask-length is specified without the other.c                 8   > [         n[        [        U ]  U5        g )N)1_REQUIRE_IP_ADDRESS_AND_MASK_LENGTH_ERROR_MESSAGEr   r.   r   )r    r!   r"   s     r   r   +RequireIpAddressAndMaskLengthError.__init__V   s    
;C	
,d<SAr   r   r%   r'   s   @r   r.   r.   S   s    MB Br   r.   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )CustomWithDefaultError[   zBRaised when custom advertisements are specified with default mode.c                 h   > [        X5      n[        R                  US9n[        [        U ]  U5        g )Nresource)_GetResourceClassStr"_CUSTOM_WITH_DEFAULT_ERROR_MESSAGEr   r   r4   r   )r    messagesresource_classresource_str	error_msgr"   s        r   r   CustomWithDefaultError.__init__^   s0    'AL299<9PI	
 $0;r   r   r%   r'   s   @r   r4   r4   [   s    J< <r   r4   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )GroupNotFoundErrord   z;Raised when an advertised group is not found in a resource.c                 h   > [        X5      n[        R                  X4S9n[        [        U ]  U5        g )N)groupr8   )r9   _GROUP_NOT_FOUND_ERROR_MESSAGEr   r   rA   r   )r    r;   r<   rD   r=   r>   r"   s         r   r   GroupNotFoundError.__init__g   s5    'AL.55 6 ,I	
d,Y7r   r   r%   r'   s   @r   rA   rA   d   s    C8 8r   rA   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )IpRangeNotFoundErrorn   z3Raised when an ip range is not found in a resource.c                 `   > [        X5      nUR                  XES9n[        [        U ]  U5        g)aF  Initializes the instance adapting the error message provided.

Args:
  messages: API messages holder.
  resource_class: The class of the resource where the ip range is not found.
  error_message: The error message to be formatted with resource_class and
    ip_range.
  ip_range: The ip range that is not found in a resource.
)ip_ranger8   N)r9   r   r   rH   r   )r    r;   r<   error_messagerK   r=   r>   r"   s          r   r   IpRangeNotFoundError.__init__q   s2     (AL$$h$NI	
.y9r   r   r%   r'   s   @r   rH   rH   n   s    ;: :r   rH   c                 r    XR                   L a  gXR                  L a  g[        SR                  U5      5      e)Nrouterpeerz!Invalid resource_class value: {0})	RouterBgpRouterBgpPeer
ValueErrorr   )r;   r<   s     r   r9   r9      s7    )))///
8??O
PPr   c                 z    [        U 5      (       a+  [        U 5      (       a  [        R                  " [        5      egg)zHChecks for incompatible flags in arguments and raises an error if found.N)HasReplaceAdvertisementFlags HasIncrementalAdvertisementFlagsr   ArgumentError-_INCOMPATIBLE_INCREMENTAL_FLAGS_ERROR_MESSAGEargss    r   CheckIncompatibleFlagsOrRaiser[      s8    "4((&t,,

%
%57 7 - )r   c                 n    U R                   =(       d#    U R                  SL=(       d    U R                  SL$ )z?Returns whether replace-style flags are specified in arguments.N)advertisement_modeset_advertisement_groupsset_advertisement_rangesrY   s    r   rU   rU      s6    

!
! 4

'
't
34

'
't
35r   c                     U R                   =(       d2    U R                  =(       d    U R                  =(       d    U R                  $ )zCReturns whether incremental-style flags are specified in arguments.)add_advertisement_groupsremove_advertisement_groupsadd_advertisement_rangesremove_advertisement_rangesrY   s    r   rV   rV      s=    

'
' L4+K+K L

'
'L+/+K+KMr   c                    SnUR                   b   [        R                  " XR                   5      nSnUR                  b!  [        R                  " UUR                  5      nSnUR
                  b!  [        R                  " U UR
                  5      nUb6  X1R                  R                  L a  U(       d  U(       a  [        X5      eU/ / 4$ X4U4$ )a  Parses and validates a completed advertisement configuration from flags.

Args:
  messages: API messages holder.
  resource_class: RouterBgp or RouterBgpPeer class type to parse for.
  args: Flag arguments to generate configuration from.

Returns:
  The validated tuple of mode, groups and prefixes.  If mode is DEFAULT,
  validates that no custom advertisements were specified and returns empty
  lists for each.

Raises:
  CustomWithDefaultError: If custom advertisements were specified at the same
  time as DEFAULT mode.
N)
r]   r   	ParseModer^   ParseGroupsr_   ParseIpRangesAdvertiseModeValueValuesEnumDEFAULTr4   )r;   r<   rZ   modegroupsprefixess         r   ParseAdvertisementsrn      s    $ 
$	("">3J3JKD&	"".&&~'+'D'DFF(	"".**8+/+H+HJH 
99AAA"8<< 2r\!!r   c                 `    UR                   UR                  R                  La  [        X5      eg)z.Validate that a router/peer is in custom mode.N)advertiseModeri   CUSTOMr4   )r;   r<   r8   s      r   ValidateCustomModerr      s3     88??@
 
::@r   c                     Ubh  X!R                   R                  L aO  UbK  X1R                   R                  L a2  [        X5      n[        R
                  " [        R                  US9SS9  ggggg)z3If necessary, prompts the user for switching modes.Nr7   T)messagecancel_on_no)ri   rq   rj   r9   r	   PromptContinue_MODE_SWITCH_MESSAGEr   )r;   r<   existing_modenew_moder=   s        r   PromptIfSwitchToDefaultModerz      sv     BBIII==EEE'AL$++\+B F  J  r   c                 f    U R                    H  nUR                  U:X  d  M  Us  $    [        U5      e)a5  Searches for and returns a BGP peer from within a router resource.

Args:
  resource: The router resource to find the peer for.
  peer_name: The name of the peer to find.

Returns:
  A reference to the specified peer, if found.

Raises:
  PeerNotFoundError: If the specified peer was not found in the router.
)bgpPeersr   r   )r8   	peer_namerP   s      r   FindBgpPeerOrRaiser~      s2     dyyIk   	)$$r   c                     U H  nXBR                   ;  d  M  [        XU5      e   UR                    Vs/ s H  oUU;  d  M
  UPM     snUl         gs  snf )a  Remove all specified groups from a resource's advertisements.

Raises an error if any of the specified advertised groups were not found in
the resource's advertisement set.

Args:
  messages: API messages holder.
  resource_class: RouterBgp or RouterBgpPeer class type being modified.
  resource: the resource (router/peer) being modified.
  groups: the advertised groups to remove.

Raises:
  GroupNotFoundError: if any group was not found in the resource.
N)advertisedGroupsrA   )r;   r<   r8   rl   rD   gs         r   RemoveGroupsFromAdvertisementsr      sU      e---x??  ***Avoa*( s   	A Ac                    U H>  nXBR                    Vs/ s H  oUR                  PM     sn;  d  M.  [        U U[        U5      e   UR                    Vs/ s H  oUR                  U;  d  M  UPM     snUl         gs  snf s  snf )a%  Removes all specified IP ranges from a resource's advertisements.

Raises an error if any of the specified advertised IP ranges were not found in
the resource's advertisement set. The IP range search is done by exact text
match (ignoring descriptions).

Args:
  messages: API messages holder.
  resource_class: RouterBgp or RouterBgpPeer class type being modified.
  resource: the resource (router/peer) being modified.
  ip_ranges: the advertised IP ranges to remove.

Raises:
  IpRangeNotFoundError: if any IP range was not found in the resource.
N)advertisedIpRangesrangerH   ,_ADVERTISED_IP_RANGE_NOT_FOUND_ERROR_MESSAGE)r;   r<   r8   	ip_rangesrK   rs         r    RemoveIpRangesFromAdvertisementsr     s    " h)D)DE)DA)DEE 


6
	   ,,!,Ay0Ha,!( F!s   A9A>*A>c                    U HH  nX1R                    Vs/ s H  oDR                  PM     sn;  d  M.  [        U U R                  [        U5      e   UR                    Vs/ s H  oDR                  U;  d  M  UPM     snUl         gs  snf s  snf )a  Removes all specified IP address ranges from a peer's custom learned routes.

Raises an error if any of the specified custom learned route IP address ranges
were not found in the peer's IP ranges set. The IP address range search is
done by exact text match.

Args:
  messages: API messages holder.
  peer: the peer being modified.
  ip_ranges: the custom learned route IP address ranges to remove.

Raises:
  IpRangeNotFoundError: if any IP address range was not found in the peer.
N)customLearnedIpRangesr   rH   rR   6_CUSTOM_LEARNED_ROUTE_IP_RANGE_NOT_FOUND_ERROR_MESSAGE)r;   rP   r   rK   r   s        r   %RemoveIpRangesFromCustomLearnedRoutesr   &  s     h)C)CD)CA)CDD 


 
 
@
	   ++ +Awwi/Ga+ $ E s   BB4Bc                    [        5       nU R                   H-  nUR                  c  M  UR                  UR                  5        M/     [        [        [        5      -
  nUR                  SU [        -   nSnXR;   ao  [        [        [        5      -
  [        [        R                  " U5      5      -
  nUR                  SU [        -   [        R                  " U5      -   nUS-  nXR;   a  Mo  U$ )zGenerates an MD5 authentication key name for the BGP peer.

Args:
  router_message: the Cloud Router that contains the relevant BGP peer.
  args: contains arguments passed to the command

Returns:
  Generated MD5 authentication key name
N      )setr|   md5AuthenticationKeyNameadd%_MAX_LENGTH_OF_MD5_AUTHENTICATION_KEYlen_MD5_AUTHENTICATION_KEY_SUFFIXr}   !_MD5_AUTHENTICATION_KEY_SUBSTRINGsix	text_type)router_messagerZ   md5_authentication_key_namesbgp_peersubstrings_max_lengthmd5_authentication_key_name"md5_authentication_key_name_suffixs          r    GenerateMd5AuthenticationKeyNamer   B  s     "% ))h((4"&&x'H'HI * @#$C& & nn++,/MM '($#C--.	/CMM<=>	? 
 	--.)	*89	:   '!+& 	$C 
%$r   )-r   
__future__r   r   r   googlecloudsdk.api_lib.computer   googlecloudsdk.callioper   googlecloudsdk.corer   core_exceptionsgooglecloudsdk.core.consoler	   r   rw   rX   r:   rE   r   r   r1   r   r   r   Errorr   r   r)   r.   r4   rA   rH   r9   r[   rU   rV   rn   rr   rz   r~   r   r   r   r   r   r   r   <module>r      s   ? &  ' 8 1 = 2 
F 
! .
 O # =  C -
 7 ; 2 "( $+ !(* %@/'' @1 16[ 6B B<[ <8 8:; :$Q75M&"R;%&0<8%r   