
    4&                     N   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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rSr\R*                  " SSSSSS9rS&S jrS r  S'S jrS rS r " S S\5      rS rS r\4S jr S r!S r"S  r#S! r$ " S" S#\5      r%S$ r&S% r'g)(z;Common helper methods for DeploymentManager V2 Deployments.    )absolute_import)division)unicode_literals)
exceptions)base)log)yaml)resource_printer)range2   z;ResponseError: code={status_code}, message={status_message}z--simple-listformatstore_constzvalue(name)zwChanges the --format flag to print the resource IDs. Otherwise either the --format value or the default format is used.)destactionconsthelpc                 N    [         R                  " U /U[        R                  S9  g)zPrints the given resource.)	resourcesprint_formatoutN)r
   Printr   r   )resourcer   s     <lib/googlecloudsdk/api_lib/deployment_manager/dm_api_util.pyPrettyPrintr   .   s    

''    c                     [         R                  " S[        R                  S9nUR	                  U 5        U H.  nUR                  U  Vs/ s H  nXC;   a  X4   OSPM     sn5        M0     UR                  5         gs  snf )a  Print a table of results with the specified columns.

Prints a table whose columns are the proto fields specified in the
header list. Any fields which cannot be found are printed as empty.

Args:
  header: A list of strings which are the field names to include in the
      table. Must match field names in the resource_list items.
  resource_list: A list of resource objects, each corresponding to a row
      in the table to print.
table)r    N)r
   Printerr   r   
AddHeading	AddRecordFinish)headerresource_listprinterr   columns        r   
PrintTabler'   6   sy     $$W#'':'	Vh%+-%+6 ,2+=x'2E%+- .   
..-s   	A:
Nc                 &   U(       a  X1l         U(       a  XAl         U " U5      nUR                  nUR                  U5      (       a  UR                  U5      O/ nXv4$ ! [        R
                   a   n[        R                  " U[        5      eSnAff = f)aT  Helper method to get the next set of paginated results.

Args:
  list_method: The method that will execute the list request.
  request: The list request ready to be executed, possibly missing the page
      token.
  resource_field: The field name of the resources in the list results.
  page_token: The page token string to pass into the request, or None
      if no page token should be included.
  limit: Optional limit on how many resources to request.

Returns:
  A tuple containing the list of results and the page token in the
  list response, or None if no page token was in the response.
N)		pageToken
maxResultsnextPageTokenget_assigned_valueapitools_exceptions	HttpErrorapi_exceptionsHttpExceptionHTTP_ERROR_FORMAT)	list_methodrequestresource_field
page_tokenlimitresponsereturn_tokenresultserrors	            r   _GetNextPager;   J   s    " "
A7#H))L--n== **>:  ""		&	& A

&
&u.?
@@As   ?A B0BBc                      U R                   R                  UR                  UUR                  S95      nU$ ! [        R
                   a   n[        R                  " U[        5      eSnAff = f)a+  Helper method gets an operation by name, or raises our custom error.

Args:
  client: The API client to use.
  messages: The API message to use.
  operation: The operation to get.
  project: The project in which to look for the operation.

Returns:
  The specified operation with up-to-date fields.
)project	operationN)	
operationsGet%DeploymentmanagerOperationsGetRequestnamer-   r.   r   r0   r1   )clientmessagesr>   r=   new_operationr:   s         r   GetOperationrF   j   sl    
=%%))66nn 	7 	
M 		&	& =

"
"5*;
<<=s   47 A+A&&A+c                     SnU R                   (       a2  U R                   R                  (       a  U R                   R                  nOU R                  (       a  U R                  nU(       a  UR                  S5      S   $ S$ )zEGiven the response from a Deployment GET, return the manifest's name.N/)updatemanifestsplit)deployment_responsemanifest_urls     r   ExtractManifestNamerO      sa    ,$7$>$>$G$G&--66L##&//L(4		C	 	$>$>r   c                       \ rS rSrSrS rSrg)ResourcesAndOutputs   z&Holds a list of resources and outputs.c                     Xl         X l        g N)r   outputs)selfr   rU   s      r   __init__ResourcesAndOutputs.__init__   s    NLr   )rU   r   N)__name__
__module____qualname____firstlineno____doc__rW   __static_attributes__ r   r   rQ   rQ      s
    .r   rQ   c                 
    XS.$ )N)rB   
finalValuer_   )rB   values     r   _BuildOutputrc      s    	,,r   c           	      (   [         R                  " U 5      n[        U[        5      (       a  SU;  a  / $ / nUS   nU H  nSU;  d  SU;  a  M  US   nUS   n[        U[        5      (       a@  [        [        U5      5       H&  nSXW4-  nUR                  [        XU   5      5        M(     Mp  [        U[        5      (       a2  U H*  n	U< SU	< S3nUR                  [        XU	   5      5        M,     M  UR                  [        XV5      5        M     U$ )a  Takes the layout from a manifest and returns the flattened outputs.

List output 'foo: [A,B]' becomes 'foo[0]: A, foo[1]: B'
Dict output 'bar: {a:1, b:2}' becomes 'bar[a]: 1, bar[b]: 2'
Lists and Dicts whose values are themselves lists or dicts are not expanded.

Args:
  manifest_layout: The 'layout' field from the manifest.

Returns:
  A list of {'name': X, 'finalValue': Y} dicts built out of the 'outputs'
  section of the layout.
rU   ra   rB   z%s[%d][])	r	   load
isinstancedictlistr   lenappendrc   )
manifest_layoutlayoutrU   basic_outputsbasic_outputrB   rb   pos
final_namekeys
             r   FlattenLayoutOutputsrt      s     99_%&	FD	!	!Yf%<I'#-#l<'6+ED&E%s5z"#+
