
    (                     ^   S r SSKJr  SSKJr  SSKJ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$ r0S% r1S*S& jr2S' r3g)+z2Common helper methods for Runtime Config commands.    )absolute_import)division)unicode_literalsN)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   5   s    	:w'	((    c                 <    SR                  [        U 5      SU/5      $ )Nr   configs)r   r   )r   configs     r   
ConfigPathr   9   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#   B   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-   G   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>h       OJt,<=HHr   c                  @   > [        [        T 5      5      R                  $ r0   r-   r2   	configsIdr3   s   r   r5   r6   i       ?:d+;<FFr   r%   r:   z(runtimeconfig.projects.configs.variablesr&   r   r*   r+   )variable_namer4   r(   s    ` r   ParseVariableNamer?   X   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                  $ r0   r1   r3   s   r   r5   !ParseWaiterName.<locals>.<lambda>}   r7   r   c                  @   > [        [        T 5      5      R                  $ r0   r9   r3   s   r   r5   rB   ~   r;   r   r<   z&runtimeconfig.projects.configs.waitersr&   r=   )waiter_namer4   r(   s    ` r   ParseWaiterNamerE   r   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)r4   requireds     r   r2   r2      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)rQ   rR   clients      r   ClientrT      s5    !!/9=&!KK$	-r   c                  ,    [        S0 U D6R                  $ N )rT   projects_configskwargss    r   ConfigClientr[      s    		&		*	**r   c                  ,    [        S0 U D6R                  $ rV   )rT   projects_configs_variablesrY   s    r   VariableClientr^      s    		&		4	44r   c                  ,    [        S0 U D6R                  $ rV   )rT   projects_configs_waitersrY   s    r   WaiterClientra      s    		&		2	22r   c                  0    [         R                  " SS5      $ rL   )r   GetMessagesModulerW   r   r   Messagesrd      s    				;;r   c                 d    [         R                  R                  R                  R	                  U S9$ )NrJ   )r
   VALUEScorer   Getrf   s    r   r)   r)      s(    						'	'	+	+X	+	>>r   c                 "    [        U SS 5      S:H  $ )Nstatus_codei  rG   errors    r   IsBadGatewayErrorro          	t	,	33r   c                 "    [        U SS 5      S:H  $ )Nrk   i  rl   rm   s    r   IsDeadlineExceededErrorrr      rp   r   c                 ~    [        U [        R                  5      =(       d    [        [        R
                  " U 5      ;   $ r0   )
isinstancesocketrQ   TIMEOUT_ERR_TEXTsix	text_typerm   s    r   IsSocketTimeoutry      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                   (       + $ r0   )done)wss     r   r5   WaitForWaiter.<locals>.<lambda>   s
    aff*r   )r4   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_TIMEOUTra   r   RetryerrS   MESSAGES_MODULE-RuntimeconfigProjectsConfigsWaitersGetRequestRelativeNamer   ProgressTrackerformatNameRetryOnResultri   WaitExceptionrtc_exceptionsWaitTimeoutErrorrn   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.Nrm   )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.
r}   
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   splitr|   s    r   r5   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_LENGTHr|   s    r   r5    FormatVariable.<locals>.<lambda>1  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   r|   s    r   r5   FormatWaiter.<locals>.<lambda>C  r   r   r   r   s    r   FormatWaiterr   >  r   r   )T)NN)F)4__doc__
__future__r   r   r   r   ru   apitools.base.pyr   %googlecloudsdk.api_lib.runtime_configr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioperH   googlecloudsdk.corer	   r
   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   rw   rv   r   r   r   r   r   r   r#   r-   r?   rE   r2   rT   r[   r^   ra   rd   r)   ro   rr   ry   r   r   r   r   r   r   rW   r   r   <module>r      s     9 &  '   % N , @ # * ) 8 * 
 .  "      )=*
D
1"4*,+53<?447-`"
BGBGr   