
    G4                     .   d Z ddlmZ ddlmZ ddlmZ ddl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ZdZ G d de      Z G d dej2                  e      Z G d dej6                  e      Z G d dej:                  e      Zy)z7Base class for Cluster Upgrade Feature CRUD operations.    )absolute_import)division)unicode_literalsN)
exceptions)base)util)log)resource_projector)timesclusterupgradec                   2    e Zd ZdZd Zed        Zd Zd Zy)ClusterUpgradeCommandz0Base class for Cluster Upgrade Feature commands.c                 (    t         | _        || _        y N)CLUSTER_UPGRADE_FEATUREfeature_nameargs)selfr   s     Plib/googlecloudsdk/command_lib/container/fleet/scopes/rollout_sequencing/base.py__init__zClusterUpgradeCommand.__init__&   s    /DDI    c                 "   d}| j                  |      }t        |      dk7  s|d   dk7  r$t        j                  dj	                  |             |d   }t        j                  |      }t        j                  |      |d<   |j                  |      S )z6Rebuilds scope name with project number instead of ID./   r   projectsz%{} is not a valid Scope resource name   )
splitlenr   Errorformatproject_utilGetProjectNumbersix	text_typejoin)name	delimitertokens
project_idproject_numbers        r   GetScopeNameWithProjectNumberz3ClusterUpgradeCommand.GetScopeNameWithProjectNumber*   s     IZZ	"F
6{a6!9
2
1
8
8
>@ @J!22:>Nn-F1I>>&!!r   c                 J    | j                   j                  j                         S )z:Required to initialize HubClient. See calliope base class.)r   calliope_commandReleaseTrackr   s    r   r.   z"ClusterUpgradeCommand.ReleaseTrack7   s    99%%2244r   c                 x    h dt        fd| j                  j                  j                         D              S )zDChecks if any Cluster Upgrade Feature related arguments are present.>   upstream_scopeupgrade_selectorreset_upstream_scopeshow_cluster_upgradedefault_upgrade_soakingshow_linked_cluster_upgradeadd_upgrade_soaking_override remove_upgrade_soaking_overridesc              3   4   K   | ]  \  }}|xr |v   y wr    ).0flag	has_valuecluster_upgrade_flagss      r   	<genexpr>z@ClusterUpgradeCommand.IsClusterUpgradeRequest.<locals>.<genexpr>G   s,      B&@?4 :T%:::&@s   )anyr   __dict__items)r   r>   s    @r   IsClusterUpgradeRequestz-ClusterUpgradeCommand.IsClusterUpgradeRequest;   s>    	  B&*ii&8&8&>&>&@B B Br   N)	__name__
__module____qualname____doc__r   staticmethodr+   r.   rC   r:   r   r   r   r   #   s)    8 
" 
"5Br   r   c                   X    e Zd ZdZed        Zed        Zed        Zd Zd Z	d Z
d Zy	)
DescribeCommandz9Command for describing a Scope's Cluster Upgrade Feature.c                 *    | j                  d      d   S )z<Extracts the project name from the full Scope resource name.r   r   )r   )r&   s    r   GetProjectFromScopeNamez'DescribeCommand.GetProjectFromScopeNameN   s     ::c?1r   c                 L   | j                   <| j                   j                  }|$t        j                  |      | j                   _        | j                  D ]L  }|j                   |j                   j                  }|)t        j                  |      |j                   _        N | S )z@Formats display strings for all cluster upgrade duration fields.)postConditionssoakingrJ   DisplayDurationgkeUpgradeOverrides)cluster_upgrade_specdefault_soakingoverrideoverride_soakings       r   FormatDurationszDescribeCommand.FormatDurationsS   s     **6,;;CCo		$6E6U6U7++3(<<		 	 	,#22::',;,K,K- (
!
!
)	 =  r   c                     t        j                  |       }t        j                  |      }t        j                  dd|      j                         S )z0Returns the display string for a duration value.z[-PT] )r   ParseDurationFormatDurationresublower)proto_duration_stringdurationiso_durations      r   rP   zDescribeCommand.DisplayDurationc   sA     ""#89H''1L66'2|,2244r   c                    t         j                  |j                        }| j                  j	                  d      r)| j                  j
                  r| j                  |||      S | j                  j	                  d      rB| j                  j                  r,t        j                  |      }| j                  ||      |d<   |S |S )zDAdds Cluster Upgrade Feature information to describe Scope response.r4   r6   clusterUpgrades)r   r+   r&   r   IsKnownAndSpecifiedr4   AddClusterUpgradeInfoToScoper6   r
   MakeSerializableGetLinkedClusterUpgradeScopes)r   scopefeature
