
    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Zd
ZefdZefdZd Zd Z G d de      Z G d de      Zy	)zData Pipelines API utilities.    )absolute_import)division)unicode_literals)
list_pager)apis)
properties)	resourcesNv1c                 .    t        j                  d|       S Ndatapipelines)r   GetMessagesModuleapi_versions    0lib/googlecloudsdk/api_lib/datapipelines/util.pyr   r          				==    c                 .    t        j                  d|       S r   )r   GetClientInstancer   s    r   r   r   "   r   r   c                 x    t         j                  j                  | j                  d      }|j	                         S )Nzdatapipelines.pipelines
collectionr	   REGISTRYParseRelativeNamenameSelfLink)resourcepipelines     r   GetPipelineURIr    &   s6    11mm 9 2 ;(				r   c                 x    t         j                  j                  | j                  d      }|j	                         S )Nzdatapipelines.pipelines.jobsr   r   )r   jobs     r   	GetJobURIr#   ,   s4    ,,mm > 	- 	@#	r   c                   \    e Zd ZdZddZd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd Zd Zd Zy)PipelinesClientz0Client for Pipelines for the Data Pipelines API.Nc                     |xs
 t               | _        |xs
 t               | _        | j                  j                  | _        y N)r   clientr   messagesprojects_locations_pipelines_serviceselfr(   r)   s      r   __init__zPipelinesClient.__init__5   s3    /-/DK3 1 3DMKK<<DMr   c                 p    | j                   j                  |      }| j                  j                  |      S )zDescribe a Pipeline in the given project and region.

    Args:
      pipeline: str, the name for the Pipeline being described.

    Returns:
      Described Pipeline Resource.
    r   )r)   1DatapipelinesProjectsLocationsPipelinesGetRequestr+   Get)r-   r   describe_reqs      r   DescribezPipelinesClient.Describe:   s6     ==RR S L==\**r   c                 p    | j                   j                  |      }| j                  j                  |      S )zDelete a Pipeline in the given project and region.

    Args:
      pipeline: str, the name for the Pipeline being described.

    Returns:
      Empty Response.
    r0   )r)   4DatapipelinesProjectsLocationsPipelinesDeleteRequestr+   Delete)r-   r   
delete_reqs      r   r7   zPipelinesClient.DeleteG   s6     SS T J==
++r   c                 p    | j                   j                  |      }| j                  j                  |      S )zStop a Pipeline in the given project and region.

    Args:
      pipeline: str, the name for the Pipeline being described.

    Returns:
      Pipeline resource.
    r0   )r)   2DatapipelinesProjectsLocationsPipelinesStopRequestr+   Stopr-   r   stop_reqs      r   r;   zPipelinesClient.StopT   s6     }}OO P H==h''r   c                 p    | j                   j                  |      }| j                  j                  |      S )zRun a Pipeline in the given project and region.

    Args:
      pipeline: str, the name for the Pipeline being described.

    Returns:
      Job resource which was created.
    r0   )r)   1DatapipelinesProjectsLocationsPipelinesRunRequestr+   Runr<   s      r   r@   zPipelinesClient.Runa   s6     }}NN O H==X&&r   c           	          | j                   j                  ||      }t        j                  | j                  j
                  |dd||d      S )a  List Pipelines for the given project and region.

    Args:
      limit: int or None, the total number of results to return.
      page_size: int, the number of entries in each batch (affects requests
        made, but not the yielded results).
      input_filter: string, optional filter to pass, eg:
        "type:BATCH,status:ALL", to filter out the pipelines based on staus or
        type.
      region: string, relative name to the region.

    Returns:
      Generator of matching devices.
    )filterparent	pipelinesListpageSizefieldmethod
