
                         
   S r SSKJr  SSKJr  SSKJr  SSKr " S S\R                  5      r	 " S S	\R                  5      r
 " S
 S\R                  5      r " S S\R                  5      r " S S\5      rS rS rS rS rg)z-Utilities for dealing with service resources.    )operations_util)
exceptions)textNc                       \ rS rSrSrg)ServiceValidationError    N__name__
__module____qualname____firstlineno____static_attributes__r	       .lib/googlecloudsdk/api_lib/app/service_util.pyr   r          r   r   c                       \ rS rSrSrg)ServicesDeleteError   r	   Nr
   r	   r   r   r   r      r   r   r   c                   $    \ rS rSr\S 5       rSrg)ServicesNotFoundError!   c           
          U " SR                  [        R                  " [        U5      SSS9SR	                  U5      SR	                  U5      5      5      $ )zFormat a ServiceNotFoundError.

Args:
  requested_services: list of str, IDs of services that were not found.
  all_services: list of str, IDs of all available services

Returns:
  ServicesNotFoundError, error with properly formatted message
z7The following {0} not found: [{1}]

All services: [{2}]zservice waszservices were)plural, )formatr   	Pluralizelenjoin)clsrequested_servicesall_servicess      r   FromServiceLists&ServicesNotFoundError.FromServiceLists#   sR     	$fNN312M"13II()IIl#	%& &r   r	   N)r   r   r   r   classmethodr#   r   r	   r   r   r   r   !   s    & &r   r   c                       \ rS rSrSrg)ServicesSplitTrafficError7   r	   Nr
   r	   r   r   r'   r'   7   r   r   r'   c                   \    \ rS rSrSrSS jrS rS r\S 5       r	S r
