
    k*                         d Z ddlmZ ddlmZ ddlm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
 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 G d dej6                        Zej:                  fdZy)znotebooks runtimes api helper.    )absolute_import)division)unicode_literalsN)util)	arg_utils)log)	resourcesc                       fd} fd} fd fd}j                   j                   |        |        |             }|S )zCreates the Runtime message for the create request.

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

  Returns:
    Runtime of the Runtime message.
  c                      d} j                  d      rj                  } j                  | j                               }j	                  |      S )Nn1-standard-4machine_type)machineTypedataDisk)virtualMachineConfig)IsSpecifiedr   VirtualMachineConfig	LocalDiskVirtualMachine)r   virtual_machine_configargsmessagess     0lib/googlecloudsdk/api_lib/notebooks/runtimes.py GetRuntimeVirtualMachineFromArgsz7CreateRuntime.<locals>.GetRuntimeVirtualMachineFromArgs'   s[    "L'&&l%:: 8+=+=+? ; A""8N"OO    c                     j                   } d }j                  d      rOt        j                  d| j                  d       j                  t        j                  j                              } | |j                        S )Nruntime_access_typezruntime-access-typec                 
    d| vS NUNSPECIFIED xs    r   <lambda>zGCreateRuntime.<locals>.GetRuntimeAccessConfigFromArgs.<locals>.<lambda>6   	    =#9r   arg_namemessage_enuminclude_filter)
accessTyperuntimeOwner)	RuntimeAccessConfigr   r   ChoiceEnumMapperAccessTypeValueValuesEnumGetEnumForChoiceEnumNameToChoicer   runtime_owner)runtime_access_config	type_enumr   r   s     r   GetRuntimeAccessConfigFromArgsz5CreateRuntime.<locals>.GetRuntimeAccessConfigFromArgs/   s    $88I-.,,(,FF9; <L;K(()A)AB<D 
 !4+=+=? ?r   c                      d } j                  d      r[j                  }t        j                  d|j                  d       j                  t        j                  j                              } | S )Npost_startup_script_behaviorzpost-startup-script-behaviorc                 
    d| vS r   r    r!   s    r   r#   zECreateRuntime.<locals>.GetPostStartupScriptBehavior.<locals>.<lambda>C   r$   r   r%   )r   RuntimeSoftwareConfigr   r,   ,PostStartupScriptBehaviorTypeValueValuesEnumr.   r/   r5   )r2   runtime_software_config_messager   r   s     r   GetPostStartupScriptBehaviorz3CreateRuntime.<locals>.GetPostStartupScriptBehavior;   sp    I67(0(F(F%,,17EE9	; <L;K(()J)JK<M	  r   c                  r   j                         } j                  d      rj                  | _        j                  d      rj                  | _        j                  d      rj                  | _        j                  d      rj                  | _	        j                  d      r        | _
        | S )Nidle_shutdown_timeoutinstall_gpu_drivercustom_gpu_driver_pathpost_startup_scriptr5   )r7   r   r<   idleShutdownTimeoutr=   installGpuDriverr>   customGpuDriverPathr?   postStartupScriptpostStartupScriptBehavior)runtime_software_configr:   r   r   s    r    GetRuntimeSoftwareConfigFromArgsz7CreateRuntime.<locals>.GetRuntimeSoftwareConfigFromArgsG   s    &<<>/0484N4N1,-151H1H.01484O4O1-.262J2J/67