scope_nameserialized_scopes        r   GetScopeWithClusterUpgradeInfoz.DescribeCommand.GetScopeWithClusterUpgradeInfoj   s    &DDUZZPJ		%%&<=		&&..uj'JJ
))
'
'(E
F
))
/
/+<<UC,0,N,N
g-()Lr   c                 Z    t        j                  |      }| j                  ||      |d<   |S )NclusterUpgrade)r
   re   GetClusterUpgradeInfoForScope)r   rg   ri   rh   rj   s        r   rd   z,DescribeCommand.AddClusterUpgradeInfoToScopex   s5    )::5A)-)K)KG*%&r   c                    | j                   j                  |j                        }||vs||   j                  s&dj	                  |      }t        j                  |      | j                   j                  | j                  j                  |j                        |   j                  xs | j                  j                         }||t        j                  ||   j                        dS )z@Gets Cluster Upgrade Feature information for the provided Scope.z8Cluster Upgrade feature is not configured for Scope: {}.)rg   statespec)	hubclientToPyDict
scopeSpecsr   r    r   r   ToPyDefaultDictmessagesScopeFeatureStatescopeStatesClusterUpgradeScopeStaterJ   rV   )r   ri   rh   scope_specsmsgrp   s         r   rn   z-DescribeCommand.GetClusterUpgradeInfoForScope~   s    ..))'*<*<=K+%
#22GfZ  
S!!&&MM++W-@-@	

	$n	4 ==113	 
 //
#22
 r   c                     	 t         j                  |      t        |g      	 	fd 	fd j                  |      } |      dd } |      dd }||gz   |z   S )zAGets Cluster Upgrade Feature information for the entire sequence.c                    | j                  dd      }|r|j                  nd}|s| gS |d   }|v r| gS j                  |       t        j	                  |      }|	k(  r
nj                  |      }	 j                  ||      } |      | gz   S # t        j                  $ r"}t        j                  |       | gcY d}~S d}~ww xY w)z5Recursively gets information for the upstream Scopes.rq   Nr   project)getupstreamScopesaddrJ   rL   
GetFeaturern   r   r   r	   warning)cluster_upgradeupstream_specupstream_scopesupstream_scope_nameupstream_scope_projectupstream_featureupstream_cluster_upgradeeUpTheStreamcurrent_projectrh   r   visiteds           r   r   zBDescribeCommand.GetLinkedClusterUpgradeScopes.<locals>.UpTheStream   s    %))&$7m8E444o   ,A.		'  kk%&.FF
  ,>'
