
                             d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	mZ ddl	m
Z
 ddl	mZ dd	lmZ ddlmZ dd
lmZ ddlmZ dZ G d dej*                        Zd Zd Z G d de      Zy)zBackend service.    )absolute_import)division)unicode_literals)batch)
exceptions)request_helper)utils)apis)
properties)parsei  c                       e Zd ZdZy)ErrorzErrors raised by this module.N)__name__
__module____qualname____doc__     4lib/googlecloudsdk/api_lib/compute/client_adapter.pyr   r   $   s    %r   r   c                     t        j                  |       }t        j                  dj                  |j                  |j
                        d|z         S )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    t         j                  j                  j                  j	                         S )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                   ~    e Zd ZdZdZddZed        Zed        Zed        Z	ed        Z
	 	 	 	 	 	 	 	 	 dd	Zdd
Zy)ClientAdapterz+Encapsulates compute apitools interactions.r"   Nc                 D   t        j                  | j                  |      | _        |xs, t        j                  | j                  | j                  |      | _        t        j                  | j                  | j                        }t        || j                        | _        y )N)r   )no_http)		core_apisResolveVersion	_API_NAME_api_versionGetClientInstance_clientGetEffectiveApiEndpointr   
_batch_url)selfr   r)   clientr   s        r   __init__zClientAdapter.__init__9   s~    !00K1D <Y88))7<DL
 44T^^595F5FHL"<1B1BCDOr   c                     | j                   S N)r-   r2   s    r   r   zClientAdapter.api_versionE   s    r   c                     | j                   S r6   )r/   r7   s    r   apitools_clientzClientAdapter.apitools_clientI   s    <<r   c                     | j                   S r6   )r1   r7   s    r   	batch_urlzClientAdapter.batch_urlM   s    ??r   c                 .    | j                   j                  S r6   )r/   MESSAGES_MODULEr7   s    r   messageszClientAdapter.messagesQ   s    <<'''r   c                     ||ng }t        t        j                  || j                  j                  | j
                  ||||||||	|
            }||rt        j                  |d       |S )aV  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httpr;   errorsproject_overrideprogress_trackerno_followupalways_return_operationfollowup_overrideslog_warnings
log_resulttimeoutzCould not fetch resource:)error_message)listr   MakeRequestsr/   rA   r1   r	   RaiseToolException)r2   r@   errors_to_collectrC   rD   rE   rF   rG   rH   rI   rJ   rB   objectss                r   rM   zClientAdapter.MakeRequestsU   s    T #4"?RF##""oo--#$;1%!	
G   V
 ;=Nr   c                    t               s~t        |      dk(  rpg }||ng }|d   \  }}}|j                  j                  }d|j                  _        	  t	        ||      |      }	|j                  |	       ||j                  _        |S t        j                  | j                        }|D ]  \  }}}|j!                  |||        |j#                  | j$                  j&                  t(              }g }||ng }|D ]  }|j*                  rt-        |j.                  t        j                        r/|j                  t        j                  |j.                               n.|j                  t        |j.                  j                               |j                  |j0                          |S # t        j                  $ r@}
|j                  t        j                  |
             |j                  d       Y d}
~
d}
~
wt        j                  $ rg}
t        |
d      r%|j                  t        |
j                               n|j                  t        |
             |j                  d       Y d}
~
d}
~
ww xY w)a  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)r;   )max_batch_size)r%   lenr3   num_retriesgetattrappendapitools_exceptions	HttpErrorapi_exceptionsHttpExceptionr   hasattrrT   r   BatchApiRequestr1   AddExecuter/   rA   _BATCH_SIZE_LIMITis_error
isinstance	exceptionresponse)r2   r@   rO   	responsesrB   servicemethodrequest_bodyrW   rf   re   batch_requestrS   payloadspayloads                  r   AsyncRequestszClientAdapter.AsyncRequests   s   " CMQ$6i$5$A rf&.qk#gv|NN..k#$gnn +77F+LA" $/gnn ++dooFm&.
"'67'673 '/ &&
,,

,= ' h i$5$A rf'))+>+H+HIMM.66w7H7HIJMM% 1 1 9 9:;))*  ? !** n229=> && 9i(
--i//0
1
--y)
+s%   $F+ +I6>5G99I6AI11I6)NFN)	NNNFFNTTNr6   )r   r   r   r   r,   r4   propertyr   r9   r;   r>   rM   rn   r   r   r   r'   r'   5   s    3)
D       ( ( #>@:r   r'   N)r   
__future__r   r   r   apitools.base.pyr   r   rZ   googlecloudsdk.api_lib.computer   r	   googlecloudsdk.api_lib.utilr
   r*   r\   googlecloudsdk.corer   six.moves.urllibr   rb   r   r   r%   objectr'   r   r   r   <module>rw      sa     &  ' " > 5 9 0 9 D * "  &J &&A
ZF Zr   