
    ['                     &   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SKJr  SSKJr  SSKJr  S rS rS r " S S\
R.                  5      rSS jrS rS r " S S\5      r " S S\5      rS rS rg)z;Default values and fallbacks for missing surface arguments.    )absolute_import)division)unicode_literalsN)apigee)deps)errors)config)log)
properties)yaml)filesc                 8   [         R                  " 5       R                  n[        R                  R                  USU -   5      n[        R                  R                  U5      (       d  0 $  [        R                  " U5      $ ! [        R                   a    0 s $ f = f)a  Returns the contents of a named cache file.

Cache files are saved as hidden YAML files in the gcloud config directory.

Args:
  name: The name of the cache file.

Returns:
  The decoded contents of the file, or an empty dictionary if the file could
  not be read for whatever reason.
.apigee-cached-)
r	   Pathsglobal_config_dirospathjoinisfiler   	load_pathYAMLParseErrorname
config_dir
cache_paths      1lib/googlecloudsdk/command_lib/apigee/defaults.py_CachedDataWithNamer   "   sw     ||~//*ww||J(9D(@A*	
	#	#I>>*%%			  Is   *B   BBc                     [         R                  " 5       R                  n[        R                  R                  USU-   5      n[        R                  " U[        R                  " U 5      5        g)zSaves `data` to a named cache file.

Cache files are saved as hidden YAML files in the gcloud config directory.

Args:
  data: The data to cache.
  name: The name of the cache file.
