
    ې                         d Z ddlZddlZddlZddlmZmZmZ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d
lmZ ddlmZ ddZddeeef   fdZ G d d      Zy)z-Utility functions for clusters command group.    N)AnyDictListSet)messages)
exceptions)concepts)
flag_types)	yaml_data)concept_parsers)presentation_specs)filesc                    t         j                  j                  d      }t        j                  j                  |j                         d|      }t        j                  d|dd      }t        j                  |g      j                  |        y)z&Adds a cluster name resource argument.z5cluster_director.clusters.projects_locations_clustersT)is_positionalapi_versionclusterz
        Name of the cluster resource.
        Formats: cluster | projects/{project}/locations/{locations}/clusters/{cluster}
      )nameconcept_specrequired
group_helpN)r   ResourceYAMLDataFromPathr	   ResourceSpecFromYamlGetDatar   ResourcePresentationSpecr   ConceptParserAddToParser)parserr   cluster_dataresource_specpresentation_specs        Alib/googlecloudsdk/command_lib/cluster_director/clusters/utils.pyAddClusterNameArgToParserr$   !   s    ++44=, ''00Dk 1 - )AA 
	 !2 34@@H    returnc                     | r| t         j                  vrt        j                  d|        t         j                  |    S )z3Returns the cluster spec for the given API version.zUnsupported API version: )r
    API_VERSION_TO_CLUSTER_FLAG_TYPEr   ToolException)r   s    r#   GetClusterFlagTyper*   5   s?     	JGG	G

"
"%>{m#L
MM		4	4[	AAr%   c                      e Zd ZdZ	 	 d:dee   fdZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Z	 	 d;dededefdZd Zd Zd Zd Zd ZdefdZdefdZdedefdZdefdZdefdZdefdZd Z	 d<defd Zded!eee f   fd"Z!d# Z"d$edee#jH                  z  fd%Z%d$edefd&Z&de'ee f   fd'Z(d( Z)d)ed*e'ee f   d+e d,edde#jT                  z  f
d-Z+d)ed*e'ee f   d,edde#jT                  z  fd.Z,d)ed*e'ee f   d/ed0ed1edde#jT                  z  fd2Z-d)ed*e'ee f   d,ede e#jT                  z  fd3Z.d4 Z/d5 Z0d6 Z1d7 Z2d8 Z3d9 Z4y)=ClusterUtilz#Represents a cluster utility class.Nupdate_maskc                     || _         || _        | j                   j                  j                  j	                         | _        || _        |r|| _        yt               | _        y)z&Initializes the cluster utility class.N)	argsmessage_moduleCONCEPTSr   Parsecluster_refexisting_clustersetr-   )selfr/   r0   r4   r-   s        r#   __init__zClusterUtil.__init__B   sO     DI(Dyy))11779D,D0;DDr%   c                 ~    t        j                  | j                  j                  | j                  j
                        S )z6Returns a cluster message from the config JSON string.)messages_utilDictToMessageWithErrorCheckr/   configr0   Cluster)r6   s    r#   MakeClusterFromConfigz!ClusterUtil.MakeClusterFromConfigP   s0    44		$--55 r%   c                    | j                         }| j                         |_        | j                         |_        | j                         |_        | j                  j                  | j                  |            |_
        |S )z2Returns a cluster message from the granular flags.slurm)MakeClusterBasicMakeClusterNetworksnetworkResourcesMakeClusterStoragesstorageResourcesMakeClusterComputecomputeResourcesr0   OrchestratorMakeClusterSlurmOrchestratororchestratorr6   r   s     r#   MakeClusterzClusterUtil.MakeClusterV   sv    ##%G#779G#779G#668G..;;//8 < G Nr%   c                    | j                   j                  j                  j                         }| j                  j                  |j                               }| j                   j                  d      r| j                   j                  |_        | j                   j                  d      rI| j                  | j                   j                  | j                  j
                  j                        |_
        |S )z*Makes a cluster message with basic fields.)r   descriptionlabels)r/   r1   r   r2   r0   r<   NameIsSpecifiedrN   
MakeLabelsrO   LabelsValue)r6   r3   r   s      r#   rA   zClusterUtil.MakeClusterBasica   s    ))$$,,224K!!)){/?/?/A)BGyy]+ II11gyyX&
))

