
    K                     0   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J	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SSKJr  SS
KJr  SS	KJr  SSKJr  SSKJ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\R@                  5      r! " S S\!5      r" " S S\"\RF                  5      r$ " S S\!\RJ                  5      r% " S S\!5      r& " S S\&\RN                  5      r' " S S\&\RP                  5      r)    S%S jr*     S&S  jr+S! r,S" r-S'S# jr.S$ r/g)(zIBase classes for [enable|disable|describe] commands for Feature resource.    )absolute_import)division)unicode_literals)
exceptions)util)
enable_api)apis)base)api_util)	resources)info)log)
properties)retryNc                   b   ^  \ rS rSrSrSr\S 5       rS
U 4S jjrS
S jr	S
S jr
S
S jrS	rU =r$ )FeatureCommand'   zFFeatureCommand is a mixin adding common utils to the Feature commands. c                 B    [         R                  " U R                  5      $ )z2The Feature info entry for this command's Feature.)r   Getfeature_name)selfs    ?lib/googlecloudsdk/command_lib/container/fleet/features/base.pyfeatureFeatureCommand.feature+   s     88D%%&&    c                 @   > [         [        U ]  U R                  U5      $ )zZBuilds the full resource name, using the core project property if no project is specified.)superr   FeatureResourceNamer   )r   project	__class__s     r   r   "FeatureCommand.FeatureResourceName0   s$    ++/+<+<gFGr   c                     U=(       d2    [         R                  R                  R                  R	                  5       n[
        R                  " SR                  U R                  R                  U5      5      $ )zFConstructs a new Error for reporting when this Feature is not enabled.z*{} Feature for project [{}] is not enabled
r   VALUEScorer    	GetOrFailr   Errorformatr   display_namer   r    s     r   FeatureNotEnabledError%FeatureCommand.FeatureNotEnabledError5   sW    C**//77AACGHOO!!7, - -r   c                     U=(       d2    [         R                  R                  R                  R	                  5       n[
        R                  " SR                  U R                  R                  U5      5      $ )zSConstructs a new Error for reporting when accessing this Feature is not authorized.z4Not authorized to access {} Feature for project [{}]r$   r+   s     r   NotAuthorizedError!FeatureCommand.NotAuthorizedError;   sW    C**//77AACG>EELL%%w	01 1r   c                      U R                   R                  U R                  U5      5      $ ! [        R                   a    U R                  U5      e[        R                   a    U R                  U5      ef = f)zBFetch this command's Feature from the API, handling common errors.)	hubclient
GetFeaturer   apitools_exceptionsHttpNotFoundErrorr,   HttpUnauthorizedErrorr/   r+   s     r   r3   FeatureCommand.GetFeatureB   sl    -^^&&t'?'?'HII00 1''0044 -##G,,-s
   ), A
