
    ,                        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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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%  \RL                  r'\RP                  " 5       (       a  Sr)OSr) " S S\*5      r+g)z>JSON gsutil Cloud API implementation for Google Cloud Storage.    )absolute_import)print_function)division)unicode_literalsN)
exceptions)config)AccessDeniedException)BadRequestException)NotFoundException)PreconditionException)ServiceException)SetUpJsonCredentialsAndCache)NoOpCredentials)pubsub_v1_client)pubsub_v1_messages)system_util)GetCertsFile)GetMaxRetryDelay)
GetNewHttp)GetNumRetries)UTF8zVInsufficient OAuth2 scope to perform this operation. Please re-run `gcloud auth login`zRInsufficient OAuth2 scope to perform this operation. Please re-run `gsutil config`c                   n   ^  \ rS rSrSrSU 4S jjrS rS rS rS r	S r
SS	 jrS
 rS rSS jrSrU =r$ )	PubsubApi8   z;Wraps calls to the Cloud Pub/Sub v1 interface via apitools.c                 $  > [         [        U ]  5         Xl        [	        5       U l        [        5       U l        SU l        [        R                  " SSS5      U l        [        R                  " SSS5      nU(       a  SU-   OSU l        U R                  U R                  -   U R                  -   U l        [        XUS	9  US
:  nUS
:  n[        R                   " U R                  U R                  UUU R"                  S9U l        ['        5       U l        U R(                  U R$                  l        [+        5       U l        U R,                  U R$                  l        [/        U R"                  [0        5      (       a  U R$                  R3                  SS5        gg)a  Performs necessary setup for interacting with Google Cloud Pub/Sub.

Args:
  logger: logging.logger for outputting log messages.
  credentials: Credentials to be used for interacting with Google Cloud
      Pub/Sub
  debug: Debug level for the API implementation (0..3).
zhttps://Credentialsgs_pubsub_hostzpubsub.googleapis.comgs_pubsub_portN: )credentials   )urlhttplog_requestlog_responser!   key'AIzaSyDnacJHrKma0048b13sh8cgxNUwulubmJM)superr   __init__loggerr   
certs_filer   r$   	http_baser   get	host_base	host_porturl_baser   apitools_clientPubsubV1r!   
api_clientr   num_retriesr   max_retry_wait
isinstancer   AddGlobalParam)selfr+   r!   debugr   r%   r&   	__class__s          #platform/gsutil/gslib/pubsub_api.pyr*   PubsubApi.__init__;   s@    
)T#%K"nDODIDNZZ/? 79DNZZ/?FN/=cN*2DN^^dnn4t~~EDM ;GA:KQJL%..4==48II;F<H;?;K;K	MDO %D"&"2"2DOO*,D%)%8%8DOO"$""O44 oo$$U%NP 5    c                     [         R                  " US9n U R                  R                  R	                  U5      $ ! [
         a  nU R                  X1S9   S nAg S nAff = f)Ntopic
topic_name)apitools_messagesPubsubProjectsTopicsGetRequestr4   projects_topicsGet TRANSLATABLE_APITOOLS_EXCEPTIONS_TranslateExceptionAndRaiser9   rC   requestes       r<   GetTopicPubsubApi.GetTopicg   sV    >>ZPGA__,,0099+ A
&&q&@A   $; 
AAAc                     [         R                  " US9n U R                  R                  R	                  U5      $ ! [
         a  nU R                  X1S9   S nAg S nAff = f)N)namerB   )rD   Topicr4   rF   CreaterH   rI   )r9   rC   rA   rL   s       r<   CreateTopicPubsubApi.CreateTopicn   sV    ##4EA__,,33E::+ A
&&q&@ArO   c                     [         R                  " US9n U R                  R                  R	                  U5      $ ! [
         a  nU R                  X1S9   SnAgSnAff = f)zOnly used in system tests.r@   rB   N)rD   !PubsubProjectsTopicsDeleteRequestr4   rF   DeleterH   rI   rJ   s       r<   DeleteTopicPubsubApi.DeleteTopicu   sY    AAGA__,,33G<<+ A
&&q&@ArO   c                     [         R                  " US9n U R                  R                  R	                  U5      $ ! [
         a  nU R                  X1S9   S nAg S nAff = f)N)resourcerB   )rD   'PubsubProjectsTopicsGetIamPolicyRequestr4   rF   GetIamPolicyrH   rI   rJ   s       r<   GetTopicIamPolicyPubsubApi.GetTopicIamPolicy~   sY    GGGA__,,99'BB+ A