batch_sizelimitbatch_size_attribute)r)   2DatapipelinesProjectsLocationsPipelinesListRequestr   YieldFromListr(   r*   )r-   rK   	page_sizeinput_filterregionlist_reqs         r   rE   zPipelinesClient.Listn   sU     }}OOF P ,H##00') )r   c                 H   |j                   }t        j                  j                  j                  j                  d      }| j                  |j                  | j                  j                  j                        }| j                  |j                  | j                  j                  j                        }d}|r&| j                  j                  j                  |      }| j                  j                  j                  j                  }|j                   r|nd}| j                  |j"                  | j                  j                  j$                        }	d}
|	r&| j                  j                  j%                  |	      }
| j                  j                  | j                  j                  |j&                  |j(                  |j*                  |j,                  |j.                  |j0                  |j2                  |j4                  ||j6                  |j8                  |j:                  |j<                  r|j<                  ng |
      |j>                  |r&| j                  j                  j                  |      nd|      }| j                  jA                  |jB                  |||      S )zCreate a Legacy Template request for the Pipeline workload.

    Args:
      args: Any, list of args needed to create a Pipeline.

    Returns:
      Legacy Template request.
    TrequiredNadditionalProperties)serviceAccountEmail
maxWorkers
numWorkersnetwork
subnetworkmachineTypetempLocation
kmsKeyNameipConfigurationworkerRegion
workerZoneenableStreamingEngineadditionalExperimentsadditionalUserLabels)environmentupdate
parameterstransformNameMapping)gcsPathlocation	projectIdlaunchParameters)"rQ   r   VALUEScoreprojectr2   ConvertDictArgumentsrh   r)   2GoogleCloudDatapipelinesV1LaunchTemplateParametersParametersValuetransform_name_mappingsTransformNameMappingValue,GoogleCloudDatapipelinesV1RuntimeEnvironmentIpConfigurationValueValuesEnumWORKER_IP_PRIVATEdisable_public_ipsadditional_user_labelsAdditionalUserLabelsValuedataflow_service_account_emailmax_workersnum_workersr[   r\   worker_machine_typetemp_locationdataflow_kms_keyworker_regionworker_zoneenable_streaming_engineadditional_experimentsrg   /GoogleCloudDatapipelinesV1LaunchTemplateRequesttemplate_file_gcs_location)r-   argsrk   
project_idparams_listtransform_mapping_listrt   
ip_privateip_configurationuser_labels_listrz   launch_parameters               r   CreateLegacyTemplateRequestz+PipelinesClient.CreateLegacyTemplateRequest   sX    {{H""''//33T3BJ++	;	;OOMK "66$$HH	"	"$ # $ ` ` z z5 !{ !7 KKjj||J%)%<%<z$00##T]]	5	56O6OQ "#}}YYss/  t  1 }}WWMMNN $ C C''''LL00++,,,++''"&">">&*&A&A $(#>#>GI!7 O 9  {{ 2= ==	;	;OO!, =L =.BF4+ X 6, ==HH//)	 I + +r   c                 L   |j                   }t        j                  j                  j                  j                  d      }| j                  |j                  | j                  j                  j                        }| j                  |j                  | j                  j                  j                        }d}|r&| j                  j                  j                  |      }| j                  j                  j                  j                  }|j                   r|nd}| j                  |j"                  | j                  j                  j$                        }	d}
|	r&| j                  j                  j%                  |	      }
d}|j&                  rs|j&                  dk(  r+| j                  j                  j(                  j*                  }n9|j&                  dk(  r*| j                  j                  j(                  j,                  }| j                  j                  |j.                  | j                  j                  |j0                  |j2                  |j4                  |j6                  |j8                  |j:                  |j<                  |j>                  ||j@                  |jB                  |jD                  ||jF                  r|jF                  ng |
      |jH                  |r&| j                  j                  j                  |      nd|      }| j                  jK                  |||	      S )
zCreate a Flex Template request for the Pipeline workload.

    Args:
      args: Any, list of args needed to create a Pipeline.

    Returns:
      Flex Template request.
    TrT   NrV   SPEED_OPTIMIZEDCOST_OPTIMIZED)rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   rc   
