
    Q                     R   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Zddl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dlmZ ddlmZ ddlmZ  G d dej8                        Zd Zd Z G d de      Z  G d de      Z! G d de       Z" G d de       Z# G d de       Z$ G d  d!e       Z% G d" d#ej8                        Z&d$ Z'd% Z(dNd'Z)dNd(Z*d) Z+d* Z,d+ Z-d, Z.dNd-Z/dNd.Z0 G d/ d0ejb                        Z2dNd1Z3d2 Z4 G d3 d4e      Z5d5 Z6 G d6 d7e      Z7d8 Z8d9 Z9dNd:Z:d; Z;d< Z<dNd=Z=d> Z>dNd?Z?dNd@Z@dNdAZAdNdBZBdNdCZCdNdDZDdE ZEdF ZFdG ZG ej                  dH e9       dIdJd&dJK      ZI ej                  dL eF       dMdJd&dJK      ZJy)Oz-Shared resource flags for Cloud Run commands.    )absolute_import)division)print_function)unicode_literalsN)global_methods)concepts)deps)util)
exceptions)	platforms)presentation_specs)log)
properties)	resources)
console_ioc                   L     e Zd ZdZ fdZej                  d        Zd Z xZ	S )PromptFallthroughz3Fall through to reading from an interactive prompt.c                 0    t         t        |   d |       y )Nfunctionhint)superr   __init__)selfr   	__class__s     3lib/googlecloudsdk/command_lib/run/resource_args.pyr   zPromptFallthrough.__init__+   s    	
T+T+E    c                      y N r   parsed_argss     r   _PromptzPromptFallthrough._Prompt.   s    r   c                 N    t        j                         sy | j                  |      S r   )r   	CanPromptr#   r!   s     r   _CallzPromptFallthrough._Call2   s     !<<$$r   )
__name__
__module____qualname____doc__r   abcabstractmethodr#   r&   __classcell__r   s   @r   r   r   (   s+    ;F 	 	%r   r   c                 2   t         j                  j                  | j                  t         j                              }|j                  d      d   }|j                  d      d   }t        j                  dd|      j                  d      j                         S )a  Produce a valid default service name from a container image path.

  Converts a file path or image path into a reasonable default service name by
  stripping file path delimeters, image tags, and image hashes.
  For example, the image name 'gcr.io/myproject/myimage:latest' would produce
  the service name 'myimage'.

  Args:
    image: str, The container path.

  Returns:
    A valid Cloud Run service name.
  :r   @z[^a-zA-Z0-9-] -)
ospathbasenamerstripsepsplitresubstriplower)image	base_names     r   _GenerateServiceNamer@   8   ss     ggu||BFF34)ooc"1%)ooc"1%)	 "i	0	6	6s	;	A	A	CCr   c                     t         j                  j                  t         j                  j                  |             \  }}|r%t         j                  j                  |      \  }}|r%t	        |      S )a'  Produce a valid default service name from a local file or directory path.

  Converts a file or directory path into a reasonable default service name by
  resolving relative paths to absolute paths, removing any extensions, and then
  removing any invalid characters.

  For example, the paths /tmp/foo/bar/.. and /tmp/foo.tar.gz would both produce
  the service name 'foo'. A source path of "." will be expanded to the current
  directory name."

  Args:
    source: str, The file or directory path.

  Returns:
    A valid Cloud Run service name.
  )r4   r5   splitextabspathr@   )sourcer5   exts      r   !_GenerateServiceNameFromLocalPathrF   M   sT    " ggrwwv67)$  &ID# 		d	##r   c                   .     e Zd ZdZ fdZd Zd Z xZS )ResourcePromptFallthroughzEFall through to reading the resource name from an interactive prompt.c                 X    t         t        |   dj                  |             || _        y )Nz.specify the {} name from an interactive prompt)r   rH   r   formatresource_type_lower)r   rK   r   s     r   r   z"ResourcePromptFallthrough.__init__g   s.    	
#T38??	

  3Dr   c                     | j                   j                         dz   }| j                  |      }t        j                  ||      S )Nz name)messagedefault)rK   
