
    
h                     L   d Z ddlZddlZddlZddlmZmZ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 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l0m1Z1  G d d      Z2 G d  d!      Z3 G d" d#      Z4 G d$ d%      Z5 G d& d'      Z6 G d( d)      Z7 G d* d+      Z8 G d, d-      Z9d.e2d/dfd0Z:d/e;fd1Z<d2e;d3e;d4ed/dfd5Z=d/e;fd6Z>y)7a  Command library for run compose resource command.

This library handles the creation of Google Cloud resources required to deploy a
Docker Compose application to Cloud Run. It's utilized by the
`gcloud run compose up` command.

The core responsibilities include:
  1.  Parsing the JSON output from the 'runcompose' Go binary, which lists
      the necessary resources based on the compose file.
  2.  Providing classes to represent these resources (e.g., Cloud Build).
  3.  Orchestrating the creation of these resources in Google Cloud.
    N)AnyDictOptional)projects_api)api_enabler)global_methods)service)api)storage_api)storage_util)apis)messages)base)iam_util)config_changes)connection_context)flags)	platforms)serverless_operations)stages)builder)tracker)
exceptions)log)
properties)	resources)yaml)progress_tracker)filesc                       e Zd ZdZ	 	 	 ddee   dee   dee   fdZedeee	f   dd fd	       Z
dd
Zdeee	f   fdZy)SecretConfigz2Represents the secret configuration for a service.Nnamefilemountc                 <    || _         || _        d | _        || _        y N)r"   r#   secret_versionr$   )selfr"   r#   r$   s       6lib/googlecloudsdk/command_lib/run/compose_resource.py__init__zSecretConfig.__init__?   s!     DIDI)-DDJ    datareturnc                      | |j                  d      |j                  d      |j                  d            }|j                  d      |_        |S )Nr"   r#   r$   )r"   r#   r$   r'   )getr'   clsr,   configs      r)   	from_dictzSecretConfig.from_dictK   sI    XXfXXfhhwF
 !HH%56FMr+   c                 Z    t        j                  d| j                         t        |        y)zCreates the secret in Google Secret Manager and adds a version.

    This method calls the internal function _create_secret_and_add_version
    to perform the actual resource creation and versioning.
    zHandling secret: %sN)r   debugr"   _create_secret_and_add_versionr(   s    r)   handlezSecretConfig.handleU   s     II#TYY/"4(r+   c                 `    | j                   | j                  | j                  | j                  dS )z5Serializes the SecretConfig instance to a dictionary.r"   r#   r$   r'   r:   r7   s    r)   to_dictzSecretConfig.to_dict_   s,     				--	 r+   NNN)r-   N__name__
__module____qualname____doc__r   strr*   classmethodr   r   r3   r8   r;    r+   r)   r!   r!   <   s    : ! !	
SM
 SM
 c]	
 4S> n  )tCH~ r+   r!   c                       e Zd ZdZ	 	 	 ddee   dee   dee   fdZedeee	f   dd fd	       Z
dd
Zdeee	f   fdZy)Configz2Represents the config configuration for a service.Nr"   r#   targetc                 <    || _         || _        || _        d | _        y r&   r"   r#   rG   bucket_name)r(   r"   r#   rG   s       r)   r*   zConfig.__init__l   s!     DIDIDK&*Dr+   r,   r-   c                      | |j                  d      |j                  d      |j                  d            }|j                  d      |_        |S )Nr"   r#   rG   )r"   r#   rG   rJ   )r/   rJ   r0   s      r)   r3   zConfig.from_dictw   sH    XXfXXfxx!F
 -0FMr+   c                 :   t        j                  d| j                         |j                          |j                  | _        | j
                  }|rt        j                  j                  |      s&t        j                  d| d| j                   d      t        j                  j                  |      }dj                  d| j                  |g      }t        j                  j                  |      r|j                  ||       y	t        j                  d| d      )
z1Handles the creation of resources for the config.Handling config: %szConfig source 'z' for config '' does not exist./configszConfig source path '' is not a file.N)r   r5   r"   ensure_bucketrJ   r#   ospathexistsr   Errorbasenamejoinisfileupload_file)r(   gcs_handlersourcesource_basenamegcs_paths        r)   r8   zConfig.handle   s    II#TYY/"..DYYF/F8>$))<M
N  gg&&v.OxxDII?@H	ww~~fh/3F8;KLMMr+   c                 `    | j                   | j                  | j                  | j                  dS )z5Serializes the ConfigConfig instance to a dictionary.rI   rI   r7   s    r)   r;   zConfig.to_dict   s,     				++''	 r+   r<   r[   
GcsHandlerr-   Nr=   rD   r+   r)   rF   rF   i   s    : ! "		+SM	+ SM	+ sm		+ 4S> h  N&tCH~ r+   rF   c                   ~    e Zd ZdZ	 ddee   dee   fdZedeee	f   dd fd       Z
d	d
deddfdZdeee	f   fdZy)BindMountConfigz6Represents the bind mount configuration for a service.Nr\   rG   c                 .    || _         || _        d | _        y r&   r\   rG   mount_source)r(   r\   rG   s      r)   r*   zBindMountConfig.__init__   s     DKDK'+Dr+   r,   r-   c                      | |j                  d      |j                  d            }|j                  d      |_        |S )Nr\   rG   )r\   rG   rf   )r/   rf   r0   s      r)   r3   zBindMountConfig.from_dict   s<    xx!xx!F
 ((>2FMr+   r[   ra   service_namec                    |j                          | j                  }|rt        j                  j	                  |      st        j                  d| d| d      t        j                  j                  |      t        j                         k(  rdj                  d|g      }n3t        j                  j                  |      }dj                  d||g      }|| _        t        j                  j                  |      r|j                  ||       yt        j                  j                  |      r|j                  ||       yt        j                  d| d      )	z5Handles the creation of resources for the bind mount.zBind mount source 'z' for service 'rN   rO   bind_mountsSource path 'z' is not a file or directory.N)rR   r\   rS   rT   rU   r   rV   abspathgetcwdrX   rW   rf   isdirupload_directoryrY   rZ   )r(   r[   rh   r\   r^   r]   s         r)   r8   zBindMountConfig.handle   s   [[F/x|n E   