A6 N)__name__
__module____qualname____firstlineno____doc__r   propertyr   r   r,   r/   r3   __static_attributes____classcell__)r!   s   @r   r   r   '   s7    N,' 'G
-1- -r   r   c                   $    \ rS rSrSrS rS rSrg)EnableCommandMixinL   z.A mixin for functionality to enable a Feature.c                     [         R                  R                  R                  R	                  5       nU R
                  R                  (       a*  [        R                  " X R
                  R                  5        [        R                  " U5      n [        R                  " SSS9nUR                  U R                  R                  X0R                   U4U R"                  SS9nS	R+                  U R
                  R,                  5      nU R?                  U R                  R@                  XXS
9$ ! [        R$                   a:    [&        R(                  " SR+                  U R
                  R,                  5      5      e[.        R0                   at  n[2        R4                  " U5      nUR6                  S:w  a  e [8        R:                  R=                  SR+                  U R
                  R,                  U5      5         S nAg S nAff = f)N   g      ?)max_retrialsexponential_sleep_multiplieri  )argsshould_retry_ifsleep_msz-Retry limit exceeded waiting for {} to enableALREADY_EXISTSz.{} Feature for project [{}] is already enabledz$Waiting for Feature {} to be created)opmessage)!r   r%   r&   r    r'   r   apir   EnableServiceIfDisabledr   LocationResourceNamer   RetryerRetryOnExceptionr2   CreateFeaturer   _FeatureAPINotEnabledMaxRetrialsExceptionr   r(   r)   r*   r4   HttpConflictErrorcore_api_exceptionsHttpErrorPayloadstatus_descriptionr   statusPrintWaitForHubOpfeature_waiter)	r   r   r    parentretryerrM   eerrormsgs	            r   EnableEnableCommandMixin.EnableO   s   $$,,668G||((,,2B2BC&&w/F 14Pg##
..
&
&))7344	 $ b" 1
7
7!!#CT^^::rOO %% *
9
@
@ll'')* * 00 !2215e		!	!%5	5	jjGNN
,,
#
#W. /s   AD) )A G=	A*G88G=c                 ^   AAU R                   R                  (       d  gU[        R                  :w  a  g[        R
                  " U5      nUR                  S:X  a4  U R                   R                  UR                  ;   a  SUR                  ;   d  g[        R                  R                  S5        g)NFFAILED_PRECONDITIONzis not enabledz/Waiting for service API enablement to finish...T)r   rO   r4   HttpBadRequestErrorrX   rY   rZ   rN   r   r[   r\   )r   exc_type	exc_value	tracebackstaterb   s         r   rU   (EnableCommandMixin._FeatureAPINotEnabledm   s    5<<&:::00;E$$(==LL--JJFGr   r8   N)r:   r;   r<   r=   r>   rd   rU   r@   r8   r   r   rC   rC   L   s    6P<r   rC   c                       \ rS rSrSrS rSrg)EnableCommand|   z2Base class for the command that enables a Feature.c                 T    U R                  U R                  R                  5       5      $ r9   )rd   messagesFeaturer   _s     r   RunEnableCommand.Run   s    ;;t}},,.//r   r8   N)r:   r;   r<   r=   r>   rv   r@   r8   r   r   ro   ro   |   s
    :0r   ro   c                   $    \ rS rSrSrS rS rSrg)DescribeCommand   zBBase class for the command that describes the status of a Feature.c                 "    U R                  5       $ r9   )r3   rt   s     r   rv   DescribeCommand.Run   s    ??r   c                 X   U Vs0 s H  n[         R                  " U5      U_M     nn0 nUR                  (       ak  UR                  R                   Vs0 s HJ  n[         R                  " UR                  5      U;   d  M)  [         R                  " UR                  5      U_ML     nnUR                  5        VVs/ s H  u  psXu;  d  M  UPM     nnnU(       a:  [        R                  " SR                  U R                  R                  U5      5      e0 n	UR                  (       ak  UR                  R                   Vs0 s HJ  n[         R                  " UR                  5      U;   d  M)  [         R                  " UR                  5      U_ML     n	nU R                  R                  R                  [        UR!                  5       5      S9Ul        U R                  R                  R#                  U Vs/ s H  nX9;   d  M
  X   PM     snS9Ul        gs  snf s  snf s  snnf s  snf s  snf )a  Leave only specs and states of Memberships in the Feature.

Respects the order of the Membership specs in the original Feature.

Args:
  feature: Feature in the v1 API.
  memberships: List of resource names according to go/resource-names.
    Ideally, the existence of these Memberships will have been verified.
Returns:
  None
Raises:
  exceptions.Error: if any of Memberships does not have a spec in Feature.
zcThe following requested memberships are not configured on the {} feature, under membershipSpecs: {})additionalPropertiesN)r   MembershipPartialNamemembershipSpecsr~   keyitemsr   r(   r)   r   r*   membershipStatesrr   rs   MembershipSpecsValuelistvaluesMembershipStatesValue)
r   r   membershipsm memberships_by_location_and_name%membership_specs_by_location_and_nameentrylocation_namemissing_memberships&membership_states_by_location_and_names
             r   filter_feature_for_memberships.DescribeCommand.filter_feature_for_memberships   s   " 3>(2=Q""1%q(+ % ( -/) ..CC/Ce''		2-. 7$