flexrsGoalrd   re   )containerSpecGcsPathrf   rg   rh   transformNameMappings)rk   rl   launchParameter)&rQ   r   rn   ro   rp   r2   rq   rh   r)   5GoogleCloudDatapipelinesV1LaunchFlexTemplateParameterrs   rt   TransformNameMappingsValue8GoogleCloudDatapipelinesV1FlexTemplateRuntimeEnvironmentrw   rx   ry   rz   r{   flexrs_goalFlexrsGoalValueValuesEnumFLEXRS_SPEED_OPTIMIZEDFLEXRS_COST_OPTIMIZEDr   r|   r}   r~   r[   r\   r   r   r   r   r   r   r   rg   3GoogleCloudDatapipelinesV1LaunchFlexTemplateRequest)r-   r   rk   r   r   r   rt   r   r   r   rz   r   r   s                r   CreateFlexTemplateRequestz)PipelinesClient.CreateFlexTemplateRequest   s    {{H""''//33T3BJ++	>	>PK "66$$KK	#	#% # $ c c ~ ~5 ! !7 WWvv  I  IJ%)%<%<z$00##NN	"	"$ "#}}ee/  @  1 K			.	.mm\\vv  N  N//mm\\vv  M  M}}ZZ!<<MM	A	A $ C C''''LL00++,,,++''"&">">"&*&A&A $(#>#>GI!7! 
B 
9" {{ 2= ==	>	>!, @O @.BF51 [ 72 ==LL( M * *r   c                    |j                   dk(  rN| j                  j                  j                  | j                  j                  j                  j                        }nM| j                  j                  j                  | j                  j                  j                  j
                        }| j                  j                  |j                  |j                        }|j                  dk(  r.| j                  |      }| j                  j                  |      }n-| j                  |      }| j                  j                  |      }|j                  r|j                  }	n|j                  dd      d   }	| j                  j                  ||	|||j                  |	      }
| j                  j!                  |
|
      }| j"                  j%                  |      S )a  Create a Pipeline in the given project and region.

    Args:
      pipeline: str, the name for the Pipeline being created.
      parent: str, relative name to the region.
      args: Any, list of args needed to create a Pipeline.

    Returns:
      Pipeline resource.
    	streamingscheduletimeZoneclassicdataflowLaunchTemplateRequestdataflowFlexTemplateRequest/   )r   displayNametypescheduleInfoschedulerServiceAccountEmailworkload)"googleCloudDatapipelinesV1PipelinerC   )pipeline_typer)   "GoogleCloudDatapipelinesV1PipelineTypeValueValuesEnumPIPELINE_TYPE_STREAMINGPIPELINE_TYPE_BATCH&GoogleCloudDatapipelinesV1ScheduleSpecr   	time_zonetemplate_typer   "GoogleCloudDatapipelinesV1Workloadr   display_namersplitscheduler_service_account_email4DatapipelinesProjectsLocationsPipelinesCreateRequestr+   Create)r-   r   rC   r   r   schedule_infolegacy_template_requestr   flex_template_requestr   pipeline_spec
create_reqs               r   r   zPipelinesClient.Create  s    [(mmFFZZ
--
:
:
N
N""$m mmFFZZ
--
:
:
N
N m MMHH I 9M Y& $ @ @ FAA(? B Ah #<<TBAA&; B =h &&l__S!,R0lMMDD "%)%I%I E M SS+8 T IJ==
++r   c                 J   g }|dk(  rd}nd}|j                   r+|dk(  r|j                  |dz          n|j                  d       |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r.|dk(  r|j                  |dz          n|j                  |d	z          |j
                  r|j                  |d
z          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                  r|j                  |dz          |j                   r|j                  |dz          |j"                  r|dk(  r|j                  |dz          |j$                  r|j                  |dz          |j&                  r|j                  |dz          |S )zGiven a set of args for the workload, create the required update mask.

    Args:
      template_type: str, the type of the pipeline.
      args: Any, object with args needed for updating a pipeline.

    Returns:
      Update mask.
    flexz9workload.dataflow_flex_template_request.launch_parameter.z<workload.dataflow_launch_template_request.launch_parameters.container_spec_gcs_pathz2workload.dataflow_launch_template_request.gcs_pathrh   rg   rt   transform_name_mappingzenvironment.max_workerszenvironment.num_workersz!environment.service_account_emailzenvironment.temp_locationzenvironment.networkzenvironment.subnetworkzenvironment.machine_typezenvironment.kms_key_namezenvironment.ip_configurationzenvironment.worker_regionzenvironment.worker_zonez#environment.enable_streaming_enginezenvironment.flexrs_goalz"environment.additional_user_labelsz"environment.additional_experiments)r   appendrh   rg   rt   r}   r~   r|   r   r[   r\   r   r   ry   r   r   r   r   rz   r   )r-   r   r   update_maskprefix_strings        r   WorkloadUpdateMaskz"PipelinesClient.WorkloadUpdateMask@  sy    KQmTm&&	&	 =+DDEOP56{{12##	&	 =+DDE=+CCD)BBC)BBC**)LLM)DDE||)>>?)AAB)CCD)CCD)GGH)DDE)BBC##)NNO	&	 =+DDE"")MMN"")MMNr   c                 |   g }d}|j                   s|j                  rtd\  }}|j                   r|j                   }|j                  d       |j                  r|j                  }|j                  d       | j                  j	                  ||      }|j
                  r|j                  d       |j                  dk(  rC|| j                  d|      z  }| j                  |      }| j                  j                  |      }nB|| j                  d	|      z  }| j                  |      }	| j                  j                  |	
      }| j                  j                  ||j
                  ||j                  |      }
