
    [                     j    S 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S
 jr
SS jrS rS rg	)z1Utilities for manipulating organization policies.    )absolute_import)division)unicode_literals)
exceptionsc                 @    [        U 5      nSR                  US   5      $ )a#  Returns the constraint from the specified policy name.

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

Args:
  policy_name: The name of the policy. A policy name has the following syntax:
    [organizations|folders|projects]/{resource_id}/policies/{constraint_name}.
zconstraints/{}   _GetPolicyNameTokensformatpolicy_namepolicy_name_tokenss     -lib/googlecloudsdk/api_lib/orgpolicy/utils.pyGetConstraintFromPolicyNamer      s&     ,K8		 	 !3A!6	77    c                 H    [        U 5      nSR                  US   US   5      $ )a0  Returns the resource from the specified policy name.

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

Args:
  policy_name: The name of the policy. A policy name has the following syntax:
    [organizations|folders|projects]/{resource_id}/policies/{constraint_name}.
z{}/{}r      r	   r   s     r   GetResourceFromPolicyNamer   %   s,     ,K8	*1-/A!/D	EEr   c                 P    [        U 5      nSR                  US   US   US   5      $ )at  Returns the associated policy name for the specified constraint name.

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

Args:
  constraint_name: The name of the constraint. A constraint name has the
    following syntax:
      [organizations|folders|projects]/{resource_id}/constraints/{constraint_name}.
z{}/{}/policies/{}r   r   r   )_GetConstraintNameTokensr   constraint_nameconstraint_name_tokenss     r   GetPolicyNameFromConstraintNamer   3   s:     4OD		#	#$:1$=$:1$=$:1$=
? ?r   Nc                 n   ^ Tc  S nOU4S jn[        [        X R                  R                  5      5      $ )aU  Returns a list of rules on the policy that contain the specified condition expression.

In the case that condition_expression is None, rules without conditions are
returned.

Args:
  policy: messages.GoogleCloudOrgpolicy{api_version}Policy, The policy object
    to search.
  condition_expression: str, The condition expression to look for.
c                     U R                   S L $ N	conditionrules    r   <lambda>,GetMatchingRulesFromPolicy.<locals>.<lambda>P   s    DNNd$:r   c                 `   > U R                   S L=(       a    U R                   R                  T:H  $ r   r   
expressionr!   condition_expressions    r   r"   r#   R   s&    DNN$$>$t4>>C\C\`tCt$tr   listfilterspecrulespolicyr(   condition_filters    ` r   GetMatchingRulesFromPolicyr1   D   s2     !:t	f%{{'8'89	::r   c                 n   ^ Tc  S nOU4S jn[        [        X R                  R                  5      5      $ )aY  Returns a list of rules on the policy that do not contain the specified condition expression.

In the case that condition_expression is None, rules with conditions are
returned.

Args:
  policy: messages.GoogleCloudOrgpolicy{api_version}Policy, The policy object
    to search.
  condition_expression: str, The condition expression to look for.
c                     U R                   S L$ r   r   r    s    r   r"   /GetNonMatchingRulesFromPolicy.<locals>.<lambda>c   s    DNN$$>r   c                 `   > U R                   S L =(       d    U R                   R                  T:g  $ r   r%   r'   s    r   r"   r4   e   s&    DNNd$:$odnn>W>W[o>o$or   r)   r.   s    ` r   GetNonMatchingRulesFromPolicyr6   W   s2     !>o	f%{{'8'89	::r   c                     U R                  S5      n[        U5      S:w  a%  [        R                  " SR	                  U 5      5      eU$ )zReturns the individual tokens from the policy name.

Args:
  policy_name: The name of the policy. A policy name has the following syntax:
    [organizations|folders|projects]/{resource_id}/policies/{constraint_name}.
/   zInvalid policy name '{}': Name must be in the form [projects|folders|organizations]/{{resource_id}}/policies/{{constraint_name}}.splitlenr   InvalidInputErrorr   r   s     r   r
   r
   j   sN     #((-	!

&
& 	L		  
r   c                     U R                  S5      n[        U5      S:w  a%  [        R                  " SR	                  U 5      5      eU$ )zReturns the individual tokens from the constraint name.

Args:
  constraint_name: The name of the constraint. A constraint name has the
    following syntax:
      [organizations|folders|projects]/{resource_id}/constraints/{constraint_name}.
r8   r9   zInvalid constraint name '{}': Name must be in the form [projects|folders|organizations]/{{resource_id}}/constraints/{{constraint_name}}.r:   r   s     r   r   r   y   sN     +005	 A%

&
& 	S		 " " 
 r   r   )__doc__
__future__r   r   r   'googlecloudsdk.command_lib.org_policiesr   r   r   r   r1   r6   r
   r    r   r   <module>rC      s:    8 &  ' >
8F?";&;& r   