D//77CCgn Nr%   c                    | j                   j                  j                         }| j                  j	                  d      r| j                  | j                  j                  j                  d            }|j                  j                  | j                   j                  j                  j                  d| | j                   j                  | j                   j                  | j                   j                  || j                  j                  j                  d                                     | j                  j	                  d	      r| j                  j	                  d
      r| j                  | j                  j                        }|j                  j                  | j                   j                  j                  j                  d| | j                   j                  | j                   j                  | j                   j                  || j!                  | j                  j"                                                       |S )z,Makes a cluster message with network fields.create_networkr   znet-rN   )networkrN   )
newNetworkr;   keyvaluerV   subnet)rV   
subnetwork)existingNetwork)r0   r<   NetworkResourcesValuer/   rQ   _GetNetworkNamerU   getadditionalPropertiesappendAdditionalPropertyNetworkResourceNetworkResourceConfigNewNetworkConfigrV   ExistingNetworkConfig_GetSubNetworkNamer\   )r6   networksnetwork_names      r#   rB   zClusterUtil.MakeClusterNetworksm   s   ""**@@BHyy-.))$))*B*B*F*Fv*NOl##**



%
%
;
;
N
N'''77,,BB!%!4!4!E!E".&*ii&>&>&B&B+' "F " C  8 	 O  yyY'DII,A,A(,K))$))*;*;<l##**



%
%
;
;
N
N'''77,,BB&*&9&9&O&O".%)%<%<TYY=M=M%N 'P ' C  8  O 
 Or%   c                    | j                   j                  j                         }d}| j                  j	                  d      rT| j                  j
                  D ]:  }| j                  |      }|dz  }|j                  j                  | j                   j                  j                  j                  || j                   j                  | j                   j                  | j                   j                  | j                  |j                  d            |j                  d      | j                   j                  |j                  d      |j                  d            g|j                  d	      |j                  d
                                     = | j                  j	                  d      r| j                  j                   D ]  }| j                  |      }|dz  }|j                  j                  | j                   j                  j                  j                  || j                   j                  | j                   j                  | j                   j#                  | j                  |                                      | j                  j	                  d      r| j                  j$                  D ]  }| j                  |      }|dz  }|j                  j                  | j                   j                  j                  j                  || j                   j                  | j                   j                  | j                   j'                  | j)                  |j                  d            |j                  d      |j                  d      |j                  d
                                      | j                  j	                  d      r| j                  j*                  D ]  }| j                  |      }|dz  }|j                  j                  | j                   j                  j                  j                  || j                   j                  | j                   j                  | j                   j-                  | j)                  |                                      | j                  j	                  d      r| j                  j.                  D ]  }| j                  |      }|dz  }| j                   j1                  |j                  d            }d|v r|j                  d      |_        |j2                  s4d|v r0| j                   j5                  |j                  d            |_        |j2                  s;|j6                  s/| j                   j0                  j8                  j:                  |_        d|v r0| j                   j=                  |j                  d            |_        |j                  j                  | j                   j                  j                  j                  || j                   j                  | j                   j                  |                          | j                  j	                  d       r| j                  j@                  D ]  }| j                  |      }|dz  }|j                  j                  | j                   j                  j                  j                  || j                   j                  | j                   j                  | j                   jC                  |      !                          |S )"z,Makes a cluster message with storage fields.
   create_filestores   r   tier
capacityGb	fileshare)rq   	fileShareprotocolrN   )	filestorerp   
fileSharesrt   rN   )newFilestorerX   rY   
filestores)ru   )existingFilestorecreate_lustres
filesystem)lustrer{   rq   rN   )	newLustrelustres)r|   )existingLustrecreate_buckets)bucketstorageClassenableAutoclass)enabled	enableHNS)	newBucketbuckets)existingBucket)"r0   r<   StorageResourcesValuer/   rQ   rn   _GetNextStorageIdrb   rc   rd   StorageResourceStorageResourceConfigNewFilestoreConfig_GetFilestoreNamera   FileShareConfigrx   ExistingFilestoreConfigrz   NewLustreConfig_GetLustreNamer~   ExistingLustreConfigr   NewBucketConfigr   GcsAutoclassConfig	autoclassStorageClassValueValuesEnumSTANDARDGcsHierarchicalNamespaceConfighierarchicalNamespacer   ExistingBucketConfig)r6   storagesstorage_counterru   
storage_idr|   
gcs_bucketgcss           r#   rD   zClusterUtil.MakeClusterStorages   s   ""**@@BH Oyy01yy22)++O<
1%%,,''==PP))99..DD%)%8%8%K%K&*&<&< )f 5' "+v!6 $ 3 3 C C/8}}\/J.7mmK.H !D !"( &/]]:%>(1m(D &L & E  :  Q 	
 34 yy\*yy++)++O<
