
                         x    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Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zy)z/Utilities for the cloud deploy target resource.    )
exceptions)target)rollout_util)log)	resourcesz&clouddeploy.projects.locations.targetsc           	      T   d}	 t        t        j                  | |t        j                  t        j                  d            }|r|d   }|S # t
        j                  $ rH}t        j                  dj                  | j                         |j                               Y d}~|S d}~ww xY w)a  Gets the releases in the specified target and the last deployment associated with the target.

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

  Returns:
    release messages associated with the target.
    last deployed rollout message.
  Raises:
   Exceptions raised by RolloutClient.GetCurrentRollout()
  N   )
filter_strorder_bylimitr   z2failed to get the current rollout of target {}: {})listr   GetFilteredRollouts DEPLOYED_ROLLOUT_FILTER_TEMPLATESUCCEED_ROLLOUT_ORDERBYapitools_exceptions	HttpErrorr   debugformatRelativeNamecontent)
target_refpipeline_refcurrent_rolloutrolloutserrors        4lib/googlecloudsdk/command_lib/deploy/target_util.pyGetCurrentRolloutr      s     /3((#DD!99	H  o
 
	 
	&	& 3IIBII!5==2 3 3 
	3s   AA B'=B""B'c                 L    t         j                  j                  | t              S )zCreates a target reference from full name.

  Args:
    target_name: str, target resource name.

  Returns:
    Target reference.
  )
collection)r   REGISTRYParseRelativeName_SHARED_TARGET_COLLECTION)target_names    r   TargetReferenceFromNamer$   ;   s(     
			-	-7 
. 
9 9    c                 @    d| v rt        |       j                         S | S )zmReturns target ID.

  Args:
    target_name_or_id: str, target full name or ID.

  Returns:
    Target ID.
  z	projects/)r$   Name)target_name_or_ids    r   TargetIdr)   I   s(     %%"#45::<<	r%   c           	      h    t         j                  j                  dt        ||t	        |       d      S )a  Creates the target reference base on the parameters.

  Returns the shared target reference.

  Args:
    target_name_or_id: str, target full name or ID.
    project: str,project number or ID.
    location_id: str, region ID.

  Returns:
    Target reference.
  N)
projectsIdlocationsId	targetsId)r   params)r   r    Parser"   r)   )r(   projectlocation_ids      r   TargetReferencer2   Y   s<     
			!	!
*$ 12 
" 
	 	r%   c                 d    t        j                         j                  | j                               S )a  Gets the target message by calling the get target API.

  Args:
    target_ref: protorpc.messages.Message, protorpc.messages.Message, target
      reference.

  Returns:
    Target message.
  Raises:
    Exceptions raised by TargetsClient's get functions
  )r   TargetsClientGetr   )r   s    r   	GetTargetr6   p   s'     
				#	#J$;$;$=	>>r%   c                 H    t        j                         j                  |       S )zPatches a target resource by calling the patch target API.

  Args:
      target_obj: apitools.base.protorpclite.messages.Message, target message.

  Returns:
      The operation message.
  )r   r4   Patch)
target_objs    r   PatchTargetr:      s     
				%	%j	11r%   c                 H    t        j                         j                  |       S )zDeletes a target resource by calling the delete target API.

  Args:
    name: str, target name.

  Returns:
    The operation message.
  )r   r4   Delete)names    r   DeleteTargetr>      s     
				&	&t	,,r%   c                 H    t        j                         j                  |       S )zList target resources by calling the list target API.

  Args:
    parent_name: str, the name of the collection that owns the targets.

  Returns:
    List of targets returns from target list API call.
  )r   r4   List)parent_names    r   
ListTargetrB      s     
				$	$[	11r%   N)__doc__apitools.base.pyr   r   "googlecloudsdk.api_lib.clouddeployr   !googlecloudsdk.command_lib.deployr   googlecloudsdk.corer   r   r"   r   r$   r)   r2   r6   r:   r>   rB    r%   r   <module>rI      sJ    6 ? 5 : # )D @9 	.?	2	-	2r%   