
    3                        S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  \R                  rS rS	 rS
 r SS jrS rS rS rS rS rS rS rS rS r SS jr SS\S\R6                  S\4S jjrSS jrS rS r S r!g)z(Notebook-executor executions api helper.    N)runtime_templates)
exceptions)parser_extensions)	resources)
console_ioc                     SU ;   a   [         R                  R                  U SS9$ [         R                  R                  U SS9$ ! [         R                   a     N4f = f)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    U R                   R                  R                  5       R                  5       $ )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    U R                   R                  R                  5       R                  5       $ )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                     UR                  [        X5      5      n[        U5      (       d  U(       d  [        R                  " SS5      eU$ )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.
  skip_workbench_check: Whether to skip validation of the execution type.

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serviceskip_workbench_checkr   s        r    ValidateAndGetWorkbenchExecutionr&   N   sM    " kk/) 
i	(	(1E

-
-	2 
 
r   c                     UR                  [        X5      5      n[        U5      (       a  [        R                  " SS5      eU$ )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   r#   r$   r   s       r   ValidateAndGetColabExecutionr(   k   sJ     kk/) )$$

-
-	; 
 
r   c                     U R                   SL$ )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                 v    S nU R                  S5      (       a   UR                  U" U 5      U R                  S9$ g)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    U R                   R                  R                  5       R                  5       $ )N)r   dataform_repository_namer   r   r   s    r   !GetDataformRepositoryResourceNameNGetDataformRepositorySourceFromArgs.<locals>.GetDataformRepositoryResourceName   s$    ==11779FFHHr   r-   )dataformRepositoryResourceName	commitShaN)IsSpecifiedHGoogleCloudAiplatformV1beta1NotebookExecutionJobDataformRepositorySource
commit_sha)r   r#   r.   s      r   #GetDataformRepositorySourceFromArgsr5      sI    I 
011\\'H'N// ]   
r   c                 ~    UR                   nU R                  S5      (       a  U" U R                  U R                  S9$ g)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)AGoogleCloudAiplatformV1beta1NotebookExecutionJobGcsNotebookSourcer2   r7   r9   )r   r#   gcs_notebook_sources      r   GetGcsNotebookSourceFromArgsr<      sH     PP  
())!!??  
r   c                     UR                   nU R                  S5      (       a#  U" [        R                  " U R                  SS9S9$ g)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)DGoogleCloudAiplatformV1beta1NotebookExecutionJobDirectNotebookSourcer2   r   ReadFromFileOrStdinr>   )r   r#   notebook_sources      r   GetDirectNotebookSourceFromArgsrD      sN     aa/	&'' ..t/B/B6:<  
r   c                 2    [        U R                  5      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   GetExecutionTimeoutFromArgsrI      s     
T##	$s	**r   c                 f    U R                   R                  R                  5       R                  5       $ )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   GetRuntimeTemplateResourceNamerL      s&     
	0	0	6	6	8	E	E	GGr   c                     UR                   nU" [        R                  " X5      [        R                  " X5      [        R                  " X5      S9$ )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   GetCustomEnvironmentSpecrW      sN     TT  
!(??O(??O/MM

 r   c                 r    [         R                  R                  U R                  SS9nUR	                  5       $ )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                    U(       aD  Sn[        X5      nUR                  5       nSnSn[        R                  " X5      n	U R                  n
O*[        X5      nSnSnU R                  n[        U 5      nSn	Sn
UR                  UU(       a  SO
[        X5      U(       a  U R                  OU R                  [        U 5      U[        X5      U R                  UUU R                  U	UU
S9$ )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*   )rW   @GoogleCloudAiplatformV1beta1NotebookExecutionJobWorkbenchRuntimerR   CreateEncryptionSpecConfigkernel_namer5   
user_emailrL   0GoogleCloudAiplatformV1beta1NotebookExecutionJobrD   execution_display_namedisplay_namerI   r<   gcs_output_uriservice_account)r   r#   workbench_executionfor_scheduledataform_repository_sourcerV   workbench_runtimeexecution_userruntime_template_nameencryption_specrm   s              r   CreateNotebookExecutionJobr{   	  s    !%6tFQQS  N ,GGO ""K!D" #__N:4@OK		B	B9	  *4:	 --248"4TD&&*?3))$(# 
C 
 r   r   r#   for_workbenchc                 N    [        U 5      n[        XUSS9nUR                  UUS9$ )aU  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)rt   ru   )notebookExecutionJobparent)r   r{   =GoogleCloudAiplatformV1beta1CreateNotebookExecutionJobRequestr   r#   r|   r   notebook_execution_jobs        r   'CreateExecutionCreateRequestForScheduler   B  sA     +40&5
-d 
	O	O1 
P 
 r   c                 b    [        U 5      n[        XUS9nUR                  UU R                  US9$ )a8  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.
)rt   )0googleCloudAiplatformV1beta1NotebookExecutionJobnotebookExecutionJobIdr   )r   r{   =AiplatformProjectsLocationsNotebookExecutionJobsCreateRequestexecution_job_idr   s        r   CreateExecutionCreateRequestr   [  sH     +40&5
- 
	O	O7M!22 
P 
 r   c                 2    UR                  [        U 5      S9$ )zBuilds 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.
rZ   )=AiplatformProjectsLocationsNotebookExecutionJobsDeleteRequestr   r   r#   s     r   CreateExecutionDeleteRequestr   q  s%     LL'- M r   c                 2    UR                  [        U 5      S9$ )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                 2    UR                  [        U 5      S9$ )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   rR   googlecloudsdk.callioper   r   googlecloudsdk.corer   googlecloudsdk.core.consoler   	Namespacer   r   r   r&   r(   r!   r5   r<   rD   rI   rL   rW   r]   r{   
ModuleTypeboolr   r   r   r    r    r   r   <module>r      s    /  _ . 5 ) 2 ''	0	5
8 38:4
****
+	H, 7<6x  
 2,$$r   