1%%,,''==PP))99..DD*.*=*=*U*U&*&<&<Y&G +V + E  :  Q 		
 , yy-.II,,&++O<
1%%,,''==PP))99..DD"&"5"5"E"E#'#6#6vzz&7I#J'-zz,'?'-zz,'?(.

=(A	 #F # E  : 	 Q 	
 -$ yyY'II%%&++O<
1%%,,''==PP))99..DD'+':':'O'O#'#6#6v#> (P ( E  :  Q 		
 & yy-.		00*++O<
1!!11>>&) 2 
 Z''^^N;#
$5$C--@@ nn%67 A #-
 !!11MMVV 
 *$!!@@$..5 A  
#
 	%%,,''==PP))99..DD"% E  :  Q 		
1 1D yyY'		))*++O<
1%%,,''==PP))99..DD'+':':'O'O#- (P ( E  :  Q 		
 * Or%   c           	      X   | j                   j                  d      sf| j                   j                  d      sK| j                   j                  d      s0| j                   j                  d      st        j                  d      t	               }| j
                  j                  j                         }| j                   j                  d      r| j                   j                  D ]}  }|j                  d      }|j                  |       |j                  j                  | j
                  j                  j                  j                  || j                  |                    | j                   j                  d      r| j                   j                  D ]}  }|j                  d      }|j                  |       |j                  j                  | j
                  j                  j                  j                  || j!                  |                    | j                   j                  d      r| j                   j"                  D ]}  }|j                  d      }|j                  |       |j                  j                  | j
                  j                  j                  j                  || j%                  |                    | j                   j                  d      r| j                   j&                  D ]}  }|j                  d      }|j                  |       |j                  j                  | j
                  j                  j                  j                  || j)                  |                    t+        |      t+        |j                        k7  rt        j                  d      |S )	z,Makes a cluster message with compute fields.on_demand_instancesspot_instancesreserved_instancesdws_flex_instanceszvAt least one of on_demand_instances, spot_instances, reserved_instances, or dws_flex_instances flag must be specified.idrY   z7Compute instances with duplicate ids are not supported.)r/   rQ   r   r)   r5   r0   r<   ComputeResourcesValuer   ra   addrb   rc   rd   _MakeOnDemandComputeResourcer   _MakeSpotComputeResourcer   _MakeReservedComputeResourcer   _MakeDwsFlexComputeResourcelen)r6   compute_idscomputeinstance
compute_ids        r#   rF   zClusterUtil.MakeClusterCompute  s    II!!"78		%%&67		%%&:;		%%&:;$$O  %K!!))??AGyy23ii33(\\$'

#$$++''==PP77A Q 	
 4 yy-.ii..(\\$'

#$$++''==PP33H= Q 	
 / yy12ii22(\\$'

#$$++''==PP77A Q 	
 3 yy12ii22(\\$'

#$$++''==PP66x@ Q 	
 3 ;3w;;<<$$
C  Nr%   c                    | j                   j                         }| j                  |      }| j                  j	                  d      ri| j                  j
                  D ]P  }|j                  d      }| j                  |      }|j                  j                  | j                  |||             R | j                  j	                  d      rE| j                  j                  D ],  }|j                  j                  | j                  |             . | j                  j	                  d      r| j                  j                  |_        | j                  j	                  d      r| j                  j                   }|j                  d      }| j                   j#                  |j                  dd      ||j                  d	      ||j                  d
