
    %                         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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eeej2                  f   Z G d d      Zy)z;Provides a utility mixin for Poco-specific gcloud commands.    )absolute_import)division)unicode_literals)Dict)messages)util)parser_extensions)base)convert)
exceptionsNc            	           e Zd ZdZddZdej                  deeef   fdZ	de
fdZ	 	 ddej                  ded	ede
fd
Zdej                  dej                  fdZdde
ddfdZde
fdZdej                  de
fdZy)PocoCommandz5A mixin for Policy Controller specific functionality.returnNc                    dg}| j                   j                  d      }|!| j                   j                  |      |_        	 | j	                  ||      S # t
        j                  $ rX}| j                         }t        j                  |      t        j                  |      k(  r| j                  |      cY d}~S |d}~ww xY w)z!Update the feature configuration.fleet_default_member_confignotarealname)nameNpolicycontroller)r   Feature"CommonFleetDefaultMemberConfigSpecfleetDefaultMemberConfigUpdategcloud_exceptionsErrorFeatureNotEnabledErrorsix	text_typeEnable)selfdefault_cfgmaskfeatureefnes         Jlib/googlecloudsdk/command_lib/container/fleet/policycontroller/command.pyupdate_fleet_defaultz PocoCommand.update_fleet_default'   s    )*Dmm##
  $ G 
--
:
:* ;  &[[w''"" '')c	q	S]]3/	/{{7##s%   A C)AB<4C:B<<Cargsc                     t        j                  |ddd      D ci c]  }t        j                  |      | c}S c c}w )z@Filtered map of short membership names to full membership paths.TF)promptprompt_cancel
autoselect)feature_baseParseMembershipsPlural
fleet_utilMembershipPartialName)r    r(   paths      r&   _membership_pathszPocoCommand._membership_pathsA   sQ     !77Ut

D 	((.4
  s   <c                 8   	 | j                   j                  | j                         j                        S # t        j
                  $ rQ}| j                         }t        j                  |      t        j                  |      k(  rt               cY d}~S |d}~ww xY w)zFetches the current specs from the server.

    If the feature is not enabled, this will return an empty dictionary.

    Returns:
      dictionary mapping from full path to membership spec.
    N)
	hubclientToPyDict
GetFeaturemembershipSpecsr   r   r   r   r   dictr    r$   r%   s      r&   current_specszPocoCommand.current_specsL   ss    ^^$$T__%6%F%FGG"" '')c	q	S]]3/	/v"   25 BABBBBignore_missingignore_metadatac           
         | j                  |      }| j                         j                         D ci c]4  \  }}t        j                  |      |v rt        j                  |      ||f6 }}}|r9|j                         D ci c]  \  }\  }}||| j                  |      f }}}}|rS|j                         D 	
cg c]  \  }	}
|	|vs|	|
f }}	}
|D ]$  \  }}|| j                  j                         f||<   & nNd}|D cg c]*  }||vr$t        j                  |j                  |            , }}|rt        j                  |      |j                         D ci c]  \  }}||
 c}}S c c}}w c c}}}w c c}
}	w c c}w c c}}w )a  Retrieves memberships specified by the command that exist in the Feature.

    Args:
      args: The argparse object passed to the command.
      ignore_missing: Use this to return a mapping that includes an 'empty' spec
        for each specified path if it doesn't already exist.
      ignore_metadata: If true, remove the Hub-managed metadata (i.e. origin).
        If the spec is being retrieved for reporting (i.e. describe) then set to
        false to get the full current value. If it is being used to update the
        spec leave as True so that the return spec can be used in a patch.

    Returns:
      A dict mapping a path to the membership spec.

    Raises:
      exceptions.DisabledMembershipError: If the membership is invalid or not
      enabled.
    2Policy Controller is not enabled for membership {})r2   r:   itemsr/   r0   _rebuild_specr   MembershipFeatureSpecr   InvalidPocoMembershipErrorformatvalues)r    r(   r<   r=   memberships_pathsr1   specspecspartial_pathsfmissingshortfullmsgmissing_membershipss                   r&   
path_specszPocoCommand.path_specs]   s   2 ..t4 ,,.4466JD$++D15FF 	((.t<6 
   -2KKM,9(lLT4 t11$78
8,9   $5$;$;$=P$=DAq%!Q$=gP +%dmmAACDe ! Ac ('d 
/
/

40@
A'  
 
334GHH ,1<<>:><D$D$J>::9 Q
 ;s#   9E1!E7?E>E>/F F	rG   c                 N    | j                   j                  |j                        S )au  Rebuilds the spec to only include information from policycontroller.

    This is necessary so that feature-level values managed by Hub are not
    unintentionally overwritten (i.e. 'origin').

    Args:
      spec: The spec found by querying the API.

    Returns:
      MembershipFeatureSpec with only policycontroller values, leaving off
      other top-level data.
    r   )r   rB   r   )r    rG   s     r&   rA   zPocoCommand._rebuild_spec   s)     ==.... /      rH   c                 .   | j                   j                  | j                  j                  |            }|sq	 | j	                          |j                  j                  D ]F  }|j                  }|j                  }t        j                   | |d|      }	| j#                  |dg|	       H y	 | j%                  dg|      S # t
        j                  $ rV}| j                         }t        j                  |      t        j                  |      k(  r| j                  |       Y d}~d}~ww xY w# t
        j                  $ rX}| j                         }t        j                  |      t        j                  |      k(  r| j                  |      cY d}~S |d}~ww xY w)aB  Merges spec changes and sends and update to the API.

    Specs refer to PolicyControllerMembershipSpec objects defined here:
    third_party/py/googlecloudsdk/generated_clients/apis/gkehub/v1alpha/gkehub_v1alpha_messages.py

    (Note the above is for the ALPHA api track. Other tracks are found
    elsewhere.)

    Args:
      specs: Specs with updates. These are merged with the existing spec (new
        values overriding) and the merged result is sent to the Update api.
      use_default_cfg: If true, use the default config for the update.

    Returns:
      None
    )r7   Nr   rG   membership_specs)r   r   r4   ToMembershipSpecsr6   r   r   r   r   r   r   r7   additionalPropertieskeyvaluer   ToV2MembershipFeatureUpdateV2r   )