S	 rS
 rS rS rSrg)Service;   z,Value class representing a service resource.Nc                 <    Xl         X l        U=(       d    0 U l        g N)projectidsplit)selfr.   id_r0   s       r   __init__Service.__init__>   s    LG"DJr   c                     [        U5      [        L =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r-   )typer*   r.   r/   r1   others     r   __eq__Service.__eq__C   s@    K7" BLLEMM)B.2gg.ACr   c                     X:X  + $ r-   r	   r7   s     r   __ne__Service.__ne__G   s    r   c                 b    UR                  S5      n[        U5      S:w  a  [        S5      eU " U6 $ )N/   zA[{0}] is not a valid resource path. Expected <project>/<service>.)r0   r   r   )r    pathpartss      r   FromResourcePathService.FromResourcePathJ   s9    JJsOE
5zQ" $C D D;r   c                 d    U R                   U R                  4UR                   UR                  4:  $ r-   r.   r/   r7   s     r   __lt__Service.__lt__R   '    LL$''"emmUXX%>>>r   c                 d    U R                   U R                  4UR                   UR                  4:*  $ r-   rF   r7   s     r   __le__Service.__le__U   '    LL$''"u}}ehh&???r   c                 d    U R                   U R                  4UR                   UR                  4:  $ r-   rF   r7   s     r   __gt__Service.__gt__X   rI   r   c                 d    U R                   U R                  4UR                   UR                  4:  $ r-   rF   r7   s     r   __ge__Service.__ge__[   rM   r   c                 N    SR                  U R                  U R                  5      $ )Nz{0}/{1})r   r.   r/   )r1   s    r   __repr__Service.__repr__^   s    DLL$''22r   )r/   r.   r0   r-   )r   r   r   r   __doc__r3   r9   r<   r%   rC   rG   rK   rO   rR   rU   r   r	   r   r   r*   r*   ;   sD    4
C  ?@?@3r   r*   c                 l    [        U 5      [        U5      -
  nU(       a  [        R                  UU5      eg r-   )setr   r#   )filtered_servicesr"   not_found_servicess      r   _ValidateServicesAreSubsetr\   b   s9    ,-L0AA

0
01C1=? ? r   c                     U(       d  [        S U  5       5      nO%[        X Vs/ s H  o"R                  PM     sn5        / nU H)  nX0 Vs/ s H  o"R                  U:X  d  M  UPM     sn-  nM+     U$ s  snf s  snf )a  Return a list of services to act on based on user arguments.

Args:
  all_services: list of Services representing all services in the project.
  args_services: list of string, service IDs to filter for, from arguments
    given by the user to the command line. If empty, match all services.

Returns:
  list of matching Services sorted by the order they were given to the
    command line.

Raises:
  ServiceValidationError: If an improper combination of arguments is given
c              3   8   #    U  H  oR                   v   M     g 7fr-   )r/   ).0ss     r   	<genexpr>&GetMatchingServices.<locals>.<genexpr>y   s     6A44s   )sortedr\   r/   )r"   args_servicesr`   matching_services
service_ids        r   GetMatchingServicesrg   i   sw     
666M}\.J\tt\.JK!j\H\TTZ5G!\HH "	 /K Is   A3
A8"A8c                 *   US:X  a  SOSn[        U R                  5        Vs/ s H  n[        U5      PM     sn5      n[        S5      nUS:X  a  Ue0 n[        R
                  " U 5       H*  u  px[        U5      U-  n	[        X5      n	U	S:X  a  UeXU'   M,     [        [        UR                  5       5      U5      n
U
S-
  n[        UR                  5       5      n[        UR                  5       5       H  u  pxX:X  d  M  Xg==   U-  ss'     U$    U$ s  snf )a  Parses the user-supplied allocations into a format acceptable by the API.

Args:
  args_allocations: The raw allocations passed on the command line. A dict
    mapping version_id (str) to the allocation (float).
  split_method: Whether the traffic will be split by ip or cookie. This
    affects the format we specify the splits in.

Returns:
  A dict mapping version id (str) to traffic split (float).

Raises:
  ServicesSplitTrafficError: if the sum of traffic allocations is zero.
ipr@      zCannot set traffic split to zero. If you would like a version to receive no traffic, send 100% of traffic to other versions or delete the service.g        g      ?)
sumvaluesfloatr'   six	iteritemsroundmaxrc   items)args_allocationssplit_methodmax_decimal_placesr`   sum_of_splitserrallocationsversionr0   
allocationtotal_splits
difference	max_splits                r   ParseTrafficAllocationsr~      s!     )D0qa)9)@)@)BC)BAuQx)BCD-!	# c
I+&67ngu-Jz6JSi% 8 s;--/02DE,c!*+$$&'){0023ngj(		 4 
= Ds   Dc                 x   0 nU H/  n [         R                  " U R                  UR                  5        M1     U(       a  0 nUR                  5        H  u  pgSR                  UU5      XV'   M     [        SR                  [        R                  " [        U5      S5      SR                  [        UR                  5       5      5      5      SR                  [        UR!                  5       5      5      -   5      eg! [         R                   a.  n[
        R                  " U5      X#R                  '    SnAGM/  SnAff = f)zDelete the given services.Nz
[{0}]: {1}zIssue deleting {0}: [{1}]

servicer   z

)r   CallAndCollectOpErrorsDeleteServicer/   MiscOperationErrorrn   	text_typerr   r   r   r   r   r   r   listkeysrl   )
api_clientserviceserrorsr   rw   printable_errorsrf   	error_msgs           r   DeleteServicesr      s   &g.,,

"
"GJJ0  !'
%1%8%89B&D" "0 '..NN3/0)<IId+00234	6 	D)00234	56 6  -- .==-fZZ.s   +C77D9"D44D9)rW   googlecloudsdk.api_lib.appr   googlecloudsdk.corer   googlecloudsdk.core.utilr   rn   Errorr   r   r   r'   objectr*   r\   rg   r~   r   r	   r   r   <module>r      s     4 7 * ) 
Z-- *** &J,, &,
 0 0 $3f $3N?4/d6r   