|Jc
;< # 
E4	 	 #!%s+
|Jc
;<  nn\$./ $  
.r   c                 r    [        U 5      U:  a'  [        R                  R                  SU-  5        U S U n U $ )NzbNote: maximum of %s resources are shown, please use describe command to show all of the resources.)rk   r   statusr   )r   resource_limits     r   LimitResourcesToDisplayrx      sB    ^n$JJ F%& ' />*I	r   c              #      #     U  H  nUv   M	     g! [         R                   a   n[        R                  " U[        5      eSnAff = f7f)z=Wraps generators to translate HttpErrors into HttpExceptions.N)r-   r.   r/   r0   r1   )	generatoryr:   s      r   YieldWithHttpExceptionsr|      sE     Ag 		&	& A

&
&u.?
@@As$   A	 A	AAAA	c                 J    U R                   R                  UR                  UUS95      nUR                   (       a  [        UR                   5      nO/ nU R                  R                  UR                  UUS95      n/ n[        U5      nU(       aP  U R                  R                  UR                  UUUS95      n	U	R                  (       a  [        U	R                  5      n[        XW5      n
U
$ ! [        R                   a   n[        R                   " U["        5      eSnAff = f)z6Returns a ResourcesAndOutputs object for a deployment.r=   
deployment)r=   r   rK   N)r   List%DeploymentmanagerResourcesListRequestrx   deploymentsr@   &DeploymentmanagerDeploymentsGetRequestrO   	manifests$DeploymentmanagerManifestsGetRequestrn   rt   rQ   r-   r.   r/   r0   r1   )rC   rD   r=   deployment_namer7   r   rM   rU   rK   manifest_response
return_valr:   s               r   FetchResourcesAndOutputsr      s'   %A$$66& 	7 	
H )(*<*<=ii ,,0077& 	8 	
 G"#67H **..

7
7( 8  
	!	!&'8'?'?@$Y8J		&	& A

&
&u.?
@@As   C+C. .D"DD"c                 .    [        XX#5      R                  $ )z*Returns the fingerprint of the deployment.)FetchDeploymentfingerprint)rC   rD   r=   r   s       r   FetchDeploymentFingerprintr     s    	7	D	P	PPr   c                      U R                   R                  UR                  UUS95      nU$ ! [        R                   a   n[
        R                  " U[        5      eSnAff = f)zReturns the deployment.r~   N)r   r@   r   r-   r.   r/   r0   r1   )rC   rD   r=   r   rM   r:   s         r   r   r     sl    	A ,,0077& 	8 	
 		&	& A

&
&u.?
@@As   *- A!AA!c                   $    \ rS rSrSrS rS rSrg)StringPropertyParseri  zFNo-op string value parser, prints a deprecation warning on first call.c                     SU l         g )NFwarned)rV   s    r   rW   StringPropertyParser.__init__  s	    DKr   c                 b    U R                   (       d  SU l         [        R                  " S5        U$ )NTzADelimiter '=' is deprecated for properties flag. Use ':' instead.)r   r   warning)rV   rb   s     r   ParseStringsAndWarn(StringPropertyParser.ParseStringsAndWarn  s%    ;;dk	kk
MOLr   r   N)rY   rZ   r[   r\   r]   rW   r   r^   r_   r   r   r   r     s    Nr   r   c                 .    [         R                  " U 5      $ rT   )r	   rg   )rb   s    r   ParseAsYamlr   $  s    	5	r   c                  8    [        5       R                  [        S.$ )N)=:)r   r   r   r_   r   r   NewParserDictr   (  s    !55
 r   )json)NN)(r]   
__future__r   r   r   apitools.base.pyr   r-   googlecloudsdk.api_lib.utilr/   googlecloudsdk.callioper   googlecloudsdk.corer   r	   googlecloudsdk.core.resourcer
   	six.movesr   MAX_RESOURCE_TO_DISPLAYr1   ArgumentSIMPLE_LIST_FLAGr   r'   r;   rF   rO   objectrQ   rc   rt   rx   r|   r   r   r   r   r   r   r_   r   r   <module>r      s     B &  ' > D ( . # $ 9   A  ==	

?@ ( CGA@=2
?& -'T 7N A'ATQ
A6 r   