
    :                    *   S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	  SSK
Jr  \R                  " SS9 " S S	5      5       rS
 r      S S jr\R                  " SS9 " S S5      5       r      S!S jr\R                  " SS9 " S S5      5       r      S"S jr " S S\R(                  5      r " S S\R(                  5      r\R.                  " S/ SQS9rS rS r          S#S jr      S$S jr    S%S jr    S&S jr  S'S jrg)(zUtils for VMware Engine private-clouds clusters commands.

Provides helpers for parsing the autoscaling settings and node type configs and
for combining settings from many sources together.
    )annotationsN)AnyDictListUnion)
exceptionsT)frozenc                  .    \ rS rSr% SrS\S'   S\S'   Srg)ScalingThresholds   zScaling thresholds for a single condition. Uses None for empty values.

Attributes:
  scale_in: The threshold for scaling in.
  scale_out: The threshold for scaling out.
intscale_in	scale_out N__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r       6lib/googlecloudsdk/command_lib/vmware/clusters/util.pyr   r      s     -.r   r   c                    Ub  U$ U $ )ztMerges two fields, favoring right one.

Args:
  left: First field.
  right: Second field.

Returns:
  Merged field.
r   leftrights     r   _MergeFieldsr   +   s     #--r   c                    U c  U$ Uc  g[        [        U R                  UR                  5      [        U R                  UR                  5      S9$ )a  Merges two ScalingThresholds objects, favoring right one.

Args:
  left: First ScalingThresholds object.
  right: Second ScalingThresholds object.

Returns:
  Merged ScalingThresholds - It contains the updated scale_in and scale_out
  values, favoring the right one.
  None - It indicates removal of threshold from autoscaling policy, favoring
  right one. Therefore, if right is None, return None.

Nr   r   )r   r   r   r   r   s     r   _MergeScalingThresholdsr"   8   sG      
\L
]	DMM5>>:T^^U__=
 r   c                  j    \ rS rSr% SrS\S'   S\S'   S\S'   S\S'   S	\S
