o
    Z                  
   @   s   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 G d
d de
jZG dd deZG dd deZdZdddddddddd	ZdZG dd dejZd"d d!ZdS )#z!The Cloud Resource Search lister.    )absolute_import)division)unicode_literals)
list_pager)apis)
exceptions)log)resource_expr_rewritec                   @      e Zd ZdZdS )ErrorzBase exception for this module.N__name__
__module____qualname____doc__ r   r   H/tmp/google-cloud-sdk/lib/googlecloudsdk/api_lib/util/resource_search.pyr          r   c                   @   r
   )CollectionNotIndexedzThe collection is not indexed.Nr   r   r   r   r   r   "   r   r   c                   @   r
   )QueryOperatorNotSupportedz(An operator in a query is not supported.Nr   r   r   r   r   r   &   r   r   i  ProjectDiskHealthCheckHttpHealthCheckHttpsHealthCheckImageInstanceTemplateInstance
Subnetwork)	zcloudresourcemanager.projectszcompute.diskszcompute.healthCheckszcompute.httpHealthCheckszcompute.httpsHealthCheckszcompute.imageszcompute.instanceGroupszcompute.instanceszcompute.subnetworks	resourcesc                   @   s    e Zd ZdZdd Zdd ZdS )QueryRewriterz$Resource filter expression rewriter.c                 C   s   |j S )ay  Rewrites global restriction in call.

    Args:
      call: A list of resource_lex._TransformCall objects. In this case the list
        has one element that is a global restriction with a global_restriction
        property that is the restriction substring to match.

    Returns:
      The global restriction rewrite which is simply the global_restriction
      string.
    )global_restriction)selfcallr   r   r   RewriteGlobalE   s   zQueryRewriter.RewriteGlobalc           
   	      s   ~dv rt dt|tr|n|g} dkr$d dd |D }n9 dkr]|}g }|D ].}|td rE||ttd	 d
  q.z	|t|  W q. t	y\   t
d|w  fdd|D }d|}	t|d	kryd|	 d }	|	S )zRewrites <key op operand>.)~zDThe [{}] operator is not supported in cloud resource search queries.projectselfLinkc                 S   s   g | ]}d  |qS )z/projects/{}/)format.0valuer   r   r   
<listcomp>g   s    z-QueryRewriter.RewriteTerm.<locals>.<listcomp>z@type.   Nz'Collection [{}] not indexed for search.c                    s"   g | ]}d j  |dqS )z{key}{op}{value})keyopr+   )r(   Quoter)   r/   r0   r"   r   r   r,   w   s    z OR z( z ))r   r(   
isinstancelist
startswith CLOUD_RESOURCE_SEARCH_COLLECTIONappendlenRESOURCE_TYPESKeyErrorr   join)
r"   r/   r0   operandkey_typevaluescollections
collectionpartsexprr   r2   r   RewriteTermS   s<   
zQueryRewriter.RewriteTermN)r   r   r   r   r$   rC   r   r   r   r   r    B   s    r    NFc           
   	   c   s    t  |\}}td|| |du rt}|r#ddd |D }nd}tdd}tj	|
|d|jj||d	d
| |ddD ]}	|rG|	jn|	jV  q@dS )a  Yields the list of Cloud Resources for collection.

  Not all collections are indexed for search.

  Args:
    limit: The max number of resources to return. None for unlimited.
    page_size: The max number of resources per response page. The defsult is
      PAGE_SIE.
    query: A resource filter expression. Use @type:collection to filter
      resources by collection. Use the resources._RESOURCE_TYPE_ collection to
      specify CloudResourceSearch resource types. By default all indexed
      resources are in play.
    sort_by: A list of field names to sort by. Prefix a name with ~ to reverse
      the sort for that name.
    uri: Return the resource URI if true.

  Raises:
    CollectionNotIndexed: If the collection is not indexed for search.
    QueryOperatorNotSupported: If the query contains an unsupported operator.
    HttpError: request/response errors.

  Yields:
    The list of Cloud Resources for collection.
  z,Resource search query="%s" remote_query="%s"N,c                 S   s*   g | ]}| d r|dd d n|qS )r%   r.   Nz desc)r5   )r*   namer   r   r   r,      s    "zList.<locals>.<listcomp>cloudresourcesearchv1Search)orderByqueryresultspageSize)servicemethodrequestfieldlimit
batch_sizebatch_size_attribute)r    Rewriter   info	PAGE_SIZEr;   r   GetClientInstancer   YieldFromListResourcesServiceMESSAGES_MODULE)CloudresourcesearchResourcesSearchRequestresourceUrlresource)
rQ   	page_sizerJ   sort_byuri_remote_queryorder_byclientresultr   r   r   List   s2   


rf   )NNNNF)r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r   googlecloudsdk.core.resourcer	   r   r   r   rV   r9   r6   Backendr    rf   r   r   r   r   <module>   s4   
>