
    A                         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mZ ddlmZ ddlmZ dZd ZddefdZd Zd Z G d d      Z G d d      Zy)z5Functions to add standardized flags in PoCo commands.    )absolute_import)division)unicode_literals)path)messages)protos)base)parser_arguments)parser_extensions)	resources)	constants)
exceptions)util)
console_iozpolicy-essentials-v2022c                  :    t        j                  dt        d      S )Nz--fleet-default-member-configa  The path to a policy-controller.yaml configuration
        file. If specified, this configuration will become the default Policy
        Controller configuration for all memberships in your fleet. It can be
        overridden with a membership-specific configuration by using the
        the `Update` command.

        To enable the Policy Controller Feature with a fleet-level default
        membership configuration, run:

          $ {command} --fleet-default-member-config=/path/to/policy-controller.yaml
      typehelpr	   Argumentstr     Hlib/googlecloudsdk/command_lib/container/fleet/policycontroller/flags.pyfleet_default_cfg_flagr   %   s    	%


 r   
include_noc                 |    dj                  | rdnd      }t        j                  |ddj                  d|            S )z/Flag for unsetting fleet default configuration.z--{}fleet-default-member-configzno- 
store_truezRemoves the fleet default configuration for policy controller.
      Memberships configured with the fleet default will maintain their current
      configuration.

          $ {} {}
      z	{command}actionr   )formatr	   r   )r   flags     r   no_fleet_default_cfg_flagr$   7   s@    	*	1	1:%2	N$	


 &d
#	
 	r   c                      t        j                  dd      } | j                  t                      | j                  t	        d             | S )z<Flag group for accepting a Fleet Default Configuration file.z4Flags for setting Fleet Default Configuration files.Tmutex)r	   ArgumentGroupAddArgumentr   r$   )config_groups    r   fleet_default_cfg_groupr+   F   sE    ##<D, 1344T:;	r   c                  >    t        j                  ddgt        d      S )z;Builds flag for setting configuration to the fleet default.z--originFLEETzaIf --origin=FLEET will set the configuration of the membership to
      the fleet default.
      )choicesr   r   r   r   r   r   origin_flagr/   P   s#    	i
	
 r   c                       e Zd ZdZdej
                  defdZed        Z	ed        Z
d Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)	PocoFlagszHandle common flags for Poco Commands.

  Use this class to keep command flags that touch similar configuration options
  on the Policy Controller feature in sync across commands.
  parsercommandc                      || _         || _        y)zConstructor.

    Args:
      parser: The argparse parser to add flags to.
      command: The command using this flag utility. i.e. 'enable'.
    N)_parser_display_name)selfr2   r3   s      r   __init__zPocoFlags.__init__c   s     DL Dr   c                     | j                   S N)r5   r7   s    r   r2   zPocoFlags.parserq   s    <<r   c                     | j                   S r:   )r6   r;   s    r   display_namezPocoFlags.display_nameu   s    r   c                 H    | j                   j                  dt        d       y)z7Adds handling for audit interval configuration changes.z--audit-intervalz=How often Policy Controller will audit resources, in seconds.r   Nr2   add_argumentintr;   s    r   add_audit_intervalzPocoFlags.add_audit_intervaly   s"    KKL  r   c                 H    | j                   j                  dt        d       y)zCAdds handling for constraint violation limit configuration changes.z--constraint-violation-limitzSThe number of violations stored on the constraint resource. Must be greater than 0.r   Nr?   r;   s    r   add_constraint_violation_limitz(PocoFlags.add_constraint_violation_limit   s$    KK&"	  r   c                     | j                   j                  dd      }|j                  dt        d       |j                  ddd	
       y)z4Adds handling for configuring exemptable namespaces.zExemptable Namespace flags.Tr&   z--exemptable-namespacesz^Namespaces that Policy Controller should ignore, separated by commas if multiple are supplied.r   z--clear-exemptable-namespacesr   zRemoves any namespace exemptions, enabling Policy Controller on all namespaces. Setting this flag will overwrite currently exempted namespaces, not append.r    N)r2   add_argument_groupr@   r   r7   groups     r   add_exemptable_namespacesz#PocoFlags.add_exemptable_namespaces   s`    KK**%T + E 
