
    #?                     R   S r SSKJr  SSKJr  SSKJ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  SSKJr  SSKJr  SSKr " S S\R(                  5      rS rS rS r SS\" S5      R2                  4S jjrS rS rS rS rS rS r S r!S r" " S S\#5      r$g) z8Utility file that contains helpers for Queued Resources.    )absolute_import)division)unicode_literalsN)metadata_utils)apis)base)
exceptions)
properties)	resources)timesc                       \ rS rSrSrSrg)BootDiskConfigurationError!   z0Error if the boot disk configuration is invalid. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       Dlib/googlecloudsdk/command_lib/compute/tpus/queued_resources/util.pyr   r   !   s    8r   r   c                 0    [         R                  " SU 5      $ )Ntpu)r   GetMessagesModule)versions    r   r   r   %   s    			w	//r   c                 \   [         R                  " U R                  U R                  5      n[	        U5      nUR
                  R                  5       n[        R                  " U5       HA  u  pVUR                  R                  UR
                  R                  R                  XVS95        MC     U$ )zCreates the metadata for the Node.

Based on googlecloudsdk.command_lib.compute.tpus.tpu_vm.util.MergeMetadata.

Args:
  args:  The gcloud args
  api_version: The api version (e.g. v2 or v2alpha1)

Returns:
  The constructed MetadataValue.
keyvalue)r   ConstructMetadataDictmetadatametadata_from_filer   NodeMetadataValuesix	iteritemsadditionalPropertiesappendAdditionalProperty)argsapi_versionmetadata_dicttpu_messagesr"   r   r    s          r   MergeMetadatar/   ,   s     !66
mmT,,- #;/,,,.(MM-0jc!!((''::s:P 1 
/r   c                    ^  U 4S jnU$ )z2Creates the repeated structure nodeSpec from args.c                 &  > [        T	5      nUR                  c  UR                  5       Ul        UR                  R                  c  UR	                  5       UR                  l        UR                  R                  R
                  (       a$  UR                  R                  R
                  S   nO@/ UR                  R                  l        UR                  5       nUR                  5       Ul        U R                  5       R                  5       Ul        UR                  (       a  UR                  UR                  l        UR                  UR                  l        UR                   (       am  / UR                  l        UR                    HL  nUR%                  UR&                  UR(                  S9nUR                  R"                  R+                  U5        MN     UR,                  (       a  UR,                  UR                  l        UR.                  (       a  UR                  R1                  5       UR                  l        / UR                  R.                  l        UR.                  R5                  5        HU  u  pxUR                  R.                  R2                  R+                  UR                  R0                  R7                  XxS95        MW     UR8                  (       a  UR8                  UR                  l        UR<                  (       a  UR?                  SS9UR                  l         T	S:X  a"  URB                  (       a  SUR                  l"        URG                  5       UR                  l$        URK                  5       UR                  l&        URN                  (       a%  URN                  UR                  RH                  l'        URP                  (       a%  URP                  UR                  RH                  l(        URR                  (       a%  URR                  UR                  RL                  l*        URV                  (       a%  URV                  UR                  RL                  l,        URZ                  (       a  URZ                  UR                  l-        UR\                  (       + UR                  RH                  l/        UR`                  (       a%  [c        UR`                  T	5      UR                  l2        [g        UT	5      UR                  l4        URj                  (       a'  URl                  (       d  [n        Rp                  " S5      eURr                  (       a  URr                  Ul:        GOURl                  (       a  T	S:X  a  URw                  5       Ul<        URl                  URx                  l=        URj                  (       a  URj                  URx                  l>        UR~                  S:X  a/  URv                  R                  R                  URx                  lB        O\UR                  5       UlD        URl                  UR                  l=        URj                  (       a  URj                  UR                  l>        U/UR                  R                  l        U$ )	Nr   )
sourceDiskmoder   T)enableSecureBootv2alpha1zACan only specify --node-prefix if --node-count is also specified.AVAILABILITY_OPTIMIZED)Er   queuedResourceQueuedResourcer   TpunodeSpecNodeSpecr$   nodeParentRelativeNameparentaccelerator_typeacceleratorTyperuntime_versionruntimeVersion	data_disk	dataDisksAttachedDiskr2   r3   r)   descriptionlabelsLabelsValuer(   itemsr*   range	cidrBlockshielded_secure_bootShieldedInstanceConfigshieldedInstanceConfigautocheckpoint_enabledautocheckpointEnabledNetworkConfignetworkConfigServiceAccountserviceAccountnetwork
subnetworkservice_accountemailscopesscopetagsinternal_ipsenableExternalIps	boot_diskParseBootDiskConfigbootDiskConfigr/   r"   node_prefix
node_countr	   ConflictingArgumentsExceptionnode_idnodeIdMultiNodeParamsmultiNodeParams	nodeCountnodeIdPrefixworkload_typeWorkloadTypeValueValuesEnumr6   workloadTypeMultisliceParamsmultisliceParams)
refr+   requestr.   	node_specrD   attached_diskr   r    r,   s
            r   ProcessCreateNodeSpec.<locals>.ProcessG   s   $[1L%+::<g!!)#/#3#3#5g !!**((,,55a8i,.g  )'')i#((*inzz|002I'+'<'<inn$$($8$8INN!~~!#inn~~)$11 ++).. 2 
 	  ''6	 &
 #'#3#3inn {{*//;;=inn35inn0))+*#2299))<< = 	
 , zz!%inn  

-
-t
-
D nn+ j T%@%@-1inn*#/#=#=#?INN $0$?$?$AINN!||-1\\inn""*04inn""-,0,@,@inn##){{,0KKinn##)yy IIinn9=9J9J5JINN  2~~&9
..+''inn# ,D+>INN44
M  ||i		
	"$0$@$@$B	!.2oo	!!+373C3C)
#
#
0!99**FF]] 
#
#
0 &2%B%B%D	"/3	"",484D4D)
$
$
1+4+G'Nr   r   r,   rt   s   ` r   CreateNodeSpecrw   D   s    Zx 
.r   r5   returnc                    [        U5      nSnSnU R                  5        H$  nXSU4;  d  M  [        SR                  XC5      5      e   U R	                  US5      R                  5       S:H  nU R	                  US5      nU(       a:  US:w  a  [        R                  " SS	5      eUc  [        S
R                  Xg5      5      e0 nU(       a  UR                  US9n	XS'   US:X  a  U(       a  UUS'   U(       a  UR                  " S0 UD6$ g)z$Parses configurations for boot disk.zkms-keyzconfidential-computez.--boot-disk only supports arguments: {} and {}FalsetrueNr5   z--boot-diskzBconfidential-compute is only available in the alpha release track.zQargument --boot-disk: with confidential-compute={} requires kms-key; received: {})
kmsKeyNamecustomerEncryptionKeyenableConfidentialComputer   )
r   keysr   formatgetlowerr	   InvalidArgumentExceptionCustomerEncryptionKeyBootDiskConfig)
boot_disk_argsr,   r.   kms_key_arg_nameconfidential_compute_arg_namearg_nameenable_confidential_computekms_keyboot_disk_config_kwargscustomer_encryption_keys
             r   r`   r`      sE    #;/,"8 %%'h*GHH&
:
A
A+  ( 6@FFH	  /6' j //

NP P &++16),4  *@@ A 7N34J#># 78 &&A)@AA	r   c                 x    A UR                   (       a'  UR                   S::  a  [        R                  " SS5      eU$ )N   z--node-countz!Node count must be greater than 1)rc   r	   r   rp   r+   rq   s      r   VerifyNodeCountr      s7    		__A-

-
-;  
.r   c                     A UR                   (       ac  [        S5      nUR                  c  UR                  5       Ul        UR                  R                  c  UR                  5       UR                  l        U$ )zECreates an empty BestEffort structure if best-effort arg flag is set.r5   )best_effortr   r7   r8   
bestEffort
BestEffort)rp   r+   rq   r.   s       r   SetBestEffortr      sc    		$Z0L%+::<g((0*6*A*A*Cg'	.r   c                    ^  U 4S jnU$ )z4Creates an empty Spot structure if spot flag is set.c                 *  > A [        T5      nUR                  c  UR                  5       Ul        UR                  (       a6  UR                  R                  c  UR	                  5       UR                  l        TS:X  a  UR
                  (       a  UR
                  R                  SS5      R                  5       nUR                  R                  R                  nXE:X  a6  UR                  R                  c  UR	                  5       UR                  l        U$ )Nr5   -_)
r   r7   r8   spotSpotprovisioning_modelreplaceupper ProvisioningModelValueValuesEnumSPOT)rp   r+   rq   r.   r   r   r,   s         r   rt   SetSpot.<locals>.Process   s    $[1L%+::<gyyW++008$0$5$5$7g!j T%<%<22::3DJJL((IINNd		#(>(>(C(C(K&2&7&7&9#Nr   r   rv   s   ` r   SetSpotr      s     
.r   c                    ^  U 4S jnU$ )z9Creates an empty Guaranteed structure if arg flag is set.c                    > A UR                   (       ac  [        T5      nUR                  c  UR                  5       Ul        UR                  R                   c  UR	                  5       UR                  l         U$ N)
guaranteedr   r7   r8   
Guaranteed)rp   r+   rq   r.   r,   s       r   rt   SetGuaranteed.<locals>.Process  sb    &{3l				'!-!<!<!>				*	*	2,8,C,C,E)Nr   r   rv   s   ` r   SetGuaranteedr      s    	 
.r   c                    ^  U 4S jnU$ )z'Sets the provisioning model enum value.c                   > A [        T5      nUR                  c  UR                  5       Ul        UR                  (       dq  UR                  (       a/  UR                  R
                  R                  UR                  l        UR                  R
                  R                  UR                  l        U$  UR                  R                  SS5      R                  5       nUR                  R                  U5      nXRR                  l        U$ ! [         a*  n[        R                  " SUR                   S35      UeS nAff = f)Nr   r   z--provisioning-modelzb is not a valid provisioning model, must be one of [standard, spot, reservation-bound, flex-start])r   r7   r8   r   r   r   r   provisioningModelSTANDARDr   r   	TypeErrorr	   r   )rp   r+   rq   r.   normalized_candidatecandidate_enumer,   s          r   rt   %SetProvisioningModel.<locals>.Process  s)   $[1L%+::<g""	''HHMM 	0 
%
%
F
F
O
O . n "44<<S#FLLN

%
%
F
F"  0>,N  //
 $$% &G G 	s   3AD
 

D>%D99D>r   rv   s   ` r   SetProvisioningModelr     s     B 
.r   c                    ^  U 4S jnU$ )z:Combine multiple timing constraints into a valid_interval.c                   > A UR                   (       a  UR                  (       d"  UR                  (       a'  UR                  (       a  [        R
                  " S5      e[        T5      n[        R                  " 5       nS nUR                  (       a  UR                  nO,UR                   (       a  UR                   R                  U5      nS nUR                  (       a  UR                  nO,UR                  (       a  UR                  R                  U5      nU(       a  U(       a  UR                  5       n[        R                  " U5      Ul        [        R                  " U5      Ul        UR                  c  UR                  5       Ul        UR!                  5       UR                  l        XrR                  R"                  l        U$ )NzEOnly one timing constraint for each of (start, end) time is permitted)valid_after_durationvalid_after_timevalid_until_durationvalid_until_timer	   rd   r   r   NowGetRelativeDateTimeIntervalFormatDateTime	startTimeendTimer7   r8   QueueingPolicyqueueingPolicyvalidInterval)	rp   r+   rq   r.   current_time
start_timeend_timevalid_intervalr,   s	           r   rt   !SetValidInterval.<locals>.Process:  sK   !!d&;&;!!d&;&;44  %[1L99;LJ((j		"	",,@@NjH&&h		"	"**>>|Lhh#,,.n!&!5!5j!An$33H=n				'!-!<!<!>.:.I.I.Kg+<J++9Nr   r   rv   s   ` r   SetValidIntervalr   7  s    "H 
.r   c                    ^  U 4S jnU$ )zCreates the target reservation name from args.

Args:
  api_version: The api version (e.g. v2 or v2alpha1)

Returns:
  Handler which sets request.queuedResource.reservationName
c                   > A UR                   (       a  UR                  (       dD  UR                  (       a  UR                  (       d"  UR                  (       a'  UR                   (       a  [        R                  " S5      eSnSnSnS nUR                   (       a)  UR                  SUR                   UR                  5      nGOUR                  (       a)  UR                  SUR                  UR                  5      nGOPUR                  (       a)  UR                  SUR                  UR                  5      nGOTS:X  aq  [        US	5      (       a`  UR                  (       aO  [        R                  R                  R                  R                  5       nUR                  SXqR                  5      nO[        US
5      (       a  UR                  (       a}  [        R                   " XAR                  5      (       aX  [        R                  R                  R                  R                  5       nUR                  XqR                  UR                  5      nU(       a  XbR"                  l        U$ )Nz&Only one reservation host is permittedz!{}/{}/locations/{}/reservations/-z^[a-zA-Z0-9-]+$z(projects/{}/locations/{}/reservations/{}projectsfoldersorganizationsv2reservedreservation)reservation_host_projectreservation_host_folderreservation_host_organizationr	   rd   r   zonehasattrr   r
   VALUEScoreproject	GetOrFailr   rematchr7   reservationName)	rp   r+   rq   patternshort_reservation_name_patternfull_reservation_name_patternreservation_namer   r,   s	           r   rt   &CreateReservationName.<locals>.Processk  s   		&	&4+G+G((T-O-O..43P3P 44
2  2G%6"$N!$$ 
d33TYY 
	%	% 
T11499 
	+	+ 
4==tyy 
	z!:!:t}}!!&&..88:g 
GYYG	}	%	%$*:*:rxx&(8(8@ @ !!&&..88:g6==
99d.. /?,Nr   r   rv   s   ` r   CreateReservationNamer   a  s    (T 
.r   c                 Z    A [        US5      (       a  UR                  (       a  SUl        U$ )z&Sets force arg to true if flag is set.forceT)r   r   r   s      r   SetForcer     s$    	T7

GM	.r   c                   *    \ rS rSrSrS rS rS rSrg)TPUQueuedResourcei  z#Helper to get TPU Queued Resources.c                     U[         R                  R                  :X  a  SU l        OSU l        [        R
                  " SU R                  5      U l        [        R                  " SU R                  5      U l        g )Nr5   r   r   )	r   ReleaseTrackALPHA_api_versionr   GetClientInstanceclientr   messages)selfrelease_tracks     r   __init__TPUQueuedResource.__init__  sZ    ))///$dd((0A0ABDK**5$2C2CDDMr   c                     U R                   $ r   )r   )r   s    r   GetMessagesTPUQueuedResource.GetMessages  s    ==r   c                 T   [         R                  R                  R                  R	                  SS9n[
        R                  R                  UX#S.SU R                  S9nU R                  R                  UR                  5       S9nU R                  R                  R	                  U5      $ )z<Retrieves the Queued Resource in the given project and zone.T)required)locationsId
projectsIdz&tpu.projects.locations.queuedResources)params
collectionr,   )name)r
   r   r   r   Getr   REGISTRYParser   r   -TpuProjectsLocationsQueuedResourcesGetRequestr>   r   "projects_locations_queuedResources)r   r   r   r   (fully_qualified_queued_resource_name_refrq   s         r   r   TPUQueuedResource.Get  s    $$,,00$0?G/8/A/A/G/G#;;%%	 0H 0, mmII5BBD J G ;;99==gFFr   )r   r   r   N)	r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    +EGr   r   )r5   )%r   
__future__r   r   r   r   googlecloudsdk.api_lib.computer   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r	   googlecloudsdk.coresdk_core_exceptionsr
   r   googlecloudsdk.core.utilr   r&   Errorr   r   r/   rw   r   r`   r   r   r   r   r   r   r   r   objectr   r   r   r   <module>r     s    ? &  ' 	 9 , ( . A * ) * 
9!4!:!: 900_F !+-z"11-`
("#L'T4nG Gr   