wwv"))+-=,78h((0o=,HIh D	ww}}V""8V4		h/&!>
? r+   c                 J    | j                   | j                  | j                  dS )z8Serializes the BindMountConfig instance to a dictionary.re   re   r7   s    r)   r;   zBindMountConfig.to_dict   s%     ++++)) r+   NNr=   rD   r+   r)   rc   rc      s    > CG,SM,2:3-, 4S> .?   C D 6tCH~ r+   rc   c                   ^    e Zd ZdZd	dee   fdZedeee	f   dd fd       Z
deee	f   fdZy)
NamedVolumeConfigz8Represents the named volume configuration for a service.Nr"   c                     || _         y r&   r"   )r(   r"   s     r)   r*   zNamedVolumeConfig.__init__   s	    DIr+   r,   r-   c                 2     | |j                  d            S )Nr"   ru   r/   )r1   r,   s     r)   r3   zNamedVolumeConfig.from_dict   s    XXf r+   c                     d| j                   iS )z:Serializes the NamedVolumeConfig instance to a dictionary.r"   ru   r7   s    r)   r;   zNamedVolumeConfig.to_dict   s     			 r+   r&   )r>   r?   r@   rA   r   rB   r*   rC   r   r   r3   r;   rD   r+   r)   rs   rs      sT    @8C=  4S> .A  
tCH~ r+   rs   c                   j    e Zd ZdZdedefdZd ZdefdZdedd	fd
Zdededd	fdZ	dededd	fdZ
y	)ra   z-Handles GCS operations for compose resources.compose_project_nameregionc                     t        j                  d||       || _        || _        t	        j
                         | _        d | _        y )Nz3Initializing GcsHandler for project %s in region %s)r   r5   rz   r{   r   StorageClient_gcs_clientrJ   )r(   rz   r{   s      r)   r*   zGcsHandler.__init__   sA    II=
 !5DDK"002D&*Dr+   c                 ~    | j                   1| j                         | _         | j                  | j                          yy)zEnsures the GCS bucket exists.N)rJ   _generate_bucket_name_ensure_bucket_existsr7   s    r)   rR   zGcsHandler.ensure_bucket   s8    335d
  !1!12  r+   r-   c                     t               }| j                  j                         }t        j                  dd|      }t        j                  dd|      }| d| d| j
                   dS )z7Generates a unique bucket name for the compose project.z[^a-z0-9-]+-z-+z-compose)_get_project_numberrz   lowerresubr{   )r(   project_numbersanitized_project_names      r)   r   z GcsHandler._generate_bucket_name   sg    (*N!66<<>VVNC9OPVVE30FGQ56a}HMMr+   rJ   Nc                    	 | j                   j                  || j                         t        j                  d| d| j                   d       	 t               }t        j                  |      }| j                   j                  |      }|j                  d      j                  }t        j                  ||d	| d
       | j                   j!                  ||       t        j                  d| d| d       y# t
        $ r }t        j                  d| d|       d}~ww xY w# t
        $ r }t        j                  d| d|       d}~ww xY w)z?Creates the GCS bucket if it doesn't exist and sets IAM policy.)locationzEnsured bucket 'z' exists in region ''.zFailed to create bucket '': NbindingsserviceAccount:zroles/storage.objectUserz!Set roles/storage.objectUser for z on bucket 'z$Failed to set IAM policy on bucket ')r~   CreateBucketIfNotExistsr{   r   r5   	Exceptionr   rV   _get_compute_service_accountr   BucketReferenceGetIamPolicyfield_by_namemessage_typer   AddBindingToIamPolicySetIamPolicy)r(   rJ   eservice_accountbucket_resourcepolicybinding_classs          r)   r   z GcsHandler._ensure_bucket_exists  s_   