&&q&@ArO   c                     [         R                  " US9n[         R                  " XS9n U R                  R                  R                  U5      $ ! [         a  nU R                  XQS9   S nAg S nAff = f)N)policy)r\   setIamPolicyRequestrB   )rD   SetIamPolicyRequest'PubsubProjectsTopicsSetIamPolicyRequestr4   rF   SetIamPolicyrH   rI   )r9   rC   rb   policy_requestrK   rL   s         r<   SetTopicIamPolicyPubsubApi.SetTopicIamPolicy   sj    &::&INGGAGA__,,99'BB+ A
&&q&@As   $A 
A2A--A2c                     U R                   R                  [        R                  5      (       a/  U R                   R	                  S[
        R                  " 5       5        U R                  XS9nU(       a  Uee )a  Translates an HTTP exception and raises the translated or original value.

Args:
  e: Any Exception.
  topic_name: Optional topic name in request that caused the exception.

Raises:
  Translated CloudApi exception, or the original exception if it was not
  translatable.
zTranslateExceptionAndRaise: %srB   )r+   isEnabledForloggingDEBUGr:   	traceback
format_exc_TranslateApitoolsException)r9   rL   rC   translated_exceptions       r<   rI   %PubsubApi._TranslateExceptionAndRaise   sc     {{..
kk8!,,.0;;	 < "  r>   c                 "   [        U[        R                  5      (       a`  [        USS 5      (       aM   [        R
                  " UR                  R                  [        5      5      nSU;   a  SUS   ;   a  US   S   $ g g g g ! [         a     g f = f)Ncontenterrormessage)
r7   apitools_exceptions	HttpErrorgetattrjsonloadsrt   decoder   	Exception)r9   
http_errorjson_objs      r<   _GetMessageFromHttpError"PubsubApi._GetMessageFromHttpError   s    *1;;<<	Y	-	-	ZZ
 2 2 9 9$ ?@( Y(72C%CG$Y// &D  
. =  	
	s   A	B 
BBc                      UR                   S   nUR                  S5      nUS:  a  X#S  R                  S5      S   nSU-  $ g ! [         a     g f = f)Nzwww-authenticatezscope="r   "   zAcceptable scopes: %s)responsefindsplitr}   )r9   r~   www_authenticate	scope_idxscopess        r<   !_GetAcceptableScopesFromHttpError+PubsubApi._GetAcceptableScopesFromHttpError   sp    #,,-?@ #''	2i	a!*-33C8;&// 
  
s   A A 
AAc                    [        U[        R                  5      (       GaF  U R                  U5      nUR                  S:X  a  [        U=(       d    SUR                  S9$ UR                  S:X  ae  S[        U5      ;   a  [        U=(       d    SUR                  S9$ S[        U5      ;   a(  [        [        UR                  U R                  U5      S9$ GOUR                  S	:X  a  S
[        U5      ;   a  [        U=(       d    SUR                  S9$ S[        U5      ;   a  [        U=(       d    SUR                  S9$ S[        U5      ;   a  [        SUR                  S9$ S[        U5      ;   a  [        SUR                  S9$ S[        U5      ;   a(  [        [        UR                  U R                  U5      S9$ [        U=(       d    UR                  UR                  S9$ UR                  S:X  a  [        X1R                  S9$ UR                  S:X  a  U(       a  [        SU-  UR                  S9$ UR                  S:X  a  [        X1R                  S9$ [        X1R                  S9$ g)a  Translates apitools exceptions into their gsutil equivalents.

Args:
  e: Any exception in TRANSLATABLE_APITOOLS_EXCEPTIONS.
  topic_name: Optional topic name in request that caused the exception.

Returns:
  ServiceException for translatable exceptions, None
  otherwise.
i  zBad Request)statusi  zLogin RequiredzAccess denied: login required.insufficient_scope)r   bodyi  z7The account for the specified project has been disabledzAccount disabled.z,Daily Limit for Unauthenticated Use Exceededz8Access denied: quota exceeded. Is your project ID valid?zUser Rate Limit Exceededz5Rate limit exceeded. Please retry this request later.zAccess Not ConfiguredzAccess Not Configured. Please go to the Google Cloud Platform Console (https://cloud.google.com/console#/project) for your project, select APIs and Auth and enable the Google Cloud Pub/Sub API.i  i  zThe topic %s already exists.i  N)r7   rw   rx   r   status_coder
   strr	   "_INSUFFICIENT_OAUTH2_SCOPE_MESSAGEr   rv   r   r   r   )r9   rL   rC   rv   s       r<   rp   %PubsubApi._TranslateApitoolsException   sA    !(2233--a0g	
#	 #7#;m*+--9 	9==Cs1v%&w (H'G./mm= = "SV+ '0]]99!<> > , ==CDAN&w'E2E./mm= =;s1vE&w (C(C./mm= = (3q61&]]$ $ %A.&* ]]$ $ "SV+ '0]]99!<> >
 'w';!))./mm= ===C ??==CJ > K'(}}6 	6==C$W]]CCgmm<<w 4r>   )
r4   r,   r/   r0   r$   r-   r+   r6   r5   r1   )NNr   )N)__name__
__module____qualname____firstlineno____doc__r*   rM   rT   rY   r_   rh   rI   r   r   rp   __static_attributes____classcell__)r;   s   @r<   r   r   8   sF    C*PXAAAAA*	(G= G=r>   r   ),r   
__future__r   r   r   r   rz   rl   rn   apitools.base.pyr   rw   botor   gslib.cloud_apir	   r
   r   r   r   gslib.gcs_json_credentialsr   gslib.no_op_credentialsr   !gslib.third_party.pubsub_apitoolsr   r2   r   rD   gslib.utilsr   gslib.utils.boto_utilr   r   r   r   gslib.utils.constantsr   rx   rH   InvokedViaCloudSdkr   objectr    r>   r<   <module>r      s    E & %  '    >  1 / - 1 , C 3 Q U # . 2 , / &$7$A$A  !!##* %
& %
R= R=r>   