Skip to content

Commit 271eb39

Browse files
vvakamesudomateo
andauthored
add import subcommand (hashicorp#117)
* add import subcommand * Update src/terraform_import.sh Co-Authored-By: Matthew Sanabria <[email protected]> Co-authored-by: Matthew Sanabria <[email protected]>
1 parent 81c2769 commit 271eb39

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/main.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ function main {
102102
source ${scriptDir}/terraform_plan.sh
103103
source ${scriptDir}/terraform_apply.sh
104104
source ${scriptDir}/terraform_output.sh
105+
source ${scriptDir}/terraform_import.sh
105106

106107
parseInputs
107108
configureCLICredentials
@@ -132,6 +133,10 @@ function main {
132133
installTerraform
133134
terraformOutput ${*}
134135
;;
136+
import)
137+
installTerraform
138+
terraformImport ${*}
139+
;;
135140
*)
136141
echo "Error: Must provide a valid value for terraform_subcommand"
137142
exit 1

src/terraform_import.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
3+
function terraformImport {
4+
# Gather the output of `terraform import`.
5+
echo "import: info: importing Terraform configuration in ${tfWorkingDir}"
6+
importOutput=$(terraform import -input=false ${*} 2>&1)
7+
importExitCode=${?}
8+
importCommentStatus="Failed"
9+
10+
# Exit code of 0 indicates success with no changes. Print the output and exit.
11+
if [ ${importExitCode} -eq 0 ]; then
12+
echo "import: info: successfully imported Terraform configuration in ${tfWorkingDir}"
13+
echo "${importOutput}"
14+
echo
15+
exit ${importExitCode}
16+
fi
17+
18+
# Exit code of !0 indicates failure.
19+
if [ ${importExitCode} -ne 0 ]; then
20+
echo "import: error: failed to import Terraform configuration in ${tfWorkingDir}"
21+
echo "${importOutput}"
22+
echo
23+
fi
24+
25+
# Comment on the pull request if necessary.
26+
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${tfComment}" == "1" ] && [ "${importCommentStatus}" == "Failed" ]; then
27+
importCommentWrapper="#### \`terraform import\` ${importCommentStatus}
28+
<details><summary>Show Output</summary>
29+
30+
\`\`\`
31+
${importOutput}
32+
\`\`\`
33+
34+
</details>
35+
36+
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${tfWorkingDir}\`*"
37+
38+
importCommentWrapper=$(stripColors "${importCommentWrapper}")
39+
echo "import: info: creating JSON"
40+
importPayload=$(echo "${importCommentWrapper}" | jq -R --slurp '{body: .}')
41+
importCommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
42+
echo "import: info: commenting on the pull request"
43+
echo "${importPayload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${importCommentsURL}" > /dev/null
44+
fi
45+
46+
exit ${importExitCode}
47+
}

0 commit comments

Comments
 (0)