..
 /  
ii[M)=dkk]"
M46o$44[Ao,,_=f**:6CCm$$

O,
-
$	 ##OV<	ii-o-> ?m2%  %k]#aS
9 ,  0S
D s1   AC9 B(D% 9	D"DD"%	E.E		Er^   source_pathc           	      j   t         j                  j                  |      st        j                  d| d      t        j
                  |      D ]  \  }}}|D ]  }t         j                  j                  ||      }t         j                  j                  ||      }dj                  ||j                  t         j                  d      g      }	t        j                  | j                  |	      }
| j                  j                  ||
         t        j                   d| d| j                   d| d       y)zUploads a directory to GCS.rk   z' is not a directory.rO   zUploaded directory '' to 'gs://'N)rS   rT   rn   r   rV   walkrX   relpathreplacesepr   ObjectReferencerJ   r~   CopyFileToGCSr   r5   )r(   r^   r   root_files_in_dir	file_name
local_filerelative_pathgcs_file_path
object_refs              r)   ro   zGcsHandler.upload_directory*  s    77==%}[M9NOPP!#!5a#)WW\\$	2

K@},,RVVS9:
 "11$2B2BMR
&&z:> $ "6 II
{m ,""#1XJa	1r+   c           	      >   t         j                  j                  |      st        j                  d| d      t        j                  | j                  |      }| j                  j                  ||       t        j                  d| d| j                   d| d       y)zUploads a single file to GCS.rk   rQ   zUploaded file 'r   rO   r   N)rS   rT   rY   r   rV   r   r   rJ   r~   r   r   r5   )r(   r^   r   r   s       r)   rZ   zGcsHandler.upload_fileA  s    77>>+&}[M9IJKK--d.>.>IJ"";
;II
+k$2B2B1C1XJaPr+   )r>   r?   r@   rA   rB   r*   rR   r   r   ro   rZ   rD   r+   r)   ra   ra      sz    5	+3 	+ 	+3NS N"s "t "H  	.# C D r+   ra   c                       e Zd ZdZ	 	 ddeeeee   f      deeee	f      fdZ
edeeef   dd fd       Zdd	Zdeeef   fd
Zy)VolumeConfigzARepresents the volume configuration for a docker compose project.N
bind_mountnamed_volumec                 >    ||ni | _         ||ni | _        d | _        y r&   r   r   rJ   )r(   r   r   s      r)   r*   zVolumeConfig.__init__O  s)    
 %/$:jDO(4(@bD&*Dr+   r,   r-   c                 r   |j                  di       j                         D ci c])  \  }}||D cg c]  }t        j                  |       c}+ }}}}|j                  di       j                         D ci c]  \  }}|t        j                  |       }}} | ||      S c c}w c c}}}w c c}}w )z2Creates a VolumeConfig instance from a dictionary.r   r   )r   r   )r/   itemsrc   r3   rs   )r1   r,   keyvalueitemr   r   s          r)   r3   zVolumeConfig.from_dictX  s    
 ((<4::<<JC 	%@%$o''-%@@<   ((>26<<>>JC 	((//>   *<@@ As   B,B'B,: B3'B,c                 l   | j                   s"| j                  st        j                  d       yt        j                  d       |j	                          |j
                  | _        | j                   j                         D ]  \  }}|D ]  }|j                  ||          t        j                  d       y)z"Handles all volume configurations.zNo volumes to handle.NzHandling volume configurations.zVolume handling complete.)r   r   r   r5   rR   rJ   r   r8   )r(   r[   rh   rj   	bm_configs        r)   r8   zVolumeConfig.handlee  s    ??4#4#4	ii'(II/0"..D &*__%:%:%<!k")l3 # &= II)*r+   c                 N   | j                   j                         D ci c]$  \  }}||D cg c]  }|j                          c}& c}}}| j                  j                         D ci c]  \  }}||j                          c}}| j                  dS c c}w c c}}}w c c}}w )z5Serializes the VolumeConfig instance to a dictionary.r   )r   r   r;   r   rJ   )r(   r   r   r   s       r)   r;   zVolumeConfig.to_dictw  s    
 #oo335
