
    -                     z    d 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 ddl
mZ d	 Zd
 Zd Zd Zd Zy)zGValidation of the arguments for the persistent-resources command group.    )absolute_import)division)unicode_literals)util)
exceptions)	constants)
validationc                 N    t        j                  | t        j                         y)zFValidate whether the given region is allowed for persistent resources.)available_regionsN)r	   ValidateRegionr   SUPPORTED_TRAINING_REGIONS)regions    Dlib/googlecloudsdk/command_lib/ai/persistent_resources/validation.pyr   r      s    	 D DF    c                 "   | j                   rt        | j                   |       t        |j                  t              rt        |j                  |       | j                   s1t        |j                  t              st        j                  dd      yy)z?Validate the argument values specified in the `create` command.--resource-pool-speczNo resource pools specified. At least one resource pool must be provided via a YAML config file (--config) or via the --resource-pool-spec arg.N)resource_pool_spec_ValidateResourcePoolSpecArgs
isinstanceresourcePoolslist$_ValidateResourcePoolSpecsFromConfigr   InvalidArgumentException)argspersistent_resource_configversions      r   ValidateCreateArgsr   !   s    	!$"9"97C*88$?("00'; 
!
!
/==t
D

-
-	$% % E "r   c                 R    | d   st        j                  dd      t        | |       y)a   Validates the argument values specified via `--resource-pool-spec` flags.

  Args:
    resource_pool_specs: List[dict], a list of resource pool specs specified via
      command line arguments.
    version: str, the API version this command will interact with, either GA or
      BETA.
  r   r   zEEmpty value is not allowed for the first `--resource-pool-spec` flag.N)r   r   '_ValidateHardwareInResourcePoolSpecArgs)resource_pool_specsr   s     r   r   r   2   s4     
Q	

-
-OQ Q **=wGr   c                    | D ]F  }|sd|vrt        j                  dd      d|v rd|vrt        j                  dd      d|v rd|vrt        j                  dd      d|vrd|vrt        j                  dd	      d
|v d|v k7  rt        j                  dd      |j                  dd      }|st        j                  d|      j
                  }|j                         D cg c]  }|j                  d      r| }}||vst        j                  ddj                  |dj                  d t        |      D                           yc c}w )a'  Validates the hardware related fields specified in `--resource-pool-spec` flags.

  Args:
    resource_pool_specs: List[dict], a list of resource pool specs specified via
      command line arguments.
    version: str, the API version this command will interact with, either GA or
      BETA.
  zmachine-typer   z9Key [machine-type] required in dict arg but not provided.zmin-replica-countzmax-replica-countzVKey [max-replica-count] required in dict arg when key [min-replica-count] is provided.zVKey [min-replica-count] required in dict arg when key [max-replica-count] is provided.zreplica-countz:Key [replica-count] required in dict arg but not provided.zaccelerator-countzaccelerator-typezPKey [accelerator-type] and [accelerator-count] are required to use accelerators.NMachineSpecNVIDIAzWFound invalid value of [accelerator-type]: {actual}. Available values are [{expected}]., c              3       K   | ]  }|  y wN .0vs     r   	<genexpr>z:_ValidateHardwareInResourcePoolSpecArgs.<locals>.<genexpr>y   s     %E1DAa1D   actualexpected)r   r   getapi_util
GetMessageAcceleratorTypeValueValuesEnumnames
startswithformatjoinsorted)r    r   specaccelerator_type	type_enumtypevalid_typess          r   r   r   C   s    "d	t	#11"GI 	I 
%,?t,K11"/0 	0
 
%,?t,K11"/0 	0 
%,?t,K11"HJ 	J 
%+=+E	F11" ! 	!
 "4d;	''7$$B$B 	 '__.
.Tx( . 	 
 ;.33$45;V* II%E1D%EE 6< 6GH HQ "F
s   $Ec                    | st        j                  dd      | D ]X  }|j                  st        j                  dd      |j                  j                  st        j                  dd      |j                  s"|j
                  st        j                  dd      |j
                  r,|j
                  j                  st        j                  dd      |j
                  r,|j
                  j                  st        j                  dd      |j                  j                  r,|j                  j                  st        j                  dd      |j                  rB|j                  j                  r,|j                  j                  st        j                  dd	      |j                  j                  st        |j                  j                  j                        }t        j                   d
|      j"                  }|j%                         D cg c]  }|j'                  d      r| }}||vst        j                  ddj)                  |dj+                  d t-        |      D                           yc c}w )zJValidate ResourcePoolSpec message instances imported from the config file.z--configzDAt least one [resourcePools] required in but not provided in config.z;Field [machineSpec] required in but not provided in config.z;Field [machineType] required in but not provided in config.z<Field [replicaCount] required in but not provided in config.z7Field [minReplicaCount] required when using autoscalingz7Field [maxReplicaCount] required when using autoscalingzMField [acceleratorType] required as [acceleratorCount] is specifiedin config.zHField [bootDiskType] required as [bootDiskSizeGb] is specifiedin config.r"   r#   zVFound invalid value of [acceleratorType]: {actual}. Available values are [{expected}].r$   c              3       K   | ]  }|  y wr&   r'   r(   s     r   r+   z7_ValidateResourcePoolSpecsFromConfig.<locals>.<genexpr>   s     #C/B!A/Br,   r-   N)r   r   machineSpecmachineTypereplicaCountautoscalingSpecminReplicaCountmaxReplicaCountacceleratorCountacceleratorTypediskSpecbootDiskSizeGbbootDiskTypestrnamer1   r2   r3   r4   r5   r6   r7   r8   )resource_poolsr   r9   r:   r;   r<   r=   s          r   r   r   |   sc   	

-
-NP P d//

GI I ''//

GI I (<(<//

HJ J 	t';';'K'K//

CE E 	t';';'K'K//

CE E 	)),,//
 
 }}$--66"mm88//
 
 ''T--==BBC%%
""@"@  %??,,4__X& ,   
	,112396())#Cvk/B#CC 4: 4EF 	Fc Xs   I8N)__doc__
__future__r   r   r   googlecloudsdk.api_lib.air   r1   googlecloudsdk.callioper   googlecloudsdk.command_lib.air   r	   r   r   r   r   r   r'   r   r   <module>rS      s>    N &  ' 6 . 3 4F%"H"6Hr<Fr   