
    "<                         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rSSKJr  SSK	r	Sr
S	rS
 rS r " S S\5      r " S S\5      rS rS r SS jrg)z.Wrapper for Cloud Run TrafficTargets messages.    )absolute_import)division)print_function)unicode_literalsN)traffic-z, c                 D    U [         :X  a  [         $ SR                  U 5      $ )Nz{}%)_MISSING_PERCENT_OR_TAGSformatpercents    .lib/googlecloudsdk/api_lib/run/traffic_pair.py_FormatPercentager   #   s     ((##<<      c                 &    [        S U  5       5      $ )z'Sums the percents of the given targets.c              3   ^   #    U  H#  oR                   (       d  M  UR                   v   M%     g 7fNr   .0ts     r   	<genexpr>_SumPercent.<locals>.<genexpr>,   s     5199YQYY   --)sum)targetss    r   _SumPercentr   *   s    	55	55r   c                   "    \ rS rSrSrSS jrSrg)
TrafficTag/   a  Contains the spec and status state for a traffic tag.

Attributes:
  tag: The name of the tag.
  url: The tag's URL, or an empty string if the tag does not have a URL
    assigned yet. Defaults to an empty string.
  inSpec: Boolean that is true if the tag is present in the spec. Defaults to
    False.
  inStatus: Boolean that is true if the tag is present in the status. Defaults
    to False.
c                 4    Xl         X l        X0l        X@l        g)zReturns a new TrafficTag.

Args:
  tag: The name of the tag.
  url: The tag's URL.
  in_spec: Boolean that is true if the tag is present in the spec.
  in_status: Boolean that is true if the tag is present in the status.
N)tagurlinSpecinStatus)selfr!   r"   in_spec	in_statuss        r   __init__TrafficTag.__init__<   s     HHKMr   )r#   r$   r!   r"   N) FF)__name__
__module____qualname____firstlineno____doc__r(   __static_attributes__ r   r   r   r   /   s    
r   r   c                       \ rS rSrSr SS jr\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       rS r\S 5       r\S 5       r\S 5       r\S 5       rSrg)TrafficTargetPairK   a	  Holder for TrafficTarget status information.

The representation of the status of traffic for a service
includes:
  o User requested assignments (spec.traffic)
  o Actual assignments (status.traffic)

Each of spec.traffic and status.traffic may contain multiple traffic targets
that reference the same revision, either directly by name or indirectly by
referencing the latest ready revision.

The spec and status traffic targets for a revision may differ after a failed
traffic update or during a successful one. A TrafficTargetPair holds all
spec and status TrafficTargets that reference the same revision by name or
reference the latest ready revision. Both the spec and status traffic targets
can be empty.

The latest revision can be included in the spec traffic targets
two ways
  o by revisionName
  o by setting latestRevision to True.

Managed cloud run provides a single combined status traffic target
for both spec entries with:
  o revisionName set to the latest revision's name
  o percent set to combined percentage for both spec entries
  o latestRevision not set

In this case both spec targets are paired with the combined status
target and a status_percent_override value is used to allocate the
combined traffic.

Attributes:
  key: Either the referenced revision name or 'LATEST' if the traffic targets
    reference the latest ready revision.
  latestRevision: Boolean indicating if the traffic targets reference the
    latest ready revision.
  revisionName: The name of the revision referenced by these traffic targets.
  specPercent: The percent of traffic allocated to the referenced revision
    in the service's spec.
  statusPercent: The percent of traffic allocated to the referenced revision
    in the service's status.
  specTags: Tags assigned to the referenced revision in the service's spec as
    a comma and space separated string.
  statusTags: Tags assigned to the referenced revision in the service's status
    as a comma and space separated string.
  urls: A list of urls that directly address the referenced revision.
  tags: A list of TrafficTag objects containing both the spec and status
    state for each traffic tag.
  displayPercent: Human-readable representation of the current percent
    assigned to the referenced revision.
  displayRevisionId: Human-readable representation of the name of the
    referenced revision.
  displayTags: Human-readable representation of the current tags assigned to
    the referenced revision.
  serviceUrl: The main URL for the service.
