
    N!                     R   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Zdd
lmZ  G d dej                         Z G d de      Z G d de	j$                        Z G d de      Z G d de      Z G d de	j$                        Z G d de      Zy	)z&Constructs to poll compute operations.    )absolute_import)division)unicode_literals)
exceptions)utils)waiter)	resourcesN)zipc                       e Zd ZdZy)ErrorzErrors raised by this module.N)__name__
__module____qualname____doc__     7lib/googlecloudsdk/api_lib/compute/operations/poller.pyr   r      s    %r   r   c                   "     e Zd ZdZ fdZ xZS )OperationErrorsz=Encapsulates multiple errors reported about single operation.c                 J   d}|D ]  }t        j                  |      sd} n |r>t        j                  dt        j                  |            }t        t
        |   |       y |D cg c]  }|j                   }}t        t
        |   dj                  |             y c c}w )NFT )titleitems, )	r   ShouldUseYamlConstructListParseErrorssuperr   __init__messagejoin)selferrorsuse_construct_listerrorformatted_errorsmessages	__class__s         r   r   zOperationErrors.__init__%   s    			U	#!  ,,%++F35OT+,<=-34VE%--Vh4OT+DIIh,?@ 5s   (B )r   r   r   r   r   __classcell__)r(   s   @r   r   r   "   s    EA Ar   r   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	PollerzCompute operations poller.Nc                     || _         |j                  | _        | j                  j                  | _        | j                  j                  j
                  | _        || _        || _        y)a  Initializes poller for compute operations.

    Args:
      resource_service: apitools.base.py.base_api.BaseApiService,
          service representing the target of operation.
      target_ref: Resource, optional reference to the expected target of the
          operation. If not provided operation.targetLink will be used instead.
      has_project: If 'projects' token should be in the target link for
          organization operations.
    N)	resource_serviceclientMESSAGES_MODULEr'   	OperationStatusValueValuesEnumstatus_enum
target_refhas_project)r"   r-   r3   r4   s       r   r   zPoller.__init__7   sQ     -D"))DKKK//DM}}..DDD DO"Dr   c                     |j                   rt        |j                   j                        |j                  | j                  j
                  k(  S )
Overrides.)r%   r   r#   statusr2   DONEr"   	operations     r   IsDonezPoller.IsDoneI   s;    IOO2233t//4444r   c                 N    t        j                  |j                               dk(  S )Nz$compute.globalOrganizationOperations)six	text_typeGetCollectionInfor"   operation_refs     r   IsGlobalOrganizationOperationz$Poller.IsGlobalOrganizationOperationP   s(    ==88: CD Dr   c                 N   | j                  |      rt        |d      ryd|j                  v rk| j                  j                  }|j                  j                  d      }d|d   z   }|j                   |j                  d      |j                  |            S t        |d      r| j                  j                  }n9t        |d	      r| j                  j                  }n| j                  j                  }|j                   |j                  d
      di |j                               S )r6   r:   org--zorganizations/   Get)r:   parentIdzoneregionWaitr   )rB   hasattrr:   r.   globalOrganizationOperationssplitrG   GetRequestTypezoneOperationsregionOperationsglobalOperationsrK   AsDict)r"   rA   service
token_list	parent_ids        r   PollzPoller.PollT   s
    ))-8W{>$(.-2I2I(I88g **005j"Z]2i[[
''
 
 
'%//)EF F }f%**g		),,g,,g<<&v&@)=)=)?@B Br   c                 :   | j                   j                  d      }|j                  rd|j                  v ry| j                  xs) t        j
                  j                  |j                        }| j                   j                   |di |j                               S )r6   rG   rD   Nr   )