d      |j                  dd      | j%                  |j                  d            | j'                  |j                  d      | j                   j"                  j(                              |_        |j*                  j,                  j                  | j/                  |             |S )z7Makes a cluster message with slurm orchestrator fields.slurm_node_sets	computeIdslurm_partitionsslurm_default_partitionslurm_login_nodemachineTypecountro   zoneenableOSLoginTenablePublicIpsstartupScriptrO   
label_args	label_cls)r   r   r   storageConfigsenableOsLoginr   r   rO   machine_type)r0   SlurmOrchestrator_GetStorageConfigsr/   rQ   r   ra   _GetComputeMachineTypeFromArgsnodeSetsrc   _MakeSlurmNodeSetr   
partitions_MakeSlurmPartitionr   defaultPartitionr   SlurmLoginNodes_GetBashScriptrR   rS   
loginNodesdisksMakeDisk)	r6   r   r@   storage_configsnode_setr   r   	partition
login_nodes	            r#   rI   z(ClusterUtil.MakeClusterSlurmOrchestratorW  s   113E(  yy./ii//(\\+.
:::F""8\?K	
 0 yy/0yy11) 8 8 CD 2 yy67#yy@@eyy/099--j^^M2l,,<<w*"~~f%("=$..):DA++JNN?,KL#1++;;GG !  = e ##DMM|M$LMLr%   c           
          |sy |t        |j                               D cg c]  \  }}|j                  ||       c}}      S c c}}w )zReturns the labels message.NrY   )rb   )sorteditemsrd   )r6   r   r   rZ   r[   s        r#   rR   zClusterUtil.MakeLabels  sY     %Z%5%5%78
8
U ((S(>8
 
s   A
r   bootsource_imagec                 h    d}|j                  d      rd}| j                  j                  |||      S )zHReturns the disk message, defaults to boot disk with empty source image.zpd-standard)z
a3-megagpuza3-ultragpuz
a4-highgpuza4x-highgpuzhyperdisk-balanced)typer   sourceImage)
startswithr0   Disk)r6   r   r   r   	disk_types        r#   r   zClusterUtil.MakeDisk  sH     IB 'i##  $  r%   c                 R    | j                         }|| j                  j                  fS )z,Returns the cluster message from the config.)r=   r/   r-   rK   s     r#   MakeClusterPatchFromConfigz&ClusterUtil.MakeClusterPatchFromConfig  s%    ((*GDII))))r%   c                     | j                         }| j                         |_        | j                  j	                  | j                  |            |_        |dj                  t        | j                              fS )z2Returns the cluster patch message and update mask.r?   ,)
MakeClusterBasicPatchMakeClusterComputePatchrG   r0   rH   !MakeClusterSlurmOrchestratorPatchrJ   joinr   r-   rK   s     r#   MakeClusterPatchzClusterUtil.MakeClusterPatch  sn    ((*G#;;=G..;;44W= < G CHHVD$4$45666r%   c                    | j                   j                         }| j                  j                  d      r6| j                  j                  |_        | j
                  j                  d       | j                  | j                  j                        }d}d}| j                  j                  d      r0| j                  j                  D ]  }| j                  |||       d} | j                  j                  d      r'|j                  | j                  j                         d}|rQ| j                  || j                   j                  j                        |_	        | j
                  j                  d       |S )	z0Makes a cluster patch message with basic fields.rN   FzLabel with key={0} not found.remove_labelsT
add_labelsr   rO   )r0   r<   r/   rQ   rN   r-   r   _ConvertMessageToDictr4   rO   r   _RemoveKeyFromDictSpecupdater   rR   rS   )r6   r   rO   is_labels_updatedexception_messagerZ   s         r#   r   z!ClusterUtil.MakeClusterBasicPatch  s0   !!))+Gyy]+ II11g
=)''(=(=(D(DEF7yy_-((###C1BC  ) yy\*mmDII(()''//;; ' gn 8$Nr%   c           	         | j                   j                  j                         }| j                  | j                  j
                        }d}d}d}| j                  j                  d      r6| j                  j                  D ]  }| j                  ||d|d|        d} | j                  j                  d	      r6| j                  j                  D ]  }| j                  ||d
