
    +              	          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  SSKJr  \R                  r\R                  rS	 rS
 rS rS rS r  S S\S\R*                  S\S\4S jjrS rS\S\R*                  S\R2                  4S jr S!S\S\R*                  S\R2                  S\4S jjrS rS rS rS rS r  S!S\S\R*                  S\4S jjr!S r"S r#g)"z'Notebook-executor schedules api helper.    N)
executions)
exceptions)parser_extensions)	resources)times)aiplatform_v1beta1_clientc                 f    U R                   R                  R                  5       R                  5       $ )zGet the resource name for the schedule.

Args:
  args: Argparse object from Command.Run

Returns:
  The resource name in the form
  projects/{project}/locations/{location}/schedules/{schedule_id}.
)CONCEPTSscheduleParseRelativeNameargss    9lib/googlecloudsdk/api_lib/notebook_executor/schedules.pyGetScheduleResourceNamer      s&     
			%	%	'	4	4	66    c                     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 schedule operation resource name

Returns:
  The operation reference object
z/schedules/z2aiplatform.projects.locations.schedules.operations)
collectionz(aiplatform.projects.locations.operations)r   REGISTRYParseRelativeName WrongResourceCollectionException)operation_names    r   ParseScheduleOperationr   ,   su     n$11
B 2   
			-	-!K 
. 
  55 
s   A AAc                 n    [         R                  R                  U R                  SS9R	                  5       $ )z$Get the URL for a schedule resource.z'aiplatform.projects.locations.schedules)relative_namer   )r   r   r   nameSelfLink)resources    r   GetScheduleUrir   D   s3    				-	-MM: 
. 
 HJr   c                 h    U R                   (       a   [        R                  " U R                   5      $ S$ )z$Get the start time for the schedule.N)
start_timer   FormatDateTimer   s    r   GetStartTimer#   L   s"    26//		doo	.KtKr   c                 h    U R                   (       a   [        R                  " U R                   5      $ S$ )z"Get the end time for the schedule.N)end_timer   r"   r   s    r   
GetEndTimer&   Q   s"    04		dmm	,G4Gr   r   messages
for_updatefor_workbenchc                     SnU(       d  [         R                  " XU5      nUR                  U R                  [	        U 5      [        U 5      U R                  U R                  U R                  U R                  US9$ )aM  Builds a Schedule message.

Args:
  args: Argparse object from Command.Run
  messages: Module containing messages definition for the specified API.
  for_update: Whether the schedule is to be used in an update request.
  for_workbench: Whether the schedule is for a Workbench execution.

Returns:
  Instance of the Schedule message.
N)displayName	startTimeendTimemaxRunCountcronmaxConcurrentRunCountallowQueueing!createNotebookExecutionJobRequest)
executions_util'CreateExecutionCreateRequestForSchedule$GoogleCloudAiplatformV1beta1Scheduledisplay_namer#   r&   max_runscron_schedulemax_concurrent_runsenable_queueing)r   r'   r(   r)   execution_create_requests        r   CreateScheduler<   V   s}    " "	??M	
 
 
	6	6##T"-- 44(((@ 
7 	
 	r   c                 V    [         R                  " U R                  R                  5      $ )zList filter for Workbench schedules.

Args:
  schedule: The schedule item returned from List API to check.

Returns:
  True if the schedule is for a Workbench notebook execution.
)r3   IsWorkbenchExecutionr2   notebookExecutionJob)r   s    r   FilterWorkbenchScheduler@   z   s&     
	-	-00EE
 r   servicec                     UR                  [        X5      5      nUR                  nUc  [        R                  " SS5      e[
        R                  " UR                  5      (       a  [        R                  " SS5      eU$ )a  Checks if the schedule is for a Colab Enterprise notebook execution and returns the schedule if so.

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

Returns:
  The schedule if it is of Colab Enterprise type.

Raises:
  InvalidArgumentException: If the schedule is not of notebook execution type
  or is of Workbench type.
SCHEDULE+Schedule is not of notebook execution type.zlSchedule is not of Colab Enterprise type. To manage Workbench schedules use `gcloud beta workbench` instead.)GetCreateScheduleGetRequestr2   r   InvalidArgumentExceptionr3   r>   r?   )r   r'   rA   r   notebook_execution_job_requests        r   ValidateAndGetColabSchedulerI      s    & [[1$AB(#+#M#M #+

-
-A  ))$99  
-
-	: 
 
