
    #                     <   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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  \R(                  " S5      rSrSrSrSrSrSrSr " S S\R:                  5      rS rS r S r!SS jr"SS jr#SS jr$S r%g) zJModule containing the command utils for resource-config/terraform surface.    )absolute_import)division)unicode_literalsN)core_exceptions)log)
console_io)files)	platforms)times)runtime)templatez^#zterraform importzterraform_import_{ts}.{suffix}z%Y%m%d-%H-%M-%Sz;
module "{module_name}" {{
  source = "{module_source}"
}}
z=
#!/bin/sh
# Terraform Import Script generated by gcloud cli
zInvalid export Path {}. Path should point to an export Terraform(.tf) file or directotry of Terraform files.
Please see `gcloud alpha resource-config bulk-export` for more details.zgcloud-export-modules.tfc                       \ rS rSrSrSrg)TerraformGenerationError:   zEBase Exception for errors that occur during import script generation. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       Blib/googlecloudsdk/command_lib/util/declarative/terraform_utils.pyr   r   :   s    Mr   r   c           	      j   [         R                  R                  U 5      (       a  U R                  S5      (       a  U /nOK[         R                  R	                  U 5      (       a  [
        R                  " U SS S9nO[        [        5      e/ n/ nU H  n[         R                  R                  U5      nSU;   d  US   R                  5       (       a  [         R                  " U5        MX  [
        R                  " U[        5      n [        U5      R                  5       n[
        R                   " [         R                  R#                  U5      5      UR%                  S5      S   R'                  5       4nUR)                  U5        M     U(       d  [/        SR1                  U S95      eU(       a4  [2        R4                  " SR1                  SR7                  U5      5      5        U$ ! [*         a    UR)                  U5         GMr  [
        R,                   a  n	[/        S	R1                  X	S
95      eSn	A	ff = f)zDRead files from export path and parse out import command statements..tfFc                 $    U R                  S5      $ )Nr   )endswith)xs    r   <lambda>"ParseExportFiles.<locals>.<lambda>G   s    E!2r   )include_dirsfile_predicatedefaultr   #   z0Could not parse Terrorm data from {path}:: {err})patherrNz-No Terraform importable data found in {path}.)r&   z=Error generating imports for the following resource files: {}
)osr&   isfiler   isdirr	   GetDirectoryTreeListing
ValueErrorINVALID_EXPORT_PATH_MSGbasenameisdigitremoveFilteredFileReaderIMPORT_REGEXlistpopExpandHomeAndVarsdirname	partitionstripappend
IndexErrorErrorr   formatr   warningjoin)
export_pathinput_filesimport_dataerror_filesin_filein_file_base_nameimport_readercommandimport_linees
             r   ParseExportFilesrJ   >   s    WW^^K  [%9%9%%@%@-K	ww}}[!!//24K
 ,
--++g((1%%):1)=)E)E)G)Gii..wEm*}%))+..rwww/GH((-a0668:;' & 

"7>> 	? 	  KKGNNIIk"	$% 
  $7##[[ *&>EE  F )* 	**s   A?G##H2H2H--H2c                      [         R                  R                  5       (       a  SOSn [        R	                  [
        R                  " [
        R                  " 5       [        5      U S9$ )z.Generate a default filename for import script.cmdsh)tssuffix)	r
   OperatingSystem	IsWindowsIMPORT_SCRIPT_DEFAULT_NAMEr=   r   FormatDateTimeNowIMPORT_DATE_FORMAT)rO   s    r   GenerateDefaultScriptFileNamerV   j   sP    --77995t&	#	*	*			eiik+=	>v 
+ 
O Or   c                    [         R                  R                  S[         R                  R                  XS95      nSR                  [         R                  R	                  U R                  US5      5      R                  [         R                  5      5      R                  S5      R                  5       nUS   R                  5       (       a  SR                  U5      nX#4$ )N.)start- r   zgcp-{})r)   r&   r?   relpathnormpathreplacesplitseplstriprstripr0   r=   )import_pathdest_dirmodule_sourcemodule_names       r   ConstructModuleParametersrg   q   s    '',,s!ww{KM-gg{**8R89??
&&6#;vvx  ^//+.K		##r   c                    U (       a  U R                  5       OSn U(       a-  [        R                  R                  UR                  5       5      OSnSnSnU (       Ga   [        R                  R	                  U 5      (       a)  SR                  U 5      n[        R                  " USSSSS9  [        R                  R                  U 5      n[        R                  R                  U 5      =(       d    [        R                  " 5       n[        R                  R                  U5      (       a5  [        R                  " U5      (       d  [        SR                  U5      5      eU(       a]  [        R                  R                  U5      (       a5  [        R                  " U5      (       d  [        SR                  U5      5      eSnUnX#4$ )	z9Helper function for generating output file and directory.Nz{} already exists.zDo you want to overwrite?TzAborted script generation.)prompt_stringr#   cancel_stringcancel_on_noz-Error writing output file: {} is not writablez>Cannot write output to directory {}. Please check permissions.)r9   r)   r&   abspathr*   r=   r   PromptContinuer/   r7   r	   GetCWDr+   HasWriteAccessInDirr   r-   )output_file
output_dir	dest_filerd   overwrite_prompts        r   ProcessOutputParametersrt   |   sN   '2!!#+7A
 0 0 23t*)(	ww~~k"".55kB
34   -Iww{+=u||~H	ww}}Xu'@'@'J'J$
9
@
@
JL L 
j!!%%j11 3396*3EG GIH		r   c           	      n   [         R                  R                  X!5      nS/ 0nU  Ha  u  pV[        XR5      u  pxUR	                  [
        5      SS n	US   R                  SR                  U	S   UU	S   R                  5       S95        Mc     [         R                  R                  US   5      US'   Sn
[        R                  R                  5       (       a  SOSnUS:X  a  [        S	5      n
OUS:X  a  [        US
'   [        S5      n
 [        R                   " USS9 n["        R$                  " U40 UD6nU
R'                  U5        SSS5        [         R(                  " US5        U[/        U 5      4$ ! , (       d  f       N2= f! [        R*                   a  n[-        SR                  U5      5      eSnAff = f)av  Generate Terraform import shell script from template.

Args:
  import_data: string, Import data for each resource.
  dest_file: string, Filename path to write the generated script to. If
    dest_file is None, then a default filename will be generated.
  dest_dir: string, Directory path to write the generated script to. If
    dest_dir is None, then script will be written to CWD.

Returns:
  tuple(string, int, [string])), the path to the generated script, number of
    import statements generated and list of files that could not be processed.

Raises:
  TerraformGenerationError: If and error occurs writing to disk/stdout.
data   Nz${cmd} module.{module_name}.{cmd_sfx}r   )rL   rf   cmd_sfxWINDOWSBASHzwindows_shell_template.tplbash_commentszbash_shell_template.tplTcreate_path  Error writing import script::{})r)   r&   r?   rg   r8   _IMPORT_CMD_PREFIXr:   r=   r9   linesepr
   rP   rQ   _BuildTemplate_BASH_COMMENTSr	   
FileWriterr   Contextrender_contextchmodr<   r   len)rB   rr   rd   output_file_namecontextrc   import_statement_rf   import_cmd_dataoutput_templatetemplate_keyfctxrI   s                  r   GenerateImportScriptr      s   $ WW\\(6RL''2#k.{ENA&001CDQRHOFOAHHA"((* I , - (3 JJOOGFO4'&//'77AACC,Y$%ABOv-GO$%>?O5			d
,/0OOA))c$$S)
, HHu% 
3{+	++
, 
,
 
 5
")0035 55s0   F )E0F 0
E>:F F4F//F4c                    [         R                  R                  X25      n[        S5      n[	        5       nU  H$  u  px[        Xs5      u  pUR                  X45        M&     / nU H,  nUR                  [        R                  US   US   S95        M.     SU0n[         R                  R                  U5      US'    [        R                  " USS9 n[        R                  " U40 UD6nUR                  U5        S	S	S	5        [         R                   " US
5        U['        U5      4$ ! , (       d  f       N2= f! [        R"                   a  n[%        SR                  U5      5      eS	nAff = f)z.Generate terraform modules file from template.zterraform_module_template.tplr   rw   )rf   re   projectmodulesTr|   Nr~   r   )r)   r&   r?   r   setrg   addr:   MODULE_TEMPLATEr=   r   r	   r   r   r   r   r   r<   r   r   )rB   r   rr   rd   r   r   module_contentsrc   r   re   rf   module_declarationsmoduler   r   r   rI   s                    r   GenerateModuleFiler      sN   WW\\(6"#BC/E/#nk!:"M45 $ f6!9F1INP    'zz':;')5			*	=OOA))c$$S) 
> HHu% 
3/	// 
>	= 
 5
")0035 55s0   :D5 )D$8D5 $
D2.D5 5E(	E##E(c                     [         R                  R                  [        5      n[         R                  R	                  USU 5      n[
        R                  " US9nU$ )Nterraform_templates)filename)r)   r&   r7   __file__r?   r   Template)template_file_namedir_nametemplate_pathfile_templates       r   r   r      sD    WW__X&('',,x)>13-##];-	r   )NN)&r   
__future__r   r   r   r)   re"googlecloudsdk.calliope.exceptionsr   googlecloudsdk.corer   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr	   r
   r   makor   r   compiler3   r   rR   rU   r   r   r.   TF_MODULES_FILENAMEr<   r   rJ   rV   rg   rt   r   r   r   r   r   r   <module>r      s    Q &  ' 	 	 > # 2 * . *   zz% ' = & 
L 
 1 N44 N)XO$D/,d08r   