|d|        d} | j                  j                  d      r6| j                  j                  D ]  }| j                  ||d|d|        d} | j                  j                  d      r6| j                  j                  D ]  }| j                  ||d|d|        d} | j                  j                  d      rP| j                  j                  D ]7  }| j                  |j                  d      || j!                  |      |       d}9 | j                  j                  d      rP| j                  j"                  D ]7  }| j                  |j                  d      || j%                  |      |       d}9 | j                  j                  d      rP| j                  j&                  D ]7  }| j                  |j                  d      || j)                  |      |       d}9 | j                  j                  d      rP| j                  j*                  D ]7  }| j                  |j                  d      || j-                  |      |       d}9 |r|j/                         D 	cg c]6  \  }}	| j                   j                  j                  j1                  ||	      8 c}	}|_        |j2                  st5        j6                  d      | j8                  j;                  d       |S c c}	}w )z:Makes a cluster compute patch message with compute fields.Fz(Compute instances with id={0} not found.z,Compute instances with id={0} already exist.remove_on_demand_instancesnewOnDemandInstancesz
On demand )rZ   	dict_specattrkey_exception_messageattr_exception_messageTremove_spot_instancesnewSpotInstanceszSpot remove_reserved_instancesnewReservedInstancesz	Reserved remove_dws_flex_instancesnewDwsFlexInstancesz	DWS Flex add_on_demand_instancesr   rZ   r   r[   r   add_spot_instancesadd_reserved_instancesadd_dws_flex_instancesrY   z"Compute instances cannot be empty.zcompute.resource_requests)r0   r<   r   r   r4   rG   r/   rQ   r   _RemoveKeyByAttrFromDictSpecr   r   r   r   _AddKeyToDictSpecra   r   r  r   r  r   r  r   r   rd   rb   r   r)   r-   r   )
r6   compute_resourcesr   is_compute_updatedex_msg_not_foundex_msg_already_existr   r   rZ   r[   s
             r#   r   z#ClusterUtil.MakeClusterComputePatch  s   ++33IIK(()>)>)O)OPGAIyy9:		<<*))'"2%/0@/A#B 	* 	
 " = yy45		77*))#"2%*+;*<#= 	* 	
 " 8 yy89		;;*))'"2%./?.@#A 	* 	
 " < yy89		;;*))&"2%./?.@#A 	* 	
 " < yy67ii77(T"33H=2	 	 	
 " 8 yy12ii22(T"//92	 	 	
 " 3 yy56ii66(T"33H=2	 	 	
 " 7 yy56ii66(T"228<2	 	 	
 " 7 
 $MMO	0 ,jc5 


%
%
;
;
N
NU O  ,	0, 33&&'KLL
670s   ;Oc           
         | j                   j                         }| j                  j                  d      r6| j                  j                  |_        | j                  j                  d       | j                  | j                  j                  j                  j                        }d}d}d}| j                  j                  d      r0| j                  j                  D ]  }| j                  |||       d} | j                  j                  d      rz| j                  j                  D ]a  }|j!                  d	      }| j#                  |||      }	d
|v r|j!                  d
      |	_        d|v r|j!                  d      |	_        |	||<   d}c | j                  j                  d      r| j                  j(                  D ]y  }| j+                  | j                        }
|j!                  d      }| j-                  ||d      }| j/                  |j!                  d	      || j1                  |||
      |       d}{ |rZt3        |j5                               |_        |j                  st7        j8                  d      | j                  j                  d       | j                  | j                  j                  j                  j:                        }d}d}d}| j                  j                  d      r0| j                  j<                  D ]  }| j                  |||       d} | j                  j                  d      rz| j                  j>                  D ]a  }|j!                  d	      }| j#                  |||      }d|v r|j!                  d      |_         d|v r|j!                  d      |_!        |||<   d}c | j                  j                  d      rP| j                  jD                  D ]7  }| j/                  |j!                  d	      || jG                  |      |       d}9 |rZt3        |j5                               |_        |j:                  st7        j8                  d      | j                  j                  d       |S )zCMakes a cluster slurm orchestrator patch message with slurm fields.r   z$orchestrator.slurm.default_partitionFz%Slurm nodesets with id={0} not found.z)Slurm nodesets with id={0} already exist.remove_slurm_node_setsTupdate_slurm_node_setsr   staticNodeCountmaxDynamicNodeCountadd_slurm_node_setsr   )use_existing_clusterr   zSlurm nodesets cannot be empty.zorchestrator.slurm.node_setsz'Slurm partitions with id={0} not found.z+Slurm partitions with id={0} already exist.remove_slurm_partitionsupdate_slurm_partitions
