
                            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dZdZ e	j6                  e	j8                  j:                  e	j8                  j<                  e	j8                  j>                        e	j@                   G d de	jB                                      Z"d Z#y)zRedeploy a rollout to a target.    )release)apis)
exceptions)base)delivery_pipeline_util)deploy_policy_util)flags)promote_util)release_util)resource_args)rollout_util)target_util)	resources)
console_ioz{description}z   To redeploy a target `prod` for delivery pipeline `test-pipeline` in region `us-central1`, run:

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

)DESCRIPTIONEXAMPLESrolloutc                   h    e Zd ZdZeZed        Z ej                  e
j                        d        Zy)RedeployzRedeploy the last release to a target.

  Redeploy the last rollout that has a state of SUCCESSFUL or FAILED to a
  target.
  If rollout-id is not specified, a rollout ID will be generated.
  c                 l   t        j                  | d       t        j                  |        t        j                  |        t        j
                  |        t        j                  | t               t        j                  | t               t        j                  |        t        j                  |        y )NT)
positional)r   AddTargetResourceArgr	   AddRolloutIDAddDeliveryPipelineAddDescriptionFlagAddAnnotationsFlag_ROLLOUTAddLabelsFlagAddStartingPhaseIdAddOverrideDeployPolicies)parsers    &lib/surface/deploy/targets/redeploy.pyArgszRedeploy.Args>   sx    &&v$?	v	f%	V$	VX.	)	V$	##F+    c                 8   |j                   j                  j                         }t        j                  |       |j                         }t        j                  j                  |j                  d|d   |d   |j                  d      }t        j                  |j                               }d}t        j                  ||       t        ||t        j                        }t!        j"                         j%                  |j                               }t'        j(                  |d       |j*                  r$t-        j.                  ||j                               dj1                  |j3                         |j3                               }	t5        j6                  |	d	
       t9        j:                  ||j<                        }
t?        j@                  |||j3                         d|jB                  |jD                  |jF                  |jH                  |jJ                  |

       y )Nz0clouddeploy.projects.locations.deliveryPipelines
projectsIdlocationsId)r&   r'   deliveryPipelinesId)
collectionparamszCannot perform redeploy.zredeploy this releasez:Are you sure you want to redeploy release {} to target {}?T)cancel_on_noF)
rollout_idannotationslabelsdescriptionstarting_phase_idoverride_deploy_policies)&CONCEPTStargetParser   	GetTargetAsDictr   REGISTRYdelivery_pipeliner   GetPipelineRelativeNameThrowIfPipelineSuspended_GetCurrentReleaser   !ROLLOUT_IN_TARGET_FILTER_TEMPLATEr   ReleaseClientGetr   CheckReleaseSupportState	abandoneddeploy_exceptionsAbandonedReleaseErrorformatNamer   PromptContinuer   CreateDeployPolicyNamesFromIDsr1   r
   Promoter,   r-   r.   r/   r0   )selfargs
target_refref_dictpipeline_refpipeline_objfailed_redeploy_prefixcurrent_release_refrelease_objpromptpoliciess              r"   RunzRedeploy.RunI   s    %%++-J*%  "H%%++E"<0#M2#'#9#9
 , L *55!!#L 833, -j,"P"P '')--((*K ))+7NO 33
 "5"B"B"D 
 	EKK$$&
(9	
 
 f48 "@@d33H ??$${{$$00!)r$   N)__name__
__module____qualname____doc___DETAILED_HELPdetailed_helpstaticmethodr#   gcloud_exception CatchHTTPErrorRaiseHTTPExceptionrB   HTTP_ERROR_FORMATrT    r$   r"   r   r   0   sK    
 !-, , 544))<<r$   r   c           	      6   t        t        j                  || |t        j                  d            }t	        |      dk  r2t        j                  dj                  |j                                     |d   }t        j                  dd      }|j                  j                  j                  |j                  j                  j                  |j                  j                  j                  g}|j                   |vr9t#        j$                  |j                         |j&                  |j                         t(        j*                  j-                  t(        j*                  j/                  |j&                  d      j1                         j3                         d	      }|S )
a  Gets the current release in the target.

  Args:
    pipeline_ref: pipeline_ref: protorpc.messages.Message, pipeline object.
    target_ref: target_ref: protorpc.messages.Message, target object.
    filter_str: Filter string to use when listing rollouts.

  Returns:
    The most recent release with the given pipeline and target with a rollout
    that is redeployable.

  Raises:
    core.Error: Target has no rollouts or rollouts in target are not
                redeployable.
     )rK   rM   
filter_strorder_bylimitz8unable to redeploy to target {}. Target has no rollouts.r   clouddeployv1zBclouddeploy.projects.locations.deliveryPipelines.releases.rollouts)r)   z9clouddeploy.projects.locations.deliveryPipelines.releases)listr   GetFilteredRolloutsENQUEUETIME_ROLLOUT_ORDERBYlencore_exceptionsErrorrD   rE   	core_apisGetMessagesModuleRolloutStateValueValuesEnum	SUCCEEDEDFAILED	CANCELLEDstaterB   RedeployRolloutErrornamer   r7   ParseRelativeNamer4   Parentr:   )rM   rK   rb   prior_rolloutsprior_rolloutmessagesredeployable_statesrP   s           r"   r<   r<      sn   ( &&#;;. 	1


BIIOO	
 
 !#-((=(++55++22++55
  33

0
0=--}/B/B  "**<<


#   vx|~L =  
r$   N)$rX   "googlecloudsdk.api_lib.clouddeployr   googlecloudsdk.api_lib.utilr   rm   r   r\   googlecloudsdk.callioper   !googlecloudsdk.command_lib.deployr   r   rB   r	   r
   r   r   r   r   googlecloudsdk.corerk   r   googlecloudsdk.core.consoler   rY   r   ReleaseTracksReleaseTrackALPHABETAGADefaultUniverseOnlyCreateCommandr   r<   r_   r$   r"   <module>r      s    & 7 9 F ( D @ M 3 : : ; : 9 = ) 2 #  T..33T5F5F5I5I Tt!! T Tn;r$   