
    '                     :   S r SSKrSSK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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 rS%S jr S&S jr!S r"S r#S r$S r%S%S jr&S r'S r(S r)S&S jr*S  r+S! r,S" r-S'S# jr.S$ r/g)(z2Common helper methods for Runtime Config commands.    N)encoding)
exceptions)apis)log)
properties)	resources)progress_tracker)retryzread operation timed outi     c                 (    SR                  SU /5      $ )N/projects)join)projects    1lib/googlecloudsdk/api_lib/runtime_config/util.pyProjectPathr   2   s    	:w'	((    c                 <    SR                  [        U 5      SU/5      $ )Nr   configs)r   r   )r   configs     r   
ConfigPathr   6   s    	;w'F;	<<r   c                 Z    SR                  [        X5      SUR                  S5      /5      $ )Nr   	variables)r   r   lstrip)r   r   variables      r   VariablePathr   :   s-    	:g.??3') 
* *r   c                 <    SR                  [        X5      SU/5      $ )Nr   waiters)r   r   )r   r   waiters      r   
WaiterPathr    ?   s    	:g.	6B	CCr   c                 P    S[         0n[        R                  R                  U SUS9$ )z{Parse a config name or URL, and return a resource.

Args:
  config_name: The config name.

Returns:
  The parsed resource.

projectsIdzruntimeconfig.projects.configs
collectionparams)Projectr   REGISTRYParse)config_namer%   s     r   ParseConfigNamer*   D   s8     G& 
			!	!+-M)/ 
" 
1 1r   c                 \   ^ U4S jU4S jS.n[         R                  R                  U SUS9$ )zParse a variable name or URL, and return a resource.

Args:
  variable_name: The variable name.
  args: CLI arguments, possibly containing a config name.

Returns:
  The parsed resource.
