
    Z                     ~   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	Z	ddl
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 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"Z" G d dejF                        Z$dZ% e
jL                  d      Z'dZ( ejR                  dg d      Z*dZ+ G d de$      Z, G d de$      Z- G d d e$      Z. G d! d"e.      Z/ G d# d$e0      Z1d% Z2d& Z3	 	 	 	 d.d'Z4d( Z5d/d)Z6d/d*Z7d+ Z8 e"jr                  ejt                         G d, d-e0             Z;y)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                       e Zd ZdZy)ClientExceptionGeneral Purpose Exception.N__name__
__module____qualname____doc__     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                       e Zd ZdZy)ResourceNotFoundExceptionr   Nr   r   r   r   r)   r)   A   r   r   r)   c                       e Zd ZdZy)ExportPathExceptionz5Exception for any errors raised creating export Path.Nr   r   r   r   r+   r+   E   s    =r   r+   c                       e Zd ZdZy)ApplyExceptionzDGeneral Exception for any errors raised applying configuration path.Nr   r   r   r   r-   r-   I   s    Lr   r-   c                       e Zd ZdZy)ApplyPathExceptionzAException for any errors raised reading apply configuration path.Nr   r   r   r   r/   r/   M   s    Ir   r/   c                   :    e Zd ZdZ	 	 d	dZd Zd Zd Zd Zd Z	y)
KrmGroupValueKindz*Value class for KRM Group Value Kind Data.Nc                 f    || _         || _        || _        || _        || _        || _        || _        y N)kindgroupversionbulk_export_supportedexport_supportediam_supportedresource_name_format)selfr4   r5   r7   r8   r9   r6   r:   s           r   __init__zKrmGroupValueKind.__init__T   s9     DIDJDL!6D,D&D 4Dr   c                 B   t        j                         }t        j                         }| j                  |d<   | j                  |d<   | j                  xs d|d<   ||d<   | j
                  xs d|d<   | j                  |d<   | j                  |d<   | j                  |d	<   |S )
z3Convert to Config Connector compatible dict format.GroupKind VersionGVKResourceNameFormatSupportsBulkExportSupportsExportSupportsIAM)	collectionsOrderedDictr5   r4   r6   r:   r7   r8   r9   )r;   gvkoutputs      r   AsDictzKrmGroupValueKind.AsDictd   s    

!
!
#C$$&F::CL))CK\\'RC	NF5M#'#<#<#BF #'#=#=F #44F ..F=Mr   c                 L    t        j                  | j                         d      S )NT
round_trip)r   dumprK   r;   s    r   __str__zKrmGroupValueKind.__str__r   s    99T[[]t44r   c                 "    | j                         S r3   )rQ   rP   s    r   __repr__zKrmGroupValueKind.__repr__u   s    <<>r   c                    t        |t              sy| j                  |j                  k(  xr | j                  |j                  k(  xr | j                  |j                  k(  xrj | j
                  |j
                  k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S NF)	
isinstancer1   r4   r5   r6   r7   r8   r9   r:   )r;   os     r   __eq__zKrmGroupValueKind.__eq__x   s    a*+II @DJJ!''$9 @LLAII%@&&!*A*AA@ !!Q%7%77@ !//1	@
 %%)?)??Ar   c                     t        t        t        | j                  | j                  | j
                  | j                  | j                  | j                  | j                  g            S r3   )
summaphashr4   r5   r6   r7   r8   r9   r:   rP   s    r   __hash__zKrmGroupValueKind.__hash__   sR    DIItzz4<<1K1K!!4#5#5t7P7P
 	 r   )NN)
r   r   r   r   r<   rK   rQ   rS   rX   r]   r   r   r   r1   r1   Q   s,    2 $(5 5Ar   r1   c                     | dk(  ry| S )zHTranslate Resource Format from gcloud values to config-connector values.	terraformhclr   )resource_formats    r   _NormalizeResourceFormatrb      s    #	r   c                     d| v rX| j                  d      d   j                  d      d   }| j                  dj                  |      dj                  |            S | S )Nzwww.googleapis.com/   /r   zwww.googleapis.com/{api})apiz{api}.googleapis.com/{api})splitreplaceformat)resource_urirf   s     r   _NormalizeUrirk      sm    l*