&
( 7""r   )namevirtualMachineaccessConfigsoftwareConfig)Runtimeruntime)r   r   r   r3   rF   rL   r:   s   ``    @r   CreateRuntimerM      sO    P
?
# <<571357	  ' 
.r   c                     t        j                  |       }t        | |      }|j                  ||| j                        S )N)parentrL   	runtimeId)r   GetParentForRuntimerM   /NotebooksProjectsLocationsRuntimesCreateRequestrL   )r   r   rO   rL   s       r   CreateRuntimeCreateRequestrS   _   sB    ##D)&$)'		A	AW 
B 
> >r   c                 P    t        j                  |       }|j                  |      S )N)rO   )r   GetParentFromArgs-NotebooksProjectsLocationsRuntimesListRequest)r   r   rO   s      r   CreateRuntimeListRequestrW   f   s&    !!$'&		?	?v	?	NNr   c                 J    | j                   j                  j                         S )N)CONCEPTSrL   Parser   s    r   GetRuntimeResourcer\   k   s    				$	$	&&r   c                    d}| j                  d      r| j                  }|j                         }| j                  d      r| j                  |_        | j                  d      rVt        j                  d|j                  d       j                  t        j                  | j                              }||_        |j                  ||      S )	z)Create and return switch runtime request.r   r   accelerator_core_countaccelerator_typezaccelerator-typec                 
    d| vS r   r    r!   s    r   r#   z)GetSwitchRuntimeRequest.<locals>.<lambda>{   s	    a!7r   r%   )r   acceleratorConfig)r   r   RuntimeAcceleratorConfigr^   	coreCountr   r,   TypeValueValuesEnumr.   r/   r_   typeSwitchRuntimeRequest)r   r   r   runtime_accelerator_configr2   s        r   GetSwitchRuntimeRequestrh   o   s     ,	n%$$L'@@B	./+/+F+F(	()**#/CC79 :J9I&&t'<'<=:? 
 '0#		&	&2L 
' 
N Nr   c                 X    t        |       j                         }|j                  |      S N)rG   )r\   RelativeName/NotebooksProjectsLocationsRuntimesDeleteRequestr   r   rL   s      r   CreateRuntimeDeleteRequestrn      s1    t$113'		A	A 
B 
 r   c                 z    t        |       j                         }|j                         }|j                  ||      S )N)rG   resetRuntimeRequest)r\   rk   ResetRuntimeRequest.NotebooksProjectsLocationsRuntimesResetRequest)r   r   rL   reset_requests       r   CreateRuntimeResetRequestrt      A    t$113'..0-		@	@ 
A 
7 7r   c                 z    t        |       j                         }|j                         }|j                  ||      S )N)rG   startRuntimeRequest)r\   rk   StartRuntimeRequest.NotebooksProjectsLocationsRuntimesStartRequest)r   r   rL   start_requests       r   CreateRuntimeStartRequestr{      ru   r   c                 z    t        |       j                         }|j                         }|j                  ||      S )N)rG   stopRuntimeRequest)r\   rk   StopRuntimeRequest-NotebooksProjectsLocationsRuntimesStopRequest)r   r   rL   stop_requests       r   CreateRuntimeStopRequestr      sA    t$113',,.,		?	?| 
@ 
5 5r   c                 r    t        |       j                         }t        | |      }|j                  ||      S )N)rG   switchRuntimeRequest)r\   rk   rh   /NotebooksProjectsLocationsRuntimesSwitchRequest)r   r   rL   switch_requests       r   CreateRuntimeSwitchRequestr      s?    t$113'*4:.		A	A 
B 
9 9r   c                 X    t        |       j                         }|j                  |      S rj   )r\   rk   ,NotebooksProjectsLocationsRuntimesGetRequestrm   s      r   CreateRuntimeDescribeRequestr      s)    t$113'		>	>G	>	LLr   c                    t        |       j                         }|j                  | j                        }| j	                  d      r| j
                  |_        | j	                  d      rd|_        | j	                  d      rd|_        | j	                  d      rd|_	        d}| j	                  d      rt        | j                        }|j                  ||j                  ||	      
      S )z$"Create and return Diagnose request.)	gcsBucketrelative_pathzenable-repairTzenable-packet-capturezenable-copy-home-filesNtimeout_minutes)diagnosticConfigtimeoutMinutes)rG   diagnoseRuntimeRequest)r\   rk   DiagnosticConfig
