
    d4                     R   d 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 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dZdZ ej<                  ej>                  j@                  ej>                  jB                        ejD                   G d dejF                                      Z$ ej<                  ej>                  jJ                        ejD                   G d de$                    Z&d Z'y)z2Rollback a Cloud Deploy target to a prior rollout.    N)
exceptions)client_util)release)base)delivery_pipeline_util)deploy_policy_util)deploy_util)flags)promote_util)release_util)resource_args)rollout_util)target_util)	resources)
console_ioz{description}z   To rollback a target 'prod' for delivery pipeline 'test-pipeline' in region 'us-central1', run:

  $ {command} prod --delivery-pipeline=test-pipeline --region=us-central1


)DESCRIPTIONEXAMPLESrollbackc                   h    e Zd ZdZeZed        Z ej                  e
j                        d        Zy)RollbackRollbacks a target to a prior rollout.

  If release is not specified, the command rollbacks the target with the last
  successful deployed release. If optional rollout-id parameter is not
  specified, a generated rollout ID will be used.
  c                    t        j                  | d       t        j                  | d       t        j                  |        t        j
                  |        t        j                  |        t        j                  | t               t        j                  | t               t        j                  |        t        j                  |        y )NT)
positionalz#Name of the release to rollback to.)r   AddTargetResourceArgr
   
AddReleaseAddRolloutIDAddDeliveryPipelineAddDescriptionFlagAddAnnotationsFlag	_ROLLBACKAddLabelsFlagAddStartingPhaseIdAddOverrideDeployPoliciesparsers    &lib/surface/deploy/targets/rollback.pyArgszRollback.ArgsB   s    &&v$?	VBC	v	f%	V$	VY/		*	V$	##F+    c                    |j                   j                  j                         }|j                         }t        j
                  j                  |j                  d|d   |d   |j                  d      }t        j                  |j                               }d}t        j                  ||       t        j                  |       t        |j                  ||      \  }}t        j                         j!                  |j                               }	t#        j$                  |	d       |	j&                  r&d}
t)        j*                  |
|j                               dj-                  |j/                         |j/                               }t#        j0                  ||	|j/                         |       t3        j4                  d	
       |j6                  xs dj-                  |j/                               }t9        j:                  ||j<                        }t?        j@                  ||	|j/                         d|jB                  |jD                  |jF                  ||jH                  xs d|
      S )N0clouddeploy.projects.locations.deliveryPipelines
projectsIdlocationsIdr+   r,   deliveryPipelinesId
collectionparamsCannot perform rollback.roll back this target'Rolling back target {} to release {}.

Tcancel_on_noRollback from {}Fstable)
rollout_idannotationslabelsdescriptionstarting_phase_idoverride_deploy_policies)%CONCEPTStargetParseAsDictr   REGISTRYdelivery_pipeliner   GetPipelineRelativeNameThrowIfPipelineSuspendedr   	GetTarget_GetCurrentAndRollbackReleaser   ReleaseClientGetr   CheckReleaseSupportState	abandoneddeploy_exceptionsAbandonedReleaseErrorformatName	PrintDiffr   PromptContinuer<   r   CreateDeployPolicyNamesFromIDsr>   r   Promoter9   r:   r;   r=   )selfargs
target_refref_dictpipeline_refpipeline_obj'failed_activity_error_annotation_prefixcurrent_release_refrollback_release_refrelease_objerror_msg_annotation_prefixpromptrollout_descriptionpoliciess                 r&   RunzRollback.RunN   s4    %%++-J  "H%%++E"<0#M2#'#9#9
 , L *55!!#L /I+33= *%0MlJ1-- '')--))+K ))+7NO$>!33
%';'H'H'J  9??/446F k:??+<f 40** .@.G.G  "/
 "@@d33H ??$${{'00<H!) r(   N)__name__
__module____qualname____doc___DETAILED_HELPdetailed_helpstaticmethodr'   gcloud_exception CatchHTTPErrorRaiseHTTPExceptionrN   HTTP_ERROR_FORMATrd    r(   r&   r   r   6   sM     !-	, 	, 544))CCr(   r   c                   &    e Zd ZdZed        Zd Zy)RollbackAlphar   c                 X    t         j                  |        t        j                  |        y )N)r   r'   r
   AddRollbackOfRolloutr$   s    r&   r'   zRollbackAlpha.Args   s     MM&	v&r(   c                 z
   |j                   j                  j                         }|j                         }t        j
                  j                  |j                  d|d   |d   |j                  d      }t        j                  |j                               }d}t        j                  ||       t        j                         j                  |j                        }t        j                   t        j                         |t        j"                  j$                  |j&                  |j(                         t+        j,                  t        j.                  |j                         |j1                         d|j2                  |j4                  |j6                  ||j8                  	            }|j2                  }	t        j
                  j;                  t        j
                  j                  |j<                  j>                  j@                  d
      jC                         j                         d      }