$
$UYY
/
6C , / !A F F H HME 	
 H  
 1F4<<,,.AB 
 .0* //DD0De''		223 7$
$
$UYY
/
6D - 0 #mm33HH!188:
 I G
  $}}44JJ ;
:: 625:
  K  GK(
/0
s5   !H(H?%H9HH=(H")%H"2	H'?H'r8   N)r:   r;   r<   r=   r>   rv   r   r@   r8   r   r   ry   ry      s    J;r   ry   c                       \ rS rSrSrS rSrg)UpdateCommandMixin   z.A mixin for functionality to update a Feature.c                 <    U R                   R                  U R                  5       X5      nSR                  U R                  R                  5      nU R                  U R                   R                  X4SS9$ ! [        R                   a    U R                  5       ef = f)z>Update provides common API, display, and error handling logic.z$Waiting for Feature {} to be updatedFrN   warnings)r2   UpdateFeaturer   r4   r5   r,   r)   r   r*   r]   r^   )r   maskpatchrM   rc   s        r   UpdateUpdateCommandMixin.Update   s    *>>''(@(@(BDPb 1
7
7!!#C%%r    00 *''))*s   *A6 6%Br8   N)r:   r;   r<   r=   r>   r   r@   r8   r   r   r   r      s
    6r   r   c                       \ rS rSrSrSrg)UpdateCommand   zBase class for the command that updates a Feature.

Because Features updates are often bespoke actions, there is no default
`Run` override like some of the other classes.
r8   N)r:   r;   r<   r=   r>   r@   r8   r   r   r   r      s    r   r   c                       \ rS rSrSr\R                  " SSSS9r\R                  " SSSS9rS	r	\
S
 5       rS rS rS rSrg)DisableCommand   zLBase class for the command that disables an entire or parts of a Feature.
  z--force
