
    %                        S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	Jr
  SSKJr  SSKJr  SSKJr  S	rS
rSrSrSrSrSrSrSrS rS rS r SS jrS\4S jr      SS\R>                  4S jjr S r!S r"S\4S jr#g)z0Utilities for the cloud deploy rollout resource.    )
exceptions)client_util)release)rollout)log)	resources)clouddeploy_v1_messageszBclouddeploy.projects.locations.deliveryPipelines.releases.rolloutszMapprovalState="NEEDS_APPROVAL" AND state="PENDING_APPROVAL" AND targetId="{}"zg(approvalState!="REJECTED" AND approvalState!="NEEDS_APPROVAL") AND state="SUCCEEDED" AND targetId="{}"ztargetId="{}"z{}-to-{}-{:04d}z{}/releases/-zDeployEndTime desczCreateTime desczEnqueueTime descc                 F    [         R                  R                  U [        S9$ )zReturns a rollout reference object from a rollout message.

Args:
  rollout_name: str, full canonical resource name of the rollout

Returns:
  Rollout reference object

collection)r   REGISTRYParseRelativeName_ROLLOUT_COLLECTION)rollout_names    5lib/googlecloudsdk/command_lib/deploy/rollout_util.pyRolloutReferenceFromNamer   /   s(     
			-	-2 
. 
     c                 v    U nSU ;   a0  [         R                  R                  U [        S9R	                  5       nU$ )zgReturns rollout ID.

Args:
  rollout_name_or_id: str, rollout full name or ID.

Returns:
  Rollout ID.
z	projects/r   )r   r   r   r   Name)rollout_name_or_id
rollout_ids     r   	RolloutIdr   =   sE     "*&&##55': 6 
df  
r   c                     [         R                  U R                  5       5      n[        R                  UR	                  5       5      n[
        R                  " 5       R                  UU[        S9$ )a  Lists the rollouts in PENDING_APPROVAL state for the releases associated with the specified target.

The rollouts must be approvalState=NEEDS_APPROVAL and
state=PENDING_APPROVAL. The returned list is sorted by rollout's create
time.

Args:
  target_ref: protorpc.messages.Message, target object.
  pipeline_ref: protorpc.messages.Message, pipeline object.

Returns:
  a sorted list of rollouts.
)release_name
filter_strorder_by)	 PENDING_APPROVAL_FILTER_TEMPLATEformatr   WILDCARD_RELEASE_NAME_TEMPLATERelativeNamer   RolloutClientListPENDING_ROLLOUT_ORDERBY)
target_refpipeline_refr   parents       r   ListPendingRolloutsr'   O   s^     066z7HI*)001J1J1LM&				 	%	%& 
& 
 r   Nc                     [         R                  UR                  5       5      n[        R                  " 5       R                  UUR                  U R                  5       5      UUUS9$ )a  Gets successfully deployed rollouts for the releases associated with the specified target and index.

Args:
  target_ref: protorpc.messages.Message, target object.
  pipeline_ref: protorpc.messages.Message, pipeline object.
  filter_str: Filter string to use when listing rollouts.
  order_by: order_by field to use when listing rollouts.
  page_size: int, the maximum number of objects to return per page.
  limit: int, the maximum number of `Rollout` objects to return.

Returns:
  a rollout object or None if no rollouts in the target.
)r   r   r   	page_sizelimit)r   r   r    r   r!   r"   r   )r$   r%   r   r   r)   r*   r&   s          r   GetFilteredRolloutsr+   g   s_      *001J1J1LM&				 	%	%"":??#45 
& 
 r   returnc                 @   [         R                  U 5      n [        R                  " 5       R	                  UR                  5       U5      n[        UR                  5       X5      $ ! [        R                   a%    [        R                  " UR                  5       5      ef = fN)!ROLLOUT_IN_TARGET_FILTER_TEMPLATEr   r   r!   r"   r    ComputeRolloutIDr   apitools_exceptions	HttpErrorcd_exceptionsListRolloutsError)	to_targetrelease_refr   rolloutss       r   GenerateRolloutIdr8      s    077	B*F$$&++  "JH K,,.	DD		&	& F

)
)+*B*B*D
EEFs   AA$ $9Bc           	      @   UnU(       d  [        X5      nU R                  5       n	[        R                  R	                  U[
        U	R                  S5      U	R                  S5      U	R                  S5      U R                  5       S.S9n
[        R                  " 5       R                  U
R                  5       UUS9n[        R                  R                  SR                  U
R                  5       U5      5        [         R"                  " 5       R%                  U
UUUUU5      n[        R                  R'                  UR(                  SS	9n[        R*                  " 5       R-                  UUS
5        [         R"                  " 5       R/                  U
R                  5       5      $ )a  Creates a rollout by calling the rollout create API and waits for the operation to finish.

Args:
  release_ref: protorpc.messages.Message, release resource object.
  to_target: str, the target to create create the rollout in.
  rollout_id: str, rollout ID.
  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, rollout starting phase.
  override_deploy_policies: List of Deploy Policies to override.

Raises:
    ListRolloutsError: an error occurred calling rollout list API.

Returns:
  The rollout resource created.

projectsIdlocationsIddeliveryPipelinesId)r:   r;   r<   
releasesId)r   params)nametargetIddescriptionz Creating rollout {} in target {}z)clouddeploy.projects.locations.operationsr   z2Waiting for rollout creation operation to complete)r8   AsDictr   r   Parser   getr   r   GetMessagesModuleRolloutr    r   statusPrintr   r   r!   Creater   r?   OperationsClientWaitForOperationGet)r6   r5   r   annotationslabelsrA   starting_phase_idoverride_deploy_policiesfinal_rollout_idresource_dictrollout_refrollout_obj	operationoperation_refs                 r   CreateRolloutrW      s   @  	(@$$&-""(($%)),7&**=9!.!2!23H!I#((*	 ) 	+ --/77##% 8 + **(//

"
"
$i
 ##%,,) $$66nn!L 7 -  11:
 
			 	$	$[%=%=%?	@@r   c           	          [        U U[        [        SSS9n/ nU HO  nU(       d  UR                  U5        O![	        U5      (       d  UR                  U5        [        U5      S:  d  MN    U$    U$ )a  Gets the currently deployed release and the next valid release that can be rolled back to.

Args:
  target_ref: protorpc.messages.Message, target resource object.
  pipeline_ref: protorpc.messages.Message, pipeline resource object.

Raises:
    HttpException: an error occurred fetching a resource.

Returns:
  An list containg the currently deployed release and the next valid
  deployable release.
N
   )r$   r%   r   r   r*   r)      )r+    DEPLOYED_ROLLOUT_FILTER_TEMPLATESUCCEED_ROLLOUT_ORDERBYappend_RolloutIsFromAbandonedReleaselen)r$   r%   iterabler7   rT   s        r   GetValidRollBackCandidatera      sr     !1&( (kook"+K88ook"
8}	/  
/r   c                 .   [        U R                  5      nUR                  5       n [        R                  " 5       R                  UR                  5       5      nUR                  $ ! [        R                   a  n[        R                  " U5      eS nAff = fr.   )r   r?   Parentr   ReleaseClientrL   r    r1   r2   r   HttpException	abandoned)rT   rS   r6   release_objerrors        r   r^   r^      s~    ()9)9:+""$+*'')--k.F.F.HIK 
		 
	&	& *

"
"5
))*s   2A% %B9BBc                     U Vs1 s H  n[        UR                  5      iM     nn[        SS5       H"  n[        R	                  XU5      nXd;  d  M   Us  $    [
        R                  " U 5      es  snf )a\  Generates a rollout ID.

Args:
  release_id: str, release ID.
  target_id: str, target ID.
  rollouts: [apitools.base.protorpclite.messages.Message], list of rollout
    messages.

Returns:
  rollout ID.

Raises:
  googlecloudsdk.command_lib.deploy.exceptions.RolloutIdExhaustedError: if
  there are more than 1000 rollouts with auto-generated ID.
   i  )r   r?   rangeROLLOUT_ID_TEMPLATEr   r3   RolloutIDExhaustedError)
release_id	target_idr7   rrollout_idsir   s          r   r0   r0     si      -55Hq166"H+5D>a %++J1EJ$  	--j99 6s   A.)NN)NNNNNN)$__doc__apitools.base.pyr   r1   "googlecloudsdk.api_lib.clouddeployr   r   r   googlecloudsdk.calliope!googlecloudsdk.command_lib.deployr3   googlecloudsdk.corer   r   4googlecloudsdk.generated_clients.apis.clouddeploy.v1r	   r   r   r[   r/   rl   r   r\   r#   ENQUEUETIME_ROLLOUT_ORDERBYr   r   r'   r+   strr8   rF   rW   ra   r^   r0    r   r   <module>r}      s    7 > : 6 6 . I # ) X I 1 !
O ! %4 !' !0 . + 0 $2 KO6F F !KA $$KA\B: :r   