
    Z              
          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e
j                        Z G d de      Z G d de      ZdZdddddddddd	ZdZ G d dej(                        Zd dZy)!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y)ErrorzBase exception for this module.N__name__
__module____qualname____doc__     2lib/googlecloudsdk/api_lib/util/resource_search.pyr   r      s    'r   r   c                       e Zd ZdZy)CollectionNotIndexedzThe collection is not indexed.Nr   r   r   r   r   r   "   s    &r   r   c                       e Zd ZdZy)QueryOperatorNotSupportedz(An operator in a query is not supported.Nr   r   r   r   r   r   &   s    0r   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                       e Zd ZdZd Zd Zy)QueryRewriterz$Resource filter expression rewriter.c                     |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calls     r   RewriteGlobalzQueryRewriter.RewriteGlobalE   s     """r   c           
         ~|dv rt        dj                  |            t        |t              r|n|g}|dk(  r!d}|D cg c]  }dj                  |       }}nf|dk(  ra|}g }|D ]X  }|j	                  t
        dz         r%|j                  |t        t
              dz   d	        @	 |j                  t        |          Z |D cg c]%  }dj                  ||| j                  |            ' }	}dj                  |	      }
t        |	      dkD  rd|
z   dz   }
|
S c c}w # t        $ r t        d
j                  |            w xY wc c}w )zRewrites <key op operand>.)~zDThe [{}] operator is not supported in cloud resource search queries.projectselfLinkz/projects/{}/z@type.   Nz'Collection [{}] not indexed for search.z{key}{op}{value})keyopvaluez OR z( z ))r   format
isinstancelist
startswith CLOUD_RESOURCE_SEARCH_COLLECTIONappendlenRESOURCE_TYPESKeyErrorr   Quotejoin)r'   r0   r1   operandkey_typevaluesr2   collections
collectionpartsexprs              r   RewriteTermzQueryRewriter.RewriteTermS   su    		V|%VBZ! ! #7D1WyF
i
 c;AB6%&&u-6fB	 kf#*  !AC!GH
--
3'G#H1#L#MN
ONMM.45 $ !"   &&32TZZ=N&O  
 ";;uD
5zA~D[4dK- C  N&9@@LN NN"s    D'D*E$EN)r   r   r   r   r)   rE   r   r   r   r$   r$   B   s    ,#*r   r$   Nc           	   #      K   t               j                  |      \  }}t        j                  d||       |t        }|r9dj                  |D cg c]  }|j                  d      r|dd dz   n| c}      }nd}t        j                  dd      }	t        j                  |	j                  |	      d	|	j                  j                  ||
      d| |d      D ]  }
|r|
j                  n|
j                     yc c}w w)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,r+   r/   z desccloudresourcesearchv1Search)orderByqueryresultspageSize)servicemethodrequestfieldlimit
batch_sizebatch_size_attribute)r$   Rewriter	   info	PAGE_SIZEr=   r6   r   GetClientInstancer   YieldFromListResourcesServiceMESSAGES_MODULE)CloudresourcesearchResourcesSearchRequestresourceUrlresource)rS   	page_sizerL   sort_byuri_remote_querynameorder_byclientresults              r   Listri      s
    2 "O++E2/!\((95,OIxx%,.%,T 04s/CabG+M%,. /H H!!"7>&((%%f-$$NN O  %
'f !$&

8
'.s   AC>"C91BC>)NNNNF)r   
__future__r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r	   googlecloudsdk.core.resourcer
   r   r   r   rX   r:   r7   Backendr$   ri   r   r   r   <module>rp      s     ( &  ' ' , * # >(J ('5 '1 1 	 &/) 1!30#'
 $/  ;)11 ;|09r   