
    [3                        d 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 ej                  Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z	 ddZ	 ddedej6                  defdZddZd Zd Z d Z!y)z(Notebook-executor executions api helper.    N)runtime_templates)
exceptions)parser_extensions)	resources)
console_ioc                     d| v r"	 t         j                  j                  | d      S t         j                  j                  | d      S # t         j                  $ r Y 6w xY w)zParse operation relative resource name to the operation reference object.

  Args:
    operation_name: The execution operation resource name

  Returns:
    The operation reference object
  z/notebookExecutionJobs/z>aiplatform.projects.locations.notebookExecutionJobs.operations)
collectionz(aiplatform.projects.locations.operations)r   REGISTRYParseRelativeName WrongResourceCollectionException)operation_names    :lib/googlecloudsdk/api_lib/notebook_executor/executions.pyParseExecutionOperationr      su     .011
N 2   
			-	-!K 
. 
  55 
s    A AAc                 f    | j                   j                  j                         j                         S )zGet the parent Location resource name for the execution or schedule resource.

  Args:
    args: Argparse object from Command.Run

  Returns:
    The resource name in the form projects/{project}/locations/{location}.
  )CONCEPTSregionParseRelativeNameargss    r   GetParentForExecutionOrScheduler   5   s&     
			#	#	%	2	2	44    c                 f    | j                   j                  j                         j                         S )zGet the resource name for the execution.

  Args:
    args: Argparse object from Command.Run

  Returns:
    The resource name in the form
    projects/{project}/locations/{location}/notebookExecutionJobs/{execution_job_id}.
  )r   	executionr   r   r   s    r   GetExecutionResourceNamer   A   s&     
	 	 	&	&	(	5	5	77r   c                 ~    |j                  t        | |            }t        |      st        j                  dd      |S )a  Checks that the execution is a Workbench execution and returns it if so.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the aiplatform API.
    service: The service to use for the API call.

  Returns:
    The execution if it is a Workbench execution.

  Raises:
    InvalidArgumentException: If the execution is not a Workbench execution.

  	EXECUTIONzeExecution is not of Workbench type. To manage Colab Enterprise executions use `gcloud colab` instead.GetCreateExecutionGetRequestIsWorkbenchExecutionr   InvalidArgumentExceptionr   messagesservicer   s       r    ValidateAndGetWorkbenchExecutionr&   N   sI     kkh/) 
i	(

-
-	2 
 
r   c                 ~    |j                  t        | |            }t        |      rt        j                  dd      |S )a  Checks that the execution is of Colab Enterprise type and returns it if so.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the aiplatform API.
    service: The service to use for the API call.

  Returns:
    The execution if it is a Colab Enterprise execution.

  Raises:
    InvalidArgumentException: If the execution is a Workbench execution.
  r   znExecution is not of Colab Enterprise type. To manage Workbench executions use `gcloud beta workbench` instead.r   r#   s       r   ValidateAndGetColabExecutionr(   i   sI     kkh/) )$

-
-	; 
 
r   c                     | j                   duS )zFilter for Workbench executions.

  Args:
    execution: The execution item to check.

  Returns:
    True if the execution is a Workbench execution.
  N)
kernelName)r   s    r   r!   r!      s     
		T	))r   c                 r    d }| j                  d      r#|j                   ||       | j                        S y)zGet the dataform repository source from the args.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the aiplatform API.

  Returns:
    DataformRepositorySource message for the execution.
  c                 f    | j                   j                  j                         j                         S )N)r   dataform_repository_namer   r   r   s    r   !GetDataformRepositoryResourceNamezNGetDataformRepositorySourceFromArgs.<locals>.GetDataformRepositoryResourceName   s$    ==11779FFHHr   r-   )dataformRepositoryResourceName	commitShaN)IsSpecifiedHGoogleCloudAiplatformV1beta1NotebookExecutionJobDataformRepositorySource
commit_sha)r   r$   r.   s      r   #GetDataformRepositorySourceFromArgsr4      sF    I 
01\\'H'N// ]   
r   c                 z    |j                   }| j                  d      r || j                  | j                        S y)zGet the GCS notebook source from the args.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the aiplatform API.

  Returns:
    GcsNotebookSource message for the execution.
  gcs_notebook_uri)uri
generationN)AGoogleCloudAiplatformV1beta1NotebookExecutionJobGcsNotebookSourcer1   r6   r8   )r   r$   gcs_notebook_sources      r   GetGcsNotebookSourceFromArgsr;      sE     PP  
()!!??  
r   c                     |j                   }| j                  d      r( |t        j                  | j                  d            S y)zCreate direct notebook source message from the args.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the aiplatform API.

  Returns:
      DirectNotebookSource message for the execution.
  direct_contentT)binary)contentN)DGoogleCloudAiplatformV1beta1NotebookExecutionJobDirectNotebookSourcer1   r   ReadFromFileOrStdinr=   )r   r$   notebook_sources      r   GetDirectNotebookSourceFromArgsrC      sK     aa/	&' ..t/B/B6:<  
r   c                 2    t        | j                        dz   S )zGet the execution timeout from the args.

  Args:
    args: Argparse object from Command.Run

  Returns:
    Serialized Duration message for the execution timeout.
  s)strexecution_timeoutr   s    r   GetExecutionTimeoutFromArgsrH      s     
