
    s*                        d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddlm	Z	 ddl
Z
d Zd	 Z G d
 dej                        Z G d dej                        Z G d de      Z G d de      Z G d de      Z G d de      Z G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Zy)z)Error classes for gcloud Apigee commands.    )absolute_import)division)unicode_literalsN)
exceptions)yamlc           	         | j                  d      D cg c]  }|d   j                         |dd z    }}dj                  |      }t        j                  ||j                               }t         |di t        |            S c c}w )a$  Returns a human readable string representation of a resource identifier.

  Args:
    resource_type: the type of resource identified by `resource_identifier`.
    resource_identifier: an ordered mapping representing a fully qualified
      identifier for an Apigee Management API resource.
   r      N  )splitupperjoincollections
namedtuplekeysreprdict)resource_typeresource_identifierword
name_wordscapitalized_type
tuple_types         /lib/googlecloudsdk/command_lib/apigee/errors.py_GetResourceIdentifierStringr      s     8E7J7J37OP7OtQ$qr(*7O*PWWZ( %%&6&9&>&>&@B* 
j54 345	66 Qs    Bc                    	 d| v rUg }| d   D ]  }d}|d   |v sd|v s||d   z  } |D cg c]  }|d   	 }}|r| d   dz   t        j                  |      z   S | d   S c c}w # t        $ r dt        j                  |       z   cY S w xY w)a#  Returns a string summarizing `error_info`.

  Attempts to interpret error_info as an error JSON returned by the Apigee
  management API. If successful, the returned string will be an error message
  from that data structure - either its top-level error message, or a list of
  precondition violations.

  If `error_info` can't be parsed, or has no known error message, returns a YAML
  formatted copy of `error_info` instead.

  Args:
    error_info: a dictionary containing the error data structure returned by the
      Apigee Management API.
  details)z+type.googleapis.com/google.rpc.QuotaFailurez2type.googleapis.com/google.rpc.PreconditionFailurez5type.googleapis.com/edge.configstore.bundle.BadBundlez@type