r   N)
r	   r   r   r   r   r   r   WriteFileContentsr   dump)datar   r   r   s       r   _SaveCachedDataWithNamer"   :   sG     ||~//*ww||J(9D(@A**diio6    c                     [         R                  " 5       R                  n[        R                  R                  USU -   5      n[        R                  R                  U5      (       a   [        R                  " U5        gg! [         a     gf = f)zDeletes a named cache file.r   N)	r	   r   r   r   r   r   r   removeOSErrorr   s      r   _DeleteCachedDataWithNamer'   H   sj    ||~//*ww||J(9D(@A*WW^^Jii
    s   (B   
BBc                   @   ^  \ rS rSrSr/ rSU 4S jjrS rS rSr	U =r
$ )FallthroughS   z5Base class for Apigee resource argument fallthroughs.c                 0   > [         [        U ]  S XU5        g N)superr)   __init__)selfhintactiveplural	__class__s       r   r.   Fallthrough.__init__W   s    	+t%dD&Ar#   c                     XR                   ;   $ )z=Returns whether `field` is handled by this fallthrough class.)_handled_fields)r/   fields     r   __contains__Fallthrough.__contains__Z   s    ((((r#   c                     [        S5      e)NzaSubclasses of googlecloudsdk.commnand_lib.apigee.Fallthrough must actually provide a fallthrough.)NotImplementedErrorr/   parsed_argss     r   _CallFallthrough._Call^   s    
	* r#    )FF)__name__
__module____qualname____firstlineno____doc__r6   r.   r8   r>   __static_attributes____classcell__r3   s   @r   r)   r)   S   s     =/B) r#   r)   c                     [        S5      =(       d    0 nU(       al  UR                  US5      nU(       a  U$  [        R                  R	                  SU05      nSU;  a  [
        R                  " SU-  S9eXBU '   [        US5        U$ X;  aT   [        R                  R	                  SU 05      nSU;  a  gUR                  SS5      U :w  a  gXBU '   [        US5        X    $ X    $ ! [
        R                  [
        R                  4 a    [
        R                  " SU-  S9e[
        R                   a  nUeSnAff = f! [
        R                  [
        R                  4 a     g[
        R                   a  nUeSnAff = f)zReturns the project mapping for the given GCP project.

Args:
  project: The GCP project name.
  user_provided_org: The organization ID provided by the user, if any.

Returns:
  The project mapping for the given GCP project.
project-mapping-v2NorganizationsIdorganizationzFPermission denied on resource "organizations/%s" (or it may not exist))message	projectId)
r   getr   OrganizationsClientProjectMappingr   UnauthorizedRequestErrorr"   EntityNotFoundErrorRequestError)projectuser_provided_orgproject_mappingsmappingproject_mappinges         r   _GetProjectMappingr[   e   s    ))=>D"""#4d;Gn 44CC 12
 0//  "	"  %4! 02FG $22AAg
&o 
	.			[$	/7	:"1w.0DE 
	""		""; ((&*I*IJ 
--  	 
 	
      &&(G(GH  gsC   AC% 'D= 6D= D= %AD:3D55D:=$E=#E=6E88E=c                    [        S5      =(       d    0 nX;   a  X   $ [        R                  R                  R                  R                  5       nU(       a  [        R                  R                  5       nO[        R                  R                  5       nUS    H  nUS    H  n0 X'   XAU'   M     M     [        US5        [        S5        X;  a  gX   $ )z:Returns the Apigee organization for the given GCP project.rJ   organizations
projectIdszproject-mappingN)r   r   VALUESapi_endpoint_overridesr   GetrP   ListListOrganizationsGlobalr"   r'   )rU   rY   	overrides	list_orgsrL   matching_projects         r   _FindMappingForProjectrg      s    '(<=C/##
 66==AAC)**//1I**BBDI0l(6*,o'*6&' 7 1 /+?@-.#		!!r#   c                  R   [         R                  R                  R                  R	                  5       n U c  [
        R                  " S5        g[        U 5      nU(       a  US   $ [        U 5      nU(       a  US   $ [
        R                  " SU 5        [
        R                  " S5        g)z@Returns the organization associated with the active GCP project.Nz5Neither Apigee organization nor GCP project is known.rL   3No Apigee organization is known for GCP project %s.vPlease provide the argument [--organization] on the command line, or set the property [api_endpoint_overrides/apigee].)	r   r_   corerU   ra   r
   warningr[   rg   )rU   rY   rX   s      r   OrganizationFromGCPProjectrm      s    ""**..0'_KKGH
 'w//>** #7+'>""++CWM++C 
r#   c                   8   ^  \ rS rSrSrS/rU 4S jrS rSrU =r	$ )!GCPProductOrganizationFallthrough   z=Falls through to the organization for the active GCP project.rL   c                 ,   > [         [        U ]  S5        g )Nzset the property [project] or provide the argument [--project] on the command line, using a Cloud Platform project with an associated Apigee organization)r-   ro   r.   )r/   r3   s    r   r.   *GCPProductOrganizationFallthrough.__init__   s    	
+T;	r#   c                     [        5       $ r,   )rm   r<   s     r   r>   'GCPProductOrganizationFallthrough._Call   s    %''r#   r@   )
rA   rB   rC   rD   rE   r6   r.   r>   rF   rG   rH   s   @r   ro   ro      s    E#$/( (r#   ro   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )StaticFallthrough   z#Falls through to a hardcoded value.c                 H   > [         [        U ]  S5        U/U l        X l        g )Nz@leave the argument unspecified for it to be chosen automatically)r-   rv   r.   r6   value)r/   argumentry   r3   s      r   r.   StaticFallthrough.__init__   s&    	
T+JL$:DJr#   c                     U R                   $ r,   )ry   r<   s     r   r>   StaticFallthrough._Call   s    ::r#   )r6   ry   )	rA   rB   rC   rD   rE   r.   r>   rF   rG   rH   s   @r   rv   rv      s    + r#   rv   c                    [         R                  R                  U 5      nU(       d@  [        R                  " SU S   4SU S   4SU S   4/5      n[
        R                  " SUS5      e[        U5      S	:  a/  S
n[
        R                  " U[        R                  " U5      -   5      eUS   S   n[        R                  R                  SU-  5        X@S'   g)ar  If `args` provides no revision, adds the deployed revision, if unambiguous.

Args:
  args: a dictionary of resource identifiers which identifies an API proxy and
    an environment, to which the deployed revision should be added.

Raises:
  EntityNotFoundError: no deployment that matches `args` exists.
  AmbiguousRequestError: more than one deployment matches `args`.
rL   rK   environmentenvironmentsIdapiapisId
deploymentundeploy   z:Found more than one deployment that matches this request.
r   revisionzUsing deployed revision `%s`revisionsIdN)r   DeploymentsClientrb   collectionsOrderedDictr   rS   lenAmbiguousRequestErrorr   r    r
   statusPrint)argsdeploymentserror_identifierrM   deployed_revisions        r   FallBackToDeployedProxyRevisionr      s     ((--d3+	"..	/01	-./%h1H0  
$
$\3CZ
PPKG

&
&w;1G'G
HH!!nZ0**14EEF)}r#   c                 z   [         R                  R                  R                  R	                  5       n[        X5      nU(       a  UR                  SS5      $ [        U5      nU(       a  UR                  SS5      $ [        R                  " SU5        [        R                  " S5        [        R                  " 5       e)z0Returns the location of the Apigee organization.locationNri   rj   )r   r_   rk   rU   ra   r[   rO   rg   r
   rl   r   LocationResolutionError)rL   rU   rX   s      r   GetOrganizationLocationr     s    ""**..0'w5';;z4(( #7+';;z4((++CWM++C 	&&((r#   r,   )rE   
__future__r   r   r   r   r   googlecloudsdk.api_libr    googlecloudsdk.calliope.conceptsr   !googlecloudsdk.command_lib.apigeer   googlecloudsdk.corer	   r
   r   r   googlecloudsdk.core.utilr   r   r"   r'   r)   r[   rg   rm   ro   rv   r   r   r@   r#   r   <module>r      s    B &  '  	 ) 1 4 & # * $ *07$"" $?#D":8( ( 
 
*:)r#   