
                            S r SSK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S
KJr  SSKJr  SSKJr  SrSr " S S\R0                  5      r " S S\R4                  5      rS\R8                  S4S jrS\4S jrS\S\ 4S jr!S\4S jr"S S jr#\R8                  4S jr$S\S\4S jr%S r&S\4S jr'g)!z Sources for Cloud Run Functions.    N)
exceptions)storage_api)storage_util)staging_bucket_util)region_name_util)types)log)
properties)	resources)timesgs://?   c                       \ rS rSrSrSrg)BucketNameError%   z!Error for bucket name generation. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       ;lib/googlecloudsdk/command_lib/run/sourcedeploys/sources.pyr   r   %   s    )r   r   c                       \ rS rSrSrSrSrg)ArchiveType)   ZipTarr   N)r   r   r   r   ZIPTARr   r   r   r   r   r   )   s    ##r   r   Tc           	         [         R                  " 5       n[        XaU5      n[        XU5      n[        R
                  " S[         U SU 35        [        R                  R                  SUUS9n	[        R                  " U U	USSUS9$ )a  Uploads a source to a staging bucket.

Args:
  source: Location of the source to be uploaded. Can be local path or a
    reference to a GCS object.
  region: The region to upload to.
  resource_ref: The Cloud Run service resource reference.
  source_bucket: The source bucket to upload to, if not None.
  archive_type: The type of archive to upload.
  respect_gitignore: boolean, whether the users .gitignore file should be
    respected when creating the achive to upload.

Returns:
  storage_v1_messages.Object, The written GCS object.
zUploading source to /zstorage.objects)
collectionbucketobjectNT)ignore_file	hide_logsrespect_gitignore)r   StorageClient_GetOrCreateBucket
_GetObjectr	   debug_GCS_PREFIXr   REGISTRYCreater   Upload)
sourceregionresource_refsource_bucketarchive_typer)   
gcs_clientbucket_nameobject_name
object_refs
             r   r1   r1   .   s    . ((**":}E+6>+))";-}Ak]KL!!((" ) *
 
	#	#)
 r   r2   c                     [         R                  R                  U 5      n[        R                  " 5       R                  U5      $ )zRetrieves the GCS object corresponding to the source location string.

Args:
  source: The source location string in the format `gs://<bucket>/<object>`.

Returns:
  storage_v1_messages.Object, The GCS object.
)r   ObjectReferenceFromUrlr   r*   	GetObject)r2   r:   s     r   GetGcsObjectr?   Z   s4     ++33F;*		"	"	$	.	.z	::r   returnc                 >    U =(       d    SR                  [        5      $ )z?Returns true if the source is located remotely in a GCS object. )
startswithr.   )r2   s    r   IsGcsObjectrD   g   s    
,B	"	";	//r   c                 |    SU R                    SU R                   3nU R                  b  USU R                   3-  nU$ )zReturns the gsutil URI of the GCS object.

Args:
  source: The storage_v1_messages.Object.

Returns:
  The gsutil URI of the format `gs://<bucket>/<object>(#<generation>)`.
r   r#   #)r%   name
generation)r2   source_paths     r   GetGsutilUrirJ   l   sH     a}5+"Qv(()**K	r   c           
         USL nU=(       d    [        U5      n[        R                  " 5       R                  R	                  S// SQS9/nSS0n [
        R                  " SU SU 35        U R                  UUS	US	US
9  U$ ! [        R                  [        R                  4 aR  nU(       aD  [        5       n[
        R                  " SU SU SU 35        U R                  UUS	US	US
9  Us SnA$ UeSnAff = f)z-Gets or Creates bucket used to store sources.NGET)zhttps://*.cloud.google.comzhttps://*.corp.google.comzhttps://*.corp.google.com:*zhttps://*.cloud.googlezhttps://*.byoid.goog)methodoriginzused-bycloudrunzCreating bucket z in region T)locationcheck_ownershipcorsenable_uniform_level_accesslabelszFailed to provision z, retrying with )_GetDefaultBucketNamer   GetMessagesBucketCorsValueListEntryr	   r-   CreateBucketIfNotExistsapi_exceptionsHttpForbiddenErrorr   BucketInWrongProjectError_GetRandomBucketName)	r7   r3   r8   using_default_bucketr%   rR   rT   erandom_buckets	            r   r+   r+   {   s8   $,7/7& '':: ; 	
$ &&II F8<=&&$( '  M''++
  *,m	ii (8 AX ((
&* )  
G5s%   1B $C7%AC2*C70C22C7c                    U[         R                  :X  a  SOSnU R                  [        5      (       d$  [        R
                  R                  U 5      (       a!  [        R
                  R                  U 5      u  pCSR                  [        R                  " [        R                  " 5       5      [        R                  " 5       R                  US9n[        R                   " U5       SUR#                  5        SU 3nU$ )z1Gets the object name for a source to be uploaded.z.tar.gzz.zipz{stamp}-{uuid}{suffix})stampuuidsuffixzs/r#   )r   r!   rC   r.   ospathisfilesplitextformatr   GetTimeStampFromDateTimeNowrc   uuid4hexr   GetKindName)r2   r4   r6   rd   _	file_nameobject_paths          r   r,   r,      s    $79V&{##rww~~f'='=  (IA '--**599;7::< . ) 
|	$%R(9(9(;'<Ai[I  
r   r3   c                     [         R                  R                  R                  R	                  SS9R                  SS5      R                  SS5      R                  SS5      nU b  [        X5      $ SU 3$ )	zjReturns the default regional bucket name.

Args:
  region: Cloud Run region.

Returns:
  GCS bucket name.
T)required:rp   .googleelgoogrun-sources-)r
   VALUEScoreprojectGetreplace_GetBucketName)r3   safe_projects     r   rU   rU      sx     $$(($(7wsCwsCwx"  
	 \* ,(r   c                     SU  SU 3n[        U5      [        ::  a  U$  [        R                  " U5      nSU  SU 3$ ! [        R                   a  n[        SU S35      UeSnAff = f)a7  Generates a regional bucket name, shortening the region if necessary.

Args:
  safe_project: The project ID, with characters unsafe for bucket names
    replaced.
  region: The Cloud Run region.

Returns:
  A valid GCS bucket name.

Raises:
  BucketNameError: If the region is too long and cannot be shortened.
ry   -z+Could not generate bucket name for region [zI] because it is too long and the region is not a known region to shorten.N)len_MAX_BUCKET_NAME_LENGTHr   ShortenGcpRegionUnknownRegionErrorr   )r   r3   bucket_name_baseshort_regionr_   s        r   r   r      s     $L>6(;	55#44V<L ~Q|n	55 
	,	, 

5fX >E 	E s   = A&A!!A&c                  J    [         R                  " 5       R                  n SU  3$ )z;Returns a random bucket name.

Returns:
  GCS bucket name.
ry   )rc   rl   rm   )rd   s    r   r]   r]      s"     ::<&x	  r   )N)(r   enumre   rc   apitools.base.pyr   rZ   googlecloudsdk.api_lib.storager   r   !googlecloudsdk.command_lib.buildsr   ,googlecloudsdk.command_lib.run.sourcedeploysr   r   googlecloudsdk.corecore_exceptionsr	   r
   r   googlecloudsdk.core.utilr   r.   r   Errorr   Enumr   r    r1   strr?   boolrD   rJ   r+   r,   rU   r   r]   r   r   r   <module>r      s    '  	  9 6 7 A I > = # * ) *  *o++ *$))  )X
; 
;0 0 0
C :z 3>// &# # .6:!c !r   