nodesetIds	exclusiveadd_slurm_partitionsz!Slurm partitions cannot be empty.zorchestrator.slurm.partitions)$r0   r   r/   rQ   r   r   r-   r   _ConvertSlurmMessageToDictr4   rJ   r@   r   r  r   r  ra   _GetValueFromDictSpecr  r  r  r   !_GetComputeMachineTypeFromClusterr  r   listvaluesr   r)   r   r  r  
nodeSetIdsr  r  r   )r6   cluster_patchr@   r   is_node_sets_updatedr  r	  node_set_idr   existing_node_setr   r   r   r   is_partitions_updatedpartition_idr   existing_partitions                     r#   r   z-ClusterUtil.MakeClusterSlurmOrchestratorPatch"  s:   113Eyy67#yy@@e
AB55**0099O !>Fyy5699+##*:	
  $	 :
 yy56ii66(ll4( 66*:
 (.6ll;L.M

+ H,2:,,#3

/ (9$# 7 yy23ii33(11$2G2GH\\+.
==D > 
 	T"%((, 3 	 	
  $ 4 O2245en^^&&'HII
9:66**00;; "@Hyy67));;,##*,<	
 !%	 <
 yy67yy88) }}T*!77*,<
 9$*3--*E

')#)2{)C

&);& $ 9 yy34yy55)d#&**952	 	 	
 !% 6 .5578e&&'JKK
:;Lr%   r&   c                 Z    | j                   j                         j                  }d| d| S )zReturns the network name.	projects/z/global/networks/r3   Parent
projectsId)r6   rV   projects      r#   r`   zClusterUtil._GetNetworkName  s0    %%'22Gwi0	::r%   c                 Z    | j                   j                         j                  }d| d| S )zReturns the subnetwork name.r$  /r%  )r6   r]   r(  s      r#   ri   zClusterUtil._GetSubNetworkName  s/    %%'22Gwiq--r%   r   c                     d| S )zReturns the next storage ID.storage )r6   r   s     r#   r   zClusterUtil._GetNextStorageId  s    _%&&r%   c                 Z    | j                   j                         j                  }d| d| S )zReturns the filestore name.r$  r*  r%  )r6   ru   r(  s      r#   r   zClusterUtil._GetFilestoreName  s/    %%'22Gwiq,,r%   c                 Z    | j                   j                         j                  }d| d| S )zReturns the Lustre name.r$  r*  r%  )r6   r|   r(  s      r#   r   zClusterUtil._GetLustreName  s/    %%'22Gwiq))r%   c                 Z    | j                   j                         j                  }d| d| S )zReturns the reservation name.r$  r*  r%  )r6   reservationr(  s      r#   _GetReservationNamezClusterUtil._GetReservationName  s/    %%'22Gwiq..r%   c                    g }| j                   j                  d      r%|j                  | j                   j                         | j                   j                  d      r%|j                  | j                   j                         | j                   j                  d      r%|j                  | j                   j
                         | j                   j                  d      r%|j                  | j                   j                         |D ])  }|j                  d      |k(  s|j                  d      c S  t        j                  d| d      )	z+Returns the compute machine type from args.r   r   r   r   r   r   Compute instances with id= not found.)
r/   rQ   extendr   r   r   r   ra   r   r)   )r6   r   	instancesr   s       r#   r   z*ClusterUtil._GetComputeMachineTypeFromArgs  s    Iyy23tyy445yy-.tyy//0yy12tyy334yy12tyy334	d	z	)||M**  
"
"
$ZL< r%   r   c                    |r1| j                  |j                        }||v r| j                  ||      S |r;| j                  | j                  j                        }||v r| j                  ||      S t	        j
                  d| d      )z.Returns the compute machine type from cluster.r4  r5  )r   rG   _GetComputeMachineTyper4   r   r)   )r6   r   r   r  r  s        r#   r  z-ClusterUtil._GetComputeMachineTypeFromCluster  s     44W5M5MN	(	(**:7HII44



0
0 
(	(**:7HII

"
"
$ZL< r%   r  c                 l   ||   j                   r||   j                   j                  S ||   j                  r||   j                  j                  S ||   j                  r||   j                  j                  S ||   j                  r||   j                  j                  S t        j                  d      )z8Returns the compute machine type from compute resources.z%Compute instances type not supported.)r   r   r   r   r   r   r)   )r6   r   r  s      r#   r9  z"ClusterUtil._GetComputeMachineType  s     $99z*??KKK$55z*;;GGG$99z*??KKK$88z*>>JJJ