c                  @   > [        [        T 5      5      R                  $ Nr*   
ConfigNamer"   argss   r   <lambda>#ParseVariableName.<locals>.<lambda>e       OJt,<=HHr   c                  @   > [        [        T 5      5      R                  $ r-   r*   r/   	configsIdr0   s   r   r2   r3   f       ?:d+;<FFr   r"   r7   z(runtimeconfig.projects.configs.variablesr#   r   r'   r(   )variable_namer1   r%   s    ` r   ParseVariableNamer<   U   s<      IF&
 
			!	!; 
" 
 r   c                 \   ^ U4S jU4S jS.n[         R                  R                  U SUS9$ )zParse a waiter name or URL, and return a resource.

Args:
  waiter_name: The waiter name.
  args: CLI arguments, possibly containing a config name.

Returns:
  The parsed resource.
c                  @   > [        [        T 5      5      R                  $ r-   r.   r0   s   r   r2   !ParseWaiterName.<locals>.<lambda>z   r4   r   c                  @   > [        [        T 5      5      R                  $ r-   r6   r0   s   r   r2   r?   {   r8   r   r9   z&runtimeconfig.projects.configs.waitersr#   r:   )waiter_namer1   r%   s    ` r   ParseWaiterNamerB   o   s<     IF&
 
			!	!9 
" 
 r   c                 |    U(       a)  [        U SS 5      (       d  [        R                  " SS5      e[        U SS 5      $ )Nr)   r   z$--config-name parameter is required.)getattrsdk_exceptionsRequiredArgumentException)r1   requireds     r   r/   r/      s=    gdM488

2
28: : 
}d	++r   c                 l    [         R                  " SS5      nU b  XR                  l        Ub  Xl        U$ Nruntimeconfigv1beta1)r   GetClientInstancehttptimeoutnum_retries)rN   rO   clients      r   ClientrQ      s5    !!/9=&!KK$	-r   c                  ,    [        S0 U D6R                  $ N )rQ   projects_configskwargss    r   ConfigClientrX      s    		&		*	**r   c                  ,    [        S0 U D6R                  $ rS   )rQ   projects_configs_variablesrV   s    r   VariableClientr[      s    		&		4	44r   c                  ,    [        S0 U D6R                  $ rS   )rQ   projects_configs_waitersrV   s    r   WaiterClientr^      s    		&		2	22r   c                  0    [         R                  " SS5      $ rI   )r   GetMessagesModulerT   r   r   Messagesra      s    				;;r   c                 d    [         R                  R                  R                  R	                  U S9$ )NrG   )r   VALUEScorer   Getrc   s    r   r&   r&      s(    						'	'	+	+X	+	>>r   c                 "    [        U SS 5      S:H  $ )Nstatus_codei  rD   errors    r   IsBadGatewayErrorrl          	t	,	33r   c                 "    [        U SS 5      S:H  $ )Nrh   i  ri   rj   s    r   IsDeadlineExceededErrorro      rm   r   c                 ~    [        U [        R                  5      =(       d    [        [        R
                  " U 5      ;   $ r-   )
isinstancesocketrN   TIMEOUT_ERR_TEXTsix	text_typerj   s    r   IsSocketTimeoutrv      s-    
 UFNN
+ 6u!557r   c                 j   Ub  UO[         nUb  UO[        n[        5       n[        R                  " US-  S9nUR
                  R                  R                  U R                  5       S9n[        R                  " SR                  U R                  5       5      5          UR                  UR                  U/US-  S S9n SSS5        WR$                  b  UR$                  R&                  b5  S	R                  U R                  5       UR$                  R&                  5      nOS
R                  U R                  5       5      n[(        R$                  " U5        U$ ! [        R                   a5    [         R"                  " SR                  U R                  5       U5      5      ef = f! , (       d  f       N= f)az  Wait for a waiter to finish.

Args:
  waiter_resource: The waiter resource to wait for.
  sleep: The number of seconds to sleep between status checks.
  max_wait: The maximum number of seconds to wait before an error is raised.

Returns:
  The last retrieved value of the Waiter.

Raises:
  WaitTimeoutError: If the wait operation takes longer than the maximum wait
      time.
Ni  )max_wait_msnamez"Waiting for waiter [{0}] to finishc                 $    U R                   (       + $ r-   )done)wss     r   r2   WaitForWaiter.<locals>.<lambda>   s
    aff*r   )r1   sleep_msshould_retry_ifz/Waiter [{0}] did not finish within {1} seconds.z(Waiter [{0}] finished with an error: {1}z$Waiter [{0}] finished with an error.)DEFAULT_WAITER_SLEEPMAX_WAITER_TIMEOUTr^   r
   RetryerrP   MESSAGES_MODULE-RuntimeconfigProjectsConfigsWaitersGetRequestRelativeNamer	   ProgressTrackerformatNameRetryOnResultrf   WaitExceptionrtc_exceptionsWaitTimeoutErrorrk   messager   )waiter_resourcesleepmax_waitwaiter_clientretryerrequestresultr   s           r   WaitForWaiterr      s    $%*>%!-X3E(.-MMho6'!!11;;%224 < 6 
 ''*11/2F2F2HIK1$$]%6%6+2).3dl5L % NfK \\||':AA



 &,,"6"68g 7==



 "gIIg	-  1++
;
B
B""$h01 11K Ks   F$!EA	F!!F$$
F2c                     U R                   SL$ )z0Returns True if the specified waiter has failed.Nrj   )r   s    r   IsFailedWaiterr      s    	T	!!r   c                     [         R                  " U 5      nSU;   a'  SU;   a  [        S5      eUS   US'   U" US   5      US'   U$ )a/  Returns a dict representation of the message with a shortened name value.

This method does three things:
1. converts message to a dict.
2. shortens the value of the name field using name_converter
3. sets atomicName to the original value of name.

Args:
  message: A protorpclite message.
  name_converter: A function that takes an atomic name as a parameter and
      returns a shortened name.

Returns:
  A dict representation of the message with a shortened name field.

Raises:
  ValueError: If the original message already contains an atomicName field.
rz   
atomicNamez4Original message cannot contain an atomicName field.)r   MessageToDict
ValueError)r   name_convertermessage_dicts      r   _DictWithShortNamer      s[    & ''0, ||#MNN!-f!5L),v*>?L	r   c                     [        U S 5      $ )z;Returns the config message as a dict with a shortened name.c                 *    U R                  S5      S   $ Nr   splitry   s    r   r2   FormatConfig.<locals>.<lambda>      $**S/"2Er   r   r   s    r   FormatConfigr         
 
G%E	FFr   c                     [        U S 5      nU(       a,  SU;   a
  US   US'   U$ [        R                  " US   5      US'   U$ )a   Returns the variable message as a dict with a shortened name.

This method first converts the variable message to a dict with a shortened
name and an atomicName. Then, decodes the variable value in the dict if the
output_value flag is True.

Args:
  message: A protorpclite message.
  output_value: A bool flag indicates whether we want to decode and output the
      values of the variables. The default value of this flag is False.

Returns:
  A dict representation of the message with a shortened name field.
c                 P    SR                  U R                  S5      [        S  5      $ )Nr   )r   r   VARIABLE_NAME_PREFIX_LENGTHry   s    r   r2    FormatVariable.<locals>.<lambda>.  s    388DJJsO,G,HIJr   textvalue)r   base64	b64decode)r   output_valuer   s      r   FormatVariabler     s]    $ $JL,  *62l7 
 %..|G/DEl7	r   c                     [        U S 5      $ )z;Returns the waiter message as a dict with a shortened name.c                 *    U R                  S5      S   $ r   r   ry   s    r   r2   FormatWaiter.<locals>.<lambda>@  r   r   r   r   s    r   FormatWaiterr   ;  r   r   )T)NN)F)0__doc__r   rr   apitools.base.pyr   %googlecloudsdk.api_lib.runtime_configr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioperE   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler	   googlecloudsdk.core.utilr
   rt   rs   r   r   r   r   r   r   r    r*   r<   rB   r/   rQ   rX   r[   r^   ra   r&   rl   ro   rv   r   r   r   r   r   r   rT   r   r   <module>r      s     9   % N , @ # * ) 8 * 
 .  "      )=*
D
1"4*,+53<?447-`"
BGBGr   