| j                  j                  |
|dj                  |            }| j                  j!                  |      S )zUpdate a Pipeline in the given project and region.

    Args:
      pipeline: str, the name for the Pipeline being updated.
      args: Any, object with args needed to update a Pipeline.

    Returns:
      Pipeline resource.
    NNNzschedule_info.schedulezschedule_info.time_zoner   r   r   r   r   r   )r   r   r   r   r   ,)r   r   
updateMask)r   r   r   r)   r   r   r   r   r   r   r   r   r   3DatapipelinesProjectsLocationsPipelinesPatchRequestjoinr+   Patch)r-   r   r   r   r   r   r   r   r   r   r   
update_reqs               r   r   zPipelinesClient.Patch  s    KM}}&h		==34	NN	45mmJJi K 1m (Y&T,,Y==k $ @ @ FAA(? B Ah T,,VT::k"<<TBAA&; B =h MMDD%%"%)%I%I E M RR+888K( S *J ==z**r   c                     g }|r?t        j                  |      D ]'  \  }}|j                  |j                  ||             ) |S )zConvert dictionary arguments to parameter list .

    Args:
      arguments: Arguments for create job using template.
      value_message: the value message of the arguments

    Returns:
      List of value_message.AdditionalProperty
    )keyvalue)six	iteritemsr   AdditionalProperty)r-   	argumentsvalue_messager   kvs         r   rq   z$PipelinesClient.ConvertDictArguments  sI     K--	*$!Q=;;;KL + r   r   )N2    r   )__name__
__module____qualname____doc__r.   r4   r7   r;   r@   rE   r   r   r   r   r   rq    r   r   r%   r%   2   sH    8=
+,(')4=+~G*R.,`Pd1+fr   r%   c                        e Zd ZdZddZddZy)
JobsClientzQClient used for interacting with job related service from the Data Pipelines API.Nc                     |xs
 t               | _        |xs
 t               | _        | j                  j                  | _        y r'   )r   r(   r   r)   !projects_locations_pipelines_jobsr+   r,   s      r   r.   zJobsClient.__init__  s3    /-/DK3 1 3DMKKAADMr   c           	          | j                   j                  |      }t        j                  | j                  |dd||d      S )af  Make API calls to list jobs for pipelines.

    Args:
      limit: int or None, the total number of results to return.
      page_size: int, the number of entries in each batch (affects requests
        made, but not the yielded results).
      pipeline: string, the name of the pipeline to list jobs for.

    Returns:
      Generator that yields jobs.
    )rC   jobsrE   rF   rG   )r)   6DatapipelinesProjectsLocationsPipelinesJobsListRequestr   rN   r+   )r-   rK   rO   r   rR   s        r   rE   zJobsClient.List  sM     }}SS T H##') )r   r   )Nr   r   )r   r   r   r   r.   rE   r   r   r   r   r     s    YB
)r   r   )r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r	   r   _DEFAULT_API_VERSIONr   r   r    r#   objectr%   r   r   r   r   <module>r      sa    $ &  ' ' , * ) 
  #7 > #7 >bf bJ) )r   