
    :                         d 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d Zd Zd Zd Zd Zd Zd Zd Zd Zy)z.colab-enterprise runtime-templates api helper.    )
exceptions)iam_util)	arg_utils)	resourcesc                 j    | j                   j                  j                         }|j                         S )zGet the parent Location resource name for the runtime template.

  Args:
    args: Argparse object from Command.Run

  Returns:
    The resource name in the form projects/{project}/locations/{location}.
  )CONCEPTSregionParseRelativeName)argsr	   s     @lib/googlecloudsdk/api_lib/colab_enterprise/runtime_templates.pyGetParentForRuntimeTemplater      s+     ==%%'&				    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 operation resource name

  Returns:
    The operation reference object
  z/notebookRuntimeTemplates/zAaiplatform.projects.locations.notebookRuntimeTemplates.operations)
collectionz(aiplatform.projects.locations.operations)r   REGISTRYParseRelativeName WrongResourceCollectionException)operation_names    r   ParseRuntimeTemplateOperationr   %   su     "^311
X 2   
			-	-!K 
. 
  55 
s    A AAc                 x    t         j                  j                  | j                  d      }|j	                         S )z1Transform a runtime template resource into a URL.z6aiplatform.projects.locations.notebookRuntimeTemplates)relative_namer   )r   r   r   nameSelfLink)resourceruntime_templates     r   GetRuntimeTemplateUrir   ;   s9    ''99MMI :  
	"	"	$$r   c                    |j                   }d}| j                  d      rOt        j                  d|j                  d       j                  t        j                  | j                              } || j                  || j                        S )zConstructs the machine spec from command line args.

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

  Returns:
      Machine spec config for the runtime template.
  Naccelerator_typezaccelerator-typec                      | dcxk7  xr | vS c S )NUNSPECIFIED xs    r   <lambda>z(GetMachineSpecFromArgs.<locals>.<lambda>T   s    m!<1!<r   arg_namemessage_enuminclude_filter)machineTypeacceleratorTypeacceleratorCount)
'GoogleCloudAiplatformV1beta1MachineSpecIsSpecifiedr   ChoiceEnumMapperAcceleratorTypeValueValuesEnumGetEnumForChoiceEnumNameToChoicer   machine_typeaccelerator_count)r   messagesmachine_spec_configaccelerator_type_enums       r   GetMachineSpecFromArgsr8   D   s     !HH	()%66#(GG< y11$2G2GHI	 
 
##+--
 r   c                 B    | j                         j                  dd      S )aX  Translates disk type user input to a format accepted by the API.

  The command input is kept in the enum format to be consistent with other
  arguments like accelerator type.

  Args:
    disk_type: The disk type enum value from user (eg PD_STANDARD).

  Returns:
    The disk type string value validated by AIPlatform API (eg pd-standard).
  _-)lowerreplace)	disk_types    r   FormatDiskTypeForApiRequestr?   ]   s     
		"	"3	,,r   c                     |j                   }| j                  d      r'| j                  d      st        j                  dd      t	        | j
                        } ||| j                        S )a  Constructs the persistent disk spec from command line args.

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

  Returns:
    Persistent disk spec config for the runtime template.
  disk_size_gbr>   z--disk-typez8Disk type must be specified when disk size is specified.)diskType
diskSizeGb).GoogleCloudAiplatformV1beta1PersistentDiskSpecr.   r   RequiredArgumentExceptionr?   r>   rA   )r   r5   persistent_disk_spec_configr>   s       r   GetPersistentDiskSpecFromArgsrG   l   sp     == 
 
n%d.>.>{.K

.
.B 
 ,DNN;I&t'8'8 r   c                 X   |j                   }d}d}| j                  d      r2| j                  j                  j	                         j                         }| j                  d      r2| j                  j                  j	                         j                         } |||| j                        S )zConstructs the network spec from command line args.

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

  Returns:
    Network spec config for the runtime template.
  Nnetwork
subnetwork)rI   rJ   enableInternetAccess)'GoogleCloudAiplatformV1beta1NetworkSpecr.   r   rI   r
   r   rJ   enable_internet_access)r   r5   network_spec_confignetwork_relative_namesubnetwork_relative_names        r   GetNetworkSpecFromArgsrQ      s     !HH!	i  MM11779FFH	l##}}77==?LLN	#)66
 r   c           
          | j                  d      r[|j                  j                  } || j                  j	                         D cg c]  \  }}|j                  ||       c}}      S yc c}}w )zConstructs the labels from command line args.

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

  Returns:
    Labels for the runtime template.
  labels)keyvalue)additionalPropertiesN)r.   3GoogleCloudAiplatformV1beta1NotebookRuntimeTemplateLabelsValuerS   itemsAdditionalProperty)r   r5   labels_messagerT   rU   s        r   GetLabelsFromArgsr\      s}     
