
    %                        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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% ejL                   ejN                  ejP                  jR                  ejP                  jT                         G d dejV                                      Z, ejN                  ejP                  jZ                         G d de,             Z.e,j                   e._         y )!z;Command for updating env vars and other configuration info.    )absolute_import)division)print_function)unicode_literals)global_methods)service)apis)messages)arg_parsers)base)util)config_changes)connection_context)
exceptions)flags)messages_util)	platforms)pretty_print)resource_args)serverless_operations)stages)concept_parsers)presentation_specs)
properties)	resources)progress_trackerc                   X    e Zd ZdZdddZed        Zed        Zd Zd Z	d	 Z
d
 Zd Zy)Replacez>Create or replace a service from a YAML service specification.zU          Creates or replaces a service from a YAML service specification.
          z          To replace the specification for a service defined in myservice.yaml

              $ {command} myservice.yaml

         )DESCRIPTIONEXAMPLESc                    t        j                  dt        j                         dddd      }t	        j
                  |g      j                  |       t        j                  |       t        j                  |       t        j                  |       |j                  ddt        j                         d	       |j                  j                  d
       y )Nz--namespacezNamespace to replace service.TF)requiredprefixeshiddenFILEstoreziThe absolute path to the YAML file with a Knative service definition for the service to update or deploy.)actiontypehelpnone)r   ResourcePresentationSpecr   GetNamespaceResourceSpecr   ConceptParserAddToParserr   AddAsyncFlagAddClientNameAndVersionFlagsAddDryRunFlagadd_argumentr   YAMLFileContentsdisplay_info	AddFormat)clsparsernamespace_presentations      #lib/surface/run/services/replace.py