store_truezForce disablement. Bypasses any prompts for confirmation. When disabling the entire feature, proceeds even if the feature is in use. Might result in unexpected behavior.)actionhelpz--fleet-default-member-configzDisable the [fleet-default membership configuration](https://cloud.google.com/kubernetes-engine/fleet-management/docs/manage-features). Does not change existing membership configurations. Does nothing if the feature is disabled.Fc                     U R                   R                  U5        U R                  (       a  U R                  R                  U5        g g r9   )
FORCE_FLAGAddToParsersupport_fleet_default FLEET_DEFAULT_MEMBER_CONFIG_FLAG)clsparsers     r   ArgsDisableCommand.Args  s6    NNv&
  	**66v> !r   c                     U R                   (       a"  UR                  (       a  U R                  5         g U R                  UR                  5        g r9   )r   fleet_default_member_configclear_fleet_defaultDisableforce)r   rI   s     r   rv   DisableCommand.Run  s/    !!d&F&F
 
ll4::r   c                     U R                   R                  U R                  5       US9nSR                  U R                  R                  5      nU R                  U R                   R                  X#SS9  g ! [        R                   a     g f = f)N)r   z$Waiting for Feature {} to be deletedFr   )
r2   DeleteFeaturer   r4   r5   r)   r   r*   r]   resourceless_waiter)r   r   rM   rN   s       r   r   DisableCommand.Disable  s    >>''(@(@(B%'Pb 5;;!!#G**B%  Q	 00 s   (A5 5BBc                    S/nU R                   R                  SS9n U R                  X5      $ ! [        R                   aH  n[
        R                  " U5      [
        R                  " U R                  5       5      :w  a  Ue S nAg S nAff = f)Nr   placeholder)name)rr   rs   r   r   r(   six	text_typer,   )r   r   r   ra   s       r   r   "DisableCommand.clear_fleet_default  s{    )*D MM!!}!5E[[%% 	q	S]]4+F+F+HI	I 
Js   / B>BBr8   N)r:   r;   r<   r=   r>   calliope_baseArgumentr   r   r   classmethodr   rv   r   r   r@   r8   r   r   r   r      sn     %%2	
* &3%;%;% 6&"  ? ?
Qr   r   c                    U R                  S5      (       d,  U R                  S5      (       d  U R                  U5      (       aN  [        R                  " U U5      (       d  U(       d  [        R                  " X5      $ [        R                  " XSS9$ U(       d  U(       d  [        X5      e[        R                  " SS9u  pVU(       a%  [        R                  " SR                  U5      5      eU(       aE  [        U5      S:X  a6  [        R                  R                  SR                  US   5      5        US   $ U(       a  [        R                  " U5      nUb  U$ [        X5      e)	a
  Returns a membership on which to run the command, given the arguments.

Allows for a `--membership` flag or a `MEMBERSHIP_NAME` positional flag.

Args:
  args: object containing arguments passed as flags with the command
  prompt: whether to prompt in console for a membership when none are provided
    in args
  autoselect: if no membership is provided and only one exists,
    automatically use that one
  search: whether to search for the membership and error if it does not exist
    (not recommended)
  flag_override: to use a custom membership flag name

Returns:
  membership: A membership resource name string

Raises:
  exceptions.Error: no memberships were found or memberships are invalid
  calliope_exceptions.RequiredArgumentException: membership was not provided

membershipMEMBERSHIP_NAMETfilter_cluster_missingLocations {} are currently unreachable. Please specify memberships using `--location` or the full resource name (projects/*/locations/*/memberships/*)   Selecting membership [{}].r   )IsKnownAndSpecifiedr   MembershipLocationSpecifiedMembershipResourceNameSearchMembershipResourceMembershipRequiredErrorr   ListMembershipsFullr   r(   r)   lenr   r[   r\   PromptForMembership)rI   prompt
autoselectsearchflag_overrideall_membershipsunreachabler   s           r   ParseMembershipr   '  s7   8 
l++t/G/G0 0 44]CC,,T-:< <CI--dBB//
d< < 


!$
66!)!=!=!"#/



2396+3FH H C(A-JJ1889KLM1..?J44r   c                    / n[        U S5      (       ar  U R                  (       aa  [        R                  " SS9u  pxU(       a%  [        R
                  " SR                  U5      5      eU(       d  [        R
                  " S5      eU$ U R                  S5      (       a  [        R                  " U 5      (       a  U[        R                  " U 5      -  nU(       aI  U HC  n	[        R                  " U	5      (       a  M   [        R
                  " SR                  U	5      5      e   U(       dB  [        [        R                  " U5      5      S:  a  [        [        R                  " U5      5      eOU[        R                  " U SS9-  nU(       a  U$ U(       d  U(       d  [!        U 5      e[        R                  " SS9u  pxU(       a%  [        R
                  " S	R                  U5      5      eU(       aF  [        U5      S:X  a7  ["        R$                  R'                  S
R                  US   5      5        US   /$ U(       a/  [        R(                  " SUS9n	U	(       a  UR+                  U	5        U$ [!        U 5      e)a6  Parses a list of membership resources from args.

Allows for a `--memberships` flag and a `--all-memberships` flag.

Args:
  args: object containing arguments passed as flags with the command
  prompt: whether to prompt in console for a membership when none are provided
    in args
  prompt_cancel: whether to include a 'cancel' option in the prompt
  autoselect: if no memberships are provided and only one exists,
    automatically use that one
  allow_cross_project: whether to allow memberships from different projects
  search: whether to check that the membership exists in the fleet

Returns:
  memberships: A list of membership resource name strings

Raises:
  exceptions.Error if no memberships were found or memberships are invalid
  calliope_exceptions.RequiredArgumentException if membership was not provided
r   Tr   zaLocations {} are currently unreachable. Please try again or specify memberships for this command.z&No Memberships available in the fleet.r   z*Membership {} does not exist in the fleet.r   r   r   r   z--memberships)flagcancel)hasattrr   r   r   r   r(   r)   r   r   r   PluralMembershipsResourceNamesGetMembershipr   GetMembershipProjectsCrossProjectErrorSearchMembershipResourcesPluralr   r   r[   r\   r   append)
rI   r   prompt_cancelr   allow_cross_projectr   r   r   r   r   s
             r   ParseMembershipsPluralr   a  s%   6 + T$%%$*>*>#+#?#?#$% O228&2EG G EFF 
m,,,,T22Y==dCCk	%J''
33""<CCJOQ Q &
 !S

)
)+
6&8:;&<	 ? ? LMM Y>>
t- -k  


!$
''!)!=!=!"#/



2396+3FH H C(A-JJ1889KLMA..J $%%r   c                     [         R                  " SS5      n U R                  R                  U R                  R                  [        R                  R                  5       S95      nUR                   Vs/ s H?  n[        UR                  5      (       a  M  [        R                  " UR                  5      PMA     sn$ s  snf )zwLists Membership IDs in the fleet for the current project.

Returns:
  A list of Membership resource IDs in the fleet.
gkehubv1beta)r_   )	core_apisGetClientInstanceprojects_locations_membershipsListMESSAGES_MODULE-GkehubProjectsLocationsMembershipsListRequesthub_base
HubCommandrQ   r   _ClusterMissingendpointr   MembershipShortnamer   )clientresponser   s      r   ListMembershipsr     s     &&x:&2277JJ$$99; K =>( !!
!!QZZ( 'dqvv&!
  
s   4B=$B=c                 L    [         R                  " SR                  U 5      5      $ )NzoMemberships for this command must belong to the same project and cannot mix project number and project ID ({}).)r   r(   r)   )projectss    r   r   r     s$    			 --3VH-=
? ?r   c                 z    U(       a  UnOU R                  S5      (       a  SnOSn[        R                  " US5      $ )a  Parses a list of membership resources from args.

Assumes a `--memberships` flag or a `MEMBERSHIP_NAME` flag unless overridden.

Args:
  args: argparse.Namespace arguments provided for the command
  flag_override: set to override the name of the membership flag

Returns:
  memberships: A list of membership resource name strings

Raises:
  exceptions.Error: if no memberships were found or memberships are invalid
  calliope_exceptions.RequiredArgumentException: if membership was not
    provided
r   r   z5At least one membership is required for this command.)r   calliope_exceptionsRequiredArgumentException)rI   r   r   s      r   r   r     sC    " D 122DD		6	6
C
E Er   c                 f    S H+  n[        X5      (       d  M  [        [        X5      SS5      s  $    g )N)
gkeClustermultiCloudClusteronPremClusterclusterMissingF)r   getattr)r   ts     r   r   r     s*    ?aq}}WQ]$4e<< @r   )FFFr   )FTFFF)r   )0r>   
__future__r   r   r   apitools.base.pyr   r4   &googlecloudsdk.api_lib.container.fleetr   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr	   r   rX   googlecloudsdk.callioper
   r   r   *googlecloudsdk.command_lib.container.fleetr   r   r   3googlecloudsdk.command_lib.container.fleet.featuresr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   r   r   r   rC   CreateCommandro   ry   r   r   DeleteCommandr   r   r   r   r   r   r   r8   r   r   <module>r     s
   P &  ' > 7 6 9 I 9 E ? G @ D * # * * 
"-X(( "-J- -`0&(C(C 0Dnm&C&C DN "&(C(C A')D)D AJ !$ "$	75v #()-&+/4"'T&r$?E6=r   