
    ['                     $   d Z ddlmZ ddlmZ ddlmZ ddlZddl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 Zd Zd Z G d de
j.                        ZddZd Zd Z G d de      Z G d de      Zd Zd Zy)z;Default values and fallbacks for missing surface arguments.    )absolute_import)division)unicode_literalsN)apigee)deps)errors)config)log)
properties)yaml)filesc                 (   t        j                         j                  }t        j                  j                  |d| z         }t        j                  j                  |      si S 	 t        j                  |      S # t        j                  $ r i cY S w xY w)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   "   st     ||~//*ww||J(9D(@A*	
	#I>>*%%			  Is   $A9 9BBc                     t        j                         j                  }t        j                  j                  |d|z         }t        j                  |t        j                  |              y)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                    t        j                         j                  }t        j                  j                  |d| z         }t        j                  j                  |      r	 t        j                  |       yy# t        $ r Y yw xY w)zDeletes a named cache file.r   N)	r	   r   r   r   r   r   r   removeOSErrorr   s      r   _DeleteCachedDataWithNamer'   H   sg    ||~//*ww||J(9D(@A*WW^^Jii
    s   "A9 9	BBc                   4     e Zd ZdZg Zd fd	Zd Zd Z xZS )Fallthroughz5Base class for Apigee resource argument fallthroughs.c                 2    t         t        |   d |||       y N)superr)   __init__)selfhintactiveplural	__class__s       r   r-   zFallthrough.__init__W   s    	+t%dD&&Ar#   c                     || j                   v S )z=Returns whether `field` is handled by this fallthrough class.)_handled_fields)r.   fields     r   __contains__zFallthrough.__contains__Z   s    D((((r#   c                     t        d      )NzaSubclasses of googlecloudsdk.commnand_lib.apigee.Fallthrough must actually provide a fallthrough.)NotImplementedErrorr.   parsed_argss     r   _CallzFallthrough._Call^   s    
	* r#   )FF)	__name__
__module____qualname____doc__r4   r-   r6   r;   __classcell__r2   s   @r   r)   r)   S   s    =/B)r#   r)   c                    t        d      xs i }|rh|j                  |d      }|r|S 	 t        j                  j	                  d|i      }d|vrt        j                  d|z        ||| <   t        |d       |S | |vrS	 t        j                  j	                  d| i      }d|vry|j                  dd      | k7  ry||| <   t        |d       ||    S ||    S # t
        j                  t
        j                  f$ r t        j                  d|z        t
        j                  $ r}|d}~ww xY w# t
        j                  t
        j                  f$ r Y yt
        j                  $ r}|d}~ww xY w)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   _GetProjectMappingrT   e   s    ))=>D"""#4d;Gn 44CC 12
 0//  "	"  %4! 02FG $$22AAg
&o 
	.			[$	/7	:"1w.0DE 
'	""	'	""; ((&*I*IJ 
--  	 
 	
      &&(G(GH  gsC   AC ?%D. %D. ;D. AD+$D&&D+.#E,E,%E''E,c                    t        d      xs i }| |v r||    S t        j                  j                  j                  j                         }|rt        j                  j                         }nt        j                  j                         }|d   D ]  }|d   D ]  }i ||<   |||<     t        |d       t        d       | |vry||    S )z:Returns the Apigee organization for the given GCP project.rC   organizations
projectIdszproject-mappingN)r   r   VALUESapi_endpoint_overridesr   GetrI   ListListOrganizationsGlobalr"   r'   )rN   rR   	overrides	list_orgsrE   matching_projects         r   _FindMappingForProjectr`      s    '(<=C/7##
 66==AAC)**//1I**BBDI0l(6*,o&'*6o&' 7 1 /+?@-.O#		!!r#   c                  6   t         j                  j                  j                  j	                         } | t        j                  d       yt        |       }|r|d   S t        |       }|r|d   S t        j                  d|        t        j                  d       y)z@Returns the organization associated with the active GCP project.Nz5Neither Apigee organization nor GCP project is known.rE   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   rX   corerN   rZ   r
   warningrT   r`   )rN   rR   rQ   s      r   OrganizationFromGCPProjectrf      s    ""**..0'_KKGH
 'w//>** #7+'>""++CWM++C 
r#   c                   .     e Zd ZdZdgZ fdZd Z xZS )!GCPProductOrganizationFallthroughz=Falls through to the organization for the active GCP project.rE   c                 ,    t         t        |   d       y )Nzset the property [project] or provide the argument [--project] on the command line, using a Cloud Platform project with an associated Apigee organization)r,   rh   r-   )r.   r2   s    r   r-   z*GCPProductOrganizationFallthrough.__init__   s    	
+T;	r#   c                     t               S r+   )rf   r9   s     r   r;   z'GCPProductOrganizationFallthrough._Call   s    %''r#   )r<   r=   r>   r?   r4   r-   r;   r@   rA   s   @r   rh   rh      s    E#$/(r#   rh   c                   (     e Zd ZdZ fdZd Z xZS )StaticFallthroughz#Falls through to a hardcoded value.c                 J    t         t        |   d       |g| _        || _        y )Nz@leave the argument unspecified for it to be chosen automatically)r,   rl   r-   r4   value)r.   argumentrn   r2   s      r   r-   zStaticFallthrough.__init__   s(    	
T+JL$:DDJr#   c                     | j                   S r+   )rn   r9   s     r   r;   zStaticFallthrough._Call   s    ::r#   )r<   r=   r>   r?   r-   r;   r@   rA   s   @r   rl   rl      s    +r#   rl   c                    t         j                  j                  |       }|s>t        j                  d| d   fd| d   fd| d   fg      }t        j                  d|d      t        |      d	kD  r-d
}t        j                  |t        j                  |      z         |d   d   }t        j                  j                  d|z         || d<   y)a  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`.
  rE   rD   environmentenvironmentsIdapiapisId
deploymentundeploy   z:Found more than one deployment that matches this request.
r   revisionzUsing deployed revision `%s`revisionsIdN)r   DeploymentsClientr[   collectionsOrderedDictr   rL   lenAmbiguousRequestErrorr   r    r
   statusPrint)argsdeploymentserror_identifierrF   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                 b   t         j                  j                  j                  j	                         }t        ||       }|r|j                  dd      S t        |      }|r|j                  dd      S t        j                  d|       t        j                  d       t        j                         )z0Returns the location of the Apigee organization.locationNrb   rc   )r   rX   rd   rN   rZ   rT   rH   r`   r
   re   r   LocationResolutionError)rE   rN   rQ   s      r   GetOrganizationLocationr     s    ""**..0'w5';;z4(( #7+';;z4((++CWM++C 	&&((r#   r+   )r?   
__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)   rT   r`   rf   rh   rl   r   r    r#   r   <module>r      s    B &  '  	 ) 1 4 & # * $ *07$"" $?#D":8( ( 
 
*:)r#   