capitalize_DefaultNameFromArgsr   PromptWithDefault)r   r"   rM   default_names       r   r#   z!ResourcePromptFallthrough._Prompto   s>    &&113g=G,,[9L''NNr   c                     t        |dd       rt        |j                        S t        |dd       rt        |j                        S y)Nr>   rD   r2   )getattrr@   r>   rF   rD   r!   s     r   rP   z.ResourcePromptFallthrough._DefaultNameFromArgst   s?    {GT*!+"3"344	h	-.{/A/ABBr   )r'   r(   r)   r*   r   r#   rP   r-   r.   s   @r   rH   rH   d   s    M3O
r   rH   c                   (     e Zd ZdZ fdZd Z xZS )RegionPromptFallthroughz>Fall through to reading the region from an interactive prompt.c                 ,    t         t        |   d       y )Nz-specify the region from an interactive prompt)r   rV   r   r   r   s    r   r   z RegionPromptFallthrough.__init__   s    	
!417r   c                    t        j                         }t        j                  |      }t        j                  |ddd      }||   }t
        j                  j                  dj                  |             |r||_	        |S )NzPlease specify a region:
T)rM   cancel_optionallow_freeformzHTo make this the default region, run `gcloud config set run/region {}`.
)
r   GetServerlessClientInstanceListRegionsr   PromptChoicer   statusPrintrJ   region)r   r"   clientall_regionsidxra   s         r   r#   zRegionPromptFallthrough._Prompt   sz    779F ,,V4K

!
!,	C FJJ	//5vf~ !kMr   r'   r(   r)   r*   r   r#   r-   r.   s   @r   rV   rV   |   s    F
r   rV   c                        e Zd Z fdZ xZS )ServicePromptFallthroughc                 ,    t         t        |   d       y )Nservice)r   rg   r   rX   s    r   r   z!ServicePromptFallthrough.__init__   s    	
"D29=r   r'   r(   r)   r   r-   r.   s   @r   rg   rg      s    > >r   rg   c                        e Zd Z fdZ xZS )WorkerPoolPromptFallthroughc                 ,    t         t        |   d       y )N
workerpool)r   rl   r   rX   s    r   r   z$WorkerPoolPromptFallthrough.__init__   s    	
%t5lCr   rj   r.   s   @r   rl   rl      s    D Dr   rl   c                        e Zd Z fdZ xZS )JobPromptFallthroughc                 ,    t         t        |   d       y )Njob)r   rp   r   rX   s    r   r   zJobPromptFallthrough.__init__   s    	
.u5r   rj   r.   s   @r   rp   rp      s    6 6r   rp   c                        e Zd Z fdZ xZS )ExecutionPromptFallthroughc                 ,    t         t        |   d       y )N	execution)r   rt   r   rX   s    r   r   z#ExecutionPromptFallthrough.__init__   s    	
$d4[Ar   rj   r.   s   @r   rt   rt      s    B Br   rt   c                   (     e Zd ZdZ fdZd Z xZS )DefaultFallthroughzrUse the namespace "default".

  For Knative only.

  For Cloud Run, raises an ArgumentError if project not set.
  c                 0    t         t        |   d d       y )Nz]For Cloud Run on Kubernetes Engine, defaults to "default". Otherwise, defaults to project ID.r   )r   rx   r   rX   s    r   r   zDefaultFallthrough.__init__   s     	
