
                         V   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  ej"                  d      Zej&                  Z ej"                  d      ZdZdj-                  e      Zd ZddZddZ	 	 	 	 	 	 	 	 ddZd Zd Zd Zy)z(Utilities for building the dataflow CLI.    )absolute_import)division)unicode_literalsN)
exceptions)
list_pager)apis)log)
properties)	resourcesz	^dataflowz^(.*)-windmill-(.*)-watermarkz dataflow.projects.locations.jobszDefaults to '{0}'.c                     	 t        j                  | j                        }|j                  di       j                  dd      S # t        $ r, t
        j                  j                  | j                         Y yw xY w)zExtract the error message from an HTTPError.

  Args:
    error: The error exceptions.HttpError thrown by the API client.

  Returns:
    A string describing the error.
  errormessage zUnknown error)	jsonloadscontentget
ValueErrorr	   errPrintresponse)r   content_objs     8lib/googlecloudsdk/command_lib/dataflow/dataflow_util.pyGetErrorMessager   2   s]    **U]]+K??7B'++Ir::	 GGMM%..!s   A A 2A87A8c                     |rdj                  |      }|rdj                  |      }|rdj                  |      }dj                  |||t        |             S )a\  Create a standard error message across commands.

  Args:
    error: The error exceptions.HttpError thrown by the API client.
    job_id: The job ID that was used in the command.
    project_id: The project ID that was used in the command.
    region_id: The region ID that was used in the command.

  Returns:
    str, a standard error message.
  z with job ID [{0}]z in project [{0}]z in regional endpoint [{0}]zFailed operation{0}{1}{2}: {3})formatr   )r   job_id
project_id	region_ids       r   MakeErrorMessager    C   s`     !((0F$++J7J-44Y?I	)	0	0Y1@1G
I I    c           	   #      K   	 | D ]  }|  y# t         j                  $ r&}t        j                  t	        ||||            d}~ww xY ww)a  Wraps a generator to catch any exceptions.

  Args:
    generator: The error exceptions.HttpError thrown by the API client.
    job_id: The job ID that was used in the command.
    project_id: The project ID that was used in the command.
    region_id: The region ID that was used in the command.

  Yields:
    The generated object.

  Raises:
    dataflow_exceptions.ServiceException: An exception for errors raised by
      the service.
  N)r   	HttpErrordataflow_exceptionsServiceExceptionr    )	generatorr   r   r   xes         r   YieldExceptionWrapperr)   Y   sP      <g 			 <

.
.FJ	:< <<s$   A AA	!AA		Ac
           
      d    d}
|	sd}
t        j                  | |||||||
      }t        ||||	      S )a  Returns a wrapped list_page.YieldFromList to catch any exceptions.

  Args:
    service: apitools_base.BaseApiService, A service with a .List() method.
    request: protorpc.messages.Message, The request message corresponding to the
      service's .List() method, with all the attributes populated except the
      .maxResults and .pageToken attributes.
    limit: int, The maximum number of records to yield. None if all available
      records should be yielded.
    batch_size: int, The number of items to retrieve per request.
    field: str, The field in the response that will be a list of items.
    batch_size_attribute: str, The name of the attribute in a response message
      holding the maximum number of results to be returned. None if
      caller-specified batch size is unsupported.
    predicate: lambda, A function that returns true for items to be yielded.
    job_id: The job ID that was used in the command.
    project_id: The project ID that was used in the command.
    region_id: The region ID that was used in the command.

  Returns:
    The wrapped generator.

  Raises:
    dataflow_exceptions.ServiceException: if list request failed.
  List
Aggregated)servicerequestlimit
batch_sizefieldbatch_size_attribute	predicatemethod)r   YieldFromListr)   )r-   r.   r/   r0   r1   r2   r3   r   r   r   r4   pagers               r   r5   r5   q   sM    F &	F

"
"/% 
ufj)	DDr!   c                     t         j                  j                  | j                  t        j
                  j                  j                  j                  | j                  dt              }|j                         S )zTransform a job resource into a URL string.

  Args:
    resource: The DisplayInfo job object

  Returns:
    URL to the job
  	projectIdlocationparams
collection)r   REGISTRYParseidr
   VALUEScoreproject	GetOrFailr:   JOBS_COLLECTIONSelfLink)resourcerefs     r   JobsUriFuncrI      sb     	  kk!((--55??'' ! 	! 	"# 
r!   c                     t         j                  j                  | t        j                  j
                  j                  j                  |dt              }|j                         S )zTransform a job ID into a URL string.

  Args:
    job_id: The job ID
    region_id: Region ID of the job's regional endpoint.

  Returns:
    URL to the job
  r8   r;   )
r   r>   r?   r
   rA   rB   rC   rD   rE   rF   )r   r   rH   s      r   JobsUriFromIdrK      sX     	  !((--55?? ! 	! 	"# 
r!   c                     | j                   }|s0t        }dj                  t              }t        j                  |       |S )zGet region to be used in Dataflow services.

  Args:
    args: Argument passed in when running gcloud dataflow command

  Returns:
    Region specified by user from --region flag in args, then fall back to
    'us-central1'.
  z`--region` not set; defaulting to '{0}'. In an upcoming release, users must specify a region explicitly. See https://cloud.google.com/dataflow/docs/concepts/regional-endpoints for additional details.)regionDATAFLOW_API_DEFAULT_REGIONr   r	   warning)argsrM   msgs      r   	GetRegionrR      s?     ;;&	(F% F./	 
 KK	-r!   )r   r   r   )Nd   items
maxResultsNr   r   r   )__doc__
__future__r   r   r   r   reapitools.base.pyr   r   googlecloudsdk.api_lib.dataflowr   r$   googlecloudsdk.corer	   r
   r   compileDATAFLOW_METRICS_RErN   WINDMILL_WATERMARK_RErE   r   DEFAULT_REGION_MESSAGEr   r    r)   r5   rI   rK   rR    r!   r   <module>ra      s    / &  '  	 ' ' 0 M # * )
 !bjj- ">>  #

#BC 4/66! "I,<4  '3 0Ef(*r!   