
    z;                         S r SSKJr  SSKJr  SSKJr  SSKrSSK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KJr  S rS rS rS rS rS rS rS r        SS jrg)zModule for making API requests.    )absolute_import)division)unicode_literalsN)batch_helper)single_request_helper)utils)waiters)log)
properties)zipc                     U  VVs/ s H  u  pnUS;   PM     nnn[        U5      (       a  g[        U5      (       d  g[        S5      es  snnf )z,Checks if all requests are of list requests.)ListAggregatedListListInstancesListManagedInstancesTFz?All requests must be either list requests or non-list requests.)allany
ValueError)requests_methodlist_requestss       4lib/googlecloudsdk/api_lib/compute/request_helper.py_RequestsAreListRequestsr   #   si    
 !) !)1    (  
 	}
IK Ks   Ac                    / n[         R                  " U 5      n US;   a  U R                  S/ 5      nOUS:X  a  U R                  S/ 5      nOUS:X  a  UR                  S5      R                  R                  S5      S   n[        R                  " U S   5       H\  nUR                  SS	5      nU(       a  US
   S:X  a  UR                  S	US   45        UR                  UR                  U/ 5      5        M^     X@R                  SS	5      4$ )a  Extracts data from one *List response page as JSON and stores in dicts.

Args:
  response: str, The *List response in JSON
  service: The service which responded to *List request
  method: str, Method used to list resources. One of 'List' or
    'AggregatedList'.
  errors: list, Errors from response will be appended to  this list.

Returns:
  Pair of:
  - List of items returned in response as dicts
  - Next page token (if present, otherwise None).
r   r   itemsr   managedInstancesr   /warningNcodeUNREACHABLEmessagenextPageToken)
jsonloadsgetGetMethodConfigrelative_pathsplitsix
itervaluesappendextendresponseservicer   errorsr   items_field_namescope_resultr!   s           r   _HandleJsonListr6   3   s     %ZZ!( ((LL"%E''LL+R0E !!..'-c
27x'89   D1g	WV_5tWY/01ll<##$4b9: : 
_d3	33    c                    / nUS;   a  U R                   nOUS:X  a  U R                  nOUR                  S5      R                  R	                  S5      S   nU R                   R
                   H  nUR                  R                  nU(       aA  UR                  UR                  R                  :X  a  UR                  SUR                  45        UR                  [        UR                  U5      5        M     X@R                  4$ )z=Extracts data from one *List response page as Message object.r   r   r   r   r    N)r   r   r)   r*   r+   additionalPropertiesvaluer!   r"   CodeValueValuesEnumr#   r.   r$   r/   getattrr%   r0   s           r   _HandleMessageListr=   _   s    
% ((NNE''%%E ..'-c
27 ;; ""**g	W\\W%@%@%L%LLtW__-.ll7<--/?@A < 
&&	&&r7   c              #     #    U (       a  [        5       (       dB  [        U 5      S:X  a3  U S   u  pVn[        R                  " XVU5      u  pUR	                  U	5        O([
        R                  " XUS9u  pUR	                  U	5        / n
[        U5       Hg  u  pU(       d  M  X   u  pVnU" XUU5      u  pU H  nUv   M	     U(       d  M7  [        R                  " U5      nUUl
        U
R                  XVU45        Mi     U
n U (       a  M  gg7f)a  Makes a series of list and/or aggregatedList batch requests.

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 method
    ('List' or 'AggregatedList'), and the third element is a protocol buffer
    representing either a list or aggregatedList request.
  http: An httplib2.Http-like object.
  batch_url: The handler for making batch requests.
  errors: A list for capturing errors. If any response contains an error, it
    is added to this list.
  response_handler: The function to extract information responses.

Yields:
  Resources encapsulated in format chosen by response_handler as they are
    received from the server.
   r   r   http	batch_urlN)_ForceBatchRequestlenr   MakeSingleRequestr/   r   MakeRequests	enumeratecopydeepcopy	pageTokenr.   )r   rA   rB   r3   response_handlerr2   r   request_body	responsesrequest_errorsnew_requestsir1   request_protobufr   next_page_tokenitemnew_request_protobufs                     r   	_ListCorerU   {   s    $ 	CMQ$6&.qk#g|"7"I"I
<#i mmN#".";";)#i mmN#L +*2+'g'/606 8e$
  
#}}-=>)8&W.BCD ,  H? 	s   CD=DDc                 $    [        XX#[        5      $ )a  Makes a series of list and/or aggregatedList batch requests.

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 method
    ('List' or 'AggregatedList'), and the third element is a protocol buffer
    representing either a list or aggregatedList request.
  http: An httplib2.Http-like object.
  batch_url: The handler for making batch requests.
  errors: A list for capturing errors. If any response contains an error, it
    is added to this list.

Returns:
  Resources encapsulated as protocol buffers as they are received
    from the server.
)rU   r=   r   rA   rB   r3   s       r   _ListrX      s    " 
896H	IIr7   c                    [        XR                  R                  R                  5      (       d  [	        S5      eU R                  5        H?  nUR                  S:w  d  M  UR                  S:w  d  M'  [        XR                  5      c  M?    g   g)zChecks whether operation argument is empty.

Args:
  operation: Operation thats checked for emptyness.
  service: Variable used to access service.client.MESSAGES_MODULE.Operation.

Returns:
  True if operation is empty, False otherwise.
zEoperation must be instance ofservice.client.MESSAGES_MODULE.OperationkindwarningsFT)
isinstanceclientMESSAGES_MODULE	Operationr   
all_fieldsnamer<   )	operationr2   fields      r   _IsEmptyOperationrd      sw     
I~~==GG	H	H
 B C C ##%e

fz!9	::&2 & 
r7   c                  f    [         R                  R                  R                  R	                  5       $ )z5Check if compute/force_batch_request property is set.)r   VALUEScomputeforce_batch_requestGetBool r7   r   rC   rC      s#    				"	"	6	6	>	>	@@r7   c              #      #    U S   S   R                   R                  5          [        XX#[        5       H  nUv   M	     SSS5        g! , (       d  f       g= f7f)a%  Makes a series of list and/or aggregatedList batch requests.

This function does all of:
- Sends batch of List/AggregatedList requests
- Extracts items from responses
- Handles pagination

All requests must be sent to the same client - Compute.

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 method
    ('List' or 'AggregatedList'), and the third element is a protocol buffer
    representing either a list or aggregatedList request.
  http: An httplib2.Http-like object.
  batch_url: The handler for making batch requests.
  errors: A list for capturing errors. If any response contains an error, it
    is added to this list.

Yields:
  Resources in dicts as they are received from the server.
r   N)r]   JsonResponseModelrU   r6   )r   rA   rB   r3   rS   s        r   ListJsonrm      sE     2 {1~..0()_Mj N 100s   !AA	 	A	
AAc              #     #    [        U 5      (       a  [        XX#S9 H  nUv   M	     g[        5       (       d0  [        U 5      S:X  a!  U S   u  pn[        R
                  " XUS9u  nnO[        R                  " XUS9u  nnUR                  U5        SnSn/ nU(       d  U  Vs/ s H  nSPM     nn[        U UU5       GH  u  nnnUc  M  Uu  nnn[        UUR                  R                  R                  5      (       GaS  [        UU5      (       GdA  UR                  R                   S;  Ga&  UnSn[#        US5      (       a}  U(       a  UnOUR$                  nUR&                  (       a  UR                  R(                  nOUUR*                  (       a  UR                  R,                  nO-UR                  R.                  nOUR                  R0                  n[#        US	5      (       aA  UR3                  [4        R6                  " UUUUUR8                  R:                  UUUS
95        GMj  UR3                  [4        R6                  " UUUUUUUS95        GM  Uv   GM     U(       ad  / n[4        R<                  " UUUUUUU	US9 H  nUv   M	     U(       a4  U
(       a,  [>        R@                  " [B        RD                  " SU5      5        ggggs  snf 7f)a  Makes one or more requests to the API.

Each request can be either a synchronous API call or an asynchronous
one. For synchronous calls (e.g., get and list), the result from the
server is yielded immediately. For asynchronous calls (e.g., calls
that return operations like insert), this function waits until the
operation reaches the DONE state and fetches the corresponding
object and yields that object (nothing is yielded for deletions).

Currently, a heterogeneous set of synchronous calls can be made
(e.g., get request to fetch a disk and instance), however, the
asynchronous requests must be homogenous (e.g., they must all be the
same verb on the same collection). In the future, heterogeneous
asynchronous requests will be supported. For now, it is up to the
client to ensure that the asynchronous requests are
homogenous. Synchronous and asynchronous requests can be mixed.

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.
  http: An httplib2.Http-like object.
  batch_url: The handler for making batch requests.
  errors: 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_result: Whether the Operation Waiter should print the result in past
    tense of each request.
  log_warnings: Whether warnings for completed operation should be printed.
  timeout: The maximum amount of time, in seconds, to wait for the operations
    to reach the DONE state.

Yields:
  A response for each request. For deletion requests, no corresponding
  responses are returned.
rW   Nr?   r   )r2   r   rL   r@   )GlobalOperationsServiceRegionOperationsServiceZoneOperationsService#GlobalOrganizationOperationsServiceGlobalAccountsOperationsServiceproject!instanceGroupManagerResizeRequest)rt   resize_request_nameno_followupfollowup_overridealways_return_operation)rt   rw   rx   ry   )operations_datarA   rB   r[   progress_trackerr3   
log_resulttimeoutz!Some requests generated warnings:)#r   rX   rC   rD   r   rE   r   rF   r/   r   r\   r]   r^   r_   rd   	__class____name__hasattrrt   zonezoneOperationsregionregionOperationsglobalOperationsglobalOrganizationOperationsr.   r	   OperationDataru   ra   WaitForOperationsr
   r!   r   ConstructList)r   rA   rB   r3   project_overrider{   rw   ry   followup_overridesr|   log_warningsr}   rS   r2   r   rL   rM   
new_errorsoperation_serviceresource_servicerz   r   requestr1   rx   rt   r[   s                              r   rF   rF      s    v h''	 j  
		#h-1"4$,QK!G\1CC\Iz )55	;Iz--

 /	(011$1.1(I2D/F*gx*&GQ8W^^;;EEFFh00"" +/ 	/
 !g	y	)	)$' (('==%nn;;
__%nn==
%nn==
#NNGG	B	C	C!!! $0$R$R$W$W'"3(?		
 	!!! '"3(?A	B no/Fr H--')	 n	 L	kk


A8
LN !x u 2s   B K"K.HK)NNFFNTTN)__doc__
__future__r   r   r   rH   r&   googlecloudsdk.api_lib.computer   r   r   r	   googlecloudsdk.corer
   r   r,   	six.movesr   r   r6   r=   rU   rX   rd   rC   rm   rF   rj   r7   r   <module>r      s    & &  '   7 @ 0 2 # * 
 K )4X'81hJ(*A
F !aNr7   