
    U                     J   S 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  \R                   \R"                  " \R$                  R&                  \R$                  R(                  5       " S S\R*                  5      5       5       rS rS rS rg)z3Implementation of gcloud dataflow yaml run command.    )apis)storage_api)storage_util)arg_parsers)base)dataflow_util)
properties)yaml)filesc                   8    \ rS rSrSrSSS.r\S 5       rS rSr	g	)
Run   z#Runs a job from the specified path.zERuns a job from the specified YAML description or Cloud Storage path.z          To run a job from YAML, run:

            $ {command} my-job --yaml-pipeline-file=gs://yaml-path --region=europe-west1
          )DESCRIPTIONEXAMPLESc                    U R                  SSSS9  U R                  SS9nUR                  SSS	9  UR                  S
SS	9  U R                  SSS[        R                  -   S9  U R                  SS[        R
                  " 5       [        R                  SS9  U R                  SSSS9  U R                  SS[        R                  " SS5      S9  U R                  SSS	9  U R                  SSS	9  g) zfRegister flags for this command.

Args:
  parser: argparse.ArgumentParser to register arguments with.
job_nameJOB_NAMEz!Unique name to assign to the job.)metavarhelpTrequiredz--yaml-pipeline-fileziPath of a file defining the YAML pipeline to run. (Must be a local file or a URL beginning with 'gs://'.))r   z--yaml-pipelinez.Inline definition of the YAML pipeline to run.z--region	REGION_IDz*Region ID of the job's regional endpoint. z--pipeline-optionszOPTIONS=VALUE;OPTION=VALUEz$Pipeline options to pass to the job.)r   typeactionr   z--jinja-variablesJSON_OBJECTz1Jinja2 variables to be used in reifying the yaml.z--template-file-gcs-locationzbGoogle Cloud Storage location of the YAML template to run. (Must be a URL beginning with 'gs://'.)z^gs://.*zMust begin with 'gs://')r   r   z	--networkztCompute Engine network for launching worker instances to run the pipeline.  If not set, the default network is used.z--subnetworkzzCompute Engine subnetwork for launching worker instances to run the pipeline.  If not set, the default subnetwork is used.N)add_argumentadd_mutually_exclusive_groupr   DEFAULT_REGION_MESSAGEr   ArgDictUpdateActionRegexpValidator)parsergroups      lib/surface/dataflow/yaml/run.pyArgsRun.Args-   sU    J-P   ///>E	F   
 P   8223	   ,  "''3   @   &8(()DF	   F   M      c           
         [        UR                  =(       d    0 5      nUR                  (       a-  [        UR                  5      nUc  UR                  US'   OX2S'   OUR                  US'   UR
                  (       a  UR
                  US'   SU;   a  SU;  a  [        US   5        [        UR                  S5      U5      nUR                  =(       d)    [        R                  R                  R                  U5      n[        R                  " [        R                   R"                  R$                  R'                  SS9UUR(                  UUUR*                  UR,                  S9n[        R                  R/                  U5      $ )zyRuns the command.

Args:
  args: The arguments that were provided to this command invocation.

Returns:
  A Job message.
yaml_pipeline_fileyaml_pipelinejinja_variableszjinja-variablesTr   )
project_id	region_idr   gcs_location
parametersnetwork
subnetwork)dictpipeline_optionsr)   _try_get_yaml_contentsr*   r+   _validate_yaml _get_region_from_yaml_or_defaultgettemplate_file_gcs_locationr   	TemplatesYAML_TEMPLATE_GCS_LOCATIONformatTemplateArgumentsr	   VALUEScoreprojectGetr   r0   r1   CreateJobFromFlexTemplate)selfargsr/   yaml_contentsr-   r.   	argumentss          r$   r   Run.Runt   sL    d++1r2J ,T-D-DEm		+/+B+B
'(&3?# %)$6$6j!&*&:&:j"#*$):*)LZ010'I
 	'' 	G>>44;;IF 
 &&$$))1155t5D!??I >>33I>>r'    N)
__name__
__module____qualname____firstlineno____doc__detailed_helpstaticmethodr%   r   __static_attributes__rG   r'   r$   r   r      s4     , 
- D DL.?r'   r   c                 n     [         R                  " U 5      ng ! [         a  n[        S5      UeS nAff = f)Nz#yaml_pipeline must be a valid yaml.)r
   load	Exception
ValueError)r*   _exns      r$   r5   r5      s6    E		- A	 E
:
;DEs    
4/4c                 $   UR                   nSn [        R                  " U 5      nU(       d  [        R                  " U5      $ SU;   aB  SUS   ;   a9  US   S   nSU;   d  SU;   a%  [        R
                  " SR                  U5      5      eU(       a(  U(       a  X#:w  a  [        SR                  X25      5      eU$ [        R                  " U5      $ ! [        R
                   a+  nU(       d  [        SR                  U5      5         SnANSnAff = f)	zEGets the region from yaml pipeline or args, or falls back to default.Noptionsregion{}zlyaml pipeline contains unparsable region: {0}. Found curly braces in region. Falling back to default region.zyFailed to get region from yaml pipeline: {0}. If using jinja variables, parsing may fail. Falling back to default region.zeRegion specified in yaml pipeline options ({0}) does not match region specified in command line ({1}))	rX   r
   rQ   r   	GetRegionYAMLParseErrorr;   printrS   )r*   rC   rX   options_regionpipeline_datarU   s         r$   r6   r6      s   ;;&.IIm,M$$T**M!h-	2J&J$Y/9n		#"7!!99?9O
 	
 &*44:F5  		 	 	&&% 
		 F3Ks   2C AC D$!D

Dc                    U R                  S5      (       d  [        R                  " U 5      $ [        R                  " 5       n[
        R                  R                  U 5      n UR                  U5      R                  5       R                  S5      $ ! [         a$  n[        SR                  X5      5         SnAgSnAff = f)z@Reads yaml contents from the specified file if it is accessable.zgs://zutf-8zUnable to read file {0} due to incorrect file path or insufficient read permissions. Will not be able to validate the yaml pipeline or determine the region from the yaml pipeline options. Error: {1}N)
startswithr   ReadFileContentsr   StorageClientr   ObjectReferenceFromUrl
ReadObjectreaddecoderR   r]   r;   )r)   storage_clientobj_refes       r$   r4   r4      s    		&	&w	/	/!!"455,,..((001CD'$$W-224;;GDD	 		  &v&8<	  
s   "-B 
B>B99B>N)rL   googlecloudsdk.api_lib.dataflowr   googlecloudsdk.api_lib.storager   r   googlecloudsdk.callioper   r   #googlecloudsdk.command_lib.dataflowr   googlecloudsdk.corer	   r
   googlecloudsdk.core.utilr   DefaultUniverseOnlyReleaseTracksReleaseTrackGABETACommandr   r5   r6   r4   rG   r'   r$   <module>rx      s    : 0 6 7 / ( = * $ * D%%(($*;*;*@*@AD?$,, D? B D?NE!'Hr'   