2
3A
6
<
<S
A!
DC : A Ac A J < C C C LN N	r   c           
      n   t        j                  |||       }t        j                  |      }|r-t	        j
                  |j                               j                  nd}	|xs g }
|r8t        |      }|st        dj                  |            |
j                  |       t        ddddt        |
      ||	g       }|j                  |d      }g }|D ]:  }t        j                   |      }|j#                  dd      }|j%                  |       < |sI|rd	j                  |      }nd
}|rdj                  |      }nd
}t        dj                  |||            |sdj'                  |      S 	 t)        j*                  |dj'                  |             |S # t,        t.        f$ r}t1        |      d}~ww xY w)a  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 [{}].r@   z 
 Matching provided filter [{}].z<No matching resources found for [{parent}] {assets} {filter})r$   assetsfilter
)asset_utilsGetParentNameForExportr   AssetListClientr   CompilestripEvaluate_BuildAssetTypeFilterFromKindr)   ri   extendr   sortedListr   MessageToJsonrh   appendjoinr   WriteFileAtomically
ValueError	TypeErrorr   )ro   rn   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   .%F F4$F//F4c                 8   | syt        j                         }g }| D ]b  }|}d|v r|j                  d      \  }}|j                  |      }	 |D ]/  }|j	                  |      }|j                  |j                         1 d |S # t         j                  $ r Y |w xY w)z(Get assetType Filter from KRM Kind list.Nre   )krm_kind)r	   ResourceNameTranslatorrg   find_krmkinds_by_kindget_resourcer   asset_inventory_typeResourceIdentifierNotFoundError)		kind_listname_translatorr   r4   r   _matching_kind_objectskind_objmeta_resources	            r   rz   rz      s    	,CCE/,dH
d{JJsOka+AA(K+('44h4GM>>? ,  
 $CC s   4BBBc                     t        j                         }t        j                         }t        j                  | d||j                  |j                        }||j                         |j                         fS )NT)r   no_exitin_strout_funcerr_func)ioStringIOr   Execwritegetvalue)cmdr   output_valueerror_value	exit_codes        r   _ExecuteBinaryr     sd    ,+""!!  ") 
L))+[-A-A-C	CCr   c                     t        j                  | d|      }|dk7  rt        d      t        j                  j                  d       |S )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           
      L   | smt        t        j                  |      D cg c]  \  }}}t        |       c}}}      }t        j
                  j                  dj                  ||             y t        j
                  j                  dj                  |             y c c}}}w )Nz/Exported {} resource configuration(s) to [{}].
z,Exported resource configuration(s) to [{}].
)rZ   oswalklenr   r   r   ri   )preexisting_file_countpathrdfiles_in_dir
file_counts         r   _BulkExportPostStatusr   !  s    	57WWT]C]1q!\\	]CEJJJGNND  JJ7>>tDF	 	Ds   Bc                      e Zd ZdZeej                  d               Zeej                  d               Zej                  d        Z	ej                  d        Z
ddZd Zd	 Zd
 Zd Z	 	 	 ddZd ZddZddZddZ	 	 	 ddZddZy)DeclarativeClientBasez)KRM Yaml Export based Declarative Client.c                      y r3   r   rP   s    r   binary_namez!DeclarativeClientBase.binary_name0       	r   c                      y r3   r   rP   s    r   binary_promptz#DeclarativeClientBase.binary_prompt5  r   r   c                      y r3   r   r;   r   s     r   
BulkExportz DeclarativeClientBase.BulkExport:      r   c                      y r3   r   r   s     r   	ExportAllzDeclarativeClientBase.ExportAll>  r   r   Nc                 X   ddl m} |s2t        j                  j                  j
                  j                         }	 |j                  | j                        | _	        || _        || _        y # |j                  $ r/ |j                  | j                  | j                        | _	        Y Lw xY w)Nr   )binary_operations)prompt)&googlecloudsdk.command_lib.util.anthosr   r   VALUEScoreaccountGetCheckForInstalledBinaryr   _export_serviceMissingExecutableExceptionInstallBinaryNoOverridesr   _use_account_impersonation_account)r;   gcp_accountimpersonatedbin_opss       r   r<   zDeclarativeClientBase.__init__B  s    S%%**22668k7$<<T=M=MNd '3D#DM	 -- 7$==