r-   rO   namer3   r	   REGISTRYParse
targetLinkrG   rS   )r"   r:   request_typer3   s       r   	GetResultzPoller.GetResultj   s    ((77>L ~~&INN2 //
KY//55i6J6JK   $$\%HJ4E4E4G%HIIr   )NF)	r   r   r   r   r   r;   rB   rW   r^   r   r   r   r+   r+   4   s!    "#$5DB,Jr   r+   c                       e Zd Zd Zy)DeletePollerc                      y)r6   Nr   r9   s     r   r^   zDeletePoller.GetResulty   s     r   Nr   r   r   r^   r   r   r   r`   r`   x   s    r   r`   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	OperationBatchz.Wrapper class for a set of batched operations.c                 (    |xs g | _         i | _        y N)_operation_refs
_responses)r"   operation_refss     r   r   zOperationBatch.__init__   s    )/RDDOr   c                 "    || j                   |<   y rf   )rh   )r"   rA   responses      r   SetResponsezOperationBatch.SetResponse   s    %-DOOM"r   c                 8    | j                   j                  |      S rf   )rh   getr@   s     r   GetResponsezOperationBatch.GetResponse   s    ??}--r   c              #   z   K   | j                   D ](  } || j                  j                  |            s%| * y wrf   )rg   rh   rn   )r"   response_funcops      r   GetWithResponsezOperationBatch.GetWithResponse   s2     ""	t**2.	/ #s   1;;c                 ,    t        | j                        S rf   )iterrg   r"   s    r   __iter__zOperationBatch.__iter__   s    $$%%r   c                 d    dj                  dj                  d | j                  D                    S )Nz[{0}]r   c              3   F   K   | ]  }t        j                  |        y wrf   )r=   r>   ).0rs     r   	<genexpr>z)OperationBatch.__str__.<locals>.<genexpr>   s      $8"6Qa"6s   !)formatr!   rg   rv   s    r   __str__zOperationBatch.__str__   s1    >>$)) $8"&"6"6$8 8 9 9r   N)
r   r   r   r   r   rl   ro   rs   rw   r~   r   r   r   rd   rd      s#    6..
&9r   rd   c                   *    e Zd ZdZddZd Zd Zd Zy)BatchPollerz Compute operations batch poller.Nc                     || _         || _        |j                  | _        |j                  | _        | j
                  j                  j                  | _        || _	        y)a  Initializes poller for compute operations.

    Args:
      compute_adapter: googlecloudsdk.api_lib.compute.client_adapter
          .ClientAdapter.
      resource_service: apitools.base.py.base_api.BaseApiService,
          service representing the target of operation.
      target_refs: Resources, optional references to the expected targets of the
          operations. If not provided operation.targetLink will be used instead.
    N)
_compute_adapter_resource_serviceapitools_client_clientr'   	_messagesr0   r1   _status_enum_target_refs)r"   compute_adapterr-   target_refss       r   r   zBatchPoller.__init__   sN     ,D-D"22DL$--DN00FFD#Dr   c                     d}|D ]:  }|j                  |      }|xr# |j                  | j                  j                  k(  }< |S )r6   T)ro   r7   r   r8   )r"   operation_batchis_donerA   rk   s        r   r;   zBatchPoller.IsDone   sG    G( ,,];hEHOOt/@/@/E/EEg ) Nr   c                     g }t        |j                   fd            }|D ]  }t        |d      r j                  j                  }n9t        |d      r j                  j
                  }n j                  j                  }|j                  d      }|j                  |d |di |j                         f        g } j                  j                  ||      }t        ||      D ]U  \  }	}|j                  ||	       |	|	j                  s(|j                  t        |	j                  j                                W |rt#        j$                  |      |S )r6   c                 V    | d u xs# | j                   j                  j                  k7  S rf   )r7   r   r8   )r{   r"   s    r   <lambda>z"BatchPoller.Poll.<locals>.<lambda>   s&    !t)Aqxx4+<+<+A+AAAr   rI   rJ   rK   r   )listrs   rL   r   rP   rQ   rR   rO   appendrS   r   AsyncRequestsr
   rl   r%   r   r#   core_exceptions
MultiError)
r"   r   requestsnot_donerA   rT   r]   errors_to_collect	responsesrk   s
   `         r   rW   zBatchPoller.Poll   s0   HO33AC DH!		',,--=(+,,//,,//++F3loow(N}7K7K7M(NOP " %%33H>OPI#&y(#;-!!-:		(..  1F1F!GH $<
 &&'899r   c                    g }| j                   j                  d      }| j                  xsF |D cg c]:  }t        j                  j                  |j                  |      j                        < c}}|D ]4  }|j                  | j                   d |di |j                         f       6 g }| j                  j                  ||      }|rt        j                  |      |S c c}w )r6   rG   r   )r   rO   r   r	   rZ   r[   ro   r\   r   rS   r   r   r   r   )	r"   r   r   r]   rA   r   r3   r   r   s	            r   r^   zBatchPoller.GetResult   s    H))88?L 	/ .	/-] 
			!	!''6AA
C-	/  "
oo

 
 


-**,
-/ 0 " %%33H>OPI&&'899	/s   ?C"rf   )r   r   r   r   r   r;   rW   r^   r   r   r   r   r      s    ($$:r   r   c                       e Zd Zd Zy)DeleteBatchPollerc                      y rf   r   )r"   r   s     r   r^   zDeleteBatchPoller.GetResult   s     r   Nrb   r   r   r   r   r      s    r   r   )r   
__future__r   r   r   googlecloudsdk.api_lib.computer   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r	   r=   	six.movesr
   r   r   OperationPollerr+   r`   objectrd   r   r   r   r   r   <module>r      s    - &  ' 5 0 . = ) 
 &J &Ae A$AJV## AJH6 9V 94N&(( Nb r   