//"8/
9 !#'#E#E!1$3 
 12o5FFF  !A  !s   5B C&C=CCc                 $   | j                  dd      }|r|j                  nd}|s| gS |d   }|v r| gS j                  |       t        j	                  |      }|k(  r	n
j                  |      }
j                  ||      }| g |      z   S )z7Recursively gets information for the downstream Scopes.rp   Nr   r~   )r   downstreamScopesr   rJ   rL   r   rn   )r   downstream_statedownstream_scopesdownstream_scope_namedownstream_scope_projectdownstream_featuredownstream_cluster_upgradeDownTheStreamr   rh   r   r   s          r   r   zDDescribeCommand.GetLinkedClusterUpgradeScopes.<locals>.DownTheStream   s    (,,Wd;/?

+
+T    02	'	)  kk'(!0!H!H
"! .@'
//":/
;  $(#E#E
!3$5 /I!JJJr   Nr   )rJ   rL   setrn   )
r   ri   rh   current_cluster_upgradeupstream_cluster_upgradesdownstream_cluster_upgradesr   r   r   r   s
   ` `   @@@@r   rf   z-DescribeCommand.GetLinkedClusterUpgradeScopes   s     &==jIO:,GG G:K K4 #@@G +,C DSb I"/0G"H"L%)@(AA'( )r   N)rD   rE   rF   rG   rH   rL   rV   rP   rk   rd   rn   rf   r:   r   r   rJ   rJ   K   sT    A      5 5.B)r   rJ   c                       e Zd ZdZd Zy)EnableCommandz4Base class for enabling the Cluster Upgrade Feature.c                     	 | j                   j                  | j                               S # t        j                  $ r< | j                  | j                  j                                | j                         cY S w xY w)zBGets the project's Cluster Upgrade Feature, enabling if necessary.)rr   r   FeatureResourceNameapitools_exceptionsHttpNotFoundErrorEnablerv   Featurer/   s    r   GetWithForceEnablez EnableCommand.GetWithForceEnable   sb    ^^&&t'?'?'ABB00  kk$--'')*__s   (+ AA:9A:N)rD   rE   rF   rG   r   r:   r   r   r   r      s
    <
r   r   c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )UpdateCommandz4Base class for updating the Cluster Upgrade Feature.c                    | j                   j                  | j                  j                  |j                        }||   j
                  xs | j                  j                         }| j                  |       | j                  |       | j                  |       |||   _        | j                  j                  | j                   j                  |||   i            }t        t        | ;  dg|      S )z,Updates Cluster Upgrade Feature information.)rt   rt   )rr   ru   rv   ScopeFeatureSpecrt   r   ClusterUpgradeScopeSpecHandleUpstreamScopesHandleDefaultSoakTimeHandleUpgradeSoakingOverridesr   ToScopeSpecssuperr   Update)r   rh   ri   scope_specs_maprR   patch	__class__s         r   r   zUpdateCommand.Update   s    nn44&&(:(:<O 	
#22 	0--/  	2334&&';<1EOJ.MM!!>>..457 " 8E ,l^UCCr   c                    | j                   j                  d      r| j                   j                  rg |_        y| j                   j                  d      r4| j                   j                  | j                   j                  g|_        yyy)z\Updates the Cluster Upgrade Feature's upstreamScopes field based on provided arguments.
    r3   r1   N)r   rc   r3   r   r1   )r   rR   s     r   r   z"UpdateCommand.HandleUpstreamScopes  sp     			%%&<=		&&,.)
))
'
'(8
9
))
"
"
.-1YY-E-E,F) / :r   c                 &   | j                   j                  d      r| j                   j                  yt        j                  | j                   j                        }|j
                  xs | j                  j                         }||_        ||_        y)zCUpdates the Cluster Upgrade Feature's postConditions.soaking field.r5   N)	r   rc   r5   r   FormatDurationForJsonrN   rv   ClusterUpgradePostConditionsrO   )r   rR   rS   post_conditionss       r   r   z#UpdateCommand.HandleDefaultSoakTime  s}    II))*CD		))111		))+O 	++ 	5224  .O*9'r   c                    | j                   j                  d      r| j                   j                  rg |_        y| j                   j                  d      r!| j                   j                  d      r| j                   j                  | j                   j
                  t        j                  | j                   j                        }|j                  xs g }| j                  j                         }| j                  j                  |      |_        | j                   j
                  d   }| j                   j
                  d   }| j                  j                  ||      |_        ||gz   }||_        yyyyy)	z?Updates the ClusterUpgrade Feature's gkeUpgradeOverrides field.r8   r7   r2   N)rO   r&   version)r&   r   )r   rc   r8   rQ   r7   r2   r   r   rv    ClusterUpgradeGKEUpgradeOverrider   rN   ClusterUpgradeGKEUpgradeupgrade)r   rR   rO   existing_gke_upgrade_overridesnew_gke_upgrade_overrideupgrade_nameupgrade_versionnew_gke_upgrade_overridess           r   r   z+UpdateCommand.HandleUpgradeSoakingOverrides  sY   		%%&HI		2213.
))
'
'(F
G
))
'
'(:
;
))
0
0
<
))
$
$
0++
))
0
02g 
2
2
8b % --
8
8
: 040Z0Z 1[ 1- YY//7l		229=o)-)O)O_ *P *6& ),D+E
E  1J.! 1 = < Hr   )	rD   rE   rF   rG   r   r   r   r   __classcell__)r   s   @r   r   r      s    <D,G:Kr   r   )rG   
__future__r   r   r   r[   apitools.base.pyr   r   3googlecloudsdk.command_lib.container.fleet.featuresr   feature_base#googlecloudsdk.command_lib.projectsr   r!   googlecloudsdk.corer	   googlecloudsdk.core.resourcer
   googlecloudsdk.core.utilr   r#   r   objectr   FeatureCommandrJ   EnableCommandMixinr   UpdateCommandMixinr   r:   r   r   <module>r      s    > &  ' 	 > T D * # ; * 
* %BF %BPL)l113H L)^L335J  IKL335J IKr   