CommonArgszReplace.CommonArgsA   s     0HH..0' !!#9 #$ %%0[%8 
v	&&v.	
))+B	  C !!&)    c                 &    | j                  |       y N)r:   r6   r7   s     r9   ArgszReplace.Args\   s    NN6r;   c                     t        j                  |t        j                  j                  | j                         |      S )N)region_label)r   GetConnectionContextr   ProductRUNReleaseTrack)selfargsrA   s      r9   _ConnectionContextzReplace._ConnectionContext`   s1    22emm!2!2!4< r;   c                 r    t        j                  |      t        j                  | j                               gS r=   )r   ReplaceServiceChangeSetLaunchStageAnnotationChangerE   )rF   new_servicerG   s      r9   _GetBaseChangeszReplace._GetBaseChangese   s3     	++K855d6G6G6IJ r;   c                      y r=    )rF   rG   rL   changess       r9   _GetMultiRegionRegionszReplace._GetMultiRegionRegionsl   s    r;   c                 T   |j                   r0t        j                  dj                  |j                               y |r0t        j                  dj                  |j                               y t        j                  dj                  |j                  |j
                               y )NzPNew configuration for [{{bold}}{serv}{{reset}}] is being applied asynchronously.)servzKNew configuration has been validated for service [{{bold}}{serv}{{reset}}].zdNew configuration has been applied to service [{{bold}}{serv}{{reset}}].
URL: {{bold}}{url}{{reset}})rS   url)async_r   Successformatnamedomain)rF   service_objdry_runrG   s       r9   _PrintSuccessMessagezReplace._PrintSuccessMessageo   s    {{"F(8(8F9 
''-v;3C3Cv'D
 ((.##);); )/ )r;   c                 <   t        j                  t        j                  t        j                        }t        |j                        }d|v r|d= |j                  di       j                  dd      }|!t        |t              st        |      |d   d<   d}	 t        j                  ||j                        }t        j                  ||      }|j"                  j$                  j'                         j)                         }|j*                  j$                  |j-                  d      r.||j*                  j$                  k7  rt        j.                  d      |j*                  j$                  }t1        j2                         t0        j4                  k(  rt6        j8                  j:                  j<                  j?                         }tA        jB                  |      }	||k7  r3|t        |	      k7  r%t        j.                  djE                  ||	            ||j*                  _        | jG                  ||      }
tH        jJ                  j'                  |j*                  jL                  d	|j*                  j$                  id
      }|jN                  r|jP                  nd}| jS                  ||      }tU        |d      r|jV                  nd}|rdnd}tY        jZ                  |      5 }|j]                  |      }| j_                  |||
      }ta        jb                  te        jf                  |||             ti        jj                  |      }|rdnd}|rd}tm        jn                  ||d|jp                  xs |      5 }|js                  ||
| ju                         ||jp                  dd||	      }ddd       | jw                  |||       |cddd       S # t        j                  $ r"}t        j                   |d       Y d}~1d}~ww xY w# 1 sw Y   _xY w# 1 sw Y   yxY w)z#Create or Update service from YAML.statusmetadata	namespaceNzPlease 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_textz7Namespace specified in file does not match passed flag.zVNamespace must be project ID [{}] or quoted number [{}] for Cloud Run (fully managed).namespacesIdzrun.namespaces.services)params
collectionr[   Fz Validating new configuration forzApplying new configuration to)	operation)regions_listzDeploying...zDeploying new service...zValidating...zDeployment failed)failure_messagesuppress_outputT)asynallow_unauthenticatedfor_replacer[   multiregion_regions)<r	   GetMessagesModuler   SERVERLESS_API_NAMESERVERLESS_API_VERSIONdictr%   get
isinstancestrr   DictToMessageWithErrorCheckServicer   ScalarTypeMismatchErrorr   MaybeRaiseCustomFieldMismatchCONCEPTSr`   ParseNamer_   IsSpecifiedConfigurationErrorr   GetPlatformPLATFORM_MANAGEDr   VALUEScoreprojectGetprojects_utilGetProjectNumberrW   rM   r   REGISTRYrX   
is_managedregionrH   hasattrr[   r   Connect
GetServicerQ   r   Inforun_messages_utilGetStartDeployMessager   ServiceStagesr   StagedProgressTrackerrU   ReleaseServicerE   r\   )rF   rG   run_messagesservice_dictr`   rL   raw_serviceer   project_numberrP   service_refrA   conn_contextr[   r'   clientrZ   regionsdeployment_stagesheadertrackers                         r9   RunzReplace.Run   s   ))**--L 		?L
 <
x
    R044[$GIZ	3%?.1)nl:{+K
!==
,,.kOOK>k ''--/446I%%1


;
'
{++55
5++EG 	G&&00i				 I$>$>	>##((00446&77@I^1D$D--++16'>+JL L &/K""";5G$$**!! 4 4 > >?, + .K
 *5)?)?;%%TL**4>L%dI6dllEG  	+,  
	&	&|	4%%k2k++D+wGg

1
1K6 !..GD"-3Mf	 11

-++0	
 ++"& ' , 

" Wd;A 
5	4[ 00 ..
 v  
5	4s=   6O BP53P(PP!O>>PP	PPN)__name__
__module____qualname____doc__detailed_helpclassmethodr:   r?   rH   rM   rQ   r\   r   rO   r;   r9   r   r   .   s[     G- * *4  
(fr;   r   c                       e Zd Zed        Zy)AlphaReplacec                 .    t         j                  |       y r=   )r   r:   r>   s     r9   r?   zAlphaReplace.Args   s    vr;   N)r   r   r   r   r?   rO   r;   r9   r   r      s      r;   r   N)/r   
__future__r   r   r   r   googlecloudsdk.api_lib.runr   r   googlecloudsdk.api_lib.utilr	   r
   r   googlecloudsdk.callioper   r   #googlecloudsdk.command_lib.projectsr   r   googlecloudsdk.command_lib.runr   r   r   r   r   r   r   r   r   r   (googlecloudsdk.command_lib.util.conceptsr   r   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   UniverseCompatibleReleaseTracksrE   BETAGACommandr   ALPHAr   rO   r;   r9   <module>r      s    B &  % ' 5 . , A / ( E 9 = 5 0 M 4 7 8 @ 1 D G * ) 8 D%%**D,=,=,@,@Aydll y B yx D%%++,7  -  r;   