
                             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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 S\R*                  5      rS rS r " S S\5      rg)zBackend service.    )absolute_import)division)unicode_literals)batch)
exceptions)request_helper)utils)apis)
properties)parsei  c                       \ rS rSrSrSrg)Error$   zErrors raised by this module. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       4lib/googlecloudsdk/api_lib/compute/client_adapter.pyr   r   $   s    %r   r   c                     [         R                  " U 5      n[         R                  " SR                  UR                  UR
                  5      SU-   5      $ )z.Return a batch URL for the given endpoint URL.z	{0}://{1}zbatch/compute/)r   urlparseurljoinformatschemenetloc)endpoint_urlapi_versionparsed_endpoints      r   _GetBatchUrlr"   (   sE    NN<0/	//1G1GH$
& &r   c                  f    [         R                  R                  R                  R	                  5       $ )z5Check if compute/force_batch_request property is set.)r   VALUEScomputeforce_batch_requestGetBoolr   r   r   _ForceBatchRequestr(   0   s#    				"	"	6	6	>	>	@@r   c                       \ rS rSrSrSrSS jr\S 5       r\S 5       r	\S 5       r
\S	 5       r         SS
 jrSS jrSrg)ClientAdapter5   z+Encapsulates compute apitools interactions.r%   Nc                 F   [         R                  " U R                  US9U l        U=(       d*    [         R                  " U R                  U R                  US9U l        [         R                  " U R                  U R                  5      n[        X@R                  5      U l        g )N)r    )no_http)		core_apisResolveVersion	_API_NAME_api_versionGetClientInstance_clientGetEffectiveApiEndpointr"   
_batch_url)selfr    r-   clientr   s        r   __init__ClientAdapter.__init__9   s|    !00K1D <Y88))7<DL
 44T^^595F5FHL"<1B1BCDOr   c                     U R                   $ N)r1   r6   s    r   r    ClientAdapter.api_versionE   s    r   c                     U R                   $ r;   )r3   r<   s    r   apitools_clientClientAdapter.apitools_clientI   s    <<r   c                     U R                   $ r;   )r5   r<   s    r   	batch_urlClientAdapter.batch_urlM   s    ??r   c                 .    U R                   R                  $ r;   )r3   MESSAGES_MODULEr<   s    r   messagesClientAdapter.messagesQ   s    <<'''r   c                     Ub  UO/ n[        [        R                  " UU R                  R                  U R
                  UUUUUUUU	U
S95      nUc  U(       a  [        R                  " USS9  U$ )a  Sends given request.

Args:
  requests: A list of requests to make. Each element must be a 3-element
    tuple where the first element is the service, the second element is the
    string name of the method on the service, and the last element is a
    protocol buffer representing the request.
  errors_to_collect: A list for capturing errors. If any response contains
    an error, it is added to this list.
  project_override: The override project for the returned operation to poll
    from.
  progress_tracker: progress tracker to be ticked while waiting for
    operations to finish.
  no_followup: If True, do not followup operation with a GET request.
  always_return_operation: If True, return operation object even if
    operation fails.
  followup_overrides: A list of new resource names to GET once the operation
    finishes. Generally used in renaming calls.
  log_warnings: Whether warnings for completed operation should be printed.
  log_result: Whether the Operation Waiter should print the result in past
    tense of each request.
  timeout: The maximum amount of time, in seconds, to wait for the
    operations to reach the DONE state.

Returns:
  A response for each request. For deletion requests, no corresponding
  responses are returned.
)requestshttprB   errorsproject_overrideprogress_trackerno_followupalways_return_operationfollowup_overrideslog_warnings
log_resulttimeoutzCould not fetch resource:)error_message)listr   MakeRequestsr3   rJ   r5   r	   RaiseToolException)r6   rI   errors_to_collectrL   rM   rN   rO   rP   rQ   rR   rS   rK   objectss                r   rV   ClientAdapter.MakeRequestsU   s    T #4"?RF##""oo--#$;1%!	
G   V
 ;=Nr   c                    [        5       (       dz  [        U5      S:X  ak  / nUb  UO/ nUS   u  pVnUR                  R                  nSUR                  l         [	        XV5      " US9n	UR                  U	5        XR                  l        U$ [        R                  " U R                  S9nU H  u  pVnUR!                  XVU5        M     UR#                  U R$                  R&                  [(        S9n/ nUb  UO/ nU H  nUR*                  (       a  [-        UR.                  [        R                  5      (       a0  UR                  [        R                  " UR.                  5      5        O.UR                  [        UR.                  R                  5      5        UR                  UR0                  5        M     U$ ! [        R                   aA  n
UR                  [        R                  " U
5      5        UR                  S5         Sn
A
GNSn
A
f[        R                   al  n
[        U
S5      (       a%  UR                  [        U
R                  5      5        OUR                  [        U
5      5        UR                  S5         Sn
A
GNSn
A
ff = f)ah  Issues async request for given set of requests.

Return immediately without waiting for the operation in progress to complete

Args:
  requests: list(tuple(service, method, payload)), where service is
    apitools.base.py.base_api.BaseApiService, method is str, method name,
    e.g. 'Get', 'CreateInstance', payload is a subclass of
    apitools.base.protorpclite.messages.Message.
  errors_to_collect: list, output only, can be None, contains instances of
    api_exceptions.HttpException for each request with exception.

Returns:
  list of responses, matching list of requests. Some responses can be
    errors.
   Nr   )requestmessage)rB   )max_batch_size)r(   lenr7   num_retriesgetattrappendapitools_exceptions	HttpErrorapi_exceptionsHttpExceptionr   hasattrr^   r   BatchApiRequestr5   AddExecuter3   rJ   _BATCH_SIZE_LIMITis_error
isinstance	exceptionresponse)r6   rI   rX   	responsesrK   servicemethodrequest_bodyra   rp   ro   batch_requestr]   payloadspayloads                  r   AsyncRequestsClientAdapter.AsyncRequests   s   " CMQ$6i$5$A rf&.qk#g|NN..k#$gnn 7+LA" $/nn ++dooFm&.
"'7'73 '/ &&
,,

,= ' h i$5$A rf'))+>+H+HIIMM.66w7H7HIJMM% 1 1 9 9:;))*  ? !** n229=> && 9i((
--i//0
1
--y)
+s%    F5 5J	6HJA!JJ)r1   r5   r3   )NFN)	NNNFFNTTNr;   )r   r   r   r   r   r0   r8   propertyr    r?   rB   rF   rV   rx   r   r   r   r   r*   r*   5   s    3)
D       ( ( #>@:r   r*   N)r   
__future__r   r   r   apitools.base.pyr   r   rd   googlecloudsdk.api_lib.computer   r	   googlecloudsdk.api_lib.utilr
   r.   rf   googlecloudsdk.corer   six.moves.urllibr   rl   r   r"   r(   objectr*   r   r   r   <module>r      sa     &  ' " > 5 9 0 9 D * "  &J &&A
ZF Zr   