
    0                         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 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)z1Utilities for the cloud deploy describe commands.    )
exceptions)delivery_pipeline_util)rollout_util)target_util)log)	resources)timesc                 x    t        j                  |       }d|i}|r|S |rt        || ||      S t        || ||      S )a  Describes details specific to the individual target, delivery pipeline qualified.

  Args:
    target_ref: protorpc.messages.Message, target reference.
    pipeline_id: str, delivery pipeline ID.
    skip_pipeline_lookup: Boolean, flag indicating whether to fetch information
      from the pipeline(s) containing target. If set, pipeline information will
      not be fetched.
    list_all_pipelines: Boolean, flag indicating whether to fetch information
      from all pipelines associated with target, if set to false, it will fetch
      information from the most recently updated one.

  Returns:
    A dictionary of <section name:output>.

  Target)r   	GetTargetDescribeTargetWithPipelineDescribeTargetWithNoPipeline)
target_refpipeline_idskip_pipeline_lookuplist_all_pipelines
target_objoutputs         1lib/googlecloudsdk/command_lib/deploy/describe.pyDescribeTargetr      sY    $ $$Z0*j!&M%j*k&,. . (
J(:FD D    c                     |j                         }t        j                  j                  dd|d   |d   |d      }t	        j
                  ||      }t        ||      }| j                  rt        |||      }|S )a  Describes details specific to the individual target, delivery pipeline qualified.

  The output contains four sections:

  target
    - detail of the target to be described.

  latest release
    - the detail of the active release in the target.

  latest rollout
    - the detail of the active rollout in the target.

  deployed
    - timestamp of the last successful deployment.

  pending approvals
    - list rollouts that require approval.
  Args:
    target_obj: protorpc.messages.Message, target object.
    target_ref: protorpc.messages.Message, target reference.
    pipeline_id: str, delivery pipeline ID.
    output: A dictionary of <section name:output>.

  Returns:
    A dictionary of <section name:output>.

  Nz0clouddeploy.projects.locations.deliveryPipelines
projectsIdlocationsId)r   r   deliveryPipelinesId
collectionparams)	AsDictr   REGISTRYParser   GetCurrentRolloutSetCurrentReleaseAndRolloutrequireApprovalListPendingApprovals)r   r   r   r   target_dictpipeline_refcurrent_rollouts          r   r   r   9   s    : !!#+##))
C#L1$]3!, * 	,  11*lK/&?&!*lFCF	-r   c                 T   t        |      }|s|S t        |      |d<   t        t        t        j
                  |            }|r*t        ||      |d<   | j                  rt        |||       |S t        ||      \  }}|r|rt        ||      |d<   | j                  rt        |||       |S )aJ  Describes details specific to the individual target.

  In addition, it will also display details about pipelines associated
  with the given target.

  The output contains the following sections:

  target
    - details of the target to be described.

  associated pipelines
    - details of the pipelines that use the target.

  For each associated pipeline, the following will be displayed:

  latest release
    - details of the active release in the target.

  latest rollout
    - details of the active rollout in the target.

  deployed
    - timestamp of the last successful deployment.

  pending approvals
    - list the rollouts that require approval.

  Args:
    target_obj: protorpc.messages.Message, target object.
    target_ref: protorpc.messages.Message, target reference.
    list_all_pipelines: Boolean, if true, will return information about all
      pipelines associated with target, otherwise, the most recently active
      pipeline information will be displayed.
    output: A dictionary of <section name:output>.

  Returns:
    A dictionary of <section name:output>.

  z'Number of associated delivery pipelineszAssociated delivery pipelineszActive Pipeline)GetTargetDeliveryPipelineslenlistmapr   PipelineToPipelineRefListAllPipelineReleaseRolloutr$   %ListPendingApprovalsMultiplePipelinesGetMostRecentlyActivePipelineSetMostRecentlyActivePipeliner%   )r   r   r   r   sorted_pipelinespipeline_refsactive_pipeline_reflatest_rollouts           r   r   r   g   s    R 0
;	M69:J6K&	23	
 