4#5#5 > 7d7s    A+ +;B)(B)c                     ddl m} 	 |j                  | j                  | j                        S # t
        $ r}t        dj                  |            d }~ww xY w)Nr   )store)r   allow_account_impersonationz"Error Configuring KCC Client: [{}])googlecloudsdk.core.credentialsr   GetFreshAccessTokenr   r   	Exceptionr   ri   )r;   r   r   s      r   	_GetTokenzDeclarativeClientBase._GetTokenP  sa    5:&&--&*&E&E ' G G  :
.
5
5a
8: ::s   &/ 	AAAc                 ,    |j                         dk(  ryy)N-FT)rx   )r;   r   s     r   _OutputToFileOrDirz(DeclarativeClientBase._OutputToFileOrDir\  s    zz|sr   c                 l   t         j                  j                  |j                               }	 t         j                  j	                  |      rt        j                  |      ryt        j                  t         j                  j                  |            r>t        j                  dj                  |      ddd       t        j                  |       yt        t        j                        # t        $ r t!        dj                  |            t        $ r t!        dj                  |            w xY w)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   abspathrx   isdirr   HasWriteAccessInDirdirnamer   PromptContinueri   MakeDirOSErrorerrnoEACCESr   r+   )r;   r   	directorys      r   _TryCreateOutputPathz*DeclarativeClientBase._TryCreateOutputPatha  s    

-II	y	!e&?&?	&J		"	"277??9#=	>!!@GGM=		?
 	dell## ; !--3VD\; ; I !;;A6$<I IIs   4C- $A/C- C- -AD3c                 X    t        |dd       xs | j                  t        |dd             S )Nresource_typesresource_types_file)getattr_ParseKindTypesFileDatar   s     r   _ParseResourceTypesz)DeclarativeClientBase._ParseResourceTypesx  s:    4) C 88t%:DACCr   c                 2   | j                   d| j                         |g}|dk(  r|st        d      |j                  |g       |dk(  r|j                  dt	        |dd      g       |s|j                  d      r|j                  d	|j                  g       n|j                  d
      r|j                  d|j                  g       nS|j                  xs2 t        j                  j                  j                  j                         }|j                  d|g       |sD|j                  d      s|j                  d      r"|j                  d| j                  |      g       t	        |dd      r|j                  d|j                  g       t	        |dd      rH|j                  dt        |j                         g       |j                   dk(  r|j                  ddg       | j#                  |j$                        r|j                  d|j$                  g       |S )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ignorerm   z--organizationro   z--folderz	--projectr   r   z--resource-typesstorage_pathNz--storage-keyra   z--resource-formatr_   z--iam-formatnonez--output)r   r   r   r{   r   IsSpecifiedrm   ro   rn   r   r   r   	GetOrFailr   r  rb   ra   r   r   )r;   r   command_namerj   skip_parentskip_filterr   rn   s           r   _GetBinaryExportCommandz-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
**($*B*B4*HI
Jt^T*	jj/4#4#456t&-	jj%*4+?+?@B C 
			,

NF+, tyy)	jj*dii()Jr   c                 "   t        |      }t        j                  dd      5  | j                  |d|      }t	        |      \  }}}ddd       dk7  rVdv rt        d	j                  |            d
|v rt        dj                  |            t        dj                  |            | j                  |j                        st        j                  j                         t        j                  j                  d       |S # 1 sw Y   xY w)z/Exports a single resource's configuration file.zExporting resourcesAborted Export.messageaborted_messager  )r   r  rj   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.)rk   r   ProgressTrackerr  r   r)   ri   r   r   r   r   outr   r   )r;   r   rj   normalized_resource_urir   r   r   r   s           r   Exportz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           
         | j                  |j                        r| j                  |j                         t        t	        j
                  |j                        D cg c]  \  }}}t        |       c}}}      }t        j                  dj                  |j                        d      5  t        ||      \  }}	}
ddd       dk7  rUd
v r7d}|j                  r|d	j                  |j                        z  }t        |      t        d
j                  |
            t        ||j                         |S t        j                  j!                  d       t#        ||      S c c}}}w # 1 sw Y   xY w)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   rZ   r   r   r   r   r  ri   r   r  r   r   r   r   r   r   )r;   r   r   asset_list_inputr   r   r   r   r   r   r   msgs               r   _CallBulkExportz%DeclarativeClientBase._CallBulkExport  sX   tyy)
		*"79wwtyy7I
