
    U                     J   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 ej                    ej"                  ej$                  j&                  ej$                  j(                         G d dej*                                      Zd Zd Zd Zy)z3Implementation of gcloud dataflow yaml run command.    )apis)storage_api)storage_util)arg_parsers)base)dataflow_util)
properties)yaml)filesc                   0    e Zd ZdZdddZed        Zd Zy)Runz#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                    | j                  ddd       | j                  d      }|j                  dd	       |j                  d
d	       | j                  dddt        j                  z          | j                  ddt	        j
                         t        j                  d       | j                  ddd       | j                  ddt	        j                  dd             | j                  dd	       | j                  dd	       y) zrRegister 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ArgszRun.Args-   sU    J-P   ///>E	F   
 P   8223	   ,  "''3   @   &8(()DF	   F   M      c           	         t        |j                  xs i       }|j                  r-t        |j                        }||j                  |d<   n||d<   n|j                  |d<   |j
                  r|j
                  |d<   d|v rd|vrt        |d          t        |j                  d      |      }|j                  xs) t        j                  j                  j                  |      }t        j                  t        j                   j"                  j$                  j'                  d      ||j(                  |||j*                  |j,                        }t        j                  j/                  |      S )zRuns 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   r.   r/   CreateJobFromFlexTemplate)selfargsr-   yaml_contentsr+   r,   	argumentss          r#   r   zRun.Runt   sN    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____doc__detailed_helpstaticmethodr$   r    r%   r#   r   r      s4     , 
- D DL.?r%   r   c                 j    	 t        j                  |       }y # t        $ r}t        d      |d }~ww xY w)Nz#yaml_pipeline must be a valid yaml.)r
   load	Exception
ValueError)r(   _exns      r#   r3   r3      s6    E		- A	 E
:
;DEs    	2-2c                    |j                   }d}	 t        j                  |       }|st        j                  |      S d|v r;d|d   v r4|d   d   }d|v sd|v r$t        j
                  dj                  |            |r$|r ||k7  rt        dj                  ||            |S t        j                  |      S # t        j
                  $ r&}|st        dj                  |             Y d}~od}~ww xY w)	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}))	rS   r
   rL   r   	GetRegionYAMLParseErrorr9   printrN   )r(   rA   rS   options_regionpipeline_datarP   s         r#   r4   r4      s   ;;&.IIm,M$$T**M!h-	2J&J$Y/9n		#"7!!99?9O
 	
 &N*44:Ff5  		 	 	&&% 
		 F3Ks   +B6 ?B6 6C/	C**C/c                 t   | j                  d      st        j                  |       S t        j                         }t
        j                  j                  |       }	 |j                  |      j                         j                  d      S # t        $ r%}t        dj                  | |             Y d}~yd}~ww xY w)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decoderM   rX   r9   )r'   storage_clientobj_refes       r#   r2   r2      s    		&	&w	/!!"455,,..((001CD'$$W-224;;GDD	 		  &v&8!<	  
s   -B	 		B7B22B7N)rG   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   r3   r4   r2   rJ   r%   r#   <module>rs      s    : 0 6 7 / ( = * $ * D%%(($*;*;*@*@AD?$,, D? B D?NE!'Hr%   