
    ;                    V   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ZddlZddlm	Z	m
Z
mZmZ ddlmZ  ej                  d	
       G d d             Zd Z	 	 	 	 	 	 d#dZ ej                  d	
       G d d             Z	 	 	 	 	 	 d$dZ ej                  d	
       G d d             Z	 	 	 	 	 	 d%dZ G d dej.                        Z G d dej.                        Z ej4                  dg d      Zd Zd Z	 	 	 	 	 	 	 	 	 	 d&dZ	 	 	 	 	 	 d'dZ	 	 	 	 d(d Z 	 	 	 	 d)d!Z!	 	 d*d"Z"y)+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                  &    e Zd ZU dZded<   ded<   y)ScalingThresholdszScaling 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_outN__name__
__module____qualname____doc____annotations__     6lib/googlecloudsdk/command_lib/vmware/clusters/util.pyr   r   !   s     -.r   r   c                    ||S | S )zMerges two fields, favoring right one.

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

  Returns:
    Merged field.
  r   leftrights     r   _MergeFieldsr   .   s     #--r   c                    | |S |yt        t        | j                  |j                        t        | j                  |j                              S )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                  b    e Zd ZU dZded<   ded<   ded<   ded<   ded	<   ded
<   ded<   ded<   y)AutoscalingPolicya>  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_thresholdsNr   r   r   r   r$   r$   V   s9     ##..//''r   r$   c                   | |S || S t        t        | j                  |j                        t        | j                  |j                        t        | j                  |j                        t        | j
                  |j
                        t        | j                  |j                        t        | j                  |j                        t        | j                  |j                        t        | j                  |j                              S )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   _MergeAutoscalingPoliciesr0   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                  :    e Zd ZU dZded<   ded<   ded<   ded<   y	)
AutoscalingSettingsa  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_policiesNr   r   r   r   r2   r2      s!    	 44r   r2   c                   | |S || S i }| j                   j                         D ]5  \  }}||j                   v rt        ||j                   |         ||<   1|||<   7 |j                   j                         D ]  \  }}|| j                   vs|||<    t        t	        | j
                  |j
                        t	        | j                  |j                        t	        | j                  |j                        |      S )zMerges two AutoscalingSettings objects, favoring right one.

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

  Returns:
    Merged AutoscalingSettings.
  r3   r4   r5   r6   )r6   itemsr0   r2   r   r3   r4   r5   )r   r   policiespolicy_namepolicys        r   MergeAutoscalingSettingsr=      s    
\L
]K(!66<<>k6e0007
%,,[9h{ %h{ ? #77==?k6$333$h{ @ 
)

%
%u'C'C *

%
%u'C'C $


!7!7 $
 r   c                       e Zd Z fdZ xZS )InvalidNodeConfigsProvidedErrorc                2    t         t        |   d|        y NzINVALID_ARGUMENT: )superr?   __init__selfdetails	__class__s     r   rC   z(InvalidNodeConfigsProvidedError.__init__   s    	
)49
WI&r   r   r   r   rC   __classcell__rG   s   @r   r?   r?           r   r?   c                       e Zd Z fdZ xZS )'InvalidAutoscalingSettingsProvidedErrorc                2    t         t        |   d|        y rA   )rB   rM   rC   rD   s     r   rC   z0InvalidAutoscalingSettingsProvidedError.__init__   s    	
14A
WI&r   rH   rJ   s   @r   rM   rM      rK   r   rM   NodeTypeConfig)typecountcustom_core_count)typenamefield_namesc                    t        j                  |       }|j                         D cg c]  \  }}|dkD  s| c}}S c c}}w )N   )collectionsCounterr9   )typestype_counts	node_typerQ   s       r   FindDuplicatedTypesr\      s@    ##E*+,7,=,=,?	M,?(	5519),?	MM	Ms   ??c                    | D cg c]  }|d   	 }}t        |      }|rt        dj                  |            | D cg c]%  }t        |d   |d   |j	                  dd            ' c}S c c}w c c}w )NrP   z"types: {} provided more than once.rQ   zcustom-core-countr   )r\   r?   formatrO   get)nodes_configsconfigrequested_node_typesduplicated_typess       r   ParseNodesConfigsParametersrd      s    7DE}V&.}E()=>
),334DE  "	
 "& 
.&/6::6I1+M "	
  F
