
    t                     .   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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.    )absolute_import)division)unicode_literals)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)ServicesSplitTrafficError:   r   Nr   r   r   r   r*   r*   :   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)selfr1   id_r3   s       r   __init__Service.__init__A   s    LG"DJr   c                     [        U5      [        L =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ r0   )typer-   r1   r2   r4   others     r   __eq__Service.__eq__F   s@    K7" BLLEMM)B.2gg.ACr   c                     X:X  + $ r0   r   r:   s     r   __ne__Service.__ne__J   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>.)r3   r!   r
   )r#   pathpartss      r   FromResourcePathService.FromResourcePathM   s9    JJsOE
5zQ" $C D D;r   c                 d    U R                   U R                  4UR                   UR                  4:  $ r0   r1   r2   r:   s     r   __lt__Service.__lt__U   '    LL$''"emmUXX%>>>r   c                 d    U R                   U R                  4UR                   UR                  4:*  $ r0   rI   r:   s     r   __le__Service.__le__X   '    LL$''"u}}ehh&???r   c                 d    U R                   U R                  4UR                   UR                  4:  $ r0   rI   r:   s     r   __gt__Service.__gt__[   rL   r   c                 d    U R                   U R                  4UR                   UR                  4:  $ r0   rI   r:   s     r   __ge__Service.__ge__^   rP   r   c                 N    SR                  U R                  U R                  5      $ )Nz{0}/{1})r   r1   r2   )r4   s    r   __repr__Service.__repr__a   s    DLL$''22r   )r2   r1   r3   r0   )r   r   r   r   __doc__r6   r<   r?   r(   rF   rJ   rN   rR   rU   rX   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 r0   )setr   r&   )filtered_servicesr%   not_found_servicess      r   _ValidateServicesAreSubsetr_   e   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r0   )r2   ).0ss     r   	<genexpr>&GetMatchingServices.<locals>.<genexpr>|   s     6A44s   )sortedr_   r2   )r%   args_servicesrc   matching_services
service_ids        r   GetMatchingServicesrj   l   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.
iprC      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maxrf   items)args_allocationssplit_methodmax_decimal_placesrc   sum_of_splitserrallocationsversionr3   
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DeleteServicer2   MiscOperationErrorrq   	text_typeru   r   r   r   r    r!   r"   listkeysro   )
api_clientserviceserrorsr   rz   printable_errorsri   	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)rZ   
__future__r   r   r   googlecloudsdk.api_lib.appr   googlecloudsdk.corer   googlecloudsdk.core.utilr   rq   Errorr
   r   r   r*   objectr-   r_   rj   r   r   r   r   r   <module>r      s     4 &  ' 6 * ) 
Z-- *** &J,, &,
 0 0 $3f $3N?4/d6r   