gcs_bucketr   r   relativePathrepairFlagEnabledpacketCaptureFlagEnabledcopyHomeFilesFlagEnabledintr   1NotebooksProjectsLocationsRuntimesDiagnoseRequestDiagnoseRuntimeRequest)r   r   rL   diagnostic_configr   s        r   CreateRuntimeDiagnoseRequestr      s    t$113'// 0  
o&%)%7%7"	o&*.'	-.15.	./15./	'($../O		C	C8+J+J,_ ,K ,N 
D 
O Or   c           
           t               j                         } fd} fd} fd}j                  |j                   |        |        j                   |                   S )z#"Create and return Migrate request.c                       j                  d      r2 j                  j                  j                         j	                         S y )Nnetwork)r   rY   r   rZ   rk   r[   s   r   GetNetworkRelativeNamez;CreateRuntimeMigrateRequest.<locals>.GetNetworkRelativeName   s8    	"]]""((*7799 #r   c                       j                  d      r2 j                  j                  j                         j	                         S y )Nsubnet)r   rY   r   rZ   rk   r[   s   r   GetSubnetRelativeNamez:CreateRuntimeMigrateRequest.<locals>.GetSubnetRelativeName   s8    !]]!!'')6688 "r   c                      d } j                  d      r[j                  }t        j                  d|j                  d       j                  t        j                  j                              } | S )Npost_startup_script_optionzpost-startup-script-optionc                 
    d| vS r   r    r!   s    r   r#   zQCreateRuntimeMigrateRequest.<locals>.GetPostStartupScriptOption.<locals>.<lambda>   r$   r   r%   )r   MigrateRuntimeRequestr   r,   &PostStartupScriptOptionValueValuesEnumr.   r/   r   )r2   request_messager   r   s     r   GetPostStartupScriptOptionz?CreateRuntimeMigrateRequest.<locals>.GetPostStartupScriptOption   so    I45 66o,,/&MM9; <L;K(()H)HI<K 
 r   )r   r   serviceAccountpostStartupScriptOption)rG   migrateRuntimeRequest)r\   rk   0NotebooksProjectsLocationsRuntimesMigrateRequestr   service_account)r   r   rL   r   r   r   s   ``    r   CreateRuntimeMigrateRequestr      sq    t$113':9	 
	B	B$::(*&(--"<">	 ;  
C 
	 	r   c                 x    t         j                  j                  | j                  d      }|j	                         S )Nz%notebooks.projects.locations.runtimes)
collection)r	   REGISTRYParseRelativeNamerG   SelfLink)resourceinstances     r   GetRuntimeURIr      s7    11mm G 2 I(				r   c                       e Zd Zej                  dfZej                  dfZej                  dfZ	ej                  dfZej                  dfZy)OperationTypecreateddeletedupdatedresetmigratedN)__name__
__module____qualname__r   CreatedResourceCREATEDeletedResourceDELETEUpdatedResourceUPDATEResetResourceRESETMIGRATEr    r   r   r   r      sR    +&+&+&g
&%  *-'r   r   c                     |j                   d   }|j                  rD |t        j                  | j                  |      dj                  |j                        d       | S t        j                  | dj                  |j                  |j                   d   | j                        |||j                   d   dk(        } |t        j                  | j                  |      dj                  |j                        d	       |S )
aH  Handles Long Running Operations for both cases of async.

  Args:
    operation: The operation to poll.
    args: ArgParse instance containing user entered arguments.
    runtime_service: The service to get the resource after the long running
      operation completes.
    release_track: base.ReleaseTrack object.
    operation_type: Enum value of type OperationType indicating the kind of
      operation to wait for.

  Raises:
    apitools.base.py.HttpError: if the request returns an HTTP error

  Returns:
    The Runtime resource if synchronous, else the Operation Resource.
  r   znotebooks runtime {0}T)kindis_asyncz8Waiting for operation on Runtime [{}] to be {} with [{}]   r   )servicerelease_track	is_deleteF)valueasync_r   GetOperationResourcerG   formatrL   WaitForOperation)	operationr   runtime_servicer   operation_typelogging_methodresponses          r   	HandleLROr      s    , "''*.	[[!!)..-@$++DLL9 $$BIILL...q19>>	C#!''*i7:H !!)..-@$++DLL9 Or   )__doc__
__future__r   r   r   enum googlecloudsdk.api_lib.notebooksr   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   r	   rM   rS   rW   r\   rh   rn   rt   r{   r   r   r   r   r   r   Enumr   r   r   r    r   r   <module>r      s    % &  '  1 : # )@F>O
'N&7759M
O0	B.DII . +11	)r   