d,1 - r   c                 B   t        j                         t         j                  k(  s%t        j                         t         j                  k(  ryt	        |dd       sGt
        j                  j                  j                  j                         st        j                  d      y )NrN   projectzThe [project] resource is not properly specified. Please specify the argument [--project] on the command line or set the property [core/project].)r   GetPlatformPLATFORM_GKEPLATFORM_KUBERNETESrT   r   VALUEScorer{   Getr   ArgumentErrorr!   s     r   r&   zDefaultFallthrough._Call   s|    9#9#99  "i&C&CCY-!!))--/ $$- 
 r   r'   r(   r)   r*   r   r&   r-   r.   s   @r   rx   rx      s    r   rx   c            
      D   t        j                  ddt        j                  t        j
                  j                  j                        t               t        j                  d      t        j                  t        j
                  j                  j                        g      S )N	namespacezJSpecific to Cloud Run for Anthos: Kubernetes namespace for the {resource}.r{   name	help_textfallthroughs)r    ResourceParameterAttributeConfigr	   PropertyFallthroughr   r   runr   rx   ArgFallthroughr   r{   r    r   r   NamespaceAttributeConfigr      sx    		2	25 
"
":#4#4#8#8#B#B
C




i
(

"
":#4#4#9#9#A#A
B	
 r   c                      t        j                  t        j                        } t	               gt        j                  j
                  z   }|| _        | S r   )copydeepcopyr    DEFAULT_PROJECT_ATTRIBUTE_CONFIGrx   r   )project_attribute_configr   s     r   ProjectAttributeConfigr      sN    !]]// //<<=, +7'	!!r   Fc                 R    | rt               g}ng }t        j                  dd|      S );Attribute config with fallthrough prompt only if requested.ri   zService for the {resource}.r   )rg   r   r   promptr   s     r   ServiceAttributeConfigr      s1    ,./LL		2	2-
 r   c                 R    | rt               g}ng }t        j                  dd|      S )r   zworker-poolzWorkerPool for the {resource}.r   )rl   r   r   r   s     r   WorkerPoolAttributeConfigr      s1    /12LL		2	20
 r   c                  0    t        j                  dd      S )Nconfigurationz!Configuration for the {resource}.r   r   r   r   r    r   r   ConfigurationAttributeConfigr   	  s    		2	2&I
 r   c                  0    t        j                  dd      S )NroutezRoute for the {resource}.r   r   r    r   r   RouteAttributeConfigr     s    		2	29
 r   c                  0    t        j                  dd      S )NrevisionzRevision for the {resource}.r   r   r    r   r   RevisionAttributeConfigr     s    		2	2!?
 r   c                  0    t        j                  dd      S )Ndomainz#Name of the domain to be mapped to.r   r   r    r   r   DomainAttributeConfigr     s    		2	2D
 r   c                 R    | rt               g}ng }t        j                  dd|      S )NjobszJob for the {resource}.r   )rp   r   r   r   s     r   JobAttributeConfigr   !  s1    (*+LL		2	2)
 r   c                 R    | rt               g}ng }t        j                  dd|      S )N
executionsz
Execution.r   )rt   r   r   r   s     r   ExecutionAttributeConfigr   -  s/    .01LL		2	2<l
 r   c                   *     e Zd ZdZd fd	Zd Z xZS ) TaskExecutionAndIndexFallthroughzAAllow the user to provide --execution and --index to find a task.c                 ^    t         t        |   dj                  |      d|       || _        y )Nz;provide the arguments `{}`  and `index` on the command lineT)activeplural)r   r   r   rJ   arg_name)r   r   r   r   s      r   r   z)TaskExecutionAndIndexFallthrough.__init__:  s:    	
*D:ELL	
  ;  DMr   c                     t        |t        j                  | j                        d       }t        |dd       }dj	                  ||      S )Nindexz{}-{})rT   concepts_utilNamespaceFormatr   rJ   )r   r"   prefixr   s       r   r&   z&TaskExecutionAndIndexFallthrough._CallD  sC    ]224==A4F K$/E>>&%((r   Fr   r.   s   @r   r   r   7  s    I)r   r   c                 T    | rt        d      g}ng }t        j                  dd|      S )NtasktaskszTask.r   )r   r   r   r   s     r   TaskAttributeConfigr   L  s1    4V<=LL		2	2gL
 r   c                      t        j                  ddt        j                  d      t        j                  t
        j                  j                  j                        t               g      S )Nra   zsThe Cloud region for the {resource}. Overrides the default `run/region` property value for this command invocation.--regionr   )
r   r   r	   r   r   r   r   r   ra   rV   r    r   r   LocationAttributeConfigr   V  sY    		2	2E 

j
)

"
":#4#4#8#8#?#?
@
!
#
 r   c                   .     e Zd ZdZ fdZd Zd Z xZS )ClusterPromptFallthroughDFall through to reading the cluster name from an interactive prompt.c                 ,    t         t        |   d       y )Nz5specify the cluster from a list of available clusters)r   r   r   rX   s    r   r   z!ClusterPromptFallthrough.__init__h  s    	
"D2?r   c                    t        j                         t         j                  k7  ryt        j                  j
                  j                  j                  d      }t        |dd      xs2 t        j                  j                  j                  j                         }|rdj                  |      nd}t        j                  ||      }|s$t        j                  dj                  |            |D cg c]  }| j!                  |||       }}t#        j$                  |dj                  |      d	      }||   }	|rd}