'   S	\S'   S	\S'   S	\S'   Srg)AutoscalingPolicyS   a(  Represents the autoscaling policy for a single node type.

Uses None for empty settings.

Attributes:
  node_type_id: The node type id.
  scale_out_size: The size of a single scale out operation.
  min_node_count: The minimum number of nodes of this type in the cluster.
  max_node_count: The maximum number of nodes of this type in the cluster.
  cpu_thresholds: The CPU thresholds.
  granted_memory_thresholds: The granted memory thresholds.
  consumed_memory_thresholds: The consumed memory thresholds.
  storage_thresholds: The storage thresholds.
strnode_type_idr   scale_out_sizemin_node_countmax_node_countr   cpu_thresholdsgranted_memory_thresholdsconsumed_memory_thresholdsstorage_thresholdsr   Nr   r   r   r   r$   r$   S   s9     ##..//''r   r$   c                   U c  U$ Uc  U $ [        [        U R                  UR                  5      [        U R                  UR                  5      [        U R                  UR                  5      [        U R
                  UR
                  5      [        U R                  UR                  5      [        U R                  UR                  5      [        U R                  UR                  5      [        U R                  UR                  5      S9$ )zMerges two AutoscalingPolicy objects, favoring right one.

Args:
  left: First AutoscalingPolicy object.
  right: Second AutoscalingPolicy object.

Returns:
  Merged AutoscalingPolicy.
r'   r(   r)   r*   r+   r,   r-   r.   )r$   r   r'   r(   r)   r*   r"   r+   r,   r-   r.   r   s     r   _MergeAutoscalingPoliciesr1   n   s     
\L
]K	 1 153E3EF!$"5"5u7K7KL!$"5"5u7K7KL!$"5"5u7K7KL,


u33 !8

(
(%*I*I! "9

)
)5+K+K" 1

!
!5#;#;
 r   c                  B    \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
rg)AutoscalingSettings   ax  Represents the autoscaling settings for a private-cloud cluster.

Uses None for empty settings.

Attributes:
  min_cluster_node_count: The minimum number of nodes in the cluster.
  max_cluster_node_count: The maximum number of nodes in the cluster.
  cool_down_period: The cool down period for autoscaling.
  autoscaling_policies: The autoscaling policies for each node type.
r   min_cluster_node_countmax_cluster_node_countr&   cool_down_periodzDict[str, AutoscalingPolicy]autoscaling_policiesr   Nr   r   r   r   r3   r3      s!    	 44r   r3   c                   U c  U$ Uc  U $ 0 nU R                   R                  5        H4  u  p4X1R                   ;   a  [        XAR                   U   5      X#'   M0  XBU'   M6     UR                   R                  5        H  u  p4X0R                   ;  d  M  XBU'   M     [        [	        U R
                  UR
                  5      [	        U R                  UR                  5      [	        U R                  UR                  5      US9$ )zMerges two AutoscalingSettings objects, favoring right one.

Args:
  left: First AutoscalingSettings object.
  right: Second AutoscalingSettings object.

Returns:
  Merged AutoscalingSettings.
r5   r6   r7   r8   )r8   itemsr1   r3   r   r5   r6   r7   )r   r   policiespolicy_namepolicys        r   MergeAutoscalingSettingsr?      s     
\L
]K(!66<<>k0007
,,[9h %{ ? #77==?k333${ @ 
)

%
%u'C'C *

%
%u'C'C $


!7!7 $
 r   c                  (   ^  \ rS rSrU 4S jrSrU =r$ )InvalidNodeConfigsProvidedError   c                2   > [         [        U ]  SU 35        g NzINVALID_ARGUMENT: )superrA   __init__selfdetails	__class__s     r   rF   (InvalidNodeConfigsProvidedError.__init__   s    	
)49
WI&r   r   r   r   r   r   rF   r   __classcell__rJ   s   @r   rA   rA           r   rA   c                  (   ^  \ rS rSrU 4S jrSrU =r$ )'InvalidAutoscalingSettingsProvidedError   c                2   > [         [        U ]  SU 35        g rD   )rE   rQ   rF   rG   s     r   rF   0InvalidAutoscalingSettingsProvidedError.__init__   s    	
14A
WI&r   r   rL   rN   s   @r   rQ   rQ      rO   r   rQ   NodeTypeConfig)typecountcustom_core_count)typenamefield_namesc                    [         R                  " U 5      nUR                  5        VVs/ s H  u  p#US:  d  M  UPM     snn$ s  snnf )N   )collectionsCounterr;   )typestype_counts	node_typerW   s       r   FindDuplicatedTypesrb      s>    ##E*+,7,=,=,?	M,?(	519),?	MM	Ms
   AAc                    U  Vs/ s H  oS   PM	     nn[        U5      nU(       a  [        SR                  U5      5      eU  Vs/ s H&  n[        US   US   UR	                  SS5      5      PM(     sn$ s  snf s  snf )NrV   z"types: {} provided more than once.rW   zcustom-core-countr   )rb   rA   formatrU   get)nodes_configsconfigrequested_node_typesduplicated_typess       r   ParseNodesConfigsParametersrj      s    7DE}V.}E()=>
),334DE  "	
 "& 
.&/6::6I1+M "	
  F
s   A5-A:c                :   [        U UU0 S9nU H  n[        UR                  S5      UR                  S5      UR                  S5      UR                  S5      [        US5      [        US5      [        US5      [        US	5      S
9nXdR                  US   '   M     U$ )a  Parses inlined autoscaling settings (passed as CLI arguments).

The resulting object can later be passed to
googlecloudsdk.api_lib.vmware.util.ConstructAutoscalingSettingsMessage.

Args:
  min_cluster_node_count: autoscaling-min-cluster-node-count CLI argument.
  max_cluster_node_count: autoscaling-max-cluster-node-count CLI argument.
  cool_down_period: autoscaling-cool-down-period CLI argument.
  autoscaling_policies: list of update-autoscaling-policy CLI arguments.

Returns:
  Equivalent AutoscalingSettings instance.
r:   znode-type-idzscale-out-sizezmin-node-countzmax-node-countzcpu-thresholdszgranted-memory-thresholdszconsumed-memory-thresholdszstorage-thresholdsr0   name)r3   r$   re    _AutoscalingThresholdsFromPolicyr8   )r5   r6   r7   r8   parsed_settingsr>   parsed_policys          r   )ParseAutoscalingSettingsFromInlinedFormatrp      s    ( (33'	/ %f%ZZ/zz"23zz"23zz"237$
 #C/#
 $D0$
 <(
M& <I((8) %, 
r   c                r    U R                  U S35      nU R                  U S35      nUc  Uc  g [        X#S9$ )Nz	-scale-inz
-scale-outr!   )re   r   )r>   	thresholdr   r   s       r   rm   rm   3  sG     ZZ9+Y/0(jjI;j12))+	H	BBr   c                P    U  H   nX!;  d  M
  [        SR                  X!S95      e   g )Nz<unsupported key: {key}, supported keys are: {supported_keys})keysupported_keys)rQ   rd   )keysru   rt   s      r   %_ValidateIfOnlySupportedKeysArePassedrw   =  s8     c
 3
H
O
O P   r   c                   S n[        U R                  5       S/5        SU ;  a  [        S5      eU S   n[        UR                  5       / SQ5        [        UR	                  S5      UR	                  S5      UR	                  S5      0 S9nS	U;  a  U$ US	   R                  5        H  u  pE[        UR                  5       / S
Q5        [        UR	                  S5      UR	                  S5      UR	                  S5      UR	                  S5      U" UR	                  S5      5      U" UR	                  S5      5      U" UR	                  S5      5      U" UR	                  S5      5      S9nXcR                  U'   M     U$ )a  Parses the autoscaling settings from the format returned by  the describe command.

The resulting object can later be passed to
googlecloudsdk.api_lib.vmware.util.ConstructAutoscalingSettingsMessage.

Args:
  cluster: dictionary with the settings. Parsed from a file provided by user.

Returns:
  Equivalent AutoscalingSettings instance.

Raises:
  InvalidAutoscalingSettingsProvidedError: if the file format was wrong.
c                    U c  g [        U R                  5       SS/5        [        U R                  S5      U R                  S5      S9$ )NscaleInscaleOutr!   )rw   rv   r   re   )thresholds_dicts    r   _ParseThresholds@ParseAutoscalingSettingsFromFileFormat.<locals>._ParseThresholds[  sQ    )J 7  $$Y/!%%j1 r   autoscalingSettingsz,autoscalingSettings not provided in the file)minClusterNodeCountmaxClusterNodeCountcoolDownPeriodautoscalingPoliciesr   r   r   r:   r   )
nodeTypeIdscaleOutSizeminNodeCountmaxNodeCountcpuThresholdsgrantedMemoryThresholdsconsumedMemoryThresholdsstorageThresholdsr   r   r   r   r   r   r   r   r0   )rw   rv   rQ   r3   re   r;   r$   r8   )clusterr}   autoscaling_settingsrn   r=   policy_settingsro   s          r   &ParseAutoscalingSettingsFromFileFormatr   I  s   $ (9N8OP')
16  !!67'! (1556KL1556KL+//0@A	/ "66&:'	EG'"k *		
 &$((6&**>:&**>:&**>:'(;(;O(LM"2 9:#
 $4 :;$
 , 34
M  9F((5A'D 
r   c                   U R                   c  gU R                   n[        UR                  UR                  UR                  0 S9nUR
                  R                   H  nUR                  UR                  pTS n[        UR                  UR                  UR                  UR                  U" UR                  5      U" UR                  5      U" UR                   5      U" UR"                  5      S9nXrR$                  U'   M     U$ )aC  Parses the autoscaling settings from the format returned by the describe command.

The resulting object can later be passed to
googlecloudsdk.api_lib.vmware.util.ConstructAutoscalingSettingsMessage.

Args:
  cluster_message: cluster object with the autoscaling settings.

Returns:
  Equivalent AutoscalingSettings istance.
Nr:   c                F    U c  g [        U R                  U R                  S9$ )Nr!   )r   rz   r{   )
thresholdss    r   r}   ?ParseAutoscalingSettingsFromApiFormat.<locals>._ParseThresholds  s*    		%%'' r   r0   )r   r3   r   r   r   r   additionalPropertiesrt   valuer$   r   r   r   r   r   r   r   r   r8   )cluster_messager   rn   itemr=   r   r}   ro   s           r   %ParseAutoscalingSettingsFromApiFormatr     s     ((0(<<'1EE1EE+::	/ #66KKd#'88TZZ &$//&33&33&33'(E(EF"233#
 $444$
 ,O,M,MNM 9F((53 L6 
r   )r   ScalingThresholds | Noner   r   returnr   )r   r$   r   r$   r   r$   )r   r3   r   r3   r   r3   )
r5   r   r6   r   r7   r&   r8   z List[Dict[str, Union[str, int]]]r   r3   )r>   zDict[str, Union[str, int]]rr   r&   r   r   )rv   	List[str]ru   r   )r   zDict[str, Any]r   r3   )r   r3   ) r   
__future__r   r]   dataclassestypingr   r   r   r   googlecloudsdk.corer   	dataclassr   r   r"   r$   r1   r3   r?   ErrorrA   rQ   
namedtuplerU   rb   rj   rp   rm   rw   r   r   r   r   r   <module>r      s   #   ) ) * d#	 	 $	
.
"+C6 d#( ( $(4#
## #L d#5 5 $5$)
)&9))Xj&6&6 j.>.>  ''6N
"111 1 ;	1
 1hC&C36CC	
	%.	[[[|55r   