"
"#J
KKr%   c                    g }t        |j                  j                  d       }|r;|d   }|j                  | j                  j                  |j                  d             t        j                  t              }|dd D ]`  }d}|j                  r|j                  j                  j                  s |j                  j                  j                  rd|d	    }|d	xx   dz  cc<   n|j                  j                  j                  s |j                  j                  j                  rd
|d    }|dxx   dz  cc<   nU|j                  j                  j                   s |j                  j                  j"                  rd|d    }|dxx   dz  cc<   |st%        j&                  d      |j                  | j                  j                  |j                  |             c |S )zReturns the storage configs.c                     | j                   S NrZ   )r,  s    r#   <lambda>z0ClusterUtil._GetStorageConfigs.<locals>.<lambda>  s    GKKr%   r>  r   z/home)r   
localMountro   Nz/sharedru   z/scratchr|   z/datar   z'Storage configuration is not supported.)r   rE   rb   rc   r0   StorageConfigrZ   collectionsdefaultdictintr[   r;   rw   ry   r}   r   r   r   r   r)   )r6   r   r   sorted_storagesfirst_storagecountersr,  local_mounts           r#   r   zClusterUtil._GetStorageConfigs  s   ?AO  55'O %a(m



+
+""  ,  &&s+H"12&k	MM  --}}##55!(;"7!89+
;
1
$
MM  **}}##22"8H#5"67+
8

!
MM  **}}##22 234+
8

!
&&5
 	
 



+
+$ , 5 '@ r%   	arg_valuec                 t   |r| j                  |      s|S |}t        j                  j                  |      st	        j
                  d|       t        j                  j                  |      rt        j                  j                  |      st	        j
                  d|       t        j                  |      S )z>Returns the bash script if argument is a valid bash file path.z'Script file path must be absolute, got z'Script file not found at absolute path=)
_CheckIfBashFileFormatospathisabsr   BadFileExceptionexistsisfiler   ReadFileContents)r6   rI  rM  s      r#   r   zClusterUtil._GetBashScript  s    D77	BD77==''3D6
:  77>>$rww~~d';''3D6
:  !!$''r%   c                 .    t        j                  d|      S )z-Checks if the argument is a bash file format.z^\S*\.(sh|bash)$)rematch)r6   rI  s     r#   rK  z"ClusterUtil._CheckIfBashFileFormat  s    88'33r%   c                 r    |si S |j                   D ci c]  }|j                  |j                   c}S c c}w )zUConvert a message with list of type AdditionalProperty(key=str, value=Any) to a dict.)rb   rZ   r[   r6   messageeachs      r#   r   z!ClusterUtil._ConvertMessageToDict  s7    i-4-I-IJ-ITDHHdjj -IJJJs   4c                 J    |si S |D ci c]  }|j                   | c}S c c}w )zIConvert a list of slurm message (SlurmNodeSet, SlurmPartition) to a dict.)r   rW  s      r#   r  z&ClusterUtil._ConvertSlurmMessageToDict  s*    i&-.gdDGGTMg...s    rZ   r   r[   r   c                 ^    ||v r$t        j                  |j                  |            |||<   y)zHAdds a cluster identifier (key) with value, if not present in dict spec.Nr   r)   format)r6   rZ   r   r[   r   s        r#   r  zClusterUtil._AddKeyToDictSpec!  s2     i$$%6%=%=c%BCCIcNr%   c                 v    ||vr$t        j                  |j                  |            |j                  |       y)z<Removes a cluster identifier (key), if present in dict spec.N)r   r)   r]  popr6   rZ   r   r   s       r#   r   z"ClusterUtil._RemoveKeyFromDictSpec-  s5     )$$%6%=%=c%BCCMM#r%   r   r   r   c                     ||vr$t        j                  |j                  |            t        ||   |d      s$t        j                  |j                  |            |j	                  |       y)zIRemoves a cluster identifier (key) by attribute, if present in dict spec.N)r   r)   r]  getattrr_  )r6   rZ   r   r   r   r   s         r#   r  z(ClusterUtil._RemoveKeyByAttrFromDictSpec5  sb     )$$%:%A%A#%FGG9S>4.$$%;%B%B3%GHHMM#r%   c                 \    ||vr$t        j                  |j                  |            ||   S )zGReturns the value message by cluster identifier (key) from a dict spec.r\  r`  s       r#   r  z!ClusterUtil._GetValueFromDictSpecD  s3     )$$%6%=%=c%BCCS>r%   c                     | j                   j                  | j                   j                  |j                  d      |j                  d                  S )zAMakes a cluster compute resource message for on demand instances.r   r   r   r   )r   )r0   ComputeResourceNewOnDemandInstancesConfigra   r6   r   s     r#   r   z(ClusterUtil._MakeOnDemandComputeResourceL  sR    ..!00KKf% ]3 L 
 /  r%   c                     | j                   j                  | j                   j                  |j                  d      |j                  d                  S )z<Makes a cluster compute resource message for spot instances.r   r   re  )r   )r0   rf  NewSpotInstancesConfigra   rh  s     r#   r   z$ClusterUtil._MakeSpotComputeResourceU  sR    ..,,CCf% ]3 D 
 /  r%   c           	          | j                   j                  | j                   j                  | j                  |j	                  d            |j	                  d                  S )z@Makes a cluster compute resource message for reserved instances.r1  r   )r1  r   )r   )r0   rf  NewReservedInstancesConfigr2  ra   rh  s     r#   r   z(ClusterUtil._MakeReservedComputeResource^  s]    ..!00KK00m1LM ]3 L 
 /  r%   c           	          | j                   j                  | j                   j                  |j                  d      |j                  d      |j                  d                  S )z@Makes a cluster compute resource message for DWS Flex instances.r   r   maxDuration)r   r   rn  )r   )r0   rf  NewDWSFlexInstancesConfigra   rh  s     r#   r   z'ClusterUtil._MakeDwsFlexComputeResourceg  s^    .. //IIf% ]3 ]3 J 
 /  r%   c                    | j                   j                  |j                  d      |j                  d      |j                  dd      |j                  d      || j                  |j                  d            | j	                  |j                  d      | j                   j                  j
                        | j                  |	      
      S )z:Makes a cluster slurm node set message from node set args.r   r   r  ro   r  r   rO   r   r   )r   resourceRequestIdr  r  r   r   rO   bootDisk)r0   SlurmNodeSetra   r   rR   rS   r   )r6   r   r   r   s       r#   r   zClusterUtil._MakeSlurmNodeSetq  s    ++<<",,{3 %6:$LL)>?&))(,,*GH||H-))66BB  
 L9 ,  r%   c                     | j                   j                  |j                  d      |j                  d      |j                  d            S )z<Makes a cluster slurm partition message from partition args.r   r  r  )r   r  r  )r0   SlurmPartitionra   )r6   r   s     r#   r   zClusterUtil._MakeSlurmPartition  sD    --====.--, .  r%   )NN)T )F)5__name__
