@rem
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set "cmd_filename=%~nx0"
set "AWS_ACCESS_KEY_ID=aaaaaaaaaa"
set "AWS_SECRET_ACCESS_KEY=ssssssssss"
set "AWS_DEFAULT_REGION=ap-northeast-1"
@rem set "TF_LOG=DEBUG"
set "CHECKPOINT_DISABLE=true"
set "TerraformCmd=.\terraform.exe"
set "TerraformCmd_apply_opt=-auto-approve"
set "TerraformCmd_fmt_opt=-recursive ."
set "dot_cmd=uuuuuuuuuu\PlantUML\graphviz\release\bin\dot.exe"
set "file_resource_list_txt=!cmd_filename!.resource.list.txt"
set "file_txt=!cmd_filename!.txt"
set "file_tf=!cmd_filename!.tf"
set "file_tmp_tf=!cmd_filename!.tmp.tf"
set "file_png=!cmd_filename!.png"
@rem ================
@rem main.
@rem ================
:main
if "%1"=="" (
goto :printUsage
) else if "%1"=="apply" (
goto :TerraformCmd_apply
) else if "%1"=="fmt" (
goto :TerraformCmd_fmt
) else if "%1"=="import" (
goto :TerraformCmd_import
) else if "%1"=="plan" (
goto :TerraformCmd_plan
) else if "%1"=="init" (
goto :TerraformCmd_init
) else if "%1"=="remove" (
goto :TerraformCmd_remove
) else if "%1"=="showInfo" (
goto :TerraformCmd_showInfo
) else (
goto :printUsage
)
goto :eof
@rem ================
@rem Print usage.
@rem ================
:printUsage
echo Usage:
echo !cmd_filename! import
echo !cmd_filename! apply
echo !cmd_filename! fmt
echo !cmd_filename! plan
echo !cmd_filename! init
echo !cmd_filename! remove
echo !cmd_filename! showInfo
goto :eof
@rem ================
@rem Filesystem delete files.
@rem ================
:Filesystem_deleteFiles
for %%z in (
%*
) do (
set "filePath=%%z"
if exist "!filePath!" (
echo ^
del "!filePath!"
del "!filePath!"
)
if exist "!filePath!" (
echo Did not delete file "${filePath}".
exit /b 11
)
)
goto :eof
@rem ================
@rem TerraformFile Make empty file_tmp_tf.
@rem ================
:TerraformFile_makeEmptyTf
if not exist "!file_resource_list_txt!" (
echo Does not exist file "!file_resource_list_txt!".
exit /b 21
)
for /f "usebackq tokens=1,2 delims= " %%y in (
`type "!file_resource_list_txt!"`
) do (
set "aws_resource_type=%%y"
set "aws_resource_name=%%z"
call :TerraformFile_printEmptyTf ^
"!aws_resource_type!" ^
"!aws_resource_name!" ^
>> "!file_tmp_tf!"
)
goto :eof
@rem ================
@rem TerraformFile Make one empty resource tf.
@rem ================
:TerraformFile_printEmptyTf
set "aws_resource_type=%~1"
set "aws_resource_name=%~2"
set "tf_resource_name=!aws_resource_name!_tf"
echo resource "!aws_resource_type!" "%!tf_resource_name!" {
echo }
goto :eof
@rem ================
@rem Terraform cmd.
@rem ================
:TerraformCmd
for %%z in (
%*
) do (
set "tf_commamd=%%z"
set "tf_commamd=!tf_commamd:"=!"
@rem "
echo # ^
"!TerraformCmd!" !tf_commamd!
"!TerraformCmd!" !tf_commamd!
if not !errorlevel!==0 (
echo Exit code is !errorlevel!.
exit /b 22
)
)
goto :eof
@rem ================
@rem Terraform Show info.
@rem ================
:TerraformCmd_showInfo
echo # ^
type "!file_resource_list_txt!"
type "!file_resource_list_txt!"
call :TerraformCmd ^
"version" ^
"providers" ^
"validate -json"
goto :eof
@rem ================
@rem Terraform apply.
@rem ================
:TerraformCmd_apply
call :Filesystem_deleteFiles ^
"!file_txt!" ^
"!file_tmp_tf!" ^
"!file_png!"
call :TerraformCmd_showInfo
call :TerraformCmd_init
call :TerraformCmd_plan
call :TerraformCmd "apply !TerraformCmd_apply_opt!"
goto :eof
@rem ================
@rem Terraform fmt.
@rem ================
:TerraformCmd_fmt
call :TerraformCmd "fmt !TerraformCmd_fmt_opt!"
goto :eof
@rem ================
@rem Terraform init.
@rem ================
:TerraformCmd_init
call :TerraformCmd_fmt
call :TerraformCmd "init"
goto :eof
@rem ================
@rem Terraform plan.
@rem ================
:TerraformCmd_plan
call :TerraformCmd "plan"
goto :eof
@rem ================
@rem Terraform remove.
@rem ================
:TerraformCmd_remove
if not exist "!file_tf!" (
echo Does not exist file "!file_tf!".
exit /b 23
)
ren "!file_tf!" "!file_tf!.tmp"
call :TerraformCmd_apply
ren "!file_tf!.tmp" "!file_tf!"
goto :eof
@rem ================
@rem Terraform import.
@rem ================
:TerraformCmd_import
call :Filesystem_deleteFiles ^
"terraform.tfstate" ^
"!file_txt!" ^
"!file_tf!" ^
"!file_tmp_tf!" ^
"!file_png!"
call :TerraformCmd_showInfo
call :TerraformFile_makeEmptyTf
echo # ^
type "!file_tmp_tf!"
type "!file_tmp_tf!"
call :TerraformCmd_init
@rem Make file_txt.
echo # ^
type "!file_resource_list_txt!"
type "!file_resource_list_txt!"
for /f "usebackq tokens=1,2 delims= " %%i in (
`type "!file_resource_list_txt!"`
) do (
set "aws_resource_type=%%i"
set "aws_resource_name=%%j"
set "tf_resource_name=!aws_resource_name!_tf"
echo # ^
aws_resource_type=!aws_resource_type! ^
aws_resource_name=!aws_resource_name! ^
tf_resource_name=!tf_resource_name!
echo # ^
"!TerraformCmd!" import "!aws_resource_type!.!tf_resource_name!" !aws_resource_name!
"!TerraformCmd!" import "!aws_resource_type!.!tf_resource_name!" !aws_resource_name!
if not !errorlevel!==0 exit /b 1
echo # ^
"!TerraformCmd!" state show -no-color "!aws_resource_type!.!tf_resource_name!"
"!TerraformCmd!" state show -no-color "!aws_resource_type!.!tf_resource_name!" >> "!file_txt!"
if not !errorlevel!==0 exit /b 2
)
call :Filesystem_deleteFiles ^
"!file_tmp_tf!"
ren "!file_txt!" "!file_tf!"
call :TerraformCmd_fmt
ren "!file_tf!" "!file_txt!"
@rem Make file_tf.
for /f "usebackq tokens=1* delims=" %%i in (
`type "!file_txt!"`
) do (
set "line=%%i"
set "line=!line: arn = # arn !"
set "line=!line: id = # id !"
set "line=!line: bucket_domain_name = # bucket_domain_name !"
set "line=!line: bucket_regional_domain_name = # bucket_regional_domain_name !"
(echo !line!) >> "!file_tf!"
)
call :TerraformCmd_fmt
echo # ^
type "!file_tf!"
type "!file_tf!"
call :TerraformCmd_plan
@rem Make file_png.
call :L_mkPng
@rem pause
goto :eof
@rem ================
@rem Make file_png.
@rem ================
:L_mkPng
if exist "!dot_cmd!" (
echo # ^
"!TerraformCmd!" graph ^| "!dot_cmd!" -Tpng ^> "!file_png!"
"!TerraformCmd!" graph | "!dot_cmd!" -Tpng > "!file_png!"
)
goto :eof
0 件のコメント:
コメントを投稿