
                             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	Zd
Z	 	 	 	 	 	 ddZd Zd Zy)z$Utilities for the promote operation.    )rollout)
exceptions)release_util)rollout_util)target_util)log)	resourceszmRelease {} is already deployed to the last target ({}) in the promotion sequence.
- Release: {}
- Target: {}
zstate="IN_PROGRESS"Nc
           
          |}
|
st        ||      }
t        j                  | |
||||||	      }t        j                  ||
      }|r+|j
                  rt        j                  j                  d       |S )a@  Creates a rollout for the given release in the destination target.

  If to_target is not specified and there is a rollout in progress, the promote
  will fail. Otherwise this computes the destination target based on the
  promotion sequence.

  Args:
    release_ref: protorpc.messages.Message, release resource object.
    release_obj: apitools.base.protorpclite.messages.Message, release message
      object.
    to_target: str, the target to promote the release to.
    is_create: bool, if creates a rollout during release creation.
    rollout_id: str, ID to assign to the generated rollout.
    annotations: dict[str,str], a dict of annotation (key,value) pairs that
      allow clients to store small amounts of arbitrary data in cloud deploy
      resources.
    labels: dict[str,str], a dict of label (key,value) pairs that can be used to
      select cloud deploy resources and to find collections of cloud deploy
      resources that satisfy certain conditions.
    description: str, rollout description.
    starting_phase_id: str, the starting phase for the rollout.
    override_deploy_policies: List of Deploy Policies to override.

  Raises:
    googlecloudsdk.command_lib.deploy.exceptions.RolloutIdExhausted
    googlecloudsdk.command_lib.deploy.exceptions.ReleaseInactiveError
  Returns:
    The rollout resource created.
  z The rollout is pending approval.)	GetToTargetIDr   CreateRolloutr   GetSnappedTargetrequireApprovalr   statusPrint)release_refrelease_obj	to_target	is_create
rollout_idannotationslabelsdescriptionstarting_phase_idoverride_deploy_policiesdest_targetrollout_resource
target_objs                5lib/googlecloudsdk/command_lib/deploy/promote_util.pyPromoter   "   sz    R +	Y7K!//	 ,,[+F*J..JJ78	    c                 N   | j                   j                  j                  st        j                  | j
                        t        j                  j                  | j
                  d      }| j                   j                  j                  d   j                  }t        t        | j                   j                  j                              }|j                         }t        |      D ]c  \  }}t        j                  |j                  |d   |d         }t        j                   |t        j                  j#                  dd|d   |d   |d   d	
            }	|	stt        j                  j#                  |	j
                  d      }
|
j%                         j'                         |j'                         k(  s|dkD  r||dz
     j                  }n}t(        j*                  j-                  t.        j1                  |j'                         |j'                         |j3                         |j3                                      |j3                         } n || j                   j                  j                  d   j                  k(  r|st        j4                         t        j6                  |      S )a  Get the to_target parameter for promote API.

  This checks the promotion sequence to get the next stage to promote the
  release to.

  Args:
    release_obj: apitools.base.protorpclite.messages.Message, release message.
    is_create: bool, if getting the target for release creation.

  Returns:
    the target ID.

  Raises:
    NoStagesError: if no pipeline stages exist in the release.
    ReleaseInactiveError: if this is not called during release creation and the
    specified release has no rollouts.
  z9clouddeploy.projects.locations.deliveryPipelines.releases)
collectionr   
projectsIdlocationsIdNz0clouddeploy.projects.locations.deliveryPipelinesdeliveryPipelinesId)r#   r$   r%   )r"   paramszBclouddeploy.projects.locations.deliveryPipelines.releases.rollouts   )deliveryPipelineSnapshotserialPipelinestagesr   NoStagesErrornamer	   REGISTRYParseRelativeNametargetIdlistreversedAsDict	enumerater   TargetReferenceGetCurrentRolloutParseParentNamer   r   r   _LAST_TARGET_IN_SEQUENCEformatRelativeNameReleaseInactiveErrorTargetId)r   r   r   r   reversed_stagesrelease_dictistage
target_refcurrent_rolloutcurrent_rollout_refs              r   r   r   b   s`   & 
	-	-	<	<	C	C

"
";#3#3
44 ""44L 5 + 22AAHHH 
 {33BBIIJ/ ##%,O,ha,,\2L4OJ
 "33  I*<8+M:'34I'J 	! 	
O %..44


Y 5  
	#	#	%	*	*	,0@0@0B	Bq5%a!e,55)
**

&--""$//#**,))+	 !--/)Q -Z 		-	-	<	<	C	CA	F	O	OP

)
)
++			i	((r    c                     t        t        j                         j                  | j	                         t
        d            }|r t        j                  |j                  |      y)au  Checks if there are any rollouts in progress for the given release.

  Args:
    release_ref: protorpc.messages.Message, release resource object.
    release_obj: apitools.base.protorpclite.messages.Message, release message
      object.
    to_target_id: string, target id (e.g. 'prod')

  Raises:
    googlecloudsdk.command_lib.deploy.exceptions.RolloutInProgressError
  r'   )limitN)	r0   r   RolloutClientListr;   IN_PROGRESS_FILTER_TEMPLATEr   RolloutInProgressErrorr,   )r   r   to_target_idrolloutss       r   CheckIfInProgressRolloutrM      s^     ""

"
"
$&A # ( 

+
+K,<,<l
KK r    )NNNNNN)__doc__"googlecloudsdk.api_lib.clouddeployr   !googlecloudsdk.command_lib.deployr   r   r   r   googlecloudsdk.corer   r	   r9   rI   r   r   rM    r    r   <module>rS      s[    + 7 8 : : 9 # )E 
 4  !=@W)tLr    