r    rH   use_default_cfgr#   r$   r%   rG   membership_pathv1_specmembershipfeatures
             r&   update_specszPocoCommand.update_specs   sW   " mm##88? $ G  ))>>$((**#99/#5w
 	ox1BC ?{{./99 $$ ))+==s}}S11
++g
	  $$ ))+==s}}S11W%
%'s=   B= *D) =D&AD!!D&)F<AFFFFc                 8   	 | j                   j                  | j                         j                        S # t        j
                  $ rQ}| j                         }t        j                  |      t        j                  |      k(  rt               cY d}~S |d}~ww xY w)zFetches the current states from the server.

    If the feature is not enabled, this will return an empty dictionary.

    Returns:
      dictionary mapping from full path to membership spec.
    N)
r4   r5   r6   membershipStatesr   r   r   r   r   r8   r9   s      r&   current_stateszPocoCommand.current_states   ss    ^^$$T__%6%G%GHH"" '')c	q	S]]3/	/vr;   c                    | j                  |      }| j                         j                         D ci c]4  \  }}t        j                  |      |v rt        j                  |      ||f6 }}}d}|D cg c]*  }||vr$t        j                  |j                  |            , }}|rt        j                  |      |j                         D ci c]  \  }}||
 c}}S c c}}w c c}w c c}}w )a3  Retrieves membership states specified that exist in the Feature.

    Args:
      args: The argparse object passed to the command.

    Returns:
      A dict mapping a path to the membership spec.

    Raises:
      exceptions.DisabledMembershipError: If the membership is invalid or not
      enabled.
    r?   )	r2   rc   r@   r/   r0   r   rC   rD   rE   )	r    r(   rF   r1   statestatesrO   rP   rG   s	            r&   path_stateszPocoCommand.path_states   s    ..t4  ..06688KD%++D15FF 	((.u=8   ?C &%Dv 	--cjj.>?%  
 112EFF ,2==?;?<D$D$J?;;# <s   9C!5/C'C,)r   N)FT)F)__name__
__module____qualname____doc__r'   r	   	Namespacer   strr2   SpecMappingr:   boolrQ   r   MessagerA   r`   rc   rg    rS   r&   r   r   $   s    =4	#--	CH~	[ ( #"	9;''9; 9; 	9;
 9;v 0 0 X5E5E "- -t -^k ""</99 "<k "<rS   r   )rk   
__future__r   r   r   typingr   apitools.base.protorpcliter   &googlecloudsdk.api_lib.container.fleetr   r/   googlecloudsdk.callioper	   3googlecloudsdk.command_lib.container.fleet.featuresr
   r-   =googlecloudsdk.command_lib.container.fleet.membershipfeaturesr   ;googlecloudsdk.command_lib.container.fleet.policycontrollerr   googlecloudsdk.corer   r   rm   rp   rn   r   rq   rS   r&   <module>r{      sR    B &  '  / E 5 T Q R ? 
 3((()g< g<rS   