J7I!3A|3|7I
J L++=DDTYYO+- %3,%.!	1k- 
a+%9# 89?)):++C  $
$-44[AC C 	4dii@ 
jjIJ(S9IJJ7 K- -s    E'/E..E7c                     | j                   dd|g}t        |      \  }}}|dk7  rt        dj                  |            |S )z1Calls `print-resources` on the underlying binary.zprint-resourcesz--output-formatr   z5Error occured while listing available resources: [{}])r   r   r   ri   )r;   output_formatr   r   r   r   s         r   _CallPrintResourcesz)DeclarativeClientBase._CallPrintResources  s]     	/1BC ,:#+>(I|[A~
A
H
H  r   c                    |s-|s+|s)t        j                  | j                  d      d      }|S |rdj                  |      }n%|rdj                  |      }ndj                  |      }t	        j
                  d|z   d	
      5  | j                  |||      }|D cg c]  }|j                          }}|cddd       S c c}w # 1 sw Y   yxY w)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   )r  TrM   z for project [{}]z for organization [{}]z for folder [{}]z!Listing exportable resource typesr  r  )rn   rm   ro   N)r   loadr  ri   r   r  ListSupportedResourcesForParentrK   )r;   rn   rm   ro   yaml_obj_listmsg_sfxsupported_kindsxs           r   ListResourcesz#DeclarativeClientBase.ListResources  s     |vii

"
"
"
8TKm#**73g	(//=g"))&1g		)	)3g=)
+ <<V = Eo-<=__o=
+ 
+
 >
+ 
+s   CC 3C CCc           	         |s|s|st        d      t        j                         }t        |||      }t	        t
        j                  |      D cg c]  }|j                  dd       c}      }g }|D ]  }		 |j                  |	      }
t        |
j                  j                  |
j                  j                  t        z   |
j                  j                  |
j                  j                  |
j                  j                         }|j#                  |        t'        |d       S c c}w # t        j$                  $ r Y w xY w)	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)ro   r   rn   "r@   )r   )r4   r5   r7   r8   r9   c                     | j                   S r3   )r4   )r$  s    r   <lambda>zGDeclarativeClientBase.ListSupportedResourcesForParent.<locals>.<lambda>C  s    !&&r   )key)r   r	   r   r   set_ASSET_TYPE_REGEXfindallrh   r   r1   r   	krm_group_KRM_GROUP_SUFFIXresource_datasupport_bulk_exportsupport_single_exportsupport_iamr   r   r|   )r;   rn   rm   ro   r   asset_list_datar$  r#   exportable_kindsassetr   rI   s               r   r   z5DeclarativeClientBase.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&8BD++E Ec                 b   ~|r/|j                         rt        j                  j                  |      st	        dj                  |            | j                  dd|d| j                         g}t        |      \  }}}|dk7  rt        dj                  ||            t        j                  |      S )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 [{}]: [{}])rx   r   r   isfiler/   ri   r   r   r   r-   r   r  )r;   
input_pathtry_resolve_refsr   r   r   r   s          r   ApplyConfigz!DeclarativeClientBase.ApplyConfigE  s    ( 	Z--/rww~~8
.
5
5j
AC C 	gtZ9IC ,:#+>(I|[A~
F
M
M+'( ( 99\""r   rU   )NFFr3   )table)NNN)F)r   r   r   r   propertyabcabstractmethodr   r   r   r   r<   r   r   r   r   r  r  r  r  r%  r   r<  r   r   r   r   r   ,  s    1	  	 	  	 	 	 	 	 
:
I.C ,0*/*/8t: KD F /337-1:@##r   r   )NNNNr3   )<r   
__future__r   r   r   r?  rG   r   r   r   reapitools.base.pyr   googlecloudsdk.api_lib.assetr    googlecloudsdk.command_lib.assetr   rt   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compiler,  r/  
namedtupler   RESOURCE_LIST_FORMATr)   r+   r-   r/   objectr1   rb   rk   r   rz   r   r   r   add_metaclassABCMetar   r   r   r   <module>rU     sH   4 &  ' 
   	 	 	 % 4 A ] 6 / # * $ 2 8 8 * 
#hnn # @ BJJ<= , &&& 9 / # #>/ >M_ MJ J6 6t *.2615/3Xv,	DF 3;;{#F {#  {#r   