T##	$s	**r   c                 f    | j                   j                  j                         j                         S )zGet the runtime template resource name from the args.

  Args:
    args: Argparse object from Command.Run

  Returns:
    The notebook runtime template resource name.
  )r   notebook_runtime_templater   r   r   s    r   GetRuntimeTemplateResourceNamerK      s&     
	0	0	6	6	8	E	E	GGr   c                     |j                   } |t        j                  | |      t        j                  | |      t        j                  | |            S )a  Get the custom environment spec from the args for a Workbench execution.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the aiplatform API.

  Returns:
    CustomEnvironmentSpec message for the execution.
  )machineSpecnetworkSpecpersistentDiskSpec)EGoogleCloudAiplatformV1beta1NotebookExecutionJobCustomEnvironmentSpecruntime_templates_utilGetMachineSpecFromArgsGetNetworkSpecFromArgsGetPersistentDiskSpecFromArgs)r   r$   custom_environment_specs      r   GetCustomEnvironmentSpecrV      sT     TT  
!(??hO(??hO/MM

 r   c                 x    t         j                  j                  | j                  d      }|j	                         S )z&Get the URL for an execution resource.z3aiplatform.projects.locations.notebookExecutionJobs)relative_namer	   )r   r
   r   nameSelfLink)resourcer   s     r   GetExecutionUrir\      s8      22MMF 3 ) 
			r   c                    |rEd}t        | |      }|j                         }d}d}t        j                  | |      }	| j                  }
n+t        | |      }d}d}| j                  }t        |       }d}	d}
|j                  ||rdnt        | |      |r| j                  n| j                  t        |       |t        | |      | j                  ||| j                  |	||
      S )a  Creates the NotebookExecutionJob message for the create request.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the AIPlatform API.
    workbench_execution: Whether this execution is for a Workbench notebook.
    for_schedule: Whether this execution is used to create a schedule.

  Returns:
    Instance of the NotebookExecutionJob message.
  N)dataformRepositorySourcedirectNotebookSourcedisplayNameexecutionTimeoutexecutionUsergcsNotebookSourcegcsOutputUri#notebookRuntimeTemplateResourceNamecustomEnvironmentSpecserviceAccountencryptionSpecworkbenchRuntimer*   )rV   @GoogleCloudAiplatformV1beta1NotebookExecutionJobWorkbenchRuntimerQ   CreateEncryptionSpecConfigkernel_namer4   
user_emailrK   0GoogleCloudAiplatformV1beta1NotebookExecutionJobrC   execution_display_namedisplay_namerH   r;   gcs_output_uriservice_account)r   r$   workbench_executionfor_scheduledataform_repository_sourcerU   workbench_runtimeexecution_userruntime_template_nameencryption_specrl   s              r   CreateNotebookExecutionJobrz     s    !%6tXFQQS  N ,GGhO ""K!Dh" #__N:4@OK		B	B9	  *4:	 --248"4T8D&&*?3))$(# 
C 
 r   r   r$   for_workbenchc                 \    t        |       }t        | ||d      }|j                  ||      S )ac  Builds a NotebookExecutionJobsCreateRequest message for a CreateSchedule request.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the specified API.
    for_workbench: Indicates whether this is a Workbench execution.

  Returns:
    Instance of the NotebookExecutionJobsCreateRequest message.
  T)rs   rt   )notebookExecutionJobparent)r   rz   =GoogleCloudAiplatformV1beta1CreateNotebookExecutionJobRequestr   r$   r{   r~   notebook_execution_jobs        r   'CreateExecutionCreateRequestForScheduler   @  sC     +40&5
H-d 
	O	O1 
P 
 r   c                 p    t        |       }t        | ||      }|j                  || j                  |      S )aF  Builds a NotebookExecutionJobsCreateRequest message.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the specified API.
    for_workbench: Indicates whether this is a Workbench execution.

  Returns:
    Instance of the NotebookExecutionJobsCreateRequest message.
  )rs   )0googleCloudAiplatformV1beta1NotebookExecutionJobnotebookExecutionJobIdr~   )r   rz   =AiplatformProjectsLocationsNotebookExecutionJobsCreateRequestexecution_job_idr   s        r   CreateExecutionCreateRequestr   Y  sJ     +40&5
H- 
	O	O7M!22 
P 
 r   c                 8    |j                  t        |             S )a  Builds a NotebookExecutionJobsDeleteRequest message.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the specified API.

  Returns:
    Instance of the NotebookExecutionJobsDeleteRequest message.
  rY   )=AiplatformProjectsLocationsNotebookExecutionJobsDeleteRequestr   r   r$   s     r   CreateExecutionDeleteRequestr   o  s%     LL'- M r   c                 8    |j                  t        |             S )zBuilds a NotebookExecutionsJobGetRequest message.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the specified API.

  Returns:
    Instance of the NotebookExecutionsJobGetRequest message.
  r   ):AiplatformProjectsLocationsNotebookExecutionJobsGetRequestr   r   s     r   r    r      s%     II'- J r   c                 8    |j                  t        |             S )zBuilds a NotebookExecutionJobsListRequest message.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the specified API.

  Returns:
    Instance of the NotebookExecutionJobsListRequest message.
  )r~   );AiplatformProjectsLocationsNotebookExecutionJobsListRequestr   r   s     r   CreateExecutionListRequestr     s%     
	M	M,T2 
N 
 r   )F)"__doc__types'googlecloudsdk.api_lib.colab_enterpriser   rQ   googlecloudsdk.callioper   r   googlecloudsdk.corer   googlecloudsdk.core.consoler   	Namespacer   r   r   r&   r(   r!   r4   r;   rC   rH   rK   rV   r\   rz   
ModuleTypeboolr   r   r   r    r    r   r   <module>r      s    /  _ . 5 ) 2 ''	0	5
864
****
+	H, 7<6x  
 2,$$r   