|j2                  stE        jF                  |j1                         |
      }	|
j                         }t        j
                  j                  |	d
|jI                  d      |jI                  d      |jI                  d      |
j1                         d      }|j                         |j<                  j>                  _         |j                  st        j
                  j;                  t        j
                  j                  |j<                  j>                  jJ                  d
      jC                         j                         d      }djM                  |j1                               |j<                  j>                  _        	 t7        jN                         jQ                  |
j                               }t[        j\                  |d       djM                  |j1                         |
j1                               }t[        j^                  |
||j1                         |       ta        jb                  d       t        j.                  |j                         |j1                         d|	|
j1                         |j<                  j>                  jJ                  |j<                  j>                  |j<                  jd                        }|j<                  j>                  S # tR        jT                  $ r}tW        jX                  |      d }~ww xY w)Nr*   r+   r,   r-   r/   r2   )r<   T)validate_onlyr9   rollout_to_rollback
release_idrollout_objstarting_phaseBclouddeploy.projects.locations.deliveryPipelines.releases.rolloutsr0   9clouddeploy.projects.locations.deliveryPipelines.releasesr.   r+   r,   r.   
releasesIdr7   r3   r4   r5   F)ru   r9   rw   rv   rx   ry   )3r?   r@   rA   rB   r   rC   rD   r   rE   rF   rG   r   GetMessagesModuleRolloutr<   r	   SetMetadataResourceTypeROLLOUTr:   r;   copydeepcopyCreateRollbackTargetrQ   r9   rollback_of_rolloutr   r=   ParseRelativeNamerollbackConfigrolloutnameParentr   GenerateRolloutIdgetrollbackOfRolloutrP   rJ   rK   apitools_exceptions	HttpErrorr   HttpExceptionr   rL   rR   r   rS   startingPhaseId)rV   rW   rX   rY   rZ   r[   r\   rx   validate_responsefinal_rollout_idr^   resource_dictnew_rollout_refr]   r_   errorra   create_responses                     r&   rd   zRollbackAlpha.Run   sd   %%++-J  "H%%++E"<0#M2#'#9#9
 , L *55!!#L /I+33= //199$$ : K %%'  (( 33%%'OO $ 8 8||#11		
 $--??  ,,4499[ 	! 	
 
	N @   ??%77
//
1 +113m!**00
Y)--l;*..}=%2%6%67L%M0557	 1 	o 
&
&
( &&..3
 %..@@



"
"..66HH] #  68<>I A 
 
#
#$7$<$<$>
? &&..:,))+//

+
+
-k ))+7NO 9??/446F k:??+<f 40,AA!!##',,.-<<DDVV%44<<(77GGO ))1119 (( ,$$U++,s   91T T: T55T:N)re   rf   rg   rh   rk   r'   rd   ro   r(   r&   rq   rq      s!     ' '
C2r(   rq   c           	         | r|j                         }t        j                  ||      }t        j                  j                  t        j                  j                  |j                  d      j                         j                         d      }t        j                  j                  | d|d   |d   |j                         | d      }||fS t        j                  ||      }t        |      dk  r2t        j                  d	j!                  |j                                     |\  }}	t        j                  j                  t        j                  j                  |j                  d      j                         j                         d      }t        j                  j                  t        j                  j                  |	j                  d      j                         j                         d      }||fS )
zrGets the current deployed release and the release that will be used by promote API to create the rollback rollout.rz   r{   r|   r+   r,   r}   r/      z>unable to rollback target {}. Target has less than 2 rollouts.)rB   r   GetCurrentRolloutr   rC   r   rA   r   r   rF   rQ   r   GetValidRollBackCandidatelencore_exceptionsErrorrP   )
rw   rZ   rX   rY   current_rolloutr]   r^   prior_rolloutscurrent_deployed_rolloutprevious_deployed_rollouts
             r&   rI   rI   -  s     "H!33JMO#,,>>    [ 	! 	
 
	N ?  %--33N"<0#M2#/#4#4#6$	
 4 	  444!;;LN >Q!!
J6*//#$  ;I77#,,>>  $))[ 	! 	
 
	N ?  %--??  %**[ 	! 	
 
	N @   444r(   )(rh   r   apitools.base.pyr   r   "googlecloudsdk.api_lib.clouddeployr   r   googlecloudsdk.api_lib.utilrl   googlecloudsdk.callioper   !googlecloudsdk.command_lib.deployr   r   r	   rN   r
   r   r   r   r   r   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   ri   r    ReleaseTracksReleaseTrackBETAGADefaultUniverseOnlyCreateCommandr   ALPHArq   rI   ro   r(   r&   <module>r      s   9  > : 6 F ( . D @ 9 M 3 : : ; : 9 = ) 2 #	 	 D%%**D,=,=,@,@A\t!! \  B\~ D%%++,Q2H Q2  -Q2h65r(   