
    5                     n   S 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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SKJr  SSKJr  SSKJr  SSKJr   " S S\R0                  5      rSS jrS rS rS r\R<                  " SS5      S 5       r\R<                  " SS5      S 5       r Sr!Sr"S r#S r$g) z6Command line processing utilities for access policies.    )absolute_import)division)unicode_literals)policies)organizations)concepts)deps)
cache_util)concept_parsers)
exceptions)log)
properties)	resourcesc                       \ rS rSrSrg)DefaultPolicyResolutionError"    N)__name__
__module____qualname____firstlineno____static_attributes__r       ?lib/googlecloudsdk/command_lib/accesscontextmanager/policies.pyr   r   "   s    r   r   Nc                 t    A UR                  S5      (       a   [        R                  " UR                  5        U$ )zBAdd the particular service filter message based on specified args.policy)IsSpecifiedr   AccessPolicyValidatorr   )refargsreqs      r   ValidateAccessPolicyArgr"   &   s-    		h$$T[[1	*r   c                      [         R                  R                  R                  n [        R
                  " SS[        R                  " U 5      /S9$ )Nr   zThe ID of the access policy.)name	help_textfallthroughs)r   VALUESaccess_context_managerr   r    ResourceParameterAttributeConfigr	   PropertyFallthrough)	property_s    r   GetAttributeConfigr,   /   sC    66==)		2	2.,,Y78
: :r   c                  >    [         R                  " SS[        5       S9$ )N#accesscontextmanager.accessPoliciesr   )resource_nameaccessPoliciesId)r   ResourceSpecr,   r   r   r   GetResourceSpecr2   7   s!    			+)+
- -r   c                     [         R                  R                  S[        5       SR	                  U5      SS9R                  U 5        g)zAdd a resource argument for an access policy.

NOTE: Must be used only if it's the only resource arg in the command.

Args:
  parser: the parser for the command.
  verb: str, the verb to describe the resource, such as 'to update'.
r   zThe access policy {}.T)requiredN)r   ConceptParserForResourcer2   formatAddToParser)parserverbs     r   AddResourceArgr;   >   sC     ++$$T*	 ,  ![(r   zorganizations-by-domain
   c                    SU -   n [        [        R                  " 5       R                  USS95      nU(       d  [        SR                  U 5      5      e[        U5      S:  a  [        SR                  X5      5      e[        R                  R                  US	   R                  S
S9$ ! [         a  n[        SR                  X5      5      eSnAff = f)a  Get the organization for the given domain.

The current user must have permission to list the organization.

Args:
  domain: str, the domain (e.g. 'example.com') to look up the organization of,
    or None to just list the organizations for the current account.

Returns:
  resources.Resource, a reference to a cloudresourcemanager.organizations
    resource

Raises:
  DefaultPolicyResolutionError: if the number of organizations matching the
    given domain is not exactly 1, or searching organizations fails.
zdomain:   )filter_limitz2Unable to resolve organization for domain [{}]: {}Nz0No matching organizations found for domain [{}].   z4Found more than one organization for domain [{}].
{}r   z"cloudresourcemanager.organizations
collection)listr   ClientList	Exceptionr   r7   lenr   REGISTRYParser$   )domainr?   orgserrs       r   _GetOrganizationrN   N   s    $ '$$&++G1+EFD 

&:AA&IK K
4y1}
&?FF	  
			!	!
1gllC 
" 
E E 
 
&<CC	 s   ,B( (
C2CCzpolicies-by-organizationc                     [        [        R                  " 5       R                  U SS95      nU(       d(  [        SR                  U R                  5       5      5      e[        U5      S:  a)  [        SR                  U R                  5       U5      5      e[        R                  R                  US   R                  S	S
9nU$ ! [         a.  n[        SR                  U R                  5       U5      5      eSnAff = f)a  Get the access policy for the given organization.

The current user must have permission to list the policies for the
organization.

Args:
  organization_ref: resources.Resource, a reference to a
    cloudresourcemanager.organizations resource to look up the policy for.

Returns:
  resources.Resource, a reference to an accesscontextmanager.accessPolicies
    resource

Raises:
  DefaultPolicyResolutionError: if the number of policies matching the
    given organization is not exactly 1, or listing policies fails.
r>   )r@   z2Unable to resolve policy for organization [{}]: {}Nz0No matching policies found for organization [{}]rA   z;Found more than one access policy for organization [{}]:
{}r   r.   rB   )rD   policies_apirE   rF   rG   r   r7   NamerH   r   rI   rJ   r$   )organization_refr   rM   
policy_refs       r   
_GetPolicyrT   t   s    &+L'')../?q.IJH 

&:AA!!#	%& & 8}q
&FMM!!#X	/0 0 !!''qk#H ( J*	 
 +
&<CC!!#S	*+ ++s   ,C 
C:)C55C:z.iam.gserviceaccount.comzdeveloper.gserviceaccount.comc                 x    U R                  S5      u    pUR                  [        5      (       d
  U[        :X  a  g U$ )N@)	partitionendswith_IAM_SUFFIX_DEVELOPER_DOMAIN)account_hosts      r   
_GetDomainr^      s5      %*!Q	]];4+<#<	+r   c                  N   [         R                  R                  R                  R	                  5       n U (       d  [
        R                  " S5        g[        U 5      nU(       d  [
        R                  " SU 5        g[        R                  " SSS9 n [        X!5      n[        X#R                  5       U45      n SSS5        UR                  5       $ ! [         a)  n[
        R                  " SU5         SnASSS5        gSnAff = f! , (       d  f       WR                  5       $ = f)z:Gets the ID of the default policy for the current account.zIUnable to automatically resolve policy since account property is not set.Nz)Unable to resolve domain for account [%s]zresource://T)createz*Unable to automatically resolve policy: %s)r   r'   corer[   Getr   infor^   meta_cache_utilGetCacherN   rT   RelativeNamer   rQ   )r[   rK   cacherR   rS   rM   s         r   GetDefaultPolicyrh      s    ""**..0'	HH  g&	HH8'Bd;u)%8e%B%B%D/13j	 < 
		 ( 	hh;SA <; <; 
	s0   D&C
DC?2D?DD
D$)N)%__doc__
__future__r   r   r   +googlecloudsdk.api_lib.accesscontextmanagerr   rP   +googlecloudsdk.api_lib.cloudresourcemanagerr    googlecloudsdk.calliope.conceptsr   r	   googlecloudsdk.command_lib.metar
   rd   googlecloudsdk.command_lib.util(googlecloudsdk.command_lib.util.conceptsr   googlecloudsdk.corer   r   r   r   Errorr   r"   r,   r2   r;   CacheResourcerN   rT   rY   rZ   r^   rh   r   r   r   <module>rt      s    = &  ' P E 5 1 I 6 D * # * ):#3#3 :-)  3R8"E 9"EJ 4b9# :#L )3 r   