
                         ~    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  G d
 de      Zy)z2Utilities for querying custom jobs in AI Platform.    )absolute_import)division)unicode_literals)
list_pager)apis)	constants)util)
console_ioc                   h    e Zd ZdZej
                  fdZd Zd Z	 	 	 ddZ	ddZ
d Zd	 Zd
 Zd Zy)CustomJobsClientz4Client used for interacting with CustomJob endpoint.c                     t        j                  t        j                  t        j                  |         }|j
                  | _        || _        |j                  | _	        t        j                  |   | _        y )N)r   GetClientInstancer   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSIONMESSAGES_MODULE	_messages_versionprojects_locations_customJobs_serviceAI_PLATFORM_MESSAGE_PREFIX_message_prefix)selfversionclients      3lib/googlecloudsdk/api_lib/ai/custom_jobs/client.py__init__zCustomJobsClient.__init__   s]    ##I$B$B$-$E$Eg$NPF++DNDM88DM$??HD    c                 f    t        | j                  dj                  | j                  |      d      S )z&Returns the API message class by name.z{prefix}{name})prefixnameN)getattrr   formatr   )r   message_names     r   
GetMessagezCustomJobsClient.GetMessage'   s9     t';';%1 	  	348: :r   c                 $    | j                  d      S )z'Retures the CustomJob resource message.	CustomJob)r$   )r   s    r   CustomJobMessagez!CustomJobsClient.CustomJobMessage/   s     ??;''r   Nc                     | j                         ||      }| | j                  d      |      |_        |r||_        | j                  t
        j                  k(  r6| j                  j                  | j                  j                  ||            S | j                  j                  | j                  j                  ||            S )a.  Constructs a request and sends it to the endpoint to create a custom job instance.

    Args:
      parent: str, The project resource path of the custom job to create.
      job_spec: The CustomJobSpec message instance for the job creation request.
      display_name: str, The display name of the custom job to create.
      kms_key_name: A customer-managed encryption key to use for the custom job.
      labels: LabelValues, map-like user-defined metadata to organize the custom
        jobs.

    Returns:
      A CustomJob message instance created.
    )displayNamejobSpecEncryptionSpec)
kmsKeyName)parent%googleCloudAiplatformV1beta1CustomJob)r-    googleCloudAiplatformV1CustomJob)r'   r$   encryptionSpeclabelsr   r   BETA_VERSIONr   Creater   2AiplatformProjectsLocationsCustomJobsCreateRequest)r   r-   job_specdisplay_namekms_key_namer1   
custom_jobs          r   r3   zCustomJobsClient.Create4   s    & )&&( (4J "C$//2B"C!##j  j}}	...]]!!
..
K
K: L OP P ]]!!
..
K
Kj L JK Kr   c                 ~    t        j                  | j                  | j                  j	                  |      dd|      S )N)r-   
customJobspageSize)fieldbatch_size_attributelimit)r   YieldFromListr   r   0AiplatformProjectsLocationsCustomJobsListRequest)r   r>   regions      r   ListzCustomJobsClient.ListZ   s@    ##GG 	H 	' r   c                 p    | j                   j                  |      }| j                  j                  |      S Nr    r   /AiplatformProjectsLocationsCustomJobsGetRequestr   Getr   r    requests      r   rH   zCustomJobsClient.Getc   s4    nnLL M G==W%%r   c                 p    | j                   j                  |      }| j                  j                  |      S rD   )r   2AiplatformProjectsLocationsCustomJobsCancelRequestr   CancelrI   s      r   rM   zCustomJobsClient.Cancelh   s4    nnOO P G==((r   c                     | j                   j                  |      }| j                  j                  |      fd}|S )zReturns a function to decide if log fetcher should continue polling.

    Args:
      name: String id of job.

    Returns:
      A one-argument function decides if log fetcher should continue.
    rE   c                 ,    | dk  ryj                   d u S )N   T)endTime)periods_without_logsresponses    r   ShouldContinuez9CustomJobsClient.CheckJobComplete.<locals>.ShouldContinuez   s    		"%%r   rF   )r   r    rJ   rT   rS   s       @r   CheckJobCompletez!CustomJobsClient.CheckJobCompletem   sC     nnLL M G}}  )H&
 r   c                     t        j                  |d      }| j                  |      }t        j                  ||      S )z@Import a messages class instance typed by name from a YAML file.F)binary)message_typestream)r
   ReadFromFileOrStdinr$   export_utilImport)r   	yaml_filer#   datarX   s        r   ImportResourceMessagez&CustomJobsClient.ImportResourceMessage   s5    )))EBD??<0L<EEr   )NNN)NN)__name__
__module____qualname____doc__r   
GA_VERSIONr   r$   r'   r3   rB   rH   rM   rU   r_    r   r   r   r      sJ    <&11 I:( $KL&
)
(Fr   r   N)rc   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.air   !googlecloudsdk.command_lib.exportr	   r[   googlecloudsdk.core.consoler
   objectr   re   r   r   <module>rm      s2    9 &  ' ' , 3 A 2iFv iFr   