violationsdescriptionmessage
)r   dumpKeyError)
error_infor   itemdetail_types	violationdescriptionss         r   _GetErrorDetailsSummaryr*   /   s    (J jY'$

 =L(\T-A
\*
** ( AKK
9i.
lK	)$t+dii.EEE i   L 
 ( $))J'''(s1   A% A% A% A !A% A%  A% %!B	B	c                       e Zd ZdZy)AmbiguousRequestErrora  Raised when the user makes a request for an ambiguously defined resource.

  Sometimes arguments are optional in the general case because their correct
  values can generally be inferred, but required for cases when that inferrence
  isn't possible. This error covers that scenario.
  N)__name__
__module____qualname____doc__r       r   r,   r,   Y   s     r1   r,   c                   8     e Zd ZdZ	 	 	 	 	 	 	 d fd	Zd Z xZS )RequestErrorz>Raised when a request to the Apigee Management API has failed.c                 0   d | _         |ri	 t        |t              st        |d      r|j	                         }t        j                  |      | _         d| j                   v r| j                   d   | _         || _        || _	        || _
        || _        |sc|sd}|sd}d|d|}|r||rd|z  ndz  }|r|d	t        ||      z   z  }| j                   r|d
t        | j                         z   z  }|r|d|z   z  }t        t        | C  |       y # t        $ r Y w xY w)Ndecodeerroraccessresourcez
Failed to r	   z (%s)r   z:
z

Details: r"   )r   
isinstancestrhasattrr5   jsonloads
ValueErrorreasonr   r   	user_helpr   r*   superr3   __init__	selfr   r   methodr?   bodyr!   r@   	__class__s	           r   rB   zRequestError.__init__f   s-    DL	 $$x)@$zz$'dll"g.$, DK&D2DDN"%+];g	7f%R7	578KM M 	M	=#:4<<#HHH	4)##	,&w//  s   A(D	 		DDc                     | j                   rt        j                  | j                         nd} t        |       || j                  || j
                  || j                        S )z@Returns a copy of the error with a new resource type and method.N)rE   r?   rF   r@   )r   r<   dumpstyper   r?   r@   rD   r   rE   rF   s       r   RewrittenErrorzRequestError.RewrittenError   sO    '+||4::dll#D4:  {{.." "r1   NNNNNNNr-   r.   r/   r0   rB   rL   __classcell__rG   s   @r   r3   r3   c   s(    F "#')0V	"r1   r3   c                   2     e Zd ZdZ	 	 	 	 d fd	Zd Z xZS )ResponseNotJSONErrorzERaised when a request to the Apigee API returns a malformed response.c           
         t        fddD              r'dj                  j                  j                  fz  }nt	        j
                        }t        t        | #  ||d|t        j                  d|i      |       | _        y )Nc              3   6   K   | ]  }t        |        y w)N)r;   ).0attrr6   s     r   	<genexpr>z0ResponseNotJSONError.__init__.<locals>.<genexpr>   s     
G,FD75$,Fs   )msglinenocolnoz%s at %d:%dparseresponse)r@   )allrX   rY   rZ   six	text_typerA   rR   rB   r<   rI   
base_error)rD   r6   r   r   rF   r@   r?   rG   s    `     r   rB   zResponseNotJSONError.__init__   s}     
G,F
GG		5<<EEf}}U#f	
.

J%& /  DOr1   c                     | j                   r| j                   d   nd} t        |       | j                  || j                  || j                        S )z5Returns a copy of the error with a new resource type.r\   N)rF   r@   )r   rJ   r`   r   r@   rK   s       r   rL   z#ResponseNotJSONError.RewrittenError   sH    '+||4<<
#D4:  .." "r1   )NNNNrN   rP   s   @r   rR   rR      s    M "#'&"r1   rR   c                   2     e Zd ZdZ	 	 	 	 	 	 	 d fd	Z xZS )UnauthorizedRequestErrorzDRaised when a request to the Apigee API had insufficient privileges.c           	          |xs d}|xs d}|s0d|d|}|r|d|z   z  }|r|dt        ||      z   z  }|r|d|z   z  }t        t        |   |||||||       y )Nr8   r7   zInsufficient privileges to z the requested z; z
Requested: r"   )r   rA   rc   rB   rC   s	           r   rB   z!UnauthorizedRequestError.__init__   s     "/ZMxF
-!g	4&= 	?%A.&0 0 	0	4)##	
"
&(;VVw	3r1   rM   r-   r.   r/   r0   rB   rO   rP   s   @r   rc   rc      s'    L "#'3 3r1   rc   c                   2     e Zd ZdZ	 	 	 	 	 	 	 d fd	Z xZS )EntityNotFoundErrorzHRaised when a request to the Apigee API mentions a nonexistant resource.c           	          |xs d}|s#d|z  }|r|dt        ||      z   z  }|r|d|z   z  }t        t        |   |||||||       y )Nr8   zRequested %s does not existz: r"   )r   rA   rg   rB   rC   s	           r   rB   zEntityNotFoundError.__init__   sv     "/ZM-?g	46}7JL L 	L	4)##	

&(;VVw	3r1   rM   re   rP   s   @r   rg   rg      s'    P "#'3 3r1   rg   c                   "     e Zd ZdZ fdZ xZS )HttpRequestErrorzxRaised for generic HTTP errors.

  Used for HTTP requests sent to an endpoint other than the Apigee Management
  API.
  c                 v    d}|j                  |||      | _        t        t        |   | j                         y )Nz]- HTTP status: {}
- Reason: {}
- Message: {}
Use the --log-http flag to see more information.)r!   )formatr!   rA   rj   rB   )rD   status_coder?   contenterr_tmplrG   s        r   rB   zHttpRequestError.__init__   s6    CH??;@DL	
D*4<<*@r1   re   rP   s   @r   rj   rj      s    A Ar1   rj   c                   "     e Zd ZdZ fdZ xZS )MissingIdentifierErrora  Raised when a request to the Apigee API is missing an expected identifier.

  Normally this situation should be caught by a required argument being missing
  or similar; this error is a fallback in case a corner case slips past those
  higher level checks.
  c                 >    d|d|d}t         t        |   |       y )NzCommand requires a z but no z was provided.)rA   rq   rB   )rD   namer!   rG   s      r   rB   zMissingIdentifierError.__init__  s    AEtLG	
 $09r1   re   rP   s   @r   rq   rq      s    : :r1   rq   c                   "     e Zd ZdZ fdZ xZS )SourcePathIsNotDirectoryErrorzRaised when the source path is not a directory.

  The deploy command validates that the file path provided by the --source
  command line flag is a directory, and if not, raises this exception.
  c                 N    dj                  |      }t        t        |   |       y )Nz"Source path is not a directory: {})rl   rA   ru   rB   )rD   src_pathrX   rG   s      r   rB   z&SourcePathIsNotDirectoryError.__init__  s#    
.
5
5h
?C	
'7<r1   re   rP   s   @r   ru   ru     s    = =r1   ru   c                   "     e Zd ZdZ fdZ xZS )BundleFileNotValidErrorzRaised when a bundle file is not valid.

  The deploy command validates that the bundle file provided by the
  --bundle-file command line flag is a valid zip archive, and if not, raises
  this exception.
  c                 N    dj                  |      }t        t        |   |       y )Nz*Bundle file is not a valid zip archive: {})rl   rA   ry   rB   )rD   bundle_filerX   rG   s      r   rB   z BundleFileNotValidError.__init__!  s#    
6
=
=k
JC	
!41#6r1   re   rP   s   @r   ry   ry         7 7r1   ry   c                   "     e Zd ZdZ fdZ xZS )LocationResolutionErrora  Raised when the unable to resolve the location of the Apigee organization.

  All the commands in the apigee surface require a valid location to execute.
  This error is raised when the gcloud command is unable to determine the
  location of the Apigee organization.
  c                 0    d}t         t        |   |       y )Nz:Failed to resolve the location of the Apigee organization.)rA   r~   rB   )rD   rX   rG   s     r   rB   z LocationResolutionError.__init__.  s    
FC	
!41#6r1   re   rP   s   @r   r~   r~   &  r|   r1   r~   )r0   
__future__r   r   r   r   r<   googlecloudsdk.corer   r   r^   r   r*   Errorr,   r3   rR   rc   rg   rj   rq   ru   ry   r~   r   r1   r   <module>r      s    0 &  '   * $ 
7&'(TJ,, 7":## 7"t"< "B3| 383, 30A| A
:Z-- 
:	=J$4$4 	=
7j.. 
7
7j.. 
7r1   