
    +                         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Zd	 Zd
 Zd Zd Zd Z	 	 	 	 	 	 ddZy)z:Utilities for the cloud deploy delivery pipeline resource.    N)
exceptions)client_util)delivery_pipeline)log)	resourcesz#serialPipeline.stages.targetId:"{}"c                 N    t         j                  j                  | d||| d      S )a  Creates the delivery pipeline reference base on the parameters.

    Returns the shared delivery pipeline reference.

  Args:
    name: str, delivery pipeline ID
    project: str,project number or ID.
    region: str, region ID.

  Returns:
    custom target type reference.
  0clouddeploy.projects.locations.deliveryPipelines)
projectsIdlocationsIddeliveryPipelinesId)
collectionparams)r   REGISTRYParse)nameprojectregions      ?lib/googlecloudsdk/command_lib/deploy/delivery_pipeline_util.pyDeliveryPipelineReferencer       s6     
			!	!
C!% 
" 
     c                     t         j                  | j                               }t        j                         j                  |j                         |d      }t        |d d      S )aA  Lists the delivery pipelines associated with the specified target.

  The returned list is sorted by the delivery pipeline's create time.
  Args:
    target_ref: protorpc.messages.Message, target object.
    location_ref: protorpc.messages.Message, location object.

  Returns:
    a sorted list of delivery pipelines.
  r   )location
filter_str	page_sizec                     | j                   S )N)
createTime)pipelines    r   <lambda>z1ListDeliveryPipelinesWithTarget.<locals>.<lambda>Q   s
    h&9&9r   T)keyreverse),_PIPELINES_WITH_GIVEN_TARGET_FILTER_TEMPLATEformatNamer   DeliveryPipelinesClientListRelativeNamesorted)
target_reflocation_refr   	pipeliness       r   ListDeliveryPipelinesWithTargetr+   8   sg     <BBoo*  779>>((* ? ) 
94
 r   c                 \    t         j                  j                  | j                  d      }|S )Nr	   )r   )r   r   ParseRelativeNamer   )r   pipeline_refs     r   PipelineToPipelineRefr/   U   s/    ##55mmC 6 , 
r   c                 B   	 t        j                         j                  |       }|S # t        j                  $ rd}t        j                  dj                  | |j                               t
        j                  j                  dj                  |              |d}~ww xY w)zGets the delivery pipeline and returns the value of its suspended field.

  Args:
    pipeline_name: str, the canonical resource name of the delivery pipeline

  Returns:
    The pipeline object
  Raises:
    apitools.base.py.HttpError
  zFailed to get pipeline {}: {}z"Unable to get delivery pipeline {}N)r   r$   Getapitools_exceptions	HttpErrorr   debugr"   contentstatusPrint)pipeline_namepipeline_objerrors      r   GetPipeliner;   ]   s    
$<<>BBL 		&	& II'..}emmL JJ9@@OP
Ks   $' BABBc                 ^    | j                   r!t        j                  | j                  |      y)ae  Checks if the delivery pipeline associated with the release is suspended.

  Args:
    pipeline_obj: protorpc.messages.Message, delivery pipeline object.
    suspended_pipeline_msg: str, error msg to show the user if the pipeline is
      suspended.

  Raises:
    googlecloudsdk.command_lib.deploy.PipelineSuspendedError if the pipeline is
    suspended
  )r8   failed_activity_msgN)	suspendedcd_exceptionsPipelineSuspendedErrorr   )r9   suspended_pipeline_msgs     r   ThrowIfPipelineSuspendedrB   u   s1     

.
."''2  r   c                 ,   |r|n dt        j                         j                  z   }t        j                         j                  ||      }	t        j                         j                  | t        j                         j                  ||	||||            S )a4  Creates a rollback rollout for the target based on the given inputs.

  Args:
    pipeline_rel_name: delivery_pipeline name
    target_id: the target to rollback
    validate_only: whether or not to validate only for the call
    release_id: the release_id to rollback to
    rollout_id: the rollout_id of the new rollout
    rollout_to_rollback: the rollout that is being rolled back by this rollout
    rollout_obj: the rollout resource to pass into rollbackTargetConfig
    starting_phase: starting_phase of the rollout

  Returns:
    RollbackTargetResponse
  z	rollback-)rolloutstartingPhaseId)	releaseIdrollbackConfig	rolloutIdrolloutToRollBacktargetIdvalidateOnly)
uuiduuid4hexr   GetMessagesModuleRollbackTargetConfigr   r$   RollbackTargetRollbackTargetRequest)
pipeline_rel_name	target_idvalidate_only
release_id
rollout_idrollout_to_rollbackrollout_objstarting_phaserollback_idrollback_target_configs
             r   CreateRollbackTargetr]      s    6 )
kDJJL<L<L.L+&88:OO> P  
	2	2	4	C	C##%;;//$ < 

 
r   )FNNNNN)__doc__rL   apitools.base.pyr   r2   "googlecloudsdk.api_lib.clouddeployr   r   !googlecloudsdk.command_lib.deployr?   googlecloudsdk.corer   r   r!   r   r+   r/   r;   rB   r]    r   r   <module>rd      s^    A  > : @ I # ) * -
0:0, +r   