5
U U3UT$,,.U335

 483D3D3J3J3L
3LZS%C 3L
 ''	 	3

s   BBB)B!Brq   r`   )r>   r?   r@   rA   r   r   rB   listrc   rs   r*   rC   r   r3   r8   r;   rD   r+   r)   r   r   L  s    I @D=A+4T/%: :;<+ T#'8"89:+ 
A4S> 
An 
A 
A+$tCH~ r+   r   c                      e Zd ZdZ	 	 	 	 	 ddeeeej                  f      deeee	f      dee
   dee   deee      f
dZed	ed
d fd       Zedeeef   d
d fd       Z	 ddededej&                  ded
d f
dZd
eeef   fdZd
efdZy)ResourcesConfigz?Represents the resources config sent form runcompose go binary.Nsource_buildssecretsvolumesprojectrP   c                     ||ni | _         ||ni | _        ||n	t               | _        || _        ||| _        y g | _        y r&   )r   r   r   r   r   rP   )r(   r   r   r   r   rP   s         r)   r*   zResourcesConfig.__init__  sJ     +8*CD%17rDL%17|~DLDL%17DLrDLr+   	json_datar-   c                 R    t        j                  |      }| j                  |      }|S )z<Parses the JSON string to create a ResourcesConfig instance.jsonloadsr3   )r1   r   r,   r2   s       r)   	from_jsonzResourcesConfig.from_json  s%     ::i D]]4 FMr+   r,   c                 :   |j                  di       j                         D ci c]%  \  }}|t        j                  j	                  |      ' }}}|j                  di       j                         D ci c]  \  }}|t
        j	                  |       }}}t        j	                  |j                  di             }|j                  dg       D cg c]  }t        j	                  |       }}|j                  d      }	 | ||||	|      S c c}}w c c}}w c c}w )z5Creates a ResourcesConfig instance from a dictionary.r   r   r   rP   r   )r   r   r   r   rP   )r/   r   r   BuildConfigr3   r!   r   rF   )
r1   r,   r   r   r   r   r   r   rP   r   s
             r)   r3   zResourcesConfig.from_dict  s'   
 ((?B7==??JC 	W  **511?   ((9b17799JC 	\##E**9   $$TXXi%<=G+/88Ir+B+B4+B   hhy!G# 
s   *D4 DDr{   repor   no_buildc                    t         j                  j                  j                  j	                  d      }t        j                  |ddg       | j                  rFt        j                  |g d       t        j                  | j                  || j                  |||       t        j                  d| j                         | j                  r|j                  t        j                  j                   j#                                t        j                  |ddg       | j                  j%                         D ]-  \  }}t        j                  d	|        |j                          / |j'                  t        j                  j                   j#                                | j(                  j*                  s#| j(                  j,                  s| j.                  rt        j                  d
       t1        | j                  |      }| j(                  j*                  s| j(                  j,                  r|j                  t        j                  j2                  j#                                t        j                  |ddg       | j(                  j                  |       |j'                  t        j                  j2                  j#                                | j.                  r|j                  t        j                  j4                  j#                                | j.                  D ]3  }	t        j                  d|	j6                         |	j                  |       5 |j'                  t        j                  j4                  j#                                | S )a  Creates or updates all resources defined in the configuration.

    This method orchestrates the handling of each type of resource,
    such as secrets, by calling their respective handle() methods.

    Args:
      region: The region of the compose project.
      repo: The repo of the compose project.
      tracker: The progress tracker to use for handling the resources.
      no_build: If true, skip building from source.

    Returns:
      The ResourcesConfig instance after handling the resources.
    Trequiredzrun.googleapis.comz#cloudresourcemanager.googleapis.com)zcloudbuild.googleapis.comstorage.googleapis.comzartifactregistry.googleapis.comz*Starting resource handling for project: %szsecretmanager.googleapis.comziam.googleapis.comzHandling secret: z4Initializing GCS handler for volumes and/or configs.r   rM   )r   VALUEScorer   Getr   check_and_enable_apisr   r   r8   r   r5   r   
StartStagecompose_trackerStagedProgressTrackerStageSECRETSget_keyr   CompleteStager   r   r   rP   ra   VOLUMESCONFIGSr"   )
r(   r{   r   r   r   
project_idr"   secret_configr[   r2   s
             r)   handle_resourcesz ResourcesConfig.handle_resources  s   * ""''//33T3BJ%%	DE
 ''
 nn




,,


 II:DLLI||

4
4
<
<
D
D
F ''
)+?
@ "&!3!3!5
$		%dV,- "6 

4
4
<
<
D
D
F ||$,,";";t||	iiFGt||V4k		 	 DLL$=$=66>>FFH	
 	))13GH	
 	K(66>>FFH	
 
66>>FFH	
 llF
)))6;;
7
--
$ # 	66>>FFH	
 Kr+   c           	         | j                   | j                  j                         D ci c]  \  }}||j                          c}}| j                  j                         D ci c]  \  }}||j                          c}}| j
                  j                         | j                  D cg c]  }|j                          c}dS c c}}w c c}}w c c}w )z8Serializes the ResourcesConfig instance to a dictionary.)r   r   r   r   rP   )r   r   r   r;   r   r   rP   )r(   r"   buildsecretcs        r)   r;   zResourcesConfig.to_dict  s     <<595G5G5M5M5O
5OkdED%--/!5O
 8<||7I7I7K
7K|tVD&..""7K
 <<''))-6AAIIK6
 


 7s   C$C
)Cc                 H    t        j                  | j                               S )z9Serializes the ResourcesConfig instance to a JSON string.r   dumpsr;   r7   s    r)   to_jsonzResourcesConfig.to_json      ::dlln%%r+   )NNNNN)F)r>   r?   r@   rA   r   r   rB   r   r   r!   r   r   rF   r*   rC   r   r   r3   r   StagedProgressTrackerboolr   r;   r   rD   r+   r)   r   r     s8   G AE37(,#(,:d3(;(;#;<=: S,./0: %	:
 }: V%:  (9   4S> .?  8 UU U  55	U
 U UntCH~ &s &r+   r   c                       e Zd ZdZ	 	 ddeeeef      deeeef      fdZededd fd       Z	ed	eee
f   dd fd
       ZdefdZdeee
f   fdZy)TranslateResultzBRepresents the translate command result from runcompose go binary.Nservicesmodelsc                 <    ||ni | _         ||| _        y i | _        y r&   r   r   )r(   r   r   s      r)   r*   zTranslateResult.__init__#  s$    
 !) 4H"DM".&DKBDKr+   r   r-   c                 N    t        j                  |      }| j                  |      S )z<Parses the JSON string to create a TranslateResult instance.r   )r1   r   r,   s      r)   r   zTranslateResult.from_json+  s!     ::i D==r+   r,   c                 ^    |j                  di       }|j                  di       } | ||      S )z5Creates a TranslateResult instance from a dictionary.r   r   r   rw   )r1   r,   r   r   s       r)   r3   zTranslateResult.from_dict1  s1     xx
B'HXXh#F00r+   c                 H    t        j                  | j                               S )z9Serializes the TranslateResult instance to a JSON string.r   r7   s    r)   r   zTranslateResult.to_json8  r   r+   c                 4    | j                   | j                  dS )z8Serializes the TranslateResult instance to a dictionary.r   r   r7   s    r)   r;   zTranslateResult.to_dict<  s     MM++ r+   rq   )r>   r?   r@   rA   r   r   rB   r*   rC   r   r   r3   r   r;   rD   r+   r)   r   r      s    J ,0)-7c3h(7 tCH~&7  (9  
 14S> 1.? 1 1&s &tCH~ r+   r   r2   r-   c           	      t   | j                   r| j                  r| j                  st        d      t        j
                  j                  | j                        st        d| j                         t        j                         }t        j                  j                  j                  j                  d      }t        j                  j!                  | j                   d|id      }|j#                  |      at%        j&                  d	| j                    d
| d       	 |j)                  |dddd       t%        j&                  d| j                    d       n#t%        j&                  d| j                    d       	 t/               }|j1                  |      }d| }d}t3        j4                  |||      so|j7                  d      j8                  }	t3        j:                  |	|||       |j=                  ||       t%        j&                  d| d| d| j                    d       n(t%        j&                  | d| d| j                    d       	 t%        j&                  d| j                   d| j                   d       tC        jD                  | j                        }
t%        j&                  d| j                    d       |jG                  ||
d      }|j                   | _$        t%        j&                  d| jH                   d | j                    d       y# t*        $ r+}t%        j,                  d| j                    d|         d}~ww xY w# t*        $ r*}t?        j@                  d| j                    d|       d}~ww xY w# t*        $ r+}t%        j,                  d!| j                    d|         d}~ww xY w)"zDCreates a secret if it doesn't exist and adds a version from a file.z3Secret name, file path and mount name are required.zSecret file not found: Tr   
projectsIdzsecretmanager.projects.secretsparams
collectionNzCreating secret 'z' in project 'r   	automatic)r   	locationslabelstagszSecret 'z
' created.zFailed to create secret 'r   z' already exists.r   z"roles/secretmanager.secretAccessorr   zSet z for z on secret 'z already exists on secret 'z$Failed to set IAM policy on secret 'zReading secret content from 'z' for secret 'zAdding new version to secret ')data_crc32czAdded secret version 'z' to secret 'z!Failed to add version to secret ')%r"   r#   r$   
ValueErrorrS   rT   rU   secrets_apiSecretsr   r   r   r   r   r   REGISTRYParse	GetOrNoner   r5   Creater   errorr   r   r   BindingInPolicyr   r   r   r   r   rV   r   ReadBinaryFileContents
AddVersionr'   )r2   secrets_clientr   
secret_refr   r   r   memberroler   secret_dataversions               r)   r6   r6   D  so    
FKKv||
J
KK		$
.v{{m<
==&&(.""**...='!!''kkG$1 ( * j)1II!&++nWIRHI
   
ii(6;;-z23
 II%67824O((4F/0F/D##FFD9**:6CCm$$



	 !!*f5	ii$tfE/!2,v{{m2NO	iiF%( ){{m2
II
'} 5\\N"	 ..v{{;KII.v{{m2>? ''KT ( G $LLFII
 !6!6 7 8[[M	_  	ii+FKK=A3?@6 
 



.v{{m3qcB 0 
 
II1&++c!EF	
sJ   9L =CM CN 	M
&MM
	N %M;;N 	N7&N22N7c                      t         j                  j                  j                  j	                  d      } t
        j                  j                  | d      }t        j                  |      }t        |j                        S )z5Retrieves the project number for the current project.Tr   zcloudresourcemanager.projects)r   )r   r   r   r   r   r   r  r  r   rB   projectNumber)r   project_refr   s      r)   r   r     sm       %%--1141@*""((< ) + [)'	W""	##r+   yaml_file_pathr{   argsc                    t         j                  j                  j                  j	                  d      }t
        j                  j                  d|  d| d| d       t        j                  t        j                  t        j                        }	 t        j                  |       }|st        j                   d|  d      	 d
	 t%        j&                  ||j(                        }t+        j(                  ||      rj0                  st        j                   d      t2        j4                  j7                  j8                  j0                  d|id      }t;        j<                  d
t>        j@                  |      }	tC        jD                  |	      5 }
|
jG                  |      }tI        jJ                        g}tM        jN                  ||
||       }|s|sd
}|sdj0                   dndj0                   d}tQ        jR                  |tU        jV                  |d
u      ddfd      5 }|
jY                  ||tZ        j\                  j^                  |d|d|      }d
d
d
       d
d
d
       r[|j                  rN|j                  j`                  r7t
        j                  j                  d|j                  j`                          y
y
y
y
# t"        j                   t        j                   f$ r }t        j                   d|  d	|       d
}~ww xY w# t$        j,                  $ r"}t        j.                  |d       Y d
}~Gd
}~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w)zDeploys a Cloud Run application from a YAML file.

  Args:
    yaml_file_path: The path to the Cloud Run service YAML file.
    region: The region to deploy the application to.
    args: The arguments passed to the command.
  Tr   zDeploying application from 'z' project 'z' in region 'r   zCould not parse YAML file 'z#Failed to read or parse YAML file 'r   NzPlease make sure that the YAML file matches the Knative service definition spec in https://kubernetes.io/docs/reference/kubernetes-api/service-resources/service-v1/#Service.)	help_textz)Service name is missing in the YAML file.namespacesIdzrun.namespaces.servicesr   )platformregion_labelzDeploying service 'z'...zUpdating service 'zDeployment failedFc                  $    d j                    dS )Nz	Service 'z' has been deployed.ru   )new_services   r)   <lambda>z$deploy_application.<locals>.<lambda>  s    	+2B2B1CCW&Xr+   )failure_messagesuppress_outputdone_message_callback)asynallow_unauthenticatedfor_replaceprefetchzService URL: )1r   r   r   r   r   r   statusPrintr   GetMessagesModuler   SERVERLESS_API_NAMESERVERLESS_API_VERSIONr   	load_pathr   rV   r   messages_utilDictToMessageWithErrorCheckServicer	   ScalarTypeMismatchErrorMaybeRaiseCustomFieldMismatchr"   r   r  r  metadatar   GetConnectionContextr   PLATFORM_MANAGEDr   Connect
GetServicer   ReplaceServiceChange	run_flagsGetAllowUnauthenticatedr   r   r   ServiceStagesReleaseServicer   ReleaseTrackALPHAurl)r  r{   r  r   run_messagesservice_dictr   raw_serviceservice_refconn_contextclientexisting_servicechangesr%  headerr   deployed_servicer  s                    @r)   deploy_applicationrJ    sQ    ""**...='**%n%5]7) LXS"
 ''((++,
>>.1L:>:J"MNN  +;;l**K //+|<K 
K,,


F
GG""((g&* ) +
 $88
)),
 $$\2f((522;?@G%==fk'7#7
 $9"   {//06";#3#3"4E:  
	/	/2$>?+X
 
..





!
!
 5# / 	
# 3F 

!
!

!
!
%
%JJ}%5%<%<%@%@$ABC & " M ++tzz	" 



-n-=SD  
	.	. 	,,	 	R
 
# 32sU   0K 6L BM-2MM#L0LLM&MMM	MM c                       t               } |  dS )zFRetrieves the default compute service account for the current project.z&-compute@developer.gserviceaccount.com)r   )r   s    r)   r   r   
  s    &(.A	BBr+   )?rA   r   rS   r   typingr   r   r   +googlecloudsdk.api_lib.cloudresourcemanagerr   googlecloudsdk.api_lib.runr   r   r	   googlecloudsdk.api_lib.secretsr
   r  googlecloudsdk.api_lib.storager   r   googlecloudsdk.api_lib.utilr   r   r.  googlecloudsdk.callioper   googlecloudsdk.command_lib.iamr   googlecloudsdk.command_lib.runr   r   r   r9  r   r   r   &googlecloudsdk.command_lib.run.composer   r   r   googlecloudsdk.corer   r   r   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   r!   rF   rc   rs   ra   r   r   r   r6   rB   r   rJ  r   rD   r+   r)   <module>rY     s#    	 	 & & D 2 5 . = 6 7 , A ( 3 9 = = 4 @ 1 : M * # * ) $ 8 ** *Z2 2j5 5p &` `F6 6rX& X&v! !HT
T
	T
n$S $aDs aDC aDs aDt aDHCc Cr+   