/r   skip_workbench_checkc                     UR                  [        X5      5      nUR                  nUc  [        R                  " SS5      eUR
                  R                  c  U(       d  [        R                  " SS5      eU$ )a  Checks if the schedule is for a Workbench notebook execution and returns the schedule if so.

Args:
  args: Argparse object from Command.Run
  messages: Module containing messages definition for the specified API.
  service: The service to use to make the request.
  skip_workbench_check: Whether to skip validation of the schedule type.

Returns:
  The schedule if it is of Workbench type.

Raises:
  InvalidArgumentException: If the schedule is not of notebook execution type.
rC   rD   zmSchedule is not of Workbench type. To manage Colab Enterprise schedules use `gcloud colab schedules` instead.)rE   rF   r2   r   rG   r?   
kernelName)r   r'   rA   rJ   r   rH   s         r   ValidateAndGetWorkbenchSchedulerM      s    ( [[t.( $,#M#M #+

-
-A  %99DDL"

-
-	; 
 
/r   c                 2    UR                  [        U 5      S9$ )zBuilds a SchedulesGetRequest message.

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

Returns:
  Instance of the SchedulesGetRequest message.
r   ).AiplatformProjectsLocationsSchedulesGetRequestr   r   r'   s     r   rF   rF      s$     ==&t, > r   c                 2    UR                  [        U 5      S9$ )zBuilds a SchedulesDeleteRequest message.

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

Returns:
  Instance of the SchedulesDeleteRequest message.
rO   )1AiplatformProjectsLocationsSchedulesDeleteRequestr   rQ   s     r   CreateScheduleDeleteRequestrT      s%     @@&t, A r   c                 2    UR                  [        U 5      S9$ )zBuilds a SchedulesPauseRequest message.

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

Returns:
  Instance of the SchedulesPauseRequest message.
rO   )0AiplatformProjectsLocationsSchedulesPauseRequestr   rQ   s     r   CreateSchedulePauseRequestrW      s%     ??&t, @ r   c                 f    UR                  U R                  S9nUR                  [        U 5      US9$ )zBuilds a SchedulesResumeRequest message.

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

Returns:
  Instance of the SchedulesResumeRequest message.
)catchUp)r   1googleCloudAiplatformV1beta1ResumeScheduleRequest)1GoogleCloudAiplatformV1beta1ResumeScheduleRequestenable_catch_up1AiplatformProjectsLocationsSchedulesResumeRequestr   )r   r'   resume_schedule_requests      r   CreateScheduleResumeRequestr_   
  sL     @@&& A   @@&t,% A r   c                 J    UR                  [        R                  " U 5      SS9$ )zBuilds a SchedulesListRequest message.

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

Returns:
  Instance of the SchedulesListRequest message.
z'create_notebook_execution_job_request:*)parentfilter)/AiplatformProjectsLocationsSchedulesListRequestr3   GetParentForExecutionOrSchedulerQ   s     r   CreateScheduleListRequestre   #  s.     
	A	A<<TB6 
B 
 r   c           
      \    UR                  [        R                  " U 5      [        XSUS9S9$ )a"  Builds a SchedulesCreateRequest message.

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

Returns:
  Instance of the SchedulesCreateRequest message.
F)r(   r)   )ra   $googleCloudAiplatformV1beta1Schedule)1AiplatformProjectsLocationsSchedulesCreateRequestr3   rd   r<   )r   r'   r)   s      r   CreateScheduleCreateRequestri   3  s:     
	C	C<<TB+9
U-, 
D 
 r   c                     / nSSSSSSSS.nUR                  5        H.  u  p4U R                  U5      (       d  M  UR                  U5        M0     S	R                  [	        [
        U5      5      $ )
zBuilds a field mask for the schedule update request.

Args:
  args: Argparse object from Command.Run

Returns:
  Field mask for the schedule update request.
r6   r!   r%   max_run_countr/   max_concurrent_run_countallow_queueing)r6   r!   r%   r7   r8   r9   r:   ,)itemsIsSpecifiedappendjoinmapstr)r   mask_fieldsargs_to_field_mapkeyvalues        r   CreateScheduleUpdateMaskry   H  st     +$ !7) &++-jc . 
#c;'	((r   c           	      X    UR                  [        U 5      [        XSS9[        U 5      S9$ )zBuilds a SchedulesPatchRequest message.

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

Returns:
  Instance of the SchedulesPatchRequest message.
T)r(   )r   rg   
updateMask)0AiplatformProjectsLocationsSchedulesPatchRequestr   r<   ry   rQ   s     r   CreateSchedulePatchRequestr}   a  s<     
	B	B"4(+9
T, *$/ 
C 
 r   )FF)F)$__doc__types(googlecloudsdk.api_lib.notebook_executorr   r3   googlecloudsdk.callioper   r   googlecloudsdk.corer   googlecloudsdk.core.utilr   8googlecloudsdk.generated_clients.apis.aiplatform.v1beta1r   AiplatformV1beta1	Namespacer   r   r   r#   r&   
ModuleTypeboolr<   r@   !ProjectsLocationsSchedulesServicerI   rM   rF   rT   rW   r_   re   ri   ry   r}    r   r   <module>r      sD   .  R . 5 ) * ^ .?? ''	
70L
H 	!
!! ! 	!H!
!! @@!P "'	%
%% @@% 	%P$$$2" HM
$//@D*)2r   