
    [              	          S r SSK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rSrSrSr " S S\R.                  5      r " S S\5      r\" \S\05      \" \5      S.r " S S\R6                  " S/ SQ5      5      r\\" SS0\\5      0r " S S\R<                  5      rS r S r! " S S \5      r"S! r#g)"z completers for resource library.    N)messages)resource)
completers)	arg_utils)registry)resource_parameter_info)
exceptions)log)
properties)	resourcesnamecloudresourcemanager.projects	projectIdc                       \ rS rSrSrSrg)Error'   z!Base error class for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       :lib/googlecloudsdk/command_lib/util/concepts/completers.pyr   r   '   s    )r   r   c                   4    \ rS rSrSrS	S jrS rS rS rSr	g)
ParentTranslator+   a2  Translates parent collections for completers.

Attributes:
  collection: str, the collection name.
  param_translation: {str: str}, lookup from the params of the child
    collection to the params of the special parent collection. If None,
    then the collections match and translate methods are a no-op.
Nc                 0    Xl         U=(       d    0 U l        g N
collectionparam_translation)selfr#   r$   s      r   __init__ParentTranslator.__init__5   s     O.4"Dr   c                     U R                   (       a>  [        R                  " U R                   5       H  u  p#UR                  U5      X'   X	 M     U$ )zETranslate from original parent params to params that match the child.)r$   six	iteritemsget)r%   params
orig_param	new_params       r   ToChildParamsParentTranslator.ToChildParams9   sF    #&==1G1G#H
*#ZZ	2 $I Mr   c                     0 n[         R                  " U R                  5       H%  u  pE UR                  U5        [        X%S5      X4'   M'     U$ ! [         a     M8  f = f)zCGet dict for translating parent params into the given message type.N)r)   r*   r$   field_by_nameKeyErrorgetattr)r%   messagerefmessage_resource_mapr-   special_params         r   MessageResourceMap#ParentTranslator.MessageResourceMapA   sh     &)]]43I3I%J!
j)
 *1T)J& &K    s   A
AAc                    U Vs0 s H.  nU R                   R                  XD5      UR                  U5      _M0     nn[        R                  " U5       HF  u  pFU R                   R                  XD5      nU(       d  M)  UR                  US5      (       a  MB  XeU'   MH      [
        R                  " [
        R                  [
        R                  R                  U R                  5      SUSS9$ s  snf ! [
        R                   aB  n[        R                  " [        R                  " U5      R                  5       5         SnAgSnAff = f)a  Parse the parent resource from parameter info and aggregations.

Args:
  parent_params: [str], a list of params in the current collection's parent
    collection.
  parameter_info: the runtime ResourceParameterInfo object.
  aggregations_dict: {str: str}, a dict of params to values that are
    being aggregated from earlier updates.

Returns:
  resources.Resource | None, the parsed parent reference or None if there
    is not enough information to parse.
N collection_infosubcollectionparam_valuesendpoint_url)r$   r+   GetValuer)   r*   r   ResourceREGISTRYGetCollectionInfor#   r   r
   info	text_typerstrip)	r%   parent_paramsparameter_infoaggregations_dictpr@   valuetranslated_namees	            r   ParseParentTranslator.ParseP   s      A 	""1(.*A*A!*DD    MM"34..2218o	|''>>(-_% 5


#,,>>tO#  " ?? 	hhs}}Q&&()s   5C*AC/ /E8E  Er"   r!   )
r   r   r   r   r   r&   r/   r9   rP   r   r   r   r   r   r   +   s    5 !r   r   
projectsId)rR   r   c                       \ rS rSrSrSrg)CollectionConfig~   zAStores data about special collections for configuring completion.r   Nr   r   r   r   rT   rT   ~   s     Jr   rT   )static_paramsid_field
param_namefilterzlifecycleState:ACTIVEc                      ^  \ rS rSrSr SU 4S jjr\S 5       rS rS r	S r
S rS	 r  SS
 jr  SS jrS r  SS jrSS jrSS jrS r  SS jrS rSrU =r$ )ResourceArgumentCompleter   z?A completer for an argument that's part of a resource argument.c                    > Xl         X0l        U=(       d    0 U l        U=(       d    [        U l        UR
                  nUR                  n	[        [        U ]&  " SUU	USS.UD6  g)zInitializes.T)r#   api_versionparam	parse_allNr   )
resource_spec_method_static_paramsDEFAULT_ID_FIELDrW   	full_namer^   superr[   r&   )r%   ra   r>   methodrV   rW   r_   kwargscollection_namer^   	__class__s             r   r&   "ResourceArgumentCompleter.__init__   sk     'L'-2D0 0DM%//O!--K	
#T3 "	
 r   c                     U R                   $ )zzGets the list method for the collection.