nd
j                  |	j&                        }
|	j)                         }|	j*                  |k7  r|	j-                         }d}
t.        j0                  j3                  dj                  ||
             |	j5                         S c c}w )zFallthrough to reading the cluster name from an interactive prompt.

    Only prompt for cluster name if the user-specified platform is GKE.

    Args:
      parsed_args: Namespace, the args namespace.

    Returns:
      A cluster name string
    NT)requiredcluster_locationz in [{}]r2   zJNo compatible clusters found{}. Ensure your cluster has Cloud Run enabled.zGKE cluster{}:rM   rZ   z- && gcloud config set run/cluster_location {}z[To make this the default cluster, run `gcloud config set run/cluster {cluster}{location}`.
)clusterlocation)r   r|   r}   r   r   r   r{   r   rT   r   r   rJ   r   MultiTenantClustersForProjectr   ConfigurationError_GetClusterDescriptionr   r^   zoneName	projectIdRelativeNamer   r_   r`   SelfLink)r   r"   r{   r   cluster_location_msgcluster_refsccluster_refs_descsrd   cluster_reflocation_help_textcluster_names               r   r#   z ClusterPromptFallthrough._Promptm  s    )"8"88$$,,00$0?G/6 	8  11557 
 0@
*+R  "??!L ))77=v"8  A 	##A'7A  
 
!
! ''(<=C s#K :
@
@  ##%L' --/lJJ	 +=   
 !!Gs   1G
c                     |j                         }|sdj                  ||j                        }||j                  k7  rdj                  ||j                        }|S )z"Description of cluster for prompt.z{} in {})r   rJ   r   r   )r   r   r   r{   responses        r   r   z/ClusterPromptFallthrough._GetClusterDescription  sS     ||~H""8W\\:h'###""8W->->?hOr   )r'   r(   r)   r*   r   r#   r   r-   r.   s   @r   r   r   e  s    L
E"N	r   r   c                      t        j                  ddt        j                  t        j
                  j                  j                        t               g      S )Nr   z\Name of the Kubernetes Engine cluster to use. Alternatively, set the property [run/cluster].r   )	r   r   r	   r   r   r   r   r   r   r    r   r   ClusterAttributeConfigr     sJ    		2	2; 
"
":#4#4#8#8#@#@
A
"
$

 
r   c                   (     e Zd ZdZ fdZd Z xZS ) ClusterLocationPromptFallthroughr   c                 ,    t         t        |   d       y )Nz;specify the cluster location from a list of available zones)r   r   r   rX   s    r   r   z)ClusterLocationPromptFallthrough.__init__  s    	
*D:Er   c                    t        |dd      xs2 t        j                  j                  j                  j                         }t        j                         t        j                  k(  r|rt        j                         D cg c]  }|j                  |k(  s| }}|s$t        j                  dj                  |            |D cg c]  }|j                   }}t!        j"                  |dj                  |      d      }||   }t$        j&                  j)                  dj                  |             |S yyc c}w c c}w )a/  Fallthrough to reading the cluster location from an interactive prompt.

    Only prompt for cluster location if the user-specified platform is GKE
    and if cluster name is already defined.

    Args:
      parsed_args: Namespace, the args namespace.

    Returns:
      A cluster location string
    r   NzYNo cluster locations found for cluster [{}]. Ensure your clusters have Cloud Run enabled.zGKE cluster location for [{}]:Tr   z\To make this the default cluster location, run `gcloud config set run/cluster_location {}`.
)rT   r   r   r   r   r   r   r|   r}   r   ListClustersr   r   r   rJ   r   r   r^   r   r_   r`   )r   r"   r   r   clusterscluster_locationsrd   r   s           r   r#   z(ClusterLocationPromptFallthrough._Prompt  s1    	Y- 	/  ((,,.  )"8"88\#0022aff6L!2   ++;;A6,;O
 	
 ,448a16684##
299,Gc
 #3'h	jj;;A6(;K o) >J8 5s   ?D7D7D<re   r.   s   @r   r   r     s    L
