
    Z                        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rSSK	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Jr  SSKJr  SSK J!r!  SSK"r" " S S\RF                  5      r$Sr%\
RL                  " S5      r'Sr(\RR                  " S/ SQ5      r*Sr+ " S S\$5      r, " S S\$5      r- " S S \$5      r. " S! S"\.5      r/ " S# S$\05      r1S% r2S& r3    S.S' jr4S( r5S/S) jr6S/S* jr7S+ r8\"Rr                  " \Rt                  5       " S, S-\05      5       r;g)0z-Module containing the KCC Declarative Client.    )absolute_import)division)unicode_literalsN)encoding)client_util)utils)resource_name_translator)
exceptions)execution_utils)log)
properties)yaml)
console_io)progress_tracker)resource_filter)filesc                       \ rS rSrSrSrg)ClientException-   General Purpose Exception. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       Rlib/googlecloudsdk/command_lib/util/declarative/clients/declarative_client_base.pyr   r   -       "r   r   z"{{"name":"{}","asset_type":"{}"}}
z\"asset_type\"\: (\".*?)\,z.cnrm.cloud.google.comApiClientArgs)snapshot_timelimit	page_sizeasset_typesparentcontent_typefilter_funcrelationship_typesztable[box](GVK.Kind:label="KRM KIND", SupportsBulkExport.yesno("x", ""):label="BULK EXPORT?", SupportsExport.yesno("x", ""):label="EXPORT?", SupportsIAM.yesno("x", ""):label="IAM?")c                       \ rS rSrSrSrg)ResourceNotFoundExceptionA   r   r   Nr   r   r   r    r,   r,   A   r!   r   r,   c                       \ rS rSrSrSrg)ExportPathExceptionE   z5Exception for any errors raised creating export Path.r   Nr   r   r   r    r/   r/   E   s    =r   r/   c                       \ rS rSrSrSrg)ApplyExceptionI   zDGeneral Exception for any errors raised applying configuration path.r   Nr   r   r   r    r2   r2   I   s    Lr   r2   c                       \ rS rSrSrSrg)ApplyPathExceptionM   zAException for any errors raised reading apply configuration path.r   Nr   r   r   r    r5   r5   M   s    Ir   r5   c                   D    \ rS rSrSr  SS jrS rS rS rS r	S	 r
S
rg)KrmGroupValueKindQ   z*Value class for KRM Group Value Kind Data.Nc                 X    Xl         X l        X`l        X0l        X@l        XPl        Xpl        g N)kindgroupversionbulk_export_supportedexport_supportediam_supportedresource_name_format)selfr<   r=   r?   r@   rA   r>   rB   s           r    __init__KrmGroupValueKind.__init__T   s+     IJL!6,& 4r   c                 X   [         R                  " 5       n[         R                  " 5       nU R                  US'   U R                  US'   U R                  =(       d    SUS'   XS'   U R
                  =(       d    SUS'   U R                  US'   U R                  US'   U R                  US	'   U$ )
z3Convert to Config Connector compatible dict format.GroupKind VersionGVKResourceNameFormatSupportsBulkExportSupportsExportSupportsIAM)	collectionsOrderedDictr=   r<   r>   rB   r?   r@   rA   )rC   gvkoutputs      r    AsDictKrmGroupValueKind.AsDictd   s    

!
!
#C$$&F::CL))CK\\'RC	N5M#'#<#<#BF #'#=#=F #44F ..F=Mr   c                 H    [         R                  " U R                  5       SS9$ )NT
round_trip)r   dumprT   rC   s    r    __str__KrmGroupValueKind.__str__r   s    99T[[]t44r   c                 "    U R                  5       $ r;   )r[   rZ   s    r    __repr__KrmGroupValueKind.__repr__u   s    <<>r   c                    [        U[        5      (       d  gU R                  UR                  :H  =(       a    U R                  UR                  :H  =(       a    U R                  UR                  :H  =(       ay    U R
                  UR
                  :H  =(       aY    U R                  UR                  :H  =(       a9    U R                  UR                  :H  =(       a    U R                  UR                  :H  $ NF)	
isinstancer8   r<   r=   r>   r?   r@   rA   rB   )rC   os     r    __eq__KrmGroupValueKind.__eq__x   s    a*++II @DJJ!''$9 @LLAII%@&&!*A*AA@ !!Q%7%77@ !//1	@
 %%)?)??Ar   c                     [        [        [        U R                  U R                  U R
                  U R                  U R                  U R                  U R                  /5      5      $ r;   )
summaphashr<   r=   r>   r?   r@   rA   rB   rZ   s    r    __hash__KrmGroupValueKind.__hash__   sR    DIItzz4<<1K1K!!4#5#5t7P7P
 	 r   )r?   r@   r=   rA   r<   rB   r>   )NN)r   r   r   r   r   rD   rT   r[   r^   rd   rj   r   r   r   r    r8   r8   Q   s,    2 $(5 5Ar   r8   c                     U S:X  a  gU $ )zHTranslate Resource Format from gcloud values to config-connector values.	terraformhclr   )resource_formats    r    _NormalizeResourceFormatrp      s    #	r   c                     SU ;   aR  U R                  S5      S   R                  S5      S   nU R                  SR                  US9SR                  US95      $ U $ )Nzwww.googleapis.com/   /r   zwww.googleapis.com/{api})apiz{api}.googleapis.com/{api})splitreplaceformat)resource_urirt   s     r    _NormalizeUriry      sm    l*


2
3A
6
<
<S
A!
DC : A Ac A J < C C C LN N	r   c                    [         R                  " X!U S9n[        R                  " U5      nU(       a.  [        R
                  " UR                  5       5      R                  OSn	U=(       d    / n
U(       a=  [        U5      nU(       d  [        SR                  U5      5      eU
R                  U5        [        SSSS[        U
5      UU	/ S9nUR                  USS9n/ nU H<  n[        R                   " U5      nUR#                  SS5      nUR%                  U5        M>     U(       dP  U(       a  S	R                  U5      nOS
nU(       a  SR                  U5      nOS
n[        SR                  UUUS95      eU(       d  SR'                  U5      $  [(        R*                  " USR'                  U5      5        U$ ! [,        [.        4 a  n[1        U5      eSnAff = f)aW  Generate a AssetInventory export data set from api list call.


Calls AssetInventory List API via shared api client (AssetListClient) and
generates a list of exportable assets. If `asset_types_filter`,
`gvk_kind_filter` or `filter_expression` is passed, it will filter out
non-matching resources. If `file_path` is None list will be returned as a
string otherwise it is written to disk at specified path.

Args:
  folder: string, folder parent for resource export.
  project: string, project parent for resource export.
  org: string, organization parent for resource export.
  file_path: string, path to write AssetInventory export file to. If None,
    results are returned as string.
  asset_types_filter: [string], list of asset types to include in the output
    file.
  filter_expression: string, a valid gcloud filter expression. See `gcloud
    topic filter` for more details.
  krm_kind_filter: [string], list of KrmKinds corresponding to asset types to
    include in the output.

Returns:
  string: file path where AssetInventory data has been written or raw data if
    `temp_file_path` is None. Returns None if no results returned from API.

Raises:
  RequiredArgumentException: If none of folder, project or org is provided.
  ResourceNotFoundException: If no resources are found or returned from
    filtering.
  ClientException: Writing file to disk.
)organizationprojectfolderNz'No matching resource types found for {})r#   r$   r%   r(   r&   r'   r)   r*   T)	do_filterz"assetType"z"asset_type"z
 With resource types in [{}].rI   z 
 Matching provided filter [{}].z<No matching resources found for [{parent}] {assets} {filter})r'   assetsfilter
)asset_utilsGetParentNameForExportr   AssetListClientr   CompilestripEvaluate_BuildAssetTypeFilterFromKindr,   rw   extendr"   sortedListr   MessageToJsonrv   appendjoinr   WriteFileAtomically
ValueError	TypeErrorr   )r}   r|   org	file_pathasset_types_filterfilter_expressionkrm_kind_filter
root_assetasset_clientr)   asset_filterkind_filtersargsasset_resultsasset_string_arrayitemitem_str	asset_msg
filter_msges                       r    GetAssetInventoryListInputr      s   N 118*,,Z8, 
 /5578AA $  $)r,0AL%
3
:
:?
KM M%	&
$ ##DD#9-d%%d+H~>Hh' 
 
3::;MNii6==>OPjj
#FMMi
 	N 	DE E 
99'((	4995G+HI  	" As   &F7 7GGGc                 L   U (       d  g[         R                  " 5       n/ nU  Hc  nUnSU;   a  UR                  S5      u  pTUR                  U5      n U H-  nUR	                  US9nUR                  UR                  5        M/     Me     U$ ! [         R                   a     M  f = f)z(Get assetType Filter from KRM Kind list.Nrs   )krm_kind)r	   ResourceNameTranslatorru   find_krmkinds_by_kindget_resourcer   asset_inventory_typeResourceIdentifierNotFoundError)		kind_listname_translatorr   r<   r   _matching_kind_objectskind_objmeta_resources	            r    r   r      s    	,CCE/,dH
d{JJsOka+AA(K+('44h4GM>>? ,  
 $CC s   3BB#"B#c                     [         R                  " 5       n[         R                  " 5       n[        R                  " U SUUR                  UR                  S9nXBR                  5       UR                  5       4$ )NT)r   no_exitin_strout_funcerr_func)ioStringIOr   Execwritegetvalue)cmdr   output_valueerror_value	exit_codes        r    _ExecuteBinaryr     sb    ,+""!!  ") 
))+[-A-A-C	CCr   c                     [         R                  " U SUS9nUS:w  a  [        S5      e[        R                  R                  S5        U$ )NT)r   r   r   r   z3The bulk-export command could not finish correctly.z
Export complete.)r   ExecWithStreamingOutputr   r   statusPrint)r   r   r   s      r    _ExecuteBinaryWithStreamingr     sI    55V-)!^
=? ?**'(	r   c           
      Z   U (       do  [        [        R                  " U5       VVVs/ s H  u  p#n[        U5      PM     snnn5      n[        R
                  R                  SR                  XQ5      5        g [        R
                  R                  SR                  U5      5        g s  snnnf )Nz/Exported {} resource configuration(s) to [{}].
z,Exported resource configuration(s) to [{}].
)rg   oswalklenr   r   r   rw   )preexisting_file_countpathrdfiles_in_dir
file_counts         r    _BulkExportPostStatusr   !  s    	57WWT]C]1q\\	]CEJJJGNN  JJ7>>tDF	 	Ds   B&c                   ,   \ rS rSrSr\\R                  S 5       5       r\\R                  S 5       5       r	\R                  S 5       r
\R                  S 5       rSS jrS	 rS
 rS rS r   SS jrS rSS jrSS jrSS jr   SS jrSS jrSrg)DeclarativeClientBasei,  z)KRM Yaml Export based Declarative Client.c                     g r;   r   rZ   s    r    binary_name!DeclarativeClientBase.binary_name0       	r   c                     g r;   r   rZ   s    r    binary_prompt#DeclarativeClientBase.binary_prompt5  r   r   c                     g r;   r   rC   r   s     r    
BulkExport DeclarativeClientBase.BulkExport:      r   c                     g r;   r   r   s     r    	ExportAllDeclarativeClientBase.ExportAll>  r   r   Nc                 Z   SSK Jn  U(       d2  [        R                  R                  R
                  R                  5       n UR                  U R                  5      U l	        X l        Xl        g ! UR                   a,    UR                  U R                  U R                  S9U l	         NHf = f)Nr   )binary_operations)prompt)&googlecloudsdk.command_lib.util.anthosr   r   VALUEScoreaccountGetCheckForInstalledBinaryr   _export_serviceMissingExecutableExceptionInstallBinaryNoOverridesr   _use_account_impersonation_account)rC   gcp_accountimpersonatedbin_opss       r    rD   DeclarativeClientBase.__init__B  s    S%%**22668k7$<<T=M=MNd '3#M	 -- 7$==


4#5#5 > 7d7s    A. .9B*)B*c                     SSK Jn   UR                  U R                  U R                  S9$ ! [
         a  n[        SR                  U5      5      eS nAff = f)Nr   )store)r   allow_account_impersonationz"Error Configuring KCC Client: [{}])googlecloudsdk.core.credentialsr   GetFreshAccessTokenr   r   	Exceptionr   rw   )rC   r   r   s      r    	_GetTokenDeclarativeClientBase._GetTokenP  sa    5:&&--&*&E&E ' G G  :
.
5
5a
8: ::s   #, 
AAAc                 .    UR                  5       S:X  a  gg)N-FT)r   )rC   r   s     r    _OutputToFileOrDir(DeclarativeClientBase._OutputToFileOrDir\  s    zz|sr   c                    [         R                  R                  UR                  5       5      n [         R                  R	                  U5      (       a  [
        R                  " U5      (       a  g[
        R                  " [         R                  R                  U5      5      (       a=  [        R                  " SR                  U5      SSSS9  [
        R                  " U5        g[        [        R                  5      e! [         a    [!        SR                  U5      5      e[         a    [!        SR                  U5      5      ef = f)z3Try to create output directory if it doesnt exists.Nz2Path {} does not exists. Do you want to create it?Tz!Export aborted. No files written.)defaultcancel_on_nocancel_stringz0Can not export to path. [{}] is not a directory.zPCan not export to path [{}]. Ensure that enclosing path exists and is writeable.)r   r   abspathr   isdirr   HasWriteAccessInDirdirnamer   PromptContinuerw   MakeDirOSErrorerrnoEACCESr   r/   )rC   r   	directorys      r    _TryCreateOutputPath*DeclarativeClientBase._TryCreateOutputPatha  s    

-II	y	!	!e&?&?	&J&J		"	"277??9#=	>	>!!@GGM=		?
 	dell## ; !--3VD\; ; I !;;A6$<I IIs   ?C= /A4C= $C= =AEc                 b    [        USS 5      =(       d    U R                  [        USS 5      5      $ )Nresource_typesresource_types_file)getattr_ParseKindTypesFileDatar   s     r    _ParseResourceTypes)DeclarativeClientBase._ParseResourceTypesx  s:    4) C 88t%:DACCr   c                    U R                   SU R                  5       U/nUS:X  a$  U(       d  [        S5      eUR                  U/5        US:X  Ga:  UR                  S[	        USS5      /5        U(       d  UR                  S5      (       a  UR                  S	UR                  /5        OUR                  S
5      (       a  UR                  SUR                  /5        OXUR                  =(       d2    [        R                  R                  R                  R                  5       nUR                  SU/5        U(       dN  UR                  S5      (       d  UR                  S5      (       a"  UR                  SU R                  U5      /5        [	        USS5      (       a  UR                  SUR                  /5        [	        USS5      (       aI  UR                  S[        UR                   5      /5        UR                   S:X  a  UR                  SS/5        U R#                  UR$                  5      (       a  UR                  SUR$                  /5        U$ )zEConstructs and returns a list representing the binary export command.--oauth2-tokenexportzF`_GetBinaryExportCommand` requires a resource uri for export commands.zbulk-exportz
--on-erroron_errorignorer{   z--organizationr}   z--folderz	--projectr  r  z--resource-typesstorage_pathNz--storage-keyro   z--resource-formatrm   z--iam-formatnonez--output)r   r   r   r   r  IsSpecifiedr{   r}   r|   r   r   r   	GetOrFailr  r  rp   ro   r   r   )rC   r   command_namerx   skip_parentskip_filterr   r|   s           r    _GetBinaryExportCommand-DeclarativeClientBase._GetBinaryExportCommand}  s    	.,C x01 	1 
jj,  }$	jj,j( CDE N++
**&(9(9:
;h''
**j$++.
/LLNJ$5$5$:$:$B$B$L$L$N'
**k7+
,-..233
**($*B*B4*HI
Jt^T**	jj/4#4#456t&--	jj%*4+?+?@B C 
			,

NF+, tyy))	jj*dii()Jr   c                 4   [        U5      n[        R                  " SSS9   U R                  USUS9n[	        U5      u  pVnSSS5        WS:w  aZ  SW;   a  [        S	R                  U5      5      eS
U;   a  [        SR                  U5      5      e[        SR                  U5      5      eU R                  UR                  5      (       d  [        R                  R                  W5        [        R                  R                  S5        U$ ! , (       d  f       N= f)z/Exports a single resource's configuration file.zExporting resourcesAborted Export.messageaborted_messager  )r   r  rx   Nr   zresource not foundz?Could not fetch resource: 
 - The resource [{}] does not exist.	Error 403zPermission Denied during export. Please ensure resource API is enabled for resource [{}] and Cloud IAM permissions are set properly.Error executing export:: [{}]zExported successfully.)ry   r   ProgressTrackerr"  r   r,   rw   r   r   r   r   outr   r   )rC   r   rx   normalized_resource_urir   r   r   r   s           r    ExportDeclarativeClientBase.Export  s   +L9		)	)%7H
J((. ) 0c .<C-@*i{
J A~		,'NV+,. 	. +%"F<02 	2
 +22;?A 	A""499--	ggmmL!JJ-.1
J 
Js    D		
Dc           
         U R                  UR                  5      (       Ga!  U R                  UR                  5        [        [        R
                  " UR                  5       VVVs/ s H  u  pEn[        U5      PM     snnn5      n[        R                  " SR                  UR                  5      SS9   [        XS9u  pn
SSS5        WS:w  a\  SW
;   a<  SnUR                  (       a  US	R                  UR                  5      -  n[        U5      e[        S
R                  U
5      5      e[        XrR                  5        U$ [        R                  R!                  S5        [#        XS9$ s  snnnf ! , (       d  f       N= f)z>Execute actual bulk-export command on config-connector binary.z)Exporting resource configurations to [{}]r%  r&  )r   r   Nr   r)  zXPermission denied during export. Please ensure the Cloud Asset Inventory API is enabled.zH Also check that Cloud IAM permissions are set for `--storage-path` [{}]r*  z/Exporting resource configurations to stdout...
)r   r   r  rg   r   r   r   r   r+  rw   r   r  r   r   r   r   r   r   )rC   r   r   asset_list_inputr   r   r   r   r   r   r   msgs               r    _CallBulkExport%DeclarativeClientBase._CallBulkExport  sO   tyy))
		*"79wwtyy7I
J7I!3|3|7I
J L++=DDTYYO+- %3%.!	k- 
a+%9# 89?)):++C  $
$-44[AC C 	4ii@ 
jjIJ(SJJ7 K- -s   &E+5E22
F c                     U R                   SSU/n[        U5      u  p4nUS:w  a  [        SR                  U5      5      eU$ )z1Calls `print-resources` on the underlying binary.zprint-resourcesz--output-formatr   z5Error occured while listing available resources: [{}])r   r   r   rw   )rC   output_formatr   r   r   r   s         r    _CallPrintResources)DeclarativeClientBase._CallPrintResources  s[     	/1BC ,:#+>(I[A~
A
H
H  r   c                    U(       d2  U(       d+  U(       d$  [         R                  " U R                  SS9SS9nU$ U(       a  SR                  U5      nO*U(       a  SR                  U5      nOSR                  U5      n[        R
                  " SU-   S	S
9   U R                  XUS9nU Vs/ s H  owR                  5       PM     nnUsSSS5        $ s  snf ! , (       d  f       g= f)a  List all exportable resources.

If parent (e.g. project, organization or folder) is passed then only list
the exportable resources for that parent.

Args:
  project: string, project to list exportable resources for.
  organization: string, organization to list exportable resources for.
  folder: string, folder to list exportable resources for.

Returns:
  supported resources formatted output listing exportable resources.

r   )r6  TrW   z for project [{}]z for organization [{}]z for folder [{}]z!Listing exportable resource typesr%  r&  )r|   r{   r}   N)r   loadr7  rw   r   r+  ListSupportedResourcesForParentrT   )rC   r|   r{   r}   yaml_obj_listmsg_sfxsupported_kindsxs           r    ListResources#DeclarativeClientBase.ListResources  s     |vii

"
"
"
8TKm#**73g	(//=g"))&1g		)	)3g=)
+ <<V = Eo-<=__o=
+ 
+
 >
+ 
+s   C*CCC
C#c           	         U(       d  U(       d  U(       d  [        S5      e[        R                  " 5       n[        X2US9n[	        [
        R                  U5       Vs/ s H  ofR                  SS5      PM     sn5      n/ nU H  n	 UR                  U	S9n
[        U
R                  R                  U
R                  R                  [        -   U
R                  R                  U
R                  R                  U
R                  R                   S9nUR#                  U5        M     ['        US S9$ s  snf ! [        R$                   a     M  f = f)	zFList all exportable resource types for a given project, org or folder.zTAt least one of project, organization or folder must be specified for this operation)r}   r   r|   "rI   )r   )r<   r=   r?   r@   rA   c                     U R                   $ r;   )r<   )r?  s    r    <lambda>GDeclarativeClientBase.ListSupportedResourcesForParent.<locals>.<lambda>C  s    !&&r   )key)r   r	   r   r   set_ASSET_TYPE_REGEXfindallrv   r   r8   r   	krm_group_KRM_GROUP_SUFFIXresource_datasupport_bulk_exportsupport_single_exportsupport_iamr   r   r   )rC   r|   r{   r}   r   asset_list_datar?  r&   exportable_kindsassetr   rR   s               r    r;  5DeclarativeClientBase.ListSupportedResourcesForParent%  s:   
 |v,- - /EEGO0:O %6%>%>%O%O		$%O K '44%4P''00((225FF"/"="=  *88NN'55AAC 	$  "(899# &EE s   D-BD22E
	E
c                    AU(       a9  UR                  5       (       a$  [        R                  R                  U5      (       d  [	        SR                  U5      5      eU R                  SSUSU R                  5       /n[        U5      u  pEnUS:w  a  [        SR                  X5      5      e[        R                  " U5      $ )a  Call apply from config-connector binary.

Applys the KRM config file specified by `path`, creating or updating the
related GCP resources. If `try_resolve_refs` is supplied, then command will
attempt to resolve the references to related resources in config,
creating a directed graph of related resources and apply them in order.

Args:
  input_path: string, KRM config file to apply.
  try_resolve_refs: boolean, if true attempt to resolve the references to
  related resources in config.

Returns:
  Yaml Object representing the updated state of the resource if successful.

Raises:
  ApplyException: if an error occurs applying config.
  ApplyPathException: if an error occurs reading file path.
z"Resource file path [{}] not found.applyz-ir  r   z:Error occured while applying configuration path [{}]: [{}])r   r   r   isfiler5   rw   r   r   r   r2   r   r:  )rC   
input_pathtry_resolve_refsr   r   r   r   s          r    ApplyConfig!DeclarativeClientBase.ApplyConfigE  s    ( 	Z--//rww~~8 8
.
5
5j
AC C 	gtZ9IC ,:#+>(I[A~
F
M
M'( ( 99\""r   )r   r   r   ra   )NFFr;   )table)NNN)F)r   r   r   r   r   propertyabcabstractmethodr   r   r   r   rD   r   r   r  r  r"  r.  r3  r7  r@  r;  rZ  r   r   r   r    r   r   ,  s    1	  	 	  	 	 	 	 	 
:
I.C ,0*/*/8t: KD F /337-1:@##r   r   )NNNNr;   )<r   
__future__r   r   r   r^  rP   r
  r   r   reapitools.base.pyr   googlecloudsdk.api_lib.assetr    googlecloudsdk.command_lib.assetr   r   8googlecloudsdk.command_lib.util.resource_map.declarativer	   googlecloudsdk.corer
   c_exceptr   r   r   r   googlecloudsdk.core.consoler   r   googlecloudsdk.core.resourcer   googlecloudsdk.core.utilr   sixErrorr   _ASSET_INVENTORY_STRINGcompilerI  rL  
namedtupler"   RESOURCE_LIST_FORMATr,   r/   r2   r5   objectr8   rp   ry   r   r   r   r   r   add_metaclassABCMetar   r   r   r    <module>rt     sB   4 &  ' 
   	 	 	 % 4 A ] 6 / # * $ 2 8 8 * 
#hnn # @ JJ<= , && 9 / # #>/ >M_ MJ J6 6t *.2615/3Xv,	DF 3;;{#F {#  {#r   