Returns:
  googlecloudsdk.command_lib.util.apis.registry.APIMethod, the method.
)rb   r%   s    r   rg    ResourceArgumentCompleter.method   s     <<r   c                 >    U R                   R                  S5      SS $ )z(Get the parent params of the collection.r<   N)r>   	GetParamsrm   s    r   _ParentParams'ResourceArgumentCompleter._ParentParams   s     ))"-cr22r   c                 <   U R                   R                  S5      S   n[        U R                  R                  5       H2  u  p#U R                  R                  UR                  5      U:X  d  M0  Un  O   Sn0 n[        U R                  R                  SU 5       H~  u  p#[        U R                  UR                  5      nU(       a+  US4XPR                  R                  UR                  5      '   MW  SXPR                  R                  UR                  5      '   M     U$ )z*Helper function to build dict of updaters.r<   rp   r   NT)NF)r>   rq   	enumeratera   
attributes	ParamNamer   CompleterForAttribute)r%   final_parami	attributeattribute_idxupdaters	completers          r   _GetUpdaters&ResourceArgumentCompleter._GetUpdaters   s     &&004R8K!$"4"4"?"?@				%	%inn	5	D A
 mH!%%n}57'(:(:INNKi	BKBFBH##--inn=>BI##--inn=>7 Or   c                     UR                   R                  UR                  5      nU R                  5       n[        R
                  " X1X$U R                  S9$ )zBuilds a ResourceParameterInfo object.

Args:
  parsed_args: the namespace.
  argument: unused.