!0	   
':	  r   c                     | j                   j                  dd      }|j                  ddd       |j                  ddd	       y
)z(Adds handling for log denies enablement.zLog Denies flags.Tr&   z--no-log-deniesr   zIf set, disable all log denies.r    z--log-denieszNIf set, log all denies and dry run failures. (To disable, use --no-log-denies)Nr2   	add_groupr@   rG   s     r   add_log_denies_enabledz PocoFlags.add_log_denies_enabled   sX    KK!!"5T!BE	.  
 
 	  r   c                     | j                   j                  dd      }t        j                  |dd       |j	                  dddd	
       y)z6Adds handling for single, multiple or all memberships.zMembership flags.Tr&   zThe membership names to act on, separated by commas if multiple are supplied. Ignored if --all-memberships is supplied; if neither is supplied, a prompt will appear with all available memberships.)pluralmembership_helpz--all-membershipsr   zFIf supplied, apply to all Policy Controllers memberships in the fleet.F)r!   r   defaultN)r2   rF   r   AddMembershipResourceArgr@   rG   s     r   add_membershipszPocoFlags.add_memberships   sZ    KK**+>d*KE&&		 
   r   c           	          | j                   j                  dd      }|j                  dt        dj	                  dj                  t        j                                     |j                  dd	d
       y)z3Adds handling for monitoring configuration changes.zMonitoring flags.Tr&   z--monitoringzMonitoring backend options Policy Controller should export metrics to, separated by commas if multiple are supplied.  Setting this flag will overwrite currently enabled backends, not append. Options: {}z, r   z--no-monitoringr   zOInclude this flag to disable the monitoring configuration of Policy Controller.r    N)r2   rF   r@   r   r"   joinr   MONITORING_BACKENDSrG   s     r   add_monitoringzPocoFlags.add_monitoring   su    KK**+>d*KE	 "6$))I,I,I"JK  	 
"	  r   c                     | j                   j                  dd      }|j                  ddd       |j                  ddd	       y
)z&Adds handling for mutation enablement.zMutation flags.Tr&   z--no-mutationr   zDisables mutation support.r    z
--mutationzDIf set, enable support for mutation. (To disable, use --no-mutation)NrK   rG   s     r   add_mutationzPocoFlags.add_mutation   sW    KK!!"34!@E	3O   
	  r   c                 @    | j                   j                  ddd       y)zkAdds handling for no content enablement.

    This prevents the template library from being installed.
    z--no-contentr   zaIf set, Policy content, including the template library and policy bundles, will not be installed.r    Nr2   r@   r;   s    r   add_no_contentzPocoFlags.add_no_content   s&    
 	KK/	  r   c                 @    | j                   j                  ddd       y )Nz--no-default-bundlesr   z@If set, skip installing the default bundle of policy-essentials.r    r[   r;   s    r   add_no_default_bundlesz PocoFlags.add_no_default_bundles   s"    KKO  r   c                     | j                   j                  dd      }|j                  ddd       |j                  ddd	       y
)z/Adds handling for referential rules enablement.zReferential Rules flags.Tr&   z--no-referential-rulesr   z#Disables referential rules support.r    z--referential-rulesz\If set, enable support for referential constraints. (To disable, use --no-referential-rules)NrK   rG   s     r   add_referential_ruleszPocoFlags.add_referential_rules  sX    KK!!"<D!IE	 2  
 
+	  r   c                 H    | j                   j                  dt        d       y)zAdds handling for version flag.z	--versionzHThe version of Policy Controller to install; defaults to latest version.r   N)r2   r@   r   r;   s    r   add_versionzPocoFlags.add_version  s$    KK	  r   N)__name__
__module____qualname____doc__r
   ArgumentInterceptorr   r8   propertyr2   r=   rB   rD   rI   rM   rS   rW   rY   r\   r^   r`   rb   r   r   r   r1   r1   \   s    !22! !    	."0,"	r   r1   c                   J   e Zd ZdZdej
                  fdZdej                  dej                  fdZ	dej                  dej                  fdZ
dej                  dej                  fdZdej                  dej                  fd	Zdej                  dej                  fd
Zdej                  dej                  fdZdej                  dej                  fdZedej                  fd       Zedej                  fd       Zdej                  fdZdej                  dej                  fdZedej                  fd       Zdej                  dej                  fdZdefdZdej                  fdZedej                  fd       Zedej                  fd       Zdej                  dej                  fdZdej                  dej                  fdZdefdZdej                  dej                  dej                  fdZdej                  dej                  fdZ y )!PocoFlagParserzConverts PocoFlag arguments to internal representations.

  hub_cfg references the PolicyControllerHubConfig object in:
  third_party/py/googlecloudsdk/generated_clients/apis/gkehub/v1alpha/gkehub_v1alpha_messages.py
  argsc                      || _         || _        y r:   )rk   r   )r7   rk   msgss      r   r8   zPocoFlagParser.__init__*  s    DIDMr   hub_cfgreturnc                 h    | j                   j                  r| j                   j                  |_        |S r:   )rk   audit_intervalauditIntervalSecondsr7   rn   s     r   update_audit_intervalz$PocoFlagParser.update_audit_interval.  s(     yy%)YY%=%=g"Nr   c                 h    | j                   j                  r| j                   j                  |_        |S r:   )rk   constraint_violation_limitconstraintViolationLimitrs   s     r   !update_constraint_violation_limitz0PocoFlagParser.update_constraint_violation_limit5  s(     yy++)-)M)Mg&Nr   c                     | j                   j                  r	g }||_        | j                   j                  r,| j                   j                  j	                  d      }||_        |S )N,)rk   clear_exemptable_namespacesexemptableNamespacesexemptable_namespacessplit)r7   rn   
namespacess      r   update_exemptable_namespacesz+PocoFlagParser.update_exemptable_namespaces<  sR     yy,,j%/g"yy&&992288=j%/g"Nr   c                 z    | j                   j                  rd|_        | j                   j                  rd|_        |S NTF)rk   
log_denieslogDeniesEnabledno_log_deniesrs   s     r   update_log_deniesz PocoFlagParser.update_log_deniesG  s1    yy!%gyy!&gNr   c                 z    | j                   j                  rd|_        | j                   j                  rd|_        |S r   )rk   mutationmutationEnabledno_mutationrs   s     r   update_mutationzPocoFlagParser.update_mutationN  s1    yy $gyy %gNr   c                    | j                   j                  rr| j                  j                         |_        | j                  j                  | j                  j
                  j                  j                        |j                  _        |S )N)installation)	rk   
no_contentr   !PolicyControllerPolicyContentSpecpolicyContent%PolicyControllerTemplateLibraryConfigInstallationValueValuesEnumNOT_INSTALLEDtemplateLibraryrs   s     r   update_no_contentz PocoFlagParser.update_no_contentU  sg    yy"mmMMOg.2mm.a.a}}JJfftt /b /g+ Nr   c                 z    | j                   j                  rd|_        | j                   j                  rd|_        |S r   )rk   referential_rulesreferentialRulesEnabledno_referential_rulesrs   s     r   update_referential_rulesz'PocoFlagParser.update_referential_rules]  s3     yy""(,g%yy%%(-g%Nr   c                 .    | j                   j                  S r:   )r    PolicyControllerMonitoringConfigr;   s    r   monitoring_backend_cfgz%PocoFlagParser.monitoring_backend_cfgf  s    ==999r   c                 .    | j                   j                  S r:   )r    BackendsValueListEntryValuesEnumr;   s    r   monitoring_backend_enumz&PocoFlagParser.monitoring_backend_enumj  s    &&GGGr   c                 (   t         j                  j                  |      }|'t        | j                  t         j                  |         s$t        j                  dj                  |            t        | j                  t         j                  |         S )NzNo such monitoring backend: {})	r   rV   gethasattrr   r   InvalidMonitoringBackendErrorr"   getattr)r7   backendinternal_names      r   _get_monitoring_enumz#PocoFlagParser._get_monitoring_enumn  s    1155g>MG$$%%g.% 44
*
1
1'
:  

&
&

'
'
0 r   c                 v   | j                   j                  r#| j                  j                  g       }||_        | j                   j                  rd| j                   j                  j                  d      D cg c]  }| j                  |       }}| j                  j                  |      }||_        |S c c}w )z2Sets or removes monitoring backends based on args.)backendsrz   )rk   no_monitoringr   r   
monitoringr~   r   )r7   rn   configr   r   s        r   update_monitoringz PocoFlagParser.update_monitoring}  s    yy}}==r=Jf!gyy --33C88g 
#
#G
,8   }}==x=Pf!gNs   7B6c                 B    | j                   j                  j                  S )zDReturns an reference to the BundlesValue class for this API channel.)r   r   BundlesValuer;   s    r   bundle_messagezPocoFlagParser.bundle_message  s     ==::GGGr   c                 d   | j                   j                  s| j                   j                  r|S | j                  |      }t	        j
                  |j                        }| j                  j                         |t        <   t	        j                  | j                         |      |_        ||_        |S )aT  Sets default bundles based on args.

    This function assumes that the hub config is being initialized for the first
    time.

    Args:
      hub_cfg: A 'PolicyControllerHubConfig' proto message.

    Returns:
      A modified hub_config, adding the default bundle; or unmodified if the
      --no-default-bundles flag is specified.
    )rk   r   no_default_bundles_get_policy_contentr   additional_properties_to_dictbundlesr   !PolicyControllerBundleInstallSpecDEFAULT_BUNDLE_NAMEset_additional_propertiesr   r   )r7   rn   policy_content_specr   s       r   update_default_bundlesz%PocoFlagParser.update_default_bundles  s     yytyy;;n227;22##G 	779   #)"B"Bw# 0GNr   c                 ^    | j                   j                  xs | j                   j                  S r:   )rk   fleet_default_member_configno_fleet_default_member_configr;   s    r   is_feature_updatez PocoFlagParser.is_feature_update  s&    		-- 	49933r   c                    | j                   j                  rjt        j                  | j                   j                        }t	        j
                  |d      }t        j                  | j                  j                  |      S y )NF)binary)
rk   r   r   
expanduserr   ReadFromFileOrStdinr   Importr   PolicyControllerMembershipSpec)r7   config_pathdatas      r   load_fleet_default_cfgz%PocoFlagParser.load_fleet_default_cfg  s^    yy,,OODII$I$IJk++
ed [[EEtLL -r   c                 .    | j                   j                  S r:   )r   r   r;   s    r   template_lib_cfgzPocoFlagParser.template_lib_cfg  s    ==>>>r   c                 .    | j                   j                  S r:   )r   r   r;   s    r   template_lib_enumz PocoFlagParser.template_lib_enum  s      <<<r   poco_cfgc                 f    |j                   | j                  j                         S |j                   S )z4Get or create new PolicyControllerPolicyContentSpec.)r   r   r   )r7   r   s     r   r   z"PocoFlagParser._get_policy_content  s-    %]]<<>>!!!r   pococ                 h    | j                   j                  r| j                   j                  |_        |S r:   )rk   version)r7   r   s     r   update_versionzPocoFlagParser.update_version  s%    yyYY&&dlKr   c                 d    | j                   j                  xr | j                   j                  dk(  S )Nr-   )rk   originr;   s    r   use_default_cfgzPocoFlagParser.use_default_cfg  s&    99;		 0 0G ;;r   feature
membershipc                     |j                   D|j                  j                  d      d   }d}t        j                  |j                  |            | j                  |       |j                   j                  |_        y)a.  Sets membership to the default fleet configuration.

    Args:
      feature: The feature spec for the project.
      membership: The membership spec being updated.

    Returns:
      Updated MembershipFeatureSpec.
    Raises:
      MissingFleetDefaultMemberConfig: If none exists on the feature.
    N/   ztNo fleet default member config specified for project {}. Use '... enable --fleet-default-member-config=config.yaml'.)fleetDefaultMemberConfignamer~   r   MissingFleetDefaultMemberConfigr"   set_origin_fleetpolicycontroller)r7   r   r   projectmsgs        r   set_default_cfgzPocoFlagParser.set_default_cfg  su     ''/""3'*gE 
 66szz'7JKK*%((99 r   c                     | j                   j                  | j                   j                  j                  j                        |_        y )N)r   )r   OriginTypeValueValuesEnumr-   r   )r7   r   s     r   r   zPocoFlagParser.set_origin_fleet  s7    ,,]]!!55;; - Jr   N)!rc   rd   re   rf   r   	Namespacer8   r   Messagert   rx   r   r   r   r   r   rh   r   r   r   r   r   r   boolr   r   r   r   r   r   r   r   r   r   r   r   rj   rj   #  s   ,66 %%%%	%%		x'7'7 H<L<L X%5%5 (:J:J x'7'7 H<L<L %% :h&6&6 : : Hx'7'7 H HX-=-= x'7'7 H<L<L   Hh.. H H%%> Mh&6&6 M ? 0 0 ? ? =!1!1 = ="(*:*: "x?O?O "!1!1 h6F6F 
<t <%%3;3C3C6)9)9 h>N>N r   rj   N)F) rf   
__future__r   r   r   osr   apitools.base.protorpcliter   7googlecloudsdk.api_lib.container.fleet.policycontrollerr   googlecloudsdk.callioper	   r
   r   *googlecloudsdk.command_lib.container.fleetr   ;googlecloudsdk.command_lib.container.fleet.policycontrollerr   r   !googlecloudsdk.command_lib.exportr   googlecloudsdk.core.consoler   r   r   r   r$   r+   r/   r1   rj   r   r   r   <module>r      sn    < &  '  / J ( 4 5 @ Q R 2 2/ $$ 	D DNP Pr   