
                         R   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  ddl!m"Z"  G d dejF                        Z$y)z;Command for updating env vars and other configuration info.    )absolute_import)division)print_function)unicode_literals)global_methods)job)apis)messages)arg_parsers)base)util)config_changes)connection_context)
exceptions)flags)messages_util)pretty_print)serverless_operations)stages)config)log)
properties)	resources)progress_trackerc                   0    e Zd ZdZdddZed        Zd Zy)Replacez6Create or replace a job from a YAML job specification.zM          Creates or replaces a job from a YAML job specification.
          zw          To replace the specification for a job defined in myjob.yaml

              $ {command} myjob.yaml

         )DESCRIPTIONEXAMPLESc                     t        j                  |        t        j                  |        | j                  ddt	        j
                         d       | j                  j                  d       y )NFILEstorezcThe absolute path to the YAML file with a Cloud Run job definition for the job to update or create.)actiontypehelpnone)r   AddAsyncFlagAddClientNameAndVersionFlagsadd_argumentr   YAMLFileContentsdisplay_info	AddFormat)parsers    lib/surface/run/jobs/replace.pyArgszReplace.Args=   s^    	v	&&v.
))+:	  ; !!&)    c           	      	   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<   	 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                  d      xs |j9                  d      }
t;        j<                  |      t;        j>                  | jA                               t;        jB                  |
r|jD                  nd|
r|jF                  ntH        jJ                  d      g}tL        jN                  jQ                  |j.                  jR                  d|j.                  j0                  id      }|jT                  r|jV                  nd}tY        jZ                  |t\        j^                  j`                  | jA                         |      }tc        jd                  |      5 }|jg                  |      }| }|rdnd}ti        jj                  tm        jn                  |||d             |dz   }tq        jr                  |tu        jv                         d|jx                        5 }|r |j{                  ||||jx                        }n|j}                  ||||jx                        }ddd       |rdnd}|jx                  r1ti        j~                  dj7                  |jR                  |             n0ti        j~                  dj7                  |jR                  |             t        j                  j                  tm        j                  | jA                         |jR                               |cddd       S # t        j                   $ r"}t        j"                  |d	       Y d}~d}~ww xY w# 1 sw Y   xY w# 1 sw Y   yxY w) zCreate or Update job 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 job definition spec in https://cloud.google.com/run/docs/reference/rest/v1/namespaces.jobs#Job)	help_textzVNamespace must be project ID [{}] or quoted number [{}] for Cloud Run (fully managed).client_nameclient_versiongcloudT)set_on_templatenamespacesIdzrun.namespaces.jobs)params
collection)region_labelCreatingUpdatingr   z job...zJob failed to deploy)failure_messagesuppress_output)asyncreatedupdatedzAJob [{{bold}}{job}{{reset}}] is being {operation} asynchronously.)r   	operationz?Job [{{bold}}{job}{{reset}}] has been successfully {operation}.)Dr	   GetMessagesModuler   SERVERLESS_API_NAMESERVERLESS_API_VERSIONdictr    r   ConfigurationErrorget
isinstancestrr   DictToMessageWithErrorCheckJobr   ScalarTypeMismatchErrorMaybeRaiseCustomFieldMismatchr   VALUEScoreprojectGetr4   r5   projects_utilGetProjectNumberformatIsSpecifiedr   ReplaceJobChangeSetLaunchStageAnnotationChangeReleaseTrack'SetClientNameAndVersionAnnotationChanger7   r8   r   CLOUD_SDK_VERSIONr   REGISTRYParsename
is_managedregionr   GetConnectionContextr   ProductRUNr   ConnectGetJobr   Inforun_messages_utilGetStartDeployMessager   StagedProgressTrackerr   	JobStagesasync_	UpdateJob	CreateJobSuccessr   r1   PrintGetRunJobMessage)selfargsrun_messagesjob_dictr5   raw_jobnew_joberU   project_numberis_either_specifiedchangesjob_refr>   conn_contextclientjob_obj	is_createrF   headertrackers                        r-   RunzReplace.RunJ   s   )).*L*L*8*O*OQLDIIH 8
8
h*HV4D"D))89 9
 &)*5513hvz":. Z,00dCIZ	3%?*-i.hz;'	*99
L$$&g.g !!&&..224I!!-g$55i@n"",,i	g	)s>/B"B++))/)HJ 	J "+G 	'M4+;+;<L+M  	''055d6G6G6IJ>> 3D" (.(@(@ 		"G   && 0 0 : :;( ' *G
 &-%7%77>>TL%::emm!2!2!4<QL 
	&	&|	4g&g+i!*:
i

1
1,2;UDE 9$f11




1++	' +2$$wdkk % ;' $$wdkk % ;'' !*9yi	$ffK	M 	 99?%,\\Y :@ :H	I 
jj

,
,T->->-@',,
OQC 
5	4S 00 *..
)* **f' ' 
5	4s?   
6R( B S-AS CS-(S;SS S*	%S--S6N)__name__
__module____qualname____doc__detailed_helpstaticmethodr.   r    r/   r-   r   r   ,   s0    >- 
* 
*fr/   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   rW   googlecloudsdk.command_lib.runr   r   r   r   rk   r   r   r   googlecloudsdk.corer   r   r   r   googlecloudsdk.core.consoler   Commandr   r   r/   r-   <module>r      se    B &  % ' 5 * , A / ( E 9 = 5 0 M 7 @ 1 & # * ) 8Ddll Dr/   