Returns:
  ResourceParameterInfo, the parameter info for runtime information.
)r}   r#   )CONCEPTSArgNameToConceptInfodestr   r   ResourceParameterInfor#   )r%   parsed_argsargumentresource_infor}   s        r   ParameterInfo'ResourceArgumentCompleter.ParameterInfo   sK      ((==hmmLM  "H"88H??$ $r   c                    [        U R                  SS  Vs/ s H  o"R                  PM     sn5      n[        U Vs/ s H  oDR                  PM     sn5      nU R                  R                   H\  n[        U R                  UR                  5      (       d  M*  UR                  U R                  R                  UR                     5        M^     UR                  U5      $ s  snf s  snf )zEValidates that parent attributes values exitst before making request.Nrp   )	set
parametersr   ra   rv   rx   addattribute_to_params_mapissubset)r%   aggregationsrL   parameters_needing_resolutionaresolved_parametersr{   s          r   ValidateAttributeSources2ResourceArgumentCompleter.ValidateAttributeSources   s    $'"9M(N9MA9M(N$O!|<|!vv|<=''22		t119>>	B	B66y~~F	H 3 )112EFF )O<s
   CC$c                 r   U R                   c  g U R                  U5      (       d  g [        R                  " SR	                  U R
                  R                  S5       Vs/ s H  nX1R                  U5      4PM     snU Vs/ s H  o3R                  UR                  4PM     snUR                  R                  5      5        U R                  X5      n U R                  XUS9n U R                   R3                  U5      nU R                   R4                  nU V	s/ s H  n	U R7                  XUUUS9PM     n
n	[        R                  " SR	                  U
 Vs/ s H  oR9                  5       PM     sn5      5        U
 Vs/ s H!  oRA                  UR9                  5       5      PM#     sn$ s  snf s  snf ! [         a  n[        R                   R"                  R$                  R'                  5       (       a  e [        R                  " [(        R*                  " U5      R-                  5       5        [/        SR	                  [1        U5      [(        R*                  " U5      R-                  5       5      5      eS nAff = fs  sn	f s  snf ! [         Ga!  n[        R                   R"                  R$                  R'                  5       (       a  e [        R                  " [(        R*                  " U5      R-                  5       5        [;        U[<        R>                  5      (       aF  [/        SR	                  [1        U5      [(        R*                  " U5      R-                  5       5      5      e[/        SR	                  U[1        U5      [(        R*                  " U5      R-                  5       5      5      eS nAff = fs  snf )	Nz9Cache query parameters={} aggregations={}resource info={}r<   )parent_translatorz0Could not build query to list completions: {} {})rJ   r   r   zcache items={}zVUpdate query failed, may not have enough information to list existing resources: {} {}zUpdate query [{}]: {} {})!rg   r   r
   rF   formatr>   rq   rB   r   rM   r   attribute_to_args_map_GetParentTranslatorBuildListQuery	Exceptionr   VALUEScoreprint_completion_tracebacksGetBoolr)   rG   rH   r   typeCallr#   _ParseResponseRelativeName
isinstancer   ValidationErrorStringToRow)r%   rJ   r   rL   r   queryrO   responseresponse_collectionritemsrz   items                r   Update ResourceArgumentCompleter.Update   s   {{((66HH	!6**44R8:8 ((+,8:(451ffagg5((>>	@A 11.O/!!.4E " Ge6!!%(h KK22
 !	" !1	 ""12@0<5F # H !	  "
 
hh&&%*
+U>>U
+- . ?DDedT..01eDDK:5  /						;	;	C	C	E	E	hhs}}Q&&()DKK
q'3==#**,. / /	/" , 6						;	;	C	C	E	E	hhs}}Q&&() 
Ax//	0	0 55;VQq!1!8!8!:6<= 	= ,33
a#--*1135 6 66 Esb   F) !F.F3 $5J I;5!J J /J >(N43
I8=B6I33I8;
J N1DN,,N1c                     U R                  X4US9nUR                  nU H  n[        XS5      n	U	c  M  XU'   M     [        XR                  S5      n
[        R
                  R                  XR                  US9$ )z:Gets a resource ref from a single item in a list response.r   r   Nr<   )r#   r,   )_GetParamValuesFromParentdetailed_paramsr4   rW   r   rD   rP   re   )r%   r   r   rJ   r   r   r@   param_namesr_   vallines              r   r   (ResourceArgumentCompleter._ParseResponse  s     11+ 2 -L &55KHT*c	!U 
 8]]B/D##66| $ M Mr   c                     U R                  XUS9nU(       d  0 $ UR                  5       nU(       a  UR                  U5      $ U$ )Nr   )	GetParentAsDictr/   )r%   rJ   r   r   
parent_refr,   s         r   r   3ResourceArgumentCompleter._GetParamValuesFromParent/  sJ    2C   EJi F,,V44Mr   c                     0 nUc  / OUnU H.  nUR                   (       d  M  UR                   X#R                  '   M0     U$ )z:Build a {str: str} dict of name to value for aggregations.)rM   r   )r%   r   rK   aggregations       r   _GetAggregationsValuesDict4ResourceArgumentCompleter._GetAggregationsValuesDict:  sG    %-2<L#				.9.?.?**+ $ r   c                 d   U R                   nUc  gUR                  5       " 5       n[        R                  " U R                  5       H  u  pg[
        R                  " XVU5        M     U R                  XUS9nU(       d  U$ 0 n	U(       a  UR                  XX5      n	[
        R                  " XUU	SS9  U$ )a  Builds a list request to list values for the given argument.

Args:
  parameter_info: the runtime ResourceParameterInfo object.
  aggregations: a list of _RuntimeParameter objects.
  parent_translator: a ParentTranslator object if needed.

Returns:
  The apitools request.
Nr   T)r7   is_primary_resource)
rg   GetRequestTyper)   r*   rc   r   SetFieldInMessager   r9   ParseResourceIntoMessage)
r%   rJ   r   r   rg   r5   fieldrM   parentr7   s
             r   r   (ResourceArgumentCompleter.BuildListQueryC  s     [[F~##%'Gd&9&9:!!'%8 ;^^N.?  AFn.AA
 &&1tM Nr   c                 F   U R                  U5      nU R                  XS9n U R                  U5        g! [        R                   aC    SR                  U R                  5       5      nU[        ;   a  [        R                  U5      s $  g[        R                   a     gf = f)z8Get a special parent translator if needed and available.rK   N.)
r   _GetRawParamValuesForParent_ParseDefaultParentr   #ParentCollectionResolutionExceptionjoinrr   _PARENT_TRANSLATORSr+   r   )r%   rJ   r   rK   r@   keys         r   r   .ResourceArgumentCompleter._GetParentTranslatord  s    77E33 4 =L
|,88 , HHT'')*c	#	#"&&s++ 
$ ?? s   4 AB 
B B c                 t   U R                  5        Vs0 s H  o3UR                  U5      _M     nn[        R                  " U=(       d    0 5       H+  u  pVU(       d  M  UR	                  US5      (       a  M'  XdU'   M-     U R
                  R                  S5      S   nUR	                  US5      c  SXG'   U$ s  snf )zAGet raw param values for the resource in prep for parsing parent.Nr<   rp   fake)rr   rB   r)   r*   r+   r>   rq   )r%   rJ   rK   rL   r@   r   rM   ry   s           r   r   5ResourceArgumentCompleter._GetRawParamValuesForParentx  s    ;?;M;M;OP;Oa~..q11;OLP}}%6%<"=	|''d33"T > &&004R8KT*2"(l Qs   B5c                     [         R                  " [         R                  U R                  SUSS9nUR	                  5       $ )z9Parse the parent for a resource using default collection.r<   Nr=   )r   rC   rD   r>   Parent)r%   r@   r   s      r   r   -ResourceArgumentCompleter._ParseDefaultParent  s<    !!,,!H ??r   c                    U R                  U5      nU R                  XS9n U(       d  U R                  U5      $ UR                  U R	                  5       UU5      $ ! [
        R                   aB  n[        R                  " [        R                  " U5      R                  5       5         SnAgSnAf[
        R                   aB  n[        R                  " [        R                  " U5      R                  5       5         SnAgSnAff = f)a  Gets the parent reference of the parsed parameters.

Args:
  parameter_info: the runtime ResourceParameterInfo object.
  aggregations: a list of _RuntimeParameter objects.
  parent_translator: a ParentTranslator for translating to a special
    parent collection, if needed.

Returns:
  googlecloudsdk.core.resources.Resource | None, the parent resource or None
    if no parent was found.
r   N)r   r   r   rP   rr   r   r   r
   rF   r)   rG   rH   r   )r%   rJ   r   r   rK   r@   rO   s          r   r   #ResourceArgumentCompleter.GetParent  s     77E33 4 =L''55$$T%7%7%9>%68 888 	hhs}}Q&&()?? 	hhs}}Q&&()s(   A  A D/8B,,D8D  Dc                     [        U[        5      (       d  gU R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ )z
Overrides.F)r   r[   ra   r#   rg   )r%   others     r   __eq__ ResourceArgumentCompleter.__eq__  sZ     e677%"5"55 (OOu///(KK5<<')r   )rb   rc   rW   ra   )NNN)NNr!   )r   r   r   r   r   r&   propertyrg   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)rj   s   @r   r[   r[      s    G 9=   30$$	G/Ed 8<'+M" DH26	 9='+B(	 48"&>) )r   r[   c                    U R                   n[        R                  " U5      nUc  gXR                  S   :X  a  UR                  $ U R                  R                  U5      nUR                  n[        R                  " UUR                  5      nUR                  SUS-    nU H!  nUR                  U:X  d  M  UR                  s  $    g)z3Gets the collection for an attribute in a resource.Nrp      )
_collection_infor   APICollectionrv   r   indexapi_nameGetAPICollectionsr^   r   )	ra   r{   resource_collection_inforesource_collectionr|   r   resource_collectionsr,   cs	            r   _MatchCollectionr     s    *;; ..  **2..###**00;-%..(!33**, ../A0AB&aF"VVm  r   c                     Sn[        X5      nU(       a3  U R                  R                  S-   U-   nU R                  R                  nOUR                  S:X  a  SnOg[
        R                  R                  UUS9$ )z4Gets collection info for an attribute in a resource.Nr   projectr   r^   )r   r   r   r^   r   r   rD   rE   )ra   r{   r^   r#   full_collection_names        r   _GetCompleterCollectionInfor     s    +9* 	&&//#5
B 00<<K ~~":				-	-.B:E 
. 
G Gr   c                   <    \ rS rSrSr  SS jr\S 5       rS rSr	g)	CompleterInfoi  z@Holds data that can be used to instantiate a resource completer.Nc                 @    Xl         X l        X0l        X@l        XPl        g r!   )rV   rW   r>   rg   rX   )r%   rV   rW   r>   rg   rX   s         r   r&   CompleterInfo.__init__  s    &M*K Or   c                    UR                    H  nUR                  U:X  d  M  Un  O   [        SR                  U5      5      eUR	                  U5      nUR
                  nUR                  n[        X5      nUR                  [        ;   ad  [        R                  UR                  5      n	[        R                  " UR                  S5      n
U	R                  nU	R                  nU	R                  nU(       d  [!        XgSSU5      $  [        R                  " UR                  SUR"                  S9n
[!        XgXU5      $ ! [        R$                   a    UR                  [&        :w  ap  UR                  R)                  S5      S   S:X  aN  [        R                  [&        5      n	[        R                  " [&        S5      n
U	R                  nU	R                  n NSn
 N[        R*                   a    Sn
 Nf = f)ah  Gets the method, param_name, and other configuration for a completer.

Args:
  resource_spec: concepts.ResourceSpec, the overall resource.
  attribute_name: str, the name of the attribute whose argument will use
    this completer.

Raises:
  AttributeError: if the attribute doesn't belong to the resource.

Returns:
  CompleterInfo, the instantiated object.
z%Attribute [{}] not found in resource.listNr   r   rp   projects)rv   r   AttributeErrorr   rw   completion_request_paramscompletion_id_fieldr   re   _SPECIAL_COLLECTIONS_MAPr+   r   	GetMethodrV   rW   rX   r   r^   UnknownMethodError_PROJECTS_COLLECTIONsplitr   )clsra   attribute_namer   r{   rX   rV   rW   r>   special_inforg   s              r   FromResourceCompleterInfo.FromResource  s    %%	
>	!	 &
 
1
8
8
HJ J((8J77M,,H1-KO  $<<-11/2K2KLl!!/";";VDf"00m&&h**j=D$
KK!!

#
#V%113f, /#% %' && 

#
#';
;''--c226*D 0334HI##$8&A$22((>> fs   *D; ;BG/G/G/.G/c                     U R                   $ )z1Get the APIMethod for an attribute in a resource.)rg   rm   s    r   r   CompleterInfo.GetMethod&  s    ;;r   )r>   rW   rg   rX   rV   )NNNNN)
r   r   r   r   r   r&   classmethodr  r   r   r   r   r   r   r     s*    HHL'+! ;% ;%zr   r   c                 `   ^ ^^  " UUU 4S jS[         5      mTR                  5       (       d  gT$ )z<Gets a resource argument completer for a specific attribute.c                   T   >^  \ rS rSrSr Y Y4UU 4S jjr\UU4S j5       rSrU =r	$ )(CompleterForAttribute.<locals>.Completeri.  z5A specific completer for this attribute and resource.c                    > [         R                  X5      n[        TU ]  " UUR                  UR
                  4UR                  UR                  UR                  S.UD6  g )N)rV   rW   r_   )	r   r  rf   r&   r>   rg   rV   rW   rX   )r%   ra   r  rh   completer_info	Completerrj   s        r   r&   1CompleterForAttribute.<locals>.Completer.__init__1  sc    $11-PnIt%


(
(


 '44!**)) r   c                 ^   > [        [        R                  TT5      R                  5       5      $ )z:Checks whether the completer is valid (has a list method).)boolr   r  r   )r  r  ra   s    r   validate1CompleterForAttribute.<locals>.Completer.validate>  s-     

$
$]N
C
M
M
OQ Qr   r   )
r   r   r   r   r   r&   r	  r  r   r   )rj   r  r  ra   s   @r   r  r  .  s,    ?%2 .  Q Qr   r  N)r[   r  )ra   r  r  s   ``@r   rx   rx   +  s2    Q Q+ Q, 
					r   )$r   collectionsapitools.base.protorpcliter   googlecloudsdk.api_lib.utilr   resource_libgooglecloudsdk.command_lib.utilr   $googlecloudsdk.command_lib.util.apisr   r   (googlecloudsdk.command_lib.util.conceptsr   googlecloudsdk.corer	   r
   r   r   r)   rd   r   _PROJECT_ID_FIELDr   objectr   r   
namedtuplerT   r   ResourceCompleterr[   r   r   r   rx   r   r   r   <module>r!     s   '  / @ 6 : 9 L * # * ) 
 6  *J *Fv F\ ##7$02C#DF!"679 J{--
 J* *H6M+N+<+<>? [)
 < < [)|(G&KF K\r   