
    5                     l   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  G d dej0                        ZddZd Zd Zd Z ej<                  dd      d        Z ej<                  dd      d        Z dZ!dZ"d Z#d Z$y) z6Command line processing utilities for access policies.    )absolute_import)division)unicode_literals)policies)organizations)concepts)deps)
cache_util)concept_parsers)
exceptions)log)
properties)	resourcesc                       e Zd Zy)DefaultPolicyResolutionErrorN)__name__
__module____qualname__     ?lib/googlecloudsdk/command_lib/accesscontextmanager/policies.pyr   r   "   s    r   r   Nc                 h    ~ |j                  d      rt        j                  |j                         |S )zBAdd the particular service filter message based on specified args.policy)IsSpecifiedr   AccessPolicyValidatorr   )refargsreqs      r   ValidateAccessPolicyArgr   &   s*    		h$$T[[1	*r   c                      t         j                  j                  j                  } t	        j
                  ddt        j                  |       g      S )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                  B    t        j                  ddt                     S )N#accesscontextmanager.accessPoliciesr   )resource_nameaccessPoliciesId)r   ResourceSpecr)   r   r   r   GetResourceSpecr/   7   s!    			+)+
- -r   c                     t         j                  j                  dt               dj	                  |      d      j                  |        y)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ForResourcer/   formatAddToParser)parserverbs     r   AddResourceArgr8   >   sC     ++$$T*	 ,  ![(r   zorganizations-by-domain
   c                    d| z   }	 t        t        j                         j                  |d            }|st        dj                  |             t        |      dkD  rt        dj                  | |            t        j                  j                  |d	   j                  d
      S # t        $ r }t        dj                  | |            d}~ww xY w)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   r4   lenr   REGISTRYParser!   )domainr<   orgserrs       r   _GetOrganizationrK   N   s    $ '$$&++G1+EFD 

&:AA&IK K
4y1}
&?FFD	  
			!	!
1gllC 
" 
E E 
 
&<CCC	 s   .B( (	C1CCzpolicies-by-organizationc                    	 t        t        j                         j                  | d            }|s(t        dj                  | j                                     t        |      dkD  r)t        dj                  | j                         |            t        j                  j                  |d   j                  d	
      }|S # t        $ r.}t        dj                  | j                         |            d}~ww xY w)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 [{}]r>   z;Found more than one access policy for organization [{}]:
{}r   r+   r?   )rA   policies_apirB   rC   rD   r   r4   NamerE   r   rF   rG   r!   )organization_refr   rJ   
policy_refs       r   
_GetPolicyrQ   t   s    &+L'')../?q.IJH 

&:AA!!#	%& & 8}q
&FMM!!#X	/0 0 !!''qk#H ( J*	 
 +
&<CC!!#S	*+ ++s   .C 	C8
)C33C8z.iam.gserviceaccount.comzdeveloper.gserviceaccount.comc                 n    | j                  d      \  }}}|j                  t              s	|t        k(  ry |S )N@)	partitionendswith_IAM_SUFFIX_DEVELOPER_DOMAIN)account_hosts      r   
_GetDomainr[      s4      %*!Q	]];4+<#<	+r   c                  0   t         j                  j                  j                  j	                         } | st        j                  d       yt        |       }|st        j                  d|        yt        j                  dd      5 }	 t        ||      }t        ||j                         |f      }	 ddd       |j                         S # t        $ r(}t        j                  d|       Y d}~ddd       yd}~ww xY w# 1 sw Y   j                         S xY w)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$   corerX   Getr   infor[   meta_cache_utilGetCacherK   rQ   RelativeNamer   rN   )rX   rH   cacherO   rP   rJ   s         r   GetDefaultPolicyre      s    ""**..0'	HH  g&	HH8'Bd;u)%8e%5%B%B%D/13j	 < 
		 ( 	hh;SA <; < 
	s0   C=(C			C:C5(C=5C::C==D)N)%__doc__
__future__r   r   r   +googlecloudsdk.api_lib.accesscontextmanagerr   rM   +googlecloudsdk.api_lib.cloudresourcemanagerr    googlecloudsdk.calliope.conceptsr   r	   googlecloudsdk.command_lib.metar
   ra   googlecloudsdk.command_lib.util(googlecloudsdk.command_lib.util.conceptsr   googlecloudsdk.corer   r   r   r   Errorr   r   r)   r/   r8   CacheResourcerK   rQ   rV   rW   r[   re   r   r   r   <module>rq      s    = &  ' P E 5 1 I 6 D * # * ):#3#3 :-)  3R8"E 9"EJ 4b9# :#L )3 r   