
                             d Z ddlmZ ddlmZ ddlm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  G d de	j                         ZddZd Z G d de	j(                        Zy)z-Module containing the KCC Declarative Client.    )absolute_import)division)unicode_literalsN)
enable_api)declarative_client_base)resource_name_translator)
properties)
console_ioc                       e Zd ZdZy)!AssetInventoryNotEnabledExceptionz2Exception for when Asset Inventory Is Not Enabled.N)__name__
__module____qualname____doc__     Elib/googlecloudsdk/command_lib/util/declarative/clients/kcc_client.pyr   r      s    :r   r   c                 R   | xs2 t         j                  j                  j                  j	                         } d}t        j                  | |      sYt        j                  ddj                  ||             rt        j                  | |       yt        dj                  ||             y)zBChecks if the cloudasset API is enabled, prompts to enable if not.zcloudasset.googleapis.comFzAPI [{}] is required to continue, but is not enabled on project [{}]. Would you like to enable and retry (this will take a few minutes)?)defaultprompt_stringzFAborted by user: API [{}] must be enabled on project [{}] to continue.N)r	   VALUEScoreproject	GetOrFailr   IsServiceEnabledr
   PromptContinueformatEnableServicer   )r   service_names     r   *CheckForAssetInventoryEnablementWithPromptr    #   s    Az((--55??A',,		$	$Wl	;  #VL':< w5-
R6,(* * 
<r   c                 ^    t        j                         j                  |       j                  S )N)collection_name)r   ResourceNameTranslatorget_resourceasset_inventory_type)
collections    r   _TranslateCollectionToAssetTyper'   5   s-    	!	8	8	:	G	G  
H 
""6"67r   c                   N    e Zd ZdZed        Zed        Zd Zd Zd Z	d Z
d Zy	)
	KccClientz=Binary Client Interface for the config-connector binary tool.c                      y)Nzconfig-connectorr   selfs    r   binary_namezKccClient.binary_name=   s    r   c                      	 y)NzThis command requires the `config-connector` binary to be installed to export GCP resource configurations. Would you like to install the `config-connector` binary to continue command execution?r   r+   s    r   binary_promptzKccClient.binary_promptA   s    	CDr   c                     |S Nr   r,   argscmds      r   !_GetBinarySpecificExportArgumentsz+KccClient._GetBinarySpecificExportArgumentsH   s    Jr   c                     t        t        |dd              |j                  d      s|j                  d      r| j                  |      S | j	                  |dd      }| j                  ||d       S )Nr   resource_typesresource_types_filebulk-exportT)skip_filterasset_list_input)r    getattrIsSpecified_CallBulkExportFromAssetList_GetBinaryExportCommand_CallBulkExportr2   s      r   
BulkExportzKccClient.BulkExportK   st    .i&()*./..t44

&
&t]
&
MCTDAAr   c                 `    |syt        j                  d|      D cg c]  }|s|	 c}S c c}w )z2Parse Resource Types data into input string Array.Nz\s+|,+)resplit)r,   	file_dataxs      r   _ParseKindTypesFileDataz!KccClient._ParseKindTypesFileDataT   s/    xx	95;5!A5;;;s   ++c                 .   t        t        |dd             | j                  |      }t        j                  t        |dd      t        |dd      t        |dd      |t        |dd            }| j                  |ddd	      }| j                  |||
      S )zCBulkExport with support for resource kind/asset type and filtering.r   Nfolderorganizationfilter)rJ   r   orgkrm_kind_filterfilter_expressionr9   Tskip_parentr:   r;   )r    r=   _ParseResourceTypesr   GetAssetInventoryListInputr@   rA   )r,   r3   	kind_argsr<   r4   s        r   r?   z&KccClient._CallBulkExportFromAssetListZ   s    .wtY/MN((.I.IItXt,i.D.$/!!$$79 
&
&m4 ' ACT<LMMr   c                    | j                  |ddd      }t        |      g}t        j                  t	        |dd      t	        |dd      xs2 t
        j                  j                  j                  j                         t	        |dd      |t	        |dd      	      }| j                  |ddd      }| j                  |||      S )
z1Exports all resources of a particular collection.r9   TrP   rJ   Nr   rK   rL   )rJ   r   rM   asset_types_filterrO   )r@   r'   r   rS   r=   r	   r   r   r   r   rA   )r,   r3   r&   r4   
asset_typer<   s         r   	ExportAllzKccClient.ExportAlli   s    

&
&m4 ' AC1*=>J.IItXt,y$/ <""''//99;D.$/%!$$79 
&
&m4 ' ACT+;<<r   N)r   r   r   r   propertyr-   r/   r5   rB   rH   r?   rX   r   r   r   r)   r)   :   sH    E  D DB<N=r   r)   r1   )r   
__future__r   r   r   rD   googlecloudsdk.api_lib.servicesr   3googlecloudsdk.command_lib.util.declarative.clientsr   8googlecloudsdk.command_lib.util.resource_map.declarativer   googlecloudsdk.corer	   googlecloudsdk.core.consoler
   ClientExceptionr   r    r'   DeclarativeClientBaser)   r   r   r   <module>rb      sU    4 &  ' 	 6 W ] * 2;(?(O(O ;
*$7
=='== ==r   