
    *              	          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 ddlmZ ej                  Zej                  Zd	 Zd
 Zd Zd Zd Z	 	 ddedej*                  dedefdZd Zdedej*                  dej2                  fdZdedej*                  dej2                  fdZd Zd Zd Zd Zd Z 	 d dedej*                  defdZ!d Z"d Z#y)!z'Notebook-executor schedules api helper.    N)
executions)
exceptions)parser_extensions)	resources)times)aiplatform_v1beta1_clientc                 f    | j                   j                  j                         j                         S )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                     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 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                 t    t         j                  j                  | j                  d      j	                         S )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                 \    | j                   rt        j                  | j                         S dS )z$Get the start time for the schedule.N)
start_timer   FormatDateTimer   s    r   GetStartTimer#   L   s"    26//		doo	.KtKr   c                 \    | j                   rt        j                  | j                         S d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           
          d}|st        j                  | ||      }|j                  | j                  t	        |       t        |       | j                  | j                  | j                  | j                  |      S )a]  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                 T    t        j                  | j                  j                        S )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                     |j                  t        | |            }|j                  }|t        j                  dd      t        j                  |j                        rt        j                  dd      |S )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   ValidateAndGetColabSchedulerJ      s    & [[1$AB(#+#M#M #+

-
-A  ))$99 
-
-	: 
 
/r   c                     |j                  t        | |            }|j                  }|t        j                  dd      |j
                  j                  t        j                  dd      |S )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.

  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?   
kernelNamerH   s        r   ValidateAndGetWorkbenchSchedulerM      s    $ [[tX.( $,#M#M #+

-
-A  $88CCK

-
-	; 
 
/r   c                 8    |j                  t        |             S )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                 8    |j                  t        |             S )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                 8    |j                  t        |             S )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                 r    |j                  | j                        }|j                  t        |       |      S )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                 N    |j                  t        j                  |       d      S )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           	      h    |j                  t        j                  |       t        | |d|            S )a0  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   .  s<     
	C	C<<TB+9
U-, 
D 
 r   c                     g }dddddddd}|j                         D ](  \  }}| j                  |      s|j                  |       * d	j                  t	        t
        |            S )
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   C  ss     +$ !7) &++-jc5 . 
#c;'	((r   c                 f    |j                  t        |       t        | |d      t        |             S )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}   \  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@   !ProjectsLocationsSchedulesServicerJ   rM   rF   rT   rW   r_   re   ri   ry   r}    r   r   <module>r      s5   .  R . 5 ) * ^ .?? ''	
70L
H 	!
!! ! 	!H!
!! @@!H 
   @@ F$$$2" HM
$//@D*)2r   