6
68HIK-.KM/#F*+!!+JvN 
- +HM+#'~"?
~#/f!!:':FC	-r   c                     g }|D ]2  }t        | |i       }|j                  dg       }|j                  |       4 ||d<   |S )aX  Fetches a list of pending rollouts for each pipeline and appends the result to a single list.

  Args:
    target_ref: protorpc.messages.Message, target object.
    pipeline_refs: protorpc.messages.Message, list of pipeline objects.
    output: dictionary object

  Returns:
    The modified output object with the list of pending rollouts.

  Pending Approvals)r%   getextend)r   r4   r   all_pending_approvalsr'   result_dictapprovals_one_pipelines          r   r0   r0      sU     #l&z<DK(__-@"E  !78 $ !6&		-r   c                     | rnt         j                  j                  | j                  d      }|j	                         j                         |d<   |j                         |d<   | j                  |d<   |S )a6  Set current release and the last deployment section in the output.

  Args:
    current_rollout: protorpc.messages.Message, rollout object.
    output: dictionary object

  Returns:
    The modified output object with the rollout's parent release, the name of
    the rollout, and the time it was deployed.

  zBclouddeploy.projects.locations.deliveryPipelines.releases.rollouts)r   zLatest releasezLatest rolloutDeployed)r   r    r!   nameParentRelativeNamedeployEndTime)r(   r   current_rollout_refs      r   r#   r#      sw     #,,22W 3 
  399;HHJF2??AF(66F:	-r   c                    	 t        j                  | |      }g }|D ]  }|j                  |j                          |r||d<   |S # t        j
                  $ r-}t        j                  d|j                  z          Y d}~|S d}~ww xY w)ah  Lists the rollouts in pending approval state for the specified target.

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

  Returns:
    The modified output object with the rollouts from the given pipeline pending
    approval on the given target.

  r8   z"Failed to list pending approvals: N)	r   ListPendingRolloutsappendr@   apitools_exceptions	HttpErrorr   debugcontent)r   r'   r   pending_approvalspending_approvals_namesroerrors          r   r%   r%      s    	D$88L" $$RWW-  $;f ! 
- 
	&	& DII2U]]BCC	-Ds   AA B"B  Bc                 *   | j                         }t        j                  j                  dd|d   |d   d      }	 t	        j
                  | |      S # t        j                  $ r,}t        j                  d|j                  z          Y d}~yd}~ww xY w)zGet all pipelines associated with a target.

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

  Returns:
    A list of delivery pipelines sorted by creation date, or an null list if
    there is an error fetching the pipelines.

  Nzclouddeploy.projects.locationsr   r   )r   r   r   z&Failed to fetch pipelines for target: )r   r   r    r!   r   ListDeliveryPipelinesWithTargetrH   rI   r   warningrK   )r   r&   location_refrO   s       r   r*   r*      s     !!#+##))
1#L1$]3 * 	,!AAL" "		&	& KK85==HIs   A B&"BBc                 p    g }|D ].  }t        j                  | |      }||j                  ||g       0 |S )aK  Retrieves the latest rollout for each delivery pipeline.

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

  Returns:
    A list with element [pipeline_ref, rollout] where the rollout is the latest
    successful rollout of the pipeline resource.

  )r   r"   rG   )r   r4   resultr'   rollouts        r   GetPipelinesAndRolloutsrW     sC     &#l++JEGmm\7+, $ 
-r   c                 Z   t        | |      }|s9t        j                  dj                  | j	                                      |d   dfS |d   \  }}t        j                  |j                        }|dd D ]2  }|\  }}t        j                  |j                        }	|	|kD  s-|}|}|	}4 ||fS )a~  Retrieves latest rollout and release information for a list of delivery pipelines.

  Args:
    target_ref: protorpc.messages.Message, target object.
    sorted_pipeline_refs: protorpc.messages.Message, a list of pipeline objects,
      sorted in descending order by create time.

  Returns:
    A tuple of the pipeline with the most recent deploy time with
     latest rollout.

  z,Target: {} has no recently active pipelines.r   N   )rW   r   rJ   formatrB   r	   ParseDateTimerC   )
r   sorted_pipeline_refspipeline_rolloutsmost_recent_pipeline_refmost_recent_rolloutmost_recent_rollout_deploy_timepipeline_rollout_tupler'   rV   rollout_deploy_times
             r   r1   r1      s     .j:NO	II<CC!# $"D((2CA2F//$)$7$7''%)! !2!" 52L'--g.C.CD<<!-#(;% !6 
"#6	66r   c                 @    | j                         t        |i       ig}|S )zRetrieves latest rollout and release information for a delivery pipeline.

  Args:
    pipeline_ref: protorpc.messages.Message a DeliveryPipeline object.
    rollout: protorpc.messages.Message a Rollout object.

  Returns:
    A content directory.

  )rB   r#   )r'   rV   r   s      r   r2   r2   @  s-     !#>w#K & 
-r   c                 N    t        j                  | |      }i }t        ||      }|S )a  Retrieves latest rollout and release information for a single delivery pipeline.

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

  Returns:
    A content directory.

  )r   r"   r#   )r   r'   r(   r   s       r   SetPipelineReleaseRolloutre   Q  s,      11*lK/&&?&	-r   c                 r    g }|D ]/  }t        | |      }|j                  |j                         |i       1 |S )a  Retrieves latest rollout and release information for each delivery pipeline.

  Args:
    target_ref: protorpc.messages.Message, target object.
    pipeline_refs: protorpc.messages.Message a list of DeliveryPipeline objects

  Returns:
    A content directory.

  )re   rG   rB   )r   r4   r   r'   pipeline_entrys        r   r/   r/   c  s@     &#l.z<HN
MM<,,.?@ $ 
-r   N)__doc__apitools.base.pyr   rH   !googlecloudsdk.command_lib.deployr   r   r   googlecloudsdk.corer   r   googlecloudsdk.core.utilr	   r   r   r   r0   r#   r%   r*   rW   r1   r2   re   r/    r   r   <module>rn      sa    8 ? D : 9 # ) *D<+\=@*268(7@"$r   