
    -                     ~   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r
  SSKJr  SSKJr   \R                  rS	rS
rSr " S S\
R(                  5      r " S S\
R(                  5      r " S S\5      rSS jrSS jr " S S\R4                  5      r " S S\5      r " S S\5      rg! \ a    \r N{f = f)z5Classes for reading and writing Anthos related files.    )absolute_import)division)unicode_literalsN)
exceptionsyaml)fileszauthentication.gke.io/v1alpha1zauthentication.gke.io/v2alpha1
apiVersionc                       \ rS rSrSrSrg)YamlConfigObjectError(   z@Raised when an invalid Operation is invoked on YamlConfigObject. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       <lib/googlecloudsdk/command_lib/anthos/common/file_parsers.pyr   r   (   s    Hr   r   c                       \ rS rSrSrSrg)YamlConfigFileError,   z%Base class for YamlConfigFile Errors.r   Nr   r   r   r   r   r   ,   s    -r   r   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )YamlConfigObjectFieldError0   z<Raised when an invalid field is used on  a YamlConfigObject.c                 ~   > SR                  X5      nU(       a  SR                  XC5      n[        [        U ]  U5        g )Nz1Invalid field [{}] for YamlConfigObject type [{}]z{}: {})formatsuperr   __init__)selffieldobject_typecustom_message	error_msg	__class__s        r   r!   #YamlConfigObjectFieldError.__init__3   s7    CJJI//)<i	
$d4Y?r   r   N)r   r   r   r   r   r!   r   __classcell__)r'   s   @r   r   r   0   s    D@ @r   r   c                    U(       d  [        U5      eUR                  U5      nUR                  5         U nU(       a  UR                  5       nXe;   a?  [        R
                  " U5      (       a$  U(       a  U(       d  X5U'   UR                  U5      nOqU(       aP  [        R
                  " U5      (       a5  U(       a)  [        R                  " 5       XV'   UR                  U5      nOX5U'   O[        SR                  U5      5      eU(       a  M  U$ )a;  Finds (potentially) nested value based on specified node_path.

If set_value is passed will set the value at item_path,
creating if needed.
Args:
    item: Dict, Map like object to search.
    item_path: str, An item_sep separated path to nested item in map.
    item_sep: str, Path item separator, default is '.'.
    set_value: object, value to set at item_path. If path is not found
      create a new item at item_path with value of set_value.

Returns:
    Object, data found in input item at item_path or None.

Raises:
  KeyError: If item_path not found or empty.
Path [{}] not found)
KeyErrorsplitreversepopr   	dict_likegetcollectionsOrderedDictr   )item	item_pathitem_sep	set_valuepartscontextparts          r   FindOrSetItemInDictr<   ;   s    $ 

9

//(
#%--/'99;D4>>'22 
5!D!g	t~~g..%113'-KK%'#$-,33I>??# 	$ 
.r   c                    U(       d  [        S5      eUR                  U5      nUR                  5         U nU(       a  UR                  5       nXT;   aZ  [        R
                  " U5      (       a?  UR                  U5      nU(       d$  U(       a  XE	 O7[        SR                  U5      5      eUnO[        SR                  U5      5      eU(       a  M  gg)a1  Finds and deletes (potentially) nested value based on specified node_path.

Args:
    item: Dict, Map like object to search.
    item_path: str, An item_sep separated path to nested item in map.
    item_sep: str, Path item separator, default is '.'.

Raises:
  KeyError: If item_path not found or empty.
zMissing Pathr,   N)r-   r.   r/   r0   r   r1   r2   r   )r5   r6   r7   r9   r:   r;   elems          r   DeleteItemInDictr?   g   s     

>
""
//(
#%--/'99;D4>>'22[[dm.55i@A
A*11)<== 	r   c                   b    \ rS rSrSrS r\S 5       rSS jrS r	S r
S	 rS
 rS rS rS rSrg)YamlConfigObject   zAbstraction for managing resource configuration Object.

Attributes:
  content: YAMLObject, The parsed underlying config data.
c                     Xl         g r)   _content)r"   contents     r   r!   YamlConfigObject.__init__   s    Mr   c                 B    [         R                  " U R                  5      $ r)   )copydeepcopyrE   r"   s    r   rF   YamlConfigObject.content   s    ==''r   Nc                 0    [        U R                  XU5      $ )a  Finds (potentially) nested value based on specified item_path.

Args:
    item_path: str, An item_sep separated path to nested item in map.
    item_sep: str, Path item separator, default is '.'.
    set_value: object, value to set at item_path. If path is not found
      create a new item at item_path with value of set_value.