hDDPP   #kk//1
1
U --#U-C1
  

s   A.
c                     |j                   }| j                  d      r2| j                  }|dk(  r	 |d      S t        |      dz   } |d|      S y)	zConstructs the idle shutdown config from command line args.

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

  Returns:
    Idle shutdown config for the runtime template.
  idle_shutdown_timeoutr   T)idleShutdownDisabledsF)r_   idleTimeoutN)6GoogleCloudAiplatformV1beta1NotebookIdleShutdownConfigr.   r^   str)r   r5   idle_shutdown_configduration_secondsduration_serializeds        r   GetIdleShutdownConfigFromArgsrg      sk     EE  
-.111!t<<./#5"0C  
r   c                 <    |j                  | j                         S )zConstructs the euc config from command line args.

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

  Returns:
    Euc config for the runtime template.
  )eucDisabled)-GoogleCloudAiplatformV1beta1NotebookEucConfig
enable_eucr   r5   s     r   GetEucConfigFromArgsrm      s'     
	?	?' 
@ 
 r   c                 :    |j                  | j                        S )zConstructs the shielded vm config from command line args.

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

  Returns:
    Shielded vm config for the runtime template.
  )enableSecureBoot),GoogleCloudAiplatformV1beta1ShieldedVmConfigenable_secure_bootrl   s     r   GetShieldedVmConfigFromArgsrr      s%     
	>	>.. 
? 
 r   c                 @    | j                  d      r| j                  S g S )Nnetwork_tags)r.   rt   r   s    r   GetNetworkTagsFromArgsrv      s     "..~>		FBFr   c                    |j                   }|j                  }|j                  }d}| j                  d      rOt	        j
                  d|j                  d       j                  t	        j                  | j                              }g }| j                  d      r.| j                  D cg c]  } ||| j                  |          }} || ||| j                  | j                        	      S c c}w )
z6Constructs the software config from command line args.Npost_startup_script_behaviorzpost-startup-script-behaviorc                     | dk7  S )N(POST_STARTUP_SCRIPT_BEHAVIOR_UNSPECIFIEDr"   r#   s    r   r%   z+GetSoftwareConfigFromArgs.<locals>.<lambda>   s    5"6r   r&   set_env_vars)r   rU   )postStartupScriptBehaviorpostStartupScriptpostStartupScriptUrl)envpostStartupScriptConfig)"GoogleCloudAiplatformV1beta1EnvVar3GoogleCloudAiplatformV1beta1PostStartupScriptConfig2GoogleCloudAiplatformV1beta1NotebookSoftwareConfigr.   r   r/   (PostStartupScriptBehaviorValueValuesEnumr1   r2   rx   r{   post_startup_scriptpost_startup_script_url)r   r5   env_varpost_startup_script_configsoftware_config!post_startup_script_behavior_enumenv_varsks           r   GetSoftwareConfigFromArgsr      s    77'BB  OO/&*#	45(1(B(B//XX6)
 ""4#D#DE & (	n%=A=N=N=NQd//23=N   

8$E 44#;;
 s   (C/c                     |j                   }| j                  d      r9 || j                  j                  j	                         j                               S y)zConstructs the encryption spec from the kms key resource arg.

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

  Returns:
    Encryption spec for the notebook resource.
  kms_key)
kmsKeyNameN)*GoogleCloudAiplatformV1beta1EncryptionSpecr.   r   r   r
   r   )r   r5   encryption_specs      r   CreateEncryptionSpecConfigr     sN     GG/	i ==((..0==?  
r   c                 f    | j                   j                  j                         j                         S )zGet the resource name for the runtime template.

  Args:
    args: Argparse object from Command.Run

  Returns:
    The resource name in the form
    projects/{project}/locations/{location}/notebookRuntimeTemplates/{runtime_template_id}.
  )r   r   r
   r   ru   s    r   GetRuntimeTemplateResourceNamer   &  s&     
	'	'	-	-	/	<	<	>>r   c                 @   |j                  | j                  | j                  | j                  t	        | |      t        | |      t        | |      t        | |      t        | |      t        | |      t        | |      t        |       t        | |      t        | |            S )zCreates the RuntimeTemplate message for the create request.

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

  Returns:
    Instance of the RuntimeTemplate message.
  )r   displayNamedescriptionmachineSpecdataPersistentDiskSpecnetworkSpecrS   idleShutdownConfig	eucConfigshieldedVmConfignetworkTagsencryptionSpecsoftwareConfig)rW   runtime_template_iddisplay_namer   r8   rG   rQ   r\   rg   rm   rr   rv   r   r   rl   s     r   CreateRuntimeTemplater   3  s     
	E	E####""(x8:4J(x8tX.6tXF$T8424B(./h?.tX> 