c                 Z    Xl         X l        X0l        X@l        XPl        X`l        SU l        g)a  Creates a new TrafficTargetPair.

Args:
  spec_targets: A list of spec TrafficTargets that all reference the same
    revision, either by name or the latest ready.
  status_targets: A list of status TrafficTargets that all reference the
    same revision, either by name or the latest ready.
  revision_name: The name of the revision referenced by the traffic targets.
  latest: A boolean indicating if these traffic targets reference the latest
    ready revision.
  status_percent_override: Percent to use for the status percent of this
    TrafficTargetPair, overriding the values in status_targets.
  service_url: The main URL for the service. Optional.

Returns:
  A new TrafficTargetPair instance.
N)_spec_targets_status_targets_revision_name_latest_status_percent_override_service_url_tags)r%   spec_targetsstatus_targetsrevision_namelateststatus_percent_overrideservice_urls          r   r(   TrafficTargetPair.__init__   s.    ( &)'L$;!#DJr   c                 p    U R                   (       a  [        R                  $ [        R                  " U 5      $ r   )latestRevisionr   LATEST_REVISION_KEYGetKeyr%   s    r   keyTrafficTargetPair.key   s.     "" '' >(/t(<>r   c                     U R                   $ )zBReturns true if the traffic targets reference the latest revision.)r9   rH   s    r   rE    TrafficTargetPair.latestRevision   s     <<r   c                     U R                   $ r   )r8   rH   s    r   revisionNameTrafficTargetPair.revisionName   s    r   c                     U R                   (       a)  [        R                  " [        U R                   5      5      $ [        $ r   )r6   six	text_typer   r
   rH   s    r   specPercentTrafficTargetPair.specPercent   s+    ]];t'9'9:;;%%r   c                     U R                   b   [        R                  " U R                   5      $ U R                  (       a)  [        R                  " [	        U R                  5      5      $ [
        $ r   )r:   rQ   rR   r7   r   r
   rH   s    r   statusPercentTrafficTargetPair.statusPercent   sL    $$0]]48899			]];t';';<==%%r   c                 ~    [         R                  [        S U R                   5       5      5      nU(       a  U$ [        $ )Nc              3   ^   #    U  H#  oR                   (       d  M  UR                   v   M%     g 7fr   r!   r   s     r   r   -TrafficTargetPair.specTags.<locals>.<genexpr>   s     :0EEuquu0r   )_TAGS_JOIN_STRINGjoinsortedr6   r
   )r%   	spec_tagss     r   specTagsTrafficTargetPair.specTags   s4    !&&:d00::<I!9?'??r   c                 ~    [         R                  [        S U R                   5       5      5      nU(       a  U$ [        $ )Nc              3   ^   #    U  H#  oR                   (       d  M  UR                   v   M%     g 7fr   rZ   r   s     r   r   /TrafficTargetPair.statusTags.<locals>.<genexpr>   s     <2eeuquu2r   )r\   r]   r^   r7   r
   )r%   status_tagss     r   
statusTagsTrafficTargetPair.statusTags   s4    #((<d22<<>K%;C+CCr   c                 :    [        S U R                   5       5      $ )Nc              3   ^   #    U  H#  oR                   (       d  M  UR                   v   M%     g 7fr   )r"   r   s     r   r   )TrafficTargetPair.urls.<locals>.<genexpr>   s     ?!5A%!%%!5r   )r^   r7   rH   s    r   urlsTrafficTargetPair.urls   s    ?!5!5???r   c                 T    U R                   c  U R                  5         U R                   $ r   )r<   _ExtractTagsrH   s    r   tagsTrafficTargetPair.tags   s"    zz
::r   c                 ,   0 nU R                    H6  nUR                  (       d  M  [        UR                  SS9XR                  '   M8     U R                   H  nUR                  (       d  M  UR                  U;   a  XR                     nO/UR	                  UR                  [        UR                  5      5      nUR
                  b  UR
                  OSUl        SUl        M     [        UR                  5       [        R                  " S5      S9U l        g)zEExtracts the traffic tag state from spec and status into TrafficTags.T)r&   Nr*   r!   rI   )r6   r!   r   r7   
setdefaultr"   r$   r^   valuesoperator
attrgetterr<   )r%   ro   spec_targetstatus_targetr!   s        r   rn   TrafficTargetPair._ExtractTags   s    D))__($Gd?? * --			d	"$$%oom//M<M<M1NO%2%6%6%B!!cgcl . 8+>+>u+EFDJr   c                     U R                   U R                  :X  a  [        U R                   5      $ SR                  [        U R                  5      [        U R                   5      5      $ )z(Returns human readable revision percent.z{:4} (currently {}))rV   rS   r   r   rH   s    r   displayPercent TrafficTargetPair.displayPercent   sX     T---t1122"))
D,,
-
D..
/1 1r   c                     U R                   (       a(  [        R                  " U 5      < SU R                  < S3$ U R                  $ )z+Returns human readable revision identifier.z (currently ))rE   r   rG   rN   rH   s    r   displayRevisionId#TrafficTargetPair.displayRevisionId   s:     $+NN4$8$($5$57 7 r   c                 z    U R                   nU R                  nX:X  a  U[        :w  a  U$ S$ SR                  X5      $ )Nr*   z{} (currently {}))r`   rf   r
   r   )r%   r_   re   s      r   displayTagsTrafficTargetPair.displayTags  s>    I//K'+CC[KK ''	??r   c                     U R                   $ )zThe main URL for the service.)r;   rH   s    r   
