
    ;                    N   S 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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\R4                  " 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.
    )absolute_import)annotations)division)unicode_literalsN)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%   ;   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)AutoscalingPolicyV   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'   V   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/   r0   r1   )r'   r"   r*   r+   r,   r-   r%   r.   r/   r0   r1   r   s     r   _MergeAutoscalingPoliciesr4   q   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   r6   r6      s!    	 44r   r6   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.
r8   r9   r:   r;   )r;   itemsr4   r6   r"   r8   r9   r:   )r    r!   policiespolicy_namepolicys        r   MergeAutoscalingSettingsrB      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: )superrD   __init__selfdetails	__class__s     r   rI   (InvalidNodeConfigsProvidedError.__init__   s    	
)49
WI&r   r   r   r   r   r   rI   r   __classcell__rM   s   @r   rD   rD           r   rD   c                  (   ^  \ rS rSrU 4S jrSrU =r$ )'InvalidAutoscalingSettingsProvidedError   c                2   > [         [        U ]  SU 35        g rG   )rH   rT   rI   rJ   s     r   rI   0InvalidAutoscalingSettingsProvidedError.__init__   s    	
14A
WI&r   r   rO   rQ   s   @r   rT   rT      rR   r   rT   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_typerZ   s       r   FindDuplicatedTypesre      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 )NrY   z"types: {} provided more than once.rZ   zcustom-core-countr   )re   rD   formatrX   get)nodes_configsconfigrequested_node_typesduplicated_typess       r   ParseNodesConfigsParametersrm      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-thresholdsr3   name)r6   r'   rh    _AutoscalingThresholdsFromPolicyr;   )r8   r9   r:   r;   parsed_settingsrA   parsed_policys          r   )ParseAutoscalingSettingsFromInlinedFormatrs     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$   )rh   r   )rA   	thresholdr   r   s       r   rp   rp   6  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)rT   rg   )keysrx   rw   s      r   %_ValidateIfOnlySupportedKeysArePassedrz   @  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$   )rz   ry   r   rh   )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   r3   )rz   ry   rT   r6   rh   r>   r'   r;   )clusterr   autoscaling_settingsrq   r@   policy_settingsrr   s          r   &ParseAutoscalingSettingsFromFileFormatr   L  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   r}   r~   )
thresholdss    r   r   ?ParseAutoscalingSettingsFromApiFormat.<locals>._ParseThresholds  s*    		%%'' r   r3   )r   r6   r   r   r   r   additionalPropertiesrw   valuer'   r   r   r   r   r   r   r   r   r;   )cluster_messager   rq   itemr@   r   r   rr   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    r6   r!   r6   r   r6   )
r8   r   r9   r   r:   r)   r;   z List[Dict[str, Union[str, int]]]r   r6   )rA   zDict[str, Union[str, int]]ru   r)   r   r   )ry   	List[str]rx   r   )r   zDict[str, Any]r   r6   )r   r6   )#r   
__future__r   r   r   r   r`   dataclassestypingr   r   r	   r
   googlecloudsdk.corer   	dataclassr   r"   r%   r'   r4   r6   rB   ErrorrD   rT   
namedtuplerX   re   rm   rs   rp   rz   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   