__module____qualname____doc__r   strr7   r=   rL   rA   rB   rD   rF   rI   rR   boolr   r   r   r   r   r   r`   ri   rD  r   r   r   r2  r   r  r   r   r9  r   r   rO  r   rK  dictr   r  r)   r  r   r  r  r   r   r   r   r   r   r-  r%   r#   r,   r,   ?   s   + "G
 s8G	
#JGR:x&P	 	  	$*
72]~]~; ;
.c .
's 's '-C -
*c *
/ /
& <A$LL04S#XL0d(c (cJ4O4O.O (4c 4d 4Kd38n K/

 c3h
 	

 
 j&&&
!%c3hDGj&&& c3h 	
 ! " j&&&!%c3hDG
Z%%% r%   r,   r=  )rz  rB  rL  rT  typingr   r   r   r   googlecloudsdk.api_lib.utilr   r9   googlecloudsdk.callioper    googlecloudsdk.calliope.conceptsr	   4googlecloudsdk.command_lib.cluster_director.clustersr
   $googlecloudsdk.command_lib.util.apisr   (googlecloudsdk.command_lib.util.conceptsr   r   googlecloudsdk.core.utilr   r$   r}  r{  r*   r,   r-  r%   r#   <module>r     sY     4  	 	 ' ' A . 5 K : D G *I(BDcN BH Hr%   