
    #                        d 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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mZ ddlmZ ddlmZ ddlmZ ddl m!Z! ejD                   ejF                  ejH                  jJ                  ejH                  jL                         G d dejN                                      Z(y)z;Command for updating env vars and other configuration info.    )absolute_import)division)print_function)unicode_literals)global_methodsworker_pool)apis)messages)arg_parsers)base)util)config_changes)connection_context)
exceptions)flags)messages_util)pretty_print)serverless_operations)stages)config)
properties)	resources)progress_trackerc                   R    e Zd ZdZdddZed        Zed        Zd Zd Z	d	 Z
d
 Zy)ReplacezFCreate or replace a worker-pool from a YAML worker-pool specification.z]          Creates or replaces a worker-pool from a YAML worker-pool specification.
          z          To replace the specification for a worker-pool defined in my-worker-pool.yaml

              $ {command} my-worker-pool.yaml

         )DESCRIPTIONEXAMPLESc                    t        j                  |       t        j                  |       t        j                  |       |j	                  ddt        j                         d       |j                  j                  d       y )NFILEstorezsThe absolute path to the YAML file with a Cloud Run worker-pool definition for the worker-pool to update or create.)actiontypehelpnone)	r   AddAsyncFlagAddClientNameAndVersionFlagsAddDryRunFlagadd_argumentr   YAMLFileContentsdisplay_info	AddFormatclsparsers     'lib/surface/run/worker_pools/replace.py
CommonArgszReplace.CommonArgs<   so     
v	&&v.	
))+B   !!&)    c                 &    | j                  |       y )N)r1   r-   s     r0   ArgszReplace.ArgsO   s    NN6r2   c                     t        j                  |t        j                  j                  | j                         |      S )N)region_label)r   GetConnectionContextr   ProductRUNReleaseTrack)selfargsr6   s      r0   _ConnectionContextzReplace._ConnectionContextS   s1    22emm!2!2!4< r2   c                 <   |j                  d      xs |j                  d      }t        j                  |      t        j                  | j	                               t        j
                  |r|j                  nd|r|j                  nt        j                  d      gS )Nclient_nameclient_versiongcloudT)set_on_template)
IsSpecifiedr   ReplaceWorkerPoolChangeSetLaunchStageAnnotationChanger:   'SetClientNameAndVersionAnnotationChanger?   r@   r   CLOUD_SDK_VERSION)r;   new_worker_poolr<   is_either_specifieds       r0   _GetBaseChangeszReplace._GetBaseChangesX   s     **=9 T=M=M> 	..?55d6G6G6IJ>> 3D" )) 	

 
r2   c                 >   |j                   r0t        j                  dj                  |j                               y |r0t        j                  dj                  |j                               y t        j                  dj                  |j                               y )NzWNew configuration for [{{bold}}{worker_pool}{{reset}}] is being applied asynchronously.r   zVNew configuration has been validated for worker pool [{{bold}}{worker_pool}{{reset}}].zSNew configuration has been applied to worker pool [{{bold}}{worker_pool}{{reset}}].)async_r   Successformatname)r;   worker_pool_objdry_runr<   s       r0   _PrintSuccessMessagezReplace._PrintSuccessMessagej   s    {{%%+V8L8LV%M 
..4f).. /5 / ..4f).. /5 /r2   c           	      2   t        j                  t        j                  t        j                        }t        |j                        }d|v r|d= d|vsd|d   vrt        j                  d      d|d   d   vri |d   d   d<   |j                  di       j                  dd      }|!t        |t              st        |      |d   d<   d}	 t        j                  ||j                        }t        j                  ||      }t$        j&                  j(                  j*                  j-                         }|j.                  j0                  e|}t3        j4                  |      }	|j.                  j0                  }||k7  r3|t        |	      k7  r%t        j                  d
j7                  ||	            ||j.                  _        | j9                  ||      }
t:        j<                  j?                  |j.                  j@                  d|j.                  j0                  id      }|jB                  r|jD                  nd}| jG                  ||      }tI        |d      r|jJ                  nd}|rdnd}tM        jN                  |      5 }|jQ                  |      }tS        jT                  tW        jX                  |||d             t[        j\                         }|rdnd}|rd}t_        j`                  ||d|jb                  xs |      5 }|je                  ||
||jb                  |      }ddd       | jg                  |||       |cddd       S # t        j                   $ r"}t        j"                  |d	       Y d}~wd}~ww xY w# 1 sw Y   _xY w# 1 sw Y   yxY w)z#Create or Update service from YAML.statusspectemplatezRspec.template is required but missing. Please check the content in your yaml file.metadata	namespaceNzPlease make sure that the YAML file matches the Cloud Run worker pool definition spec in https://cloud.google.com/run/docs/reference/rest/v1/namespaces.workerpools#WorkerPool)	help_textzVNamespace must be project ID [{}] or quoted number [{}] for Cloud Run (fully managed).namespacesIdzrun.namespaces.workerpools)params
collectionrQ   Fz Validating new configuration forzApplying new configuration to
workerpool)	operationresource_kind_lowerzDeploying...zDeploying new worker pool...zValidating...zDeployment failed)failure_messagesuppress_output)asynrQ   )4r
   GetMessagesModuler   SERVERLESS_API_NAMESERVERLESS_API_VERSIONdictr    r   ConfigurationErrorget
isinstancestrr   DictToMessageWithErrorCheck
WorkerPoolr	   ScalarTypeMismatchErrorMaybeRaiseCustomFieldMismatchr   VALUEScoreprojectGetrW   rX   projects_utilGetProjectNumberrN   rJ   r   REGISTRYParserO   
is_managedregionr=   hasattrrQ   r   ConnectGetWorkerPoolr   Inforun_messages_utilGetStartDeployMessager   WorkerPoolStagesr   StagedProgressTrackerrL   ReplaceWorkerPoolrR   )r;   r<   run_messagesworker_pool_dictrX   rH   raw_worker_poolerq   project_numberchangesworker_pool_refr6   conn_contextrQ   r"   clientrP   deployment_stagesheadertrackers                        r0   RunzReplace.Run   s   ))**--L DII ##
8
$&&-f55))8  )&1*==9;vz*:6 !$$Z488dKIZ	3%?25i.z";/O%AA
L33o $..Mo !!&&..224I))5g$55i@n!**44i	g	)s>/B"B++))/)H
 	
 *3O&""?D9G((..  %%O44>>
 0 / O #2"<"<$  **4>L%dI6dllEG  	+,  
	&	&|	4,,_=o

1
1".	 !113+.1O  
 11

-++0	
  22 3 
 $?A 
5	4] 00 ..
g @ % 
5	4s=   6M	 ?BN!N#N	M>M99M>N
	NNN)__name__
__module____qualname____doc__detailed_helpclassmethodr1   r4   r=   rJ   rR   r    r2   r0   r   r   +   sV     O	
- * *$  
$*rr2   r   N))r   
__future__r   r   r   r   googlecloudsdk.api_lib.runr   r	   googlecloudsdk.api_lib.utilr
   r   r   googlecloudsdk.callioper   r   #googlecloudsdk.command_lib.projectsr   rs   googlecloudsdk.command_lib.runr   r   r   r   r}   r   r   r   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler   UniverseCompatibleReleaseTracksr:   ALPHABETACommandr   r   r2   r0   <module>r      s    B &  % ' 5 2 , A / ( E 9 = 5 0 M 7 @ 1 & * ) 8 D%%++T->->-C-CDDdll D E Dr2   