F 
 r   c                 l    t        |       }t        | |      }|j                  || j                  |      S )a  Builds a CreateNotebookRuntimeTemplateRequest message.

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

  Returns:
    Instance of the CreateNotebookRuntimeTemplateRequest message.
  )3googleCloudAiplatformV1beta1NotebookRuntimeTemplatenotebookRuntimeTemplateIdparent)r   r   @AiplatformProjectsLocationsNotebookRuntimeTemplatesCreateRequestr   )r   r5   r   r   s       r   "CreateRuntimeTemplateCreateRequestr   N  sC     't,&*4:OO>N$($<$< P r   c                 8    |j                  t        |             S )a  Builds a DeleteNotebookRuntimeTemplateRequest message.

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

  Returns:
    Instance of the DeleteNotebookRuntimeTemplateRequest message.
  r   )@AiplatformProjectsLocationsNotebookRuntimeTemplatesDeleteRequestr   rl   s     r   "CreateRuntimeTemplateDeleteRequestr   d  s%     OO-d3 P r   c                 8    |j                  t        |             S )a   Builds a GetNotebookRuntimeTemplateRequest message.

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

  Returns:
    Instance of the GetNotebookRuntimeTemplateRequest message.
  r   )=AiplatformProjectsLocationsNotebookRuntimeTemplatesGetRequestr   rl   s     r   CreateRuntimeTemplateGetRequestr   v  s%     LL-d3 M r   c                 8    |j                  t        |             S )a  Builds a ListNotebookRuntimeTemplatesRequest message.

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

  Returns:
    Instance of the ListNotebookRuntimeTemplatesRequest message.
  )r   )>AiplatformProjectsLocationsNotebookRuntimeTemplatesListRequestr   rl   s     r    CreateRuntimeTemplateListRequestr     s%     MM,T2 N r   c                 8    |j                  t        |             S )a  Builds a RuntimeTemplatesGetIamPolicyRequest message.

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

  Returns:
    Instance of the RuntimeTemplatesGetIamPolicyRequest message.
  )r   )FAiplatformProjectsLocationsNotebookRuntimeTemplatesGetIamPolicyRequestr   rl   s     r   (CreateRuntimeTemplateGetIamPolicyRequestr     s%     
	X	X-d3 
Y 
5 5r   c                 ^    |j                  |       }|j                  |t        |            S )a+  Builds a RuntimeTemplatesSetIamPolicyRequest message.

  Args:
    iam_policy: The IAM policy to set.
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the specified API.

  Returns:
    Instance of the RuntimeTemplatesSetIamPolicyRequest message.
  )policy)googleIamV1SetIamPolicyRequestr   )GoogleIamV1SetIamPolicyRequestFAiplatformProjectsLocationsNotebookRuntimeTemplatesSetIamPolicyRequestr   )
iam_policyr   r5   google_iam_set_policy_requests       r   (CreateRuntimeTemplateSetIamPolicyRequestr     sB     #+"I"I #J #		X	X%B-d3 
Y 
 r   c                 p    t        j                  | j                  |j                        }t	        || |      S )a  Reads policy file from args to build a RuntimeTemplatesSetIamPolicyRequest.

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

  Returns:
    Instance of the RuntimeTemplatesSetIamPolicyRequest message.
  )r   ParsePolicyFilepolicy_fileGoogleIamV1Policyr   )r   r5   r   s      r   0CreateRuntimeTemplateSetIamPolicyRequestFromFiler     s4     ##
224&	1&$	IIr   N) __doc__googlecloudsdk.callioper   googlecloudsdk.command_lib.iamr   $googlecloudsdk.command_lib.util.apisr   googlecloudsdk.corer   r   r   r   r8   r?   rG   rQ   r\   rg   rm   rr   rv   r   r   r   r   r   r   r   r   r   r   r   r"   r   r   <module>r      s    5 . 3 : ),%2-82.2G@&
?6,$$$5&Jr   