
    [                     f    d 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 Z	dd
Z
ddZd Zd Zy	)z1Utilities for manipulating organization policies.    )absolute_import)division)unicode_literals)
exceptionsc                 @    t        |       }dj                  |d         S )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    t        |       }dj                  |d   |d         S )a<  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    t        |       }dj                  |d   |d   |d         S )a  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                 l    d }nfd}t        t        || j                  j                              S )ac  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                     | j                   d u S N	conditionrules    r   <lambda>z,GetMatchingRulesFromPolicy.<locals>.<lambda>P   s    DNNd$:r   c                 V    | j                   d uxr | j                   j                  k(  S r   r   
expressionr!   condition_expressions    r   r"   z,GetMatchingRulesFromPolicy.<locals>.<lambda>R   s&    DNN$$>$t4>>C\C\`tCt$tr   listfilterspecrulespolicyr'   condition_filters    ` r   GetMatchingRulesFromPolicyr0   D   s4     !:t	f%v{{'8'89	::r   c                 l    d }nfd}t        t        || j                  j                              S )ag  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                     | j                   d uS r   r   r    s    r   r"   z/GetNonMatchingRulesFromPolicy.<locals>.<lambda>c   s    DNN$$>r   c                 V    | j                   d u xs | j                   j                  k7  S r   r$   r&   s    r   r"   z/GetNonMatchingRulesFromPolicy.<locals>.<lambda>e   s&    DNNd$:$odnn>W>W[o>o$or   r(   r-   s    ` r   GetNonMatchingRulesFromPolicyr4   W   s4     !>o	f%v{{'8'89	::r   c                     | j                  d      }t        |      dk7  r$t        j                  dj	                  |             |S )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                     | j                  d      }t        |      dk7  r$t        j                  dj	                  |             |S )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}.
  r6   r7   zInvalid constraint name '{}': Name must be in the form [projects|folders|organizations]/{{resource_id}}/constraints/{{constraint_name}}.r8   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   r0   r4   r
   r    r   r   <module>rA      s:    8 &  ' >
8F?";&;& r   