
    0                         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	 rS
 rS rS rS rS rS rS rS rS rS rS rg)z1Utilities for the cloud deploy describe commands.    )
exceptions)delivery_pipeline_util)rollout_util)target_util)log)	resources)timesc                     [         R                  " U 5      nSU0nU(       a  U$ U(       a  [        X@UU5      $ [        X@X55      $ )ap  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      sS    $ $$Z0*j!&M%jk&,. . (
(:D D    c                     UR                  5       n[        R                  R                  SSUS   US   US.S9n[        R
                  " X5      n[        Xc5      nU R                  (       a  [        XU5      nU$ )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*K/&?&!*FCF	-r   c                    [        U5      nU(       d  U$ [        U5      US'   [        [        [        R
                  U5      5      nU(       a-  [        X5      US'   U R                  (       a  [        XU5        U$ [        X5      u  pgU(       a  U(       a  [        Xg5      US'   U R                  (       a  [        XU5        U$ )a  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/#F*+!!+JvN 
- +H+#'~"?
#/f!!:FC	-r   c                     / nU H2  n[        X0 5      nUR                  S/ 5      nUR                  U5        M4     X2S'   U$ )aJ  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      sQ     #l&zDK(__-@"E  !78 $ !6		-r   c                     U (       ak  [         R                  R                  U R                  SS9nUR	                  5       R                  5       US'   UR                  5       US'   U R                  US'   U$ )a(  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                      [         R                  " X5      n/ nU H  nUR                  UR                  5        M      U(       a  XBS'   U$ ! [        R
                   a.  n[        R                  " SUR                  -   5         SnAU$ SnAff = f)aX  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" $$RWW-  $; ! 
- 
	&	& DII2U]]BCC	-Ds   AA B#BBc                 (   U R                  5       n[        R                  R                  SSUS   US   S.S9n [        R
                  " X5      $ ! [        R                   a-  n[        R                  " SUR                  -   5         SnAgSnAff = f)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" "		&	& KK85==HIs   A B$#BBc                 v    / nU H0  n[         R                  " X5      nUc  M  UR                  X4/5        M2     U$ )a=  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     s?     &#l++JEGmm\+, $ 
-r   c                 h   [        X5      nU(       d:  [        R                  " SR                  U R	                  5       5      5        US   S4$ US   u  p4[
        R                  " UR                  5      nUSS  H4  nUu  px[
        R                  " UR                  5      n	X:  d  M.  UnUnU	nM6     X44$ )an  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O	II<CC!# $"D((2CA2F/$)$7$7''%)! !2!" 52L--g.C.CD<!-#(;% !6 
"	66r   c                 @    U R                  5       [        U0 5      0/nU$ )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                 L    [         R                  " X5      n0 n[        X#5      nU$ )zRetrieves 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*K/&&?&	-r   c                 t    / nU H/  n[        X5      nUR                  UR                  5       U05        M1     U$ )zRetrieves 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   