
    4&                     N   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mZ dd	lmZ dd
lmZ dZdZ ej*                  ddddd      Zd&dZd Z	 	 d'dZd Zd Z G d de      Zd Zd ZefdZ d Z!d Z"d  Z#d! Z$ G d" d#e      Z%d$ Z&d% Z'y)(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                 R    t        j                  | g|t        j                         y)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                     t        j                  dt        j                        }|j	                  |        |D ]*  }|j                  | D cg c]  }||v r||   nd c}       , |j                          yc c}w )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   s}     $$W#'':'	Vh%+-%+6 ,2X+=x'2E%+- .   
..-s   A7
Nc                 
   |r||_         |r||_        	  | |      }|j                  }|j                  |      r|j                  |      ng }||fS # t        j
                  $ r}t        j                  |t              d}~ww xY w)al  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    " "G
GA7#H))L--n= **>:  \""		&	& A

&
&u.?
@@As   ;A B#A==Bc                     	 | j                   j                  |j                  ||j                              }|S # t        j
                  $ r}t        j                  |t              d}~ww xY w)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   7: A,A''A,c                     d}| j                   r-| j                   j                  r| j                   j                  }n| j                  r| j                  }|r|j                  d      d   S d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                       e Zd ZdZd Zy)ResourcesAndOutputsz&Holds a list of resources and outputs.c                      || _         || _        y N)r   outputs)selfr   rT   s      r   __init__zResourcesAndOutputs.__init__   s    DNDLr   N)__name__
__module____qualname____doc__rV    r   r   rQ   rQ      s
    .r   rQ   c                     | |dS )N)rB   
finalValuer[   )rB   values     r   _BuildOutputr_      s    e	,,r   c           	         t        j                  |       }t        |t              rd|vrg S g }|d   }|D ]  }d|vsd|vr|d   }|d   }t        |t              r?t        t        |            D ]'  }d||fz  }|j                  t        |||                ) et        |t              r.|D ](  }	|d|	d}|j                  t        |||	                * |j                  t        ||              |S )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.
  rT   r]   rB   z%s[%d][])	r	   load
isinstancedictlistr   lenappendr_   )
manifest_layoutlayoutrT   basic_outputsbasic_outputrB   r^   pos
final_namekeys
             r   FlattenLayoutOutputsrp      s     99_%&	FD	!Yf%<I'#-#l<'6+ED&E%s5z"#s+
|Jc
;< # 
E4	 #!%s+
|Jc
;<  nn\$./ $  
.r   c                 p    t        |       |kD  r't        j                  j                  d|z         | d | } | S )NzbNote: maximum of %s resources are shown, please use describe command to show all of the resources.)rg   r   statusr   )r   resource_limits     r   LimitResourcesToDisplayrt      sB    ^n$JJ F%& ' />*I	r   c              #      K   	 | D ]  }|  y# t         j                  $ r}t        j                  |t              d}~ww xY ww)z=Wraps generators to translate HttpErrors into HttpExceptions.N)r-   r.   r/   r0   r1   )	generatoryr:   s      r   YieldWithHttpExceptionsrx      sF     Ag 		&	& A

&
&u.?
@@As"   A AA=AAc                 <   	 | j                   j                  |j                  ||            }|j                   rt        |j                         }ng }| j                  j                  |j                  ||            }g }t        |      }|rN| j                  j                  |j                  |||            }	|	j                  rt        |	j                        }t        ||      }
|
S # t        j                  $ r}t        j                   |t"              d}~ww xY w)z6Returns a ResourcesAndOutputs object for a deployment.r=   
deployment)r=   r{   rK   N)r   List%DeploymentmanagerResourcesListRequestrt   deploymentsr@   &DeploymentmanagerDeploymentsGetRequestrO   	manifests$DeploymentmanagerManifestsGetRequestrj   rp   rQ   r-   r.   r/   r0   r1   )rC   rD   r=   deployment_namer7   r   rM   rT   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                 2    t        | |||      j                  S )z*Returns the fingerprint of the deployment.)FetchDeploymentfingerprint)rC   rD   r=   r   s       r   FetchDeploymentFingerprintr     s    	7O	D	P	PPr   c                     	 | j                   j                  |j                  ||            }|S # t        j                  $ r}t        j                  |t              d}~ww xY w)zReturns the deployment.rz   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   -0 A"AA"c                       e Zd ZdZd Zd Zy)StringPropertyParserzFNo-op string value parser, prints a deprecation warning on first call.c                     d| _         y )NF)warned)rU   s    r   rV   zStringPropertyParser.__init__  s	    DKr   c                 V    | j                   sd| _         t        j                  d       |S )NTzADelimiter '=' is deprecated for properties flag. Use ':' instead.)r   r   warning)rU   r^   s     r   ParseStringsAndWarnz(StringPropertyParser.ParseStringsAndWarn  s%    ;;dk	kk
MOLr   N)rW   rX   rY   rZ   rV   r   r[   r   r   r   r     s    Nr   r   c                 ,    t        j                  |       S rS   )r	   rc   )r^   s    r   ParseAsYamlr   $  s    	5	r   c                  8    t               j                  t        dS )N)=:)r   r   r   r[   r   r   NewParserDictr   (  s    !55
 r   )json)NN)(rZ   
__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   r_   rp   rt   rx   r   r   r   r   r   r   r[   r   r   <module>r      s     B &  ' > D ( . # $ 9   A  !4==	

?@ ( CGA@=2
?& -'T 7N A'ATQ
A6 r   