s
   A+*A0c                D   t        | ||i       }|D ]  }t        |j                  d      |j                  d      |j                  d      |j                  d      t        |d      t        |d      t        |d      t        |d	      
      }||j                  |d   <    |S )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.
  r8   znode-type-idzscale-out-sizezmin-node-countzmax-node-countzcpu-thresholdszgranted-memory-thresholdszconsumed-memory-thresholdszstorage-thresholdsr/   name)r2   r$   r_    _AutoscalingThresholdsFromPolicyr6   )r3   r4   r5   r6   parsed_settingsr<   parsed_policys          r   )ParseAutoscalingSettingsFromInlinedFormatrj     s    ( (33'	/ %f%ZZ/zz"23zz"23zz"237$
 #C/#
 $D0$
 <(
M& <IO((8) %, 
r   c                v    | j                  | d      }| j                  | d      }||y t        ||      S )Nz	-scale-inz
-scale-outr!   )r_   r   )r<   	thresholdr   r   s       r   rg   rg   6  sI     ZZ9+Y/0(jjI;j12))+	H		BBr   c                R    | D ]"  }||vst        dj                  ||             y )Nz<unsupported key: {key}, supported keys are: {supported_keys})keysupported_keys)rM   r^   )keysro   rn   s      r   %_ValidateIfOnlySupportedKeysArePassedrq   @  s<     c
. 3
H
O
On P   r   c                   d }t        | j                         dg       d| vrt        d      | d   }t        |j                         g d       t        |j	                  d      |j	                  d      |j	                  d      i       }d	|vr|S |d	   j                         D ]  \  }}t        |j                         g d
       t        |j	                  d      |j	                  d      |j	                  d      |j	                  d       ||j	                  d             ||j	                  d             ||j	                  d             ||j	                  d                  }||j                  |<    |S )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                    | y t        | j                         ddg       t        | j                  d      | j                  d            S )NscaleInscaleOutr!   )rq   rp   r   r_   )thresholds_dicts    r   _ParseThresholdsz@ParseAutoscalingSettingsFromFileFormat.<locals>._ParseThresholds^  sQ    )J 7  $$Y/!%%j1 r   autoscalingSettingsz,autoscalingSettings not provided in the file)minClusterNodeCountmaxClusterNodeCountcoolDownPeriodautoscalingPoliciesry   rz   r{   r8   r|   )
nodeTypeIdscaleOutSizeminNodeCountmaxNodeCountcpuThresholdsgrantedMemoryThresholdsconsumedMemoryThresholdsstorageThresholdsr}   r~   r   r   r   r   r   r   r/   )rq   rp   rM   r2   r_   r9   r$   r6   )clusterrw   autoscaling_settingsrh   r;   policy_settingsri   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O((5A'D 
r   c                   | j                   y| j                   }t        |j                  |j                  |j                  i       }|j
                  j                  D ]  }|j                  |j                  }}d }t        |j                  |j                  |j                  |j                   ||j                         ||j                         ||j                          ||j"                              }||j$                  |<    |S )aQ  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.
  Nr8   c                J    | y t        | j                  | j                        S )Nr!   )r   rt   ru   )
thresholdss    r   rw   z?ParseAutoscalingSettingsFromApiFormat.<locals>._ParseThresholds  s*    		%%'' r   r/   )rx   r2   ry   rz   r{   r|   additionalPropertiesrn   valuer$   r}   r~   r   r   r   r   r   r   r6   )cluster_messager   rh   itemr;   r   rw   ri   s           r   %ParseAutoscalingSettingsFromApiFormatr     s    ((0(<<'1EE1EE+::	/ #66KKd#'88TZZK &$//&33&33&33'(E(EF"233#
 $444$
 ,O,M,MNM 9FO((53 L6 
r   )r   ScalingThresholds | Noner   r   returnr   )r   r$   r   r$   r   r$   )r   r2   r   r2   r   r2   )
r3   r   r4   r   r5   r%   r6   z List[Dict[str, Union[str, int]]]r   r2   )r<   zDict[str, Union[str, int]]rl   r%   r   r   )rp   	List[str]ro   r   )r   zDict[str, Any]r   r2   )r   r2   )#r   
__future__r   r   r   r   rW   dataclassestypingr   r   r	   r
   googlecloudsdk.corer   	dataclassr   r   r"   r$   r0   r2   r=   Errorr?   rM   
namedtuplerO   r\   rd   rj   rg   rq   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   