$r   r   c                      t        j                  ddt        j                  t        j
                  j                  j                        t               g      S )Nr   z`Zone in which the {resource} is located. Alternatively, set the property [run/cluster_location].r   )	r   r   r	   r   r   r   r   r   r   r    r   r   ClusterLocationAttributeConfigr     sK    		2	2D 
"
":#4#4#8#8#I#I
J
*
,

 
r   c                  r    t        j                  dt         j                  t               t	               d      S )Nz!container.projects.zones.clustersr   )r   r   	clusterIdresource_name)r   ResourceSpecr   r   r   r    r   r   GetClusterResourceSpecr     s0    			)99)+&(
 r   c                 V    t        j                  dt               t        |       d      S )Nzrun.namespaces.servicesri   )namespacesId
servicesIdr   )r   r   r   r   r   s    r   GetServiceResourceSpecr     s)    			+-'/	
 r   c                  T    t        j                  dt               t               d      S )Nzrun.namespaces.configurationsr   )r   configurationsIdr   )r   r   r   r   r    r   r   GetConfigurationResourceSpecr     s'    			%+-35#	
 r   c                  T    t        j                  dt               t               d      S )Nzrun.namespaces.routesr   )r   routesIdr   )r   r   r   r   r    r   r   GetRouteResourceSpecr   '  s'    			+-#%	
 r   c                 x    t        j                  d| s
t               nt         j                  t	               d      S )Nzrun.namespaces.revisionsr   )r   revisionsIdr   )r   r   r   r   r   )is_worker_pool_revisions    r   GetRevisionResourceSpecr   1  s5    			 $ ,-44)+
 r   c                  T    t        j                  dt               t               d      S )Nzrun.namespaces.domainmappingsDomainMapping)r   domainmappingsIdr   )r   r   r   r   r    r   r   GetDomainMappingResourceSpecr   <  s'    			%+-,.#	
 r   c                 f    t        j                  dt         j                  t        |       dd      S )Nzrun.namespaces.jobsr   Jobv1)r   jobsIdr   api_version)r   r   r   r   r   s    r   GetJobResourceSpecr  E  s/    			<<v.
 r   c                 f    t        j                  dt         j                  t        |       dd      S )Nzrun.namespaces.executionsr   	Executionr  )r   executionsIdr   r  )r   r   r   r   r   s    r   GetExecutionResourceSpecr
  O  s/    			!<<+6:
 r   c                 f    t        j                  dt         j                  t        |       dd      S )Nzrun.namespaces.tasksr   Taskr  )r   tasksIdr   r  )r   r   r   r   r   s    r   GetTaskResourceSpecr  Y  s/    			<<!0
 r   c                 d    t        j                  dt         j                  t        |       dd      S )Nzrun.namespaces.workerpools
WorkerPoolr  )r   workerpoolsIdr   r  )r   r   r   r   r   s    r   GetV1WorkerPoolResourceSpecr  c  s/    			"<<-f5 
 r   c                 v    t        j                  dt         j                  t               t	        |       dd      S )Nz"run.projects.locations.workerPoolsr  v2)
projectsIdlocationsIdworkerPoolsIdr   r  )r   r   r   r   r   r   s    r   GetV2WorkerPoolResourceSpecr  m  s5    			*::)+-f5 
 r   c           	          t        j                  dt         j                  t               t	        |       t               dd      S )Nz,run.projects.locations.workerPools.revisionsWorkerPoolRevisionr  )r  r  r  r   r   r  )r   r   r   r   r   r   r   s    r   #GetV2WorkerPoolRevisionResourceSpecr  x  s;    			4::)+-f5)+(
 r   c                  B    t        j                  ddt                     S )Nzrun.projectsr{   )r   r  )r   r   r   r    r   r   GetProjectResourceSpecr    s!    			')
 r   c                  b    t        j                  dt         j                  t               dd      S )Nzrun.projects.locationsRegionr  )r  r  r   r  )r   r   r   r   r    r   r   GetRegionResourceSpecr     s-    			::)+
 r   c                      	 t        j                  dt               d      S # t        j                  $ r# t        j                  dt               d      cY S w xY w)z*Returns a resource spec for the namespace.zrun.namespacesr   )r   r   zrun.api.v1.namespaces)r   r   r   r   InvalidCollectionExceptionr    r   r   GetNamespaceResourceSpecr#    s\    
  -/! 
 
	-	-   -/! s   " 3AAz	--clusterz(Kubernetes Engine cluster to connect to.T)hiddenr   prefixesr   zCloud region to use.r   )Kr*   
__future__r   r   r   r   r+   r   r4   r:   googlecloudsdk.api_lib.runr    googlecloudsdk.calliope.conceptsr   r	   r
   r   googlecloudsdk.command_lib.runr   r   (googlecloudsdk.command_lib.util.conceptsr   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler   Fallthroughr   r@   rF   rH   rV   rg   rl   rp   rt   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r  r  r   r#  ResourcePresentationSpecCLUSTER_PRESENTATIONREGION_PRESENTATIONr    r   r   <module>r1     s   4 &  % ' 
  	 	 5 5 1 B 5 4 G # * ) 2%(( % D*$. 1 0/ 6>8 >D"; D64 6B!: B")) "J "

	)t':': )*X0 Xv,'8 ,^	& C)BB.  B(AA r   