Returns:
    Object, item found in map at item_path or None.
)r<   rE   )r"   r6   r7   r8   s       r   _FindOrSetItemYamlConfigObject._FindOrSetItem   s     t}}i9MMr   c                     [         R                  " U R                  5        [         R                  " U R                  SS9$ )NT
round_trip)r   convert_to_block_textrE   dumprK   s    r   __str__YamlConfigObject.__str__   s)    t}}-99T]]t44r   c                 "    U R                  XS9  g )N)r8   rN   )r"   keyvalues      r   __setitem__YamlConfigObject.__setitem__   s    -r   c                 $    U R                  U5      $ r)   rX   r"   rY   s     r   __getitem__YamlConfigObject.__getitem__   s    s##r   c                 0    [        U R                  U5        g r)   )r?   rE   r^   s     r   __delitem__YamlConfigObject.__delitem__   s    T]]C(r   c                 ,    [        U R                  5      $ r)   )iterrE   rK   s    r   __iter__YamlConfigObject.__iter__   s    r   c                 ,    [        U R                  5      $ r)   )lenrE   rK   s    r   __len__YamlConfigObject.__len__   s    t}}r   c                 H     U R                  U5        g! [         a     gf = f)NFT)rN   r-   )r"   key_paths     r   __contains__YamlConfigObject.__contains__   s.    
(#   s    
!!rD   .N)r   r   r   r   r   r!   propertyrF   rN   rU   r[   r_   rb   rf   rj   rn   r   r   r   r   rA   rA      sI     ( (N5.$)r   rA   c                   V    \ rS rSrSrSrSrSr\S 5       r	S r
S rS	 rS
 rSS jrSrg)LoginConfigObject   z#Auth Login Config file abstraction.zspec.preferredAuthenticationzspec.authenticationz	spec.namec                     U [            $ r)   )API_VERSIONrK   s    r   versionLoginConfigObject.version   s    r   c                     U R                  SS9nU Vs/ s H  nUS   U:X  d  M  XB   c  M  UPM     nnU(       a  UR                  5       $ g s  snf )NF)	name_onlyname)GetAuthProvidersr0   )r"   method_namemethod_type	providersxfounds         r   _FindMatchingAuthMethod)LoginConfigObject._FindMatchingAuthMethod   sc    %%%6IaV9# 	
() 	
9 
  YY[s   AAAc                      U R                  5       nU R                  US5      nU(       a  g g! [        [        4 a     gf = f)z9Returns true is the current preferredAuth Method is ldap.ldapTF)GetPreferredAuthr   r   r-   )r"   	auth_name
found_auths      r   IsLdapLoginConfigObject.IsLdap   sS    '')i//	6Bj	 

  '1 
s   ). A Ac                     U R                   [        :X  a  X R                     $ [        U R                  U R                  R
                  SR                  [        5      5      eNzrequires config version [{}]rx   AUTH_VERSION_2_ALPHAPREFERRED_AUTH_KEYr   r'   r   r   rK   s    r   r   "LoginConfigObject.GetPreferredAuth   sT    ||++))**&t'>'>'+~~'>'>'E'L'L+?(AB Br   c                     U R                   [        :X  a  XU R                  '   g [        U R                  U R                  R
                  SR                  [        5      5      er   r   )r"   
auth_values     r   SetPreferredAuth"LoginConfigObject.SetPreferredAuth   sS    ||++&04""#&t'>'>'+~~'>'>'E'L'L+?(AB Br   c                      X R                      nU(       d  g U(       a  U Vs/ s H  o3S   PM	     sn$ U$ ! [         a     g f = fs  snf )Nr|   )AUTH_PROVIDERS_KEYr-   )r"   r{   r   providers       r   r}   "LoginConfigObject.GetAuthProviders   sV    ../i /89y8vy99  
 :s   6 A
AAr   NT)r   r   r   r   r   r   r   CLUSTER_NAME_KEYrr   rx   r   r   r   r   r}   r   r   r   r   rt   rt      sD    +5,  
BB	r   rt   c                       \ rS rSrSrSS jr\S 5       r\S 5       r\S 5       r	\S 5       r
\S	 5       rS
 rS rS rS r SS jrS rSrg)YamlConfigFile   a  Utility class for searching and editing collections of YamlObjects.

Attributes:
  item_type: class, YamlConfigObject class type of the items in file
  file_contents: str, YAML contents used to load YamlConfigObjects
  file_path: str, file path that YamlConfigObjects were loaded from
  data: [YamlObject], data loaded from file path. Could be 1 or more objects.
  yaml: str, yaml string representation of object.
Nc                    X l         X0l        Xl        U R                   (       d  U R                  (       d  [        S5      eU R                   (       a>   [        R
                  " U R                   SS9nU Vs/ s H
  oQ" U5      PM     snU l        g U R                  (       a>   [        R                  " U R                  SS9nU Vs/ s H
  oQ" U5      PM     snU l        g g s  snf ! [        R                   a  n[        SR                  U5      5      eS nAff = fs  snf ! [        R                   a  n[        SR                  U5      5      eS nAff = f)NzNCould Not Initialize YamlConfigFile:file_contents And file_path Are Both EmptyTrQ   zError Parsing Config File: [{}]zError Loading Config File: [{}])_file_contents
_file_path
_item_typer   r   load_all_dataYAMLParseErrorr   load_all_pathFileLoadError)r"   	item_typefile_contents	file_pathitemsr   fes          r   r!   YamlConfigFile.__init__  s"   'OOt !M N N Pd11dC,12EqilE2
 
P""4??tD,12EqilE2
 
 3   P!"C"J"J2"NOOP
 3 P!"C"J"J2"NOOPsT   #C$ 5CC$ !#D DD C$ $D8DDD E3EEc                     U R                   $ r)   )r   rK   s    r   r   YamlConfigFile.item_type      ??r   c                     U R                   $ r)   )r   rK   s    r   dataYamlConfigFile.data!  s    ::r   c                     [        U R                  5      S:X  a  [        U R                  S   5      $ SR                  U R                   Vs/ s H  n[        U5      PM     sn5      $ s  snf )N   r   z---
)ri   r   strjoin)r"   r   s     r   r   YamlConfigFile.yaml%  sN    
4::!A<<4AQ4554s   A'c                     U R                   $ r)   )r   rK   s    r   r   YamlConfigFile.file_contents+  s    r   c                     U R                   $ r)   )r   rK   s    r   r   YamlConfigFile.file_path/  r   r   c                     U R                   $ r)   r   rK   s    r   rU   YamlConfigFile.__str__3  s    99r   c                     [        U[        5      (       ab  [        U R                  5      [        UR                  5      :H  =(       a0    [	        S [        U R                  UR                  5       5       5      $ g)Nc              3   .   #    U  H  u  pX:H  v   M     g 7fr)   r   ).0r   ys      r   	<genexpr>(YamlConfigFile.__eq__.<locals>.<genexpr>9  s     @%?TQ!&%?s   F)
isinstancer   ri   r   allzip)r"   others     r   __eq__YamlConfigFile.__eq__6  sS    %(($))nEJJ/ A@SEJJ%?@@Br   c                 f    / nU R                    H  nXA   U:X  d  M  UR                  U5        M      U$ )z7Find all YamlObjects with matching data at search_path.r   append)r"   search_pathrZ   resultsobjs        r   FindMatchingItemYamlConfigFile.FindMatchingItem<  s3    Gyy		U	"s  Nr   c                 l    / nU R                    H!  nX1   nU(       d  M  UR                  U5        M#     U$ )z,Find all data in YamlObjects at search_path.r   )r"   r   r   r   rZ   s        r   FindMatchingItemData#YamlConfigFile.FindMatchingItemDataD  s5    Gyye	u  Nr   c                     / nU R                  X5      nU H  nXHU'   UR                  U5        M     U(       a  U R                  5         U$ )z-Find all matching YamlObjects and set values.)r   r   WriteToDisk)	r"   object_pathobject_valuer6   
item_valuepersistr   found_items	ymlconfigs	            r   SetMatchingItemData"YamlConfigFile.SetMatchingItemDataM  sM     G''BK 	'	nnY ! 
Nr   c                    U R                   (       d  [        S5      e[        R                  " 5       n[        R                  " USSS9n[
        R                  " U R                   Vs/ s H  o3R                  PM     snUS9  [        R                  " U R                   5       nUR                  S5        UR                  UR                  5       5        SSS5        gs  snf ! , (       d  f       g= f)zOverwrite Original Yaml File.z-Could Not Write To Config File: Path Is Empty
zutf-8)newlineencoding)streamr   N)r   r   ioBytesIOTextIOWrapperr   dump_all_round_tripr   rF   r	   BinaryFileWriterseekwritegetvalue)r"   out_file_buftmp_yaml_bufr   fs        r   r   YamlConfigFile.WriteToDiskY  s     >> OPP::<L##L$-46L;Aii;$02				/1ggl##%& 
0	/ <	/	/s   &C!1C  
C.)r   r   r   r   )NNr   )r   r   r   r   r   r!   rr   r   r   r   r   r   rU   r   r   r   r   r   r   r   r   r   r   r      s    P,     6 6
     :>
'r   r   rp   )rq   )r   
__future__r   r   r   r3   rI   r   googlecloudsdk.corer   core_exceptionsr   googlecloudsdk.core.utilr	   abccollections_abcAttributeErrorAUTH_VERSION_1_ALPHAr   rw   Errorr   r   r   r<   r?   MutableMappingrA   rt   objectr   r   r   r   <module>r      s    < &  '   	 > $ * OO/ 8 7 IO11 I./// .@!6 @)X>>455 4n<( <~i'V i'y   / s   B1 1B<;B<