serviceUrlTrafficTargetPair.serviceUrl  s     r   )r9   r8   r;   r6   r:   r7   r<   Nr*   )r+   r,   r-   r.   r/   r(   propertyrI   rE   rN   rS   rV   r`   rf   rk   ro   rn   r{   r   r   r   r0   r1   r   r   r3   r3   K   s$   8J ,.8 > >     & & & & @ @
 D D
 @ @  
G$ 1 1   @ @  r   r3   c                     SnU(       aW  [         R                  U ;   aC  [         R                  U;  a/  X1;   a*  X   nXQ[         R                  '   X0;   a  [        U5      nU$ X	 U$ )a{  Splits the fully-managed latest status target.

For managed the status target for the latest revision is
included by revisionName only and may hold the combined traffic
percent for both latestRevisionName and latestRevision spec targets.
Here we adjust keys in status_dict to match with spec_dict.

Args:
  spec_dict: Dictionary mapping revision name or 'LATEST' to the spec
    traffic target referencing that revision.
  status_dict: Dictionary mapping revision name or 'LATEST' to the status
    traffic target referencing that revision. Modified by this function.
  is_platform_managed: Boolean indicating if the current platform is Cloud Run
    fully-managed.
  latest_ready_revision_name: The name of the latest ready revision.

Returns:
  Optionally, the id of the list of status targets containing the combined
  traffic referencing the latest ready revision by name and by latest.
N)r   rF   id)	spec_dictstatus_dictis_platform_managedlatest_ready_revision_namecombined_status_targets_idlatest_status_targetss         r   _SplitManagedLatestStatusTargetr     sm    ,  $g99YF!!4 /'C/D++,!.#%&;#<  
$# 
1	##r   c                     Sn[        U5      U:X  aN  [        U[        R                     5      n[        U5      n[	        XV5      nU [        R                  :X  a  UnU$ Xg-
  nU$ )zFComputes the optional override percent to apply to the status percent.N)r   r   r   rF   min)rI   r   r>   r   percent_overridespec_by_latest_percentstatus_percentstatus_by_latest_percents           r   _PercentOverrider   6  sq     55(73N3N)OP 0N"#9J
g)))1 
 (B	r   c                    [        U 5      n[        U5      n[        XVX#5      n/ n[        U5      R                  U5       Hm  n	UR	                  U	/ 5      n
UR	                  U	/ 5      n[        XUU5      nU	[        R                  :X  a  UnSnOU	nSnUR                  [        XXX5      5        Mo     [        U[        R                  S9$ )a  Returns a list of TrafficTargetPairs for a Service.

Given the spec and status traffic targets wrapped in a TrafficTargets instance
for a sevice, this function pairs up all spec and status traffic targets that
reference the same revision (either by name or the latest ready revision) into
TrafficTargetPairs. This allows the caller to easily see any differences
between the spec and status traffic.

For fully-managed Cloud Run, the status target for the latest revision is
included by revisionName only and may hold the combined traffic
percent for both latestRevisionName and latestRevision spec targets. This
function splits the fully-managed status target for the latest revision into
a target for the percent allocated to the latest revision by name and a target
for the percent allocated to the latest revision because it is latest.

Args:
  spec_traffic: A traffic.TrafficTargets instance wrapping the spec traffic.
  status_traffic: A traffic.TrafficTargets instance wrapping the status
    traffic.
  is_platform_managed: Boolean indicating whether the current platform is
    fully-managed or Anthos/GKE.
  latest_ready_revision_name: The name of the service's latest ready revision.
  service_url: The main URL for the service. Optional.
Returns:
  A list of TrafficTargetPairs representing the current state of the service's
  traffic assignments. The TrafficTargetPairs are sorted by revision name,
  with targets referencing the latest ready revision at the end.
TFrr   )dictr   setuniongetr   r   rF   appendr3   r^   SortKeyFromTarget)spec_trafficstatus_trafficr   r   rB   r   r   r   resultkr=   r>   r   r?   r@   s                  r   GetTrafficTargetPairsr   E  s    F < )^$+>1 O&y>,a==B'L __Q+N'n(BDG'''0mfmf
MM,*	9: - 
G55	66r   r   )r/   
__future__r   r   r   r   ru   googlecloudsdk.api_lib.runr   rQ   r
   r\   r   r   objectr   r3   r   r   r   r1   r   r   <module>r      sk    5 &  % '  . 
    !6
 8E EP $F  CE87r   