
    (                     T   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z ddlmZ ddlm
Z dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlZdZdZdZdZd Zd Zd Zd Z d Z!d Z"d Z#d(dZ$d)dZ%d Z&d Z'd Z(d Z)d(dZ*d Z+d  Z,d! Z-d)d"Z.d# Z/d$ Z0d% Z1d*d&Z2d' Z3y)+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                 (    dj                  d| g      S )N/projects)join)projects    1lib/googlecloudsdk/api_lib/runtime_config/util.pyProjectPathr   5   s    	:w'	((    c                 <    dj                  t        |       d|g      S )Nr   configs)r   r   )r   configs     r   
ConfigPathr   9   s    	;w'F;	<<r   c                 \    dj                  t        | |      d|j                  d      g      S )Nr   	variables)r   r   lstrip)r   r   variables      r   VariablePathr   =   s/    	:gv.??3') 
* *r   c                 >    dj                  t        | |      d|g      S )Nr   waiters)r   r   )r   r   waiters      r   
WaiterPathr#   B   s    	:gv.	6B	CCr   c                 V    dt         i}t        j                  j                  | d|      S )zParse 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                 ^    fdfdd}t         j                  j                  | d|      S )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        t                     j                  S Nr-   
ConfigNamer%   argss   r   <lambda>z#ParseVariableName.<locals>.<lambda>h       OJt,<=HHr   c                  @    t        t                     j                  S r0   r-   r2   	configsIdr3   s   r   r5   z#ParseVariableName.<locals>.<lambda>i       ?:d+;<FFr   r%   r9   z(runtimeconfig.projects.configs.variablesr&   r   r*   r+   )variable_namer4   r(   s    ` r   ParseVariableNamer>   X   s<      IF&
 
			!	!; 
" 
 r   c                 ^    fdfdd}t         j                  j                  | d|      S )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        t                     j                  S r0   r1   r3   s   r   r5   z!ParseWaiterName.<locals>.<lambda>}   r6   r   c                  @    t        t                     j                  S r0   r8   r3   s   r   r5   z!ParseWaiterName.<locals>.<lambda>~   r:   r   r;   z&runtimeconfig.projects.configs.waitersr&   r<   )waiter_namer4   r(   s    ` r   ParseWaiterNamerC   r   s<     IF&
 
			!	!9 
" 
 r   c                 f    |r#t        | dd       st        j                  dd      t        | dd       S )Nr,   r   z$--config-name parameter is required.)getattrsdk_exceptionsRequiredArgumentException)r4   requireds     r   r2   r2      s:    gdM48

2
28: : 
}d	++r   c                 j    t        j                  dd      }| | |j                  _        |||_        |S Nruntimeconfigv1beta1)r   GetClientInstancehttptimeoutnum_retries)rO   rP   clients      r   ClientrR      s9    !!/9=&!FKK$F	-r   c                  ,    t        di | j                  S N )rR   projects_configskwargss    r   ConfigClientrY      s    		&		*	**r   c                  ,    t        di | j                  S rT   )rR   projects_configs_variablesrW   s    r   VariableClientr\      s    		&		4	44r   c                  ,    t        di | j                  S rT   )rR   projects_configs_waitersrW   s    r   WaiterClientr_      s    		&		2	22r   c                  .    t        j                  dd      S rJ   )r   GetMessagesModulerU   r   r   Messagesrb      s    				;;r   c                 j    t         j                  j                  j                  j	                  |       S )NrH   )r
   VALUEScorer   Getrd   s    r   r)   r)      s(    						'	'	+	+X	+	>>r   c                 "    t        | dd       dk(  S )Nstatus_codei  rE   errors    r   IsBadGatewayErrorrm          	t	,	33r   c                 "    t        | dd       dk(  S )Nri   i  rj   rk   s    r   IsDeadlineExceededErrorrp      rn   r   c                 p    t        | t        j                        xs t        t	        j
                  |       v S r0   )
isinstancesocketrO   TIMEOUT_ERR_TEXTsix	text_typerk   s    r   IsSocketTimeoutrw      s-    
 UFNN
+ 6u!557r   c                 `   ||nt         }||nt        }t               }t        j                  |dz        }|j
                  j                  j                  | j                               }t        j                  dj                  | j                                     5  	 |j                  |j                  |g|dz  d       }	 ddd       j$                  |j$                  j&                  5d	j                  | j                         |j$                  j&                        }nd
j                  | j                               }t)        j$                  |       |S # t        j                  $ r4 t!        j"                  dj                  | j                         |            w xY w# 1 sw Y   xY w)a  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                     | j                    S r0   )done)wss     r   r5   z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_TIMEOUTr_   r   RetryerrQ   MESSAGES_MODULE-RuntimeconfigProjectsConfigsWaitersGetRequestRelativeNamer   ProgressTrackerformatNameRetryOnResultrg   WaitExceptionrtc_exceptionsWaitTimeoutErrorrl   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$$F-c                     | j                   duS )z0Returns True if the specified waiter has failed.Nrk   )r"   s    r   IsFailedWaiterr      s    	T	!!r   c                     t        j                  |       }d|v r%d|v rt        d      |d   |d<    ||d         |d<   |S )aI  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                     t        | d       S )z;Returns the config message as a dict with a shortened name.c                 *    | j                  d      d   S Nr   splitrz   s    r   r5   zFormatConfig.<locals>.<lambda>      $**S/"2Er   r   r   s    r   FormatConfigr         
 
G%E	FFr   c                 v    t        | d       }|r)d|v r
|d   |d<   |S t        j                  |d         |d<   |S )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    dj                  | j                  d      t        d        S )Nr   )r   r   VARIABLE_NAME_PREFIX_LENGTHrz   s    r   r5   z 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                     t        | d       S )z;Returns the waiter message as a dict with a shortened name.c                 *    | j                  d      d   S r   r   rz   s    r   r5   zFormatWaiter.<locals>.<lambda>C  r   r   r   r   s    r   FormatWaiterr   >  r   r   )T)NN)F)4__doc__
__future__r   r   r   r   rs   apitools.base.pyr   %googlecloudsdk.api_lib.runtime_configr   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioperF   googlecloudsdk.corer	   r
   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   ru   rt   r   r   r   r   r   r   r#   r-   r>   rC   r2   rR   rY   r\   r_   rb   r)   rm   rp   rw   r   r   r   r   r   r   rU   r   r   <module>r      s     9 &  '   % N , @ # * ) 8 * 
 .  "      )=*
D
1"4*,+53<?447-`"
BGBGr   