
    L,                         S r SSKJr  SSKJr  SSKJr  SSKrSSK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rSrSrS rS rS rS rS rS rS rS rS rS rS rS r S r!SS jr"g)zOrg Policy command utilities.    )absolute_import)division)unicode_literalsN)encoding)service)
exceptions)yaml)fileszconstraints/zcustomConstraints/zpolicy.c                     U R                   R                  [        5      (       a  U R                   $ [        U R                   -   $ )a\  Returns the constraint from the user-specified arguments.

A constraint has the following syntax: constraints/{constraint_name}.

This handles both cases in which the user specifies and does not specify the
constraint prefix.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
)
constraint
startswithCONSTRAINT_PREFIXargss    4lib/googlecloudsdk/command_lib/org_policies/utils.pyGetConstraintFromArgsr   #   s3     
__ 122??	T__	,,    c                     U R                   R                  [        5      (       a  U R                   [        [        5      S $ U R                   $ )a  Returns the constraint name from the user-specified arguments.

This handles both cases in which the user specifies and does not specify the
constraint prefix.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
N)r   r   r   lenr   s    r   GetConstraintNameFromArgsr   5   s<     
__ 122??301233	r   c                     U R                   R                  [        5      (       a  U R                   [        [        5      S $ U R                   $ )a(  Returns the custom constraint name from the user-specified arguments.

This handles both cases in which the user specifies and does not specify the
custom constraint prefix.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
N)custom_constraintr   CUSTOM_CONSTRAINT_PREFIXr   r   s    r   GetCustomConstraintNameFromArgsr   E   sB     
&&'?@@!!#&>"?"@AA			r   c                     U R                   =(       d    U R                  =(       d    U R                  nU R                   (       a  SnOU R                  (       a  SnOSnSR                  X!5      $ )a  Returns the resource from the user-specified arguments.

A resource has the following syntax:
[organizations|folders|projects]/{resource_id}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
organizationsfoldersprojectsz{}/{})organizationfolderprojectformat)r   resource_idresource_types      r   GetResourceFromArgsr%   U   sO     !!@T[[@DLL+	#M{{MM		33r   c                 P    [        U 5      n[        U 5      nSR                  X5      $ )a)  Returns the policy name from the user-specified arguments.

A policy name has the following syntax:
[organizations|folders|projects]/{resource_id}/policies/{constraint_name}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
z{}/policies/{})r%   r   r"   )r   resourceconstraint_names      r   GetPolicyNameFromArgsr)   k   s)     !&(-d3/		 	 	;;r   c                 T    U R                   n[        U 5      nSR                  UU5      $ )a-  Returns the CustomConstraint from the user-specified arguments.

A CustomConstraint has the following syntax:
organizations/{organization_id}/customConstraints/{constraint_name}.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
z%organizations/{}/customConstraints/{})r   r   r"   )r   organization_idr(   s      r   GetCustomConstraintFromArgsr,   {   s3     %%/3D9/	0	7	78G
I Ir   c                 2   U R                   c  U R                   $ U R                   R                  [        5      (       a  U R                   $ U R                   S:X  d   U R                   S:X  d  U R                   S:X  a  [        U R                   -   $ U R                   $ )aD  Returns the update-mask from the user-specified arguments.

This handles both cases in which the user specifies and does not specify the
policy prefix for partial update of spec or dry_run_spec fields.

Args:
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
specdry_run_spec
dryRunSpec)update_maskr   UPDATE_MASK_POLICY_PREFIXr   s    r   GetUpdateMaskFromArgsr3      s     
""#<==F"d&6&6.&HL($t'7'777			r   c                 Z    [         R                  " U 5      R                  5       nSU-   S-   $ )zHReturns the organization policy message name based on the release_track.GoogleCloudOrgpolicyPolicy)org_policy_serviceGetApiVersion
capitalize)release_trackapi_versions     r   _GetPolicyMessageNamer<      s+    "00?JJL+	+	-	88r   c                    [         R                  " U 5      n [        R                  " U5      n[        R
                  " U5      n[        R                  " U5      n[        U[        U5      5      n [        R                  " Xd5      $ ! [        R                   a    Un NZf = f! [         a*  n[        R                  " SR!                  X5      5      eSnAff = f)Returns a message populated from the JSON or YAML file on the specified filepath.

Args:
  filepath: str, A local path to an object specification in JSON or YAML
    format.
  release_track: calliope.base.ReleaseTrack, Release track of the command.
Unable to parse file [{}]: {}.N)r
   ReadFileContentsr	   loadjsondumpsYAMLParseErrorr7   OrgPolicyMessagesgetattrr<   r   JsonToMessage	Exceptionr   InvalidInputErrorr"   filepathr:   file_contentsyaml_objjson_strorg_policy_messagesmessagees           r   GetMessageFromFilerR      s     ((2-yy'Hzz(#H +<<]K')>})MN'!!'44 
		 H 
 

&
&'G'N'N(  s)   ,B 0B" BB"
C,%CCc                    [         R                  " U 5      n [        R                  " U5      n[        R
                  " U5      n[        R                  " U5      n[        US5      n [        R                  " Xd5      $ ! [        R                   a    Un NQf = f! [         a*  n[        R                  " SR                  X5      5      eSnAff = f)r>   &GoogleCloudOrgpolicyV2CustomConstraintr?   N)r
   r@   r	   rA   rB   rC   rD   r7   rE   rF   r   rG   rH   r   rI   r"   rJ   s           r   "GetCustomConstraintMessageFromFilerU      s     ((2-yy'Hzz(#H +<<]K'<>'!!'44 
		 H 
 

&
&'G'N'N(  s)   ,A= 'B =BB
C#%CCc                    [         R                  " U 5      nUR                  R                  (       d  U $ [	        UR
                  5      nUR                  R                   HO  nUR                  c  M  UR                  R                   Vs/ s H  nXd;  d  M
  UPM     snUR                  l        MQ     [        X25      $ s  snf )ai  Removes the specified allowed values from all policy rules containing the specified condition.

It searches for and removes the specified values from the
lists of allowed values on those rules. Any modified rule with empty lists
of allowed values and denied values after this operation is deleted.

Args:
  policy: messages.GoogleCloudOrgpolicy{api_version}Policy, The policy to be
    updated.
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
  release_track: calliope.base.ReleaseTrack, Release track of the command.

Returns:
  The updated policy.
)	copydeepcopyr.   rulessetvaluevaluesallowedValues_DeleteRulesWithEmptyValuespolicyr   r:   
new_policyspecified_valuesrule_to_updater[   s          r   RemoveAllowedValuesFromPolicyrd      s    " }}V$*			M _"--n(+22@@-@E* @-n) . 
%Z	??-   		B=B=c                    [         R                  " U 5      nUR                  R                  (       d  U $ [	        UR
                  5      nUR                  R                   HO  nUR                  c  M  UR                  R                   Vs/ s H  nXd;  d  M
  UPM     snUR                  l        MQ     [        X25      $ s  snf )aD  Removes the specified denied values from all policy rules.

It searches for and removes the specified values from the
lists of denied values on those rules. Any modified rule with empty lists
of allowed values and denied values after this operation is deleted.

Args:
  policy: messages.GoogleCloudOrgpolicy{api_version}Policy, The policy to be
    updated.
  args: argparse.Namespace, An object that contains the values for the
    arguments specified in the Args method.
  release_track: calliope.base.ReleaseTrack, Release track of the command.

Returns:
  The updated policy.
)	rW   rX   r.   rY   rZ   r[   r\   deniedValuesr^   r_   s          r   RemoveDeniedValuesFromPolicyrh      s    " }}V$*			M _"--n(+22??,?E* ?,n( . 
%Z	??,re   c                     [         R                  " U 5      n[        R                  " U5      nUR	                  5       nUR                  US9nUR                  R                   Vs/ s H  ofU:w  d  M
  UPM     snUR                  l        U$ s  snf )a)  Delete any rule with empty lists of allowed values and denied values and no other field set.

Args:
  policy: messages.GoogleCloudOrgpolicy{api_version}Policy, The policy to be
    updated.
  release_track: calliope.base.ReleaseTrack, Release track of the command.

Returns:
  The updated policy.
)r\   )rW   rX   r7   OrgPolicyApi%BuildPolicySpecPolicyRuleStringValuesBuildPolicySpecPolicyRuler.   rY   )r`   r:   ra   org_policy_apir\   matching_empty_rulerules          r   r^   r^     s     }}V$*%22=A.??A&&@@@O ",,,t8K0Kd,*// 
	s   $	B1Bc                    [         R                  " U5      n[        R                  " U 5      nSnUb  UR                  R                  US9nUR                  US9nUR                  R                  R                  U5        Xd4$ )a  Creates a rule on the policy that contains the specified condition expression.

In the case that condition_expression is None, a rule without a condition is
created.

Args:
  policy: messages.GoogleCloudOrgpolicy{api_version}Policy, The policy object
    to be updated.
  release_track: release track of the command
  condition_expression: str, The condition expression to create a new rule
    with.

Returns:
  The rule that was created as well as the new policy that includes this
  rule.
N)
expression)	condition)
r7   rj   rW   rX   messagesGoogleTypeExprrl   r.   rY   append)r`   r:   condition_expressionrm   ra   rr   new_rules          r   CreateRuleOnPolicyrx   3  s    " &22=A.}}V$*)%''66' 7 )I 55	5J(//x(		r   )N)#__doc__
__future__r   r   r   rW   rB   apitools.base.pyr    googlecloudsdk.api_lib.orgpolicyr   r7   'googlecloudsdk.command_lib.org_policiesr   googlecloudsdk.corer	   googlecloudsdk.core.utilr
   r   r   r2   r   r   r   r%   r)   r,   r3   r<   rR   rU   rd   rh   r^   rx    r   r   <module>r      s    $ &  '   % J > $ *" / % -$   4,< I"(924@B@B0r   