Skip to content

Commit e4e9a73

Browse files
authored
feat: Added terraform_wrapper_module_for_each hook (antonbabenko#376)
1 parent aededd0 commit e4e9a73

File tree

5 files changed

+484
-8
lines changed

5 files changed

+484
-8
lines changed

.github/.container-structure-test-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ commandTests:
5555
args: [ "--version" ]
5656
expectedOutput: [ "([0-9]+\\.){2}[0-9]+\\n$" ]
5757

58+
- name: "hcledit"
59+
command: "hcledit"
60+
args: [ "version" ]
61+
expectedOutput: [ "([0-9]+\\.){2}[0-9]+\\n$" ]
62+
5863
fileExistenceTests:
5964
- name: 'terrascan init'
6065
path: '/root/.terrascan/pkg/policies/opa/rego/github/github_repository/privateRepoEnabled.rego'

.pre-commit-hooks.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@
113113
exclude: \.terraform\/.*$
114114
require_serial: true
115115

116+
- id: terraform_wrapper_module_for_each
117+
name: Terraform wrapper with for_each in module
118+
description: Generate Terraform wrappers with for_each in module.
119+
entry: hooks/terraform_wrapper_module_for_each.sh
120+
language: script
121+
pass_filenames: false
122+
always_run: false
123+
require_serial: true
124+
files: \.tf$
125+
exclude: \.terraform\/.*$
126+
116127
- id: terrascan
117128
name: terrascan
118129
description: Runs terrascan on Terraform templates.

Dockerfile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ ARG TERRASCAN_VERSION=${TERRASCAN_VERSION:-false}
3535
ARG TFLINT_VERSION=${TFLINT_VERSION:-false}
3636
ARG TFSEC_VERSION=${TFSEC_VERSION:-false}
3737
ARG TFUPDATE_VERSION=${TFUPDATE_VERSION:-false}
38+
ARG HCLEDIT_VERSION=${HCLEDIT_VERSION:-false}
3839

3940

4041
# Tricky thing to install all tools by set only one arg.
@@ -49,7 +50,8 @@ RUN if [ "$INSTALL_ALL" != "false" ]; then \
4950
echo "export TERRASCAN_VERSION=latest" >> /.env && \
5051
echo "export TFLINT_VERSION=latest" >> /.env && \
5152
echo "export TFSEC_VERSION=latest" >> /.env && \
52-
echo "export TFUPDATE_VERSION=latest" >> /.env \
53+
echo "export TFUPDATE_VERSION=latest" >> /.env && \
54+
echo "export HCLEDIT_VERSION=latest" >> /.env \
5355
; else \
5456
touch /.env \
5557
; fi
@@ -138,6 +140,16 @@ RUN . /.env && \
138140
) && tar -xzf tfupdate.tgz tfupdate && rm tfupdate.tgz \
139141
; fi
140142

143+
# hcledit
144+
RUN . /.env && \
145+
if [ "$HCLEDIT_VERSION" != "false" ]; then \
146+
( \
147+
HCLEDIT_RELEASES="https://api.github.com/repos/minamijoyo/hcledit/releases" && \
148+
[ "$HCLEDIT_VERSION" = "latest" ] && curl -L "$(curl -s ${HCLEDIT_RELEASES}/latest | grep -o -E -m 1 "https://.+?_linux_amd64.tar.gz")" > hcledit.tgz \
149+
|| curl -L "$(curl -s ${HCLEDIT_RELEASES} | grep -o -E -m 1 "https://.+?${HCLEDIT_VERSION}_linux_amd64.tar.gz")" > hcledit.tgz \
150+
) && tar -xzf hcledit.tgz hcledit && rm hcledit.tgz \
151+
; fi
152+
141153
# Checking binaries versions and write it to debug file
142154
RUN . /.env && \
143155
F=tools_versions_info && \
@@ -151,6 +163,7 @@ RUN . /.env && \
151163
(if [ "$TFLINT_VERSION" != "false" ]; then ./tflint --version >> $F; else echo "tflint SKIPPED" >> $F ; fi) && \
152164
(if [ "$TFSEC_VERSION" != "false" ]; then echo "tfsec $(./tfsec --version)" >> $F; else echo "tfsec SKIPPED" >> $F ; fi) && \
153165
(if [ "$TFUPDATE_VERSION" != "false" ]; then echo "tfupdate $(./tfupdate --version)" >> $F; else echo "tfupdate SKIPPED" >> $F ; fi) && \
166+
(if [ "$HCLEDIT_VERSION" != "false" ]; then echo "hcledit $(./hcledit version)" >> $F; else echo "hcledit SKIPPED" >> $F ; fi) && \
154167
echo -e "\n\n" && cat $F && echo -e "\n\n"
155168

156169

README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ If you are using `pre-commit-terraform` already or want to support its developme
4646
* [terraform_tflint](#terraform_tflint)
4747
* [terraform_tfsec](#terraform_tfsec)
4848
* [terraform_validate](#terraform_validate)
49+
* [terraform_wrapper_module_for_each](#terraform_wrapper_module_for_each)
4950
* [terrascan](#terrascan)
5051
* [tfupdate](#tfupdate)
5152
* [Authors](#authors)
5253
* [License](#license)
53-
* [Additional terms of use for users from Russia and Belarus](#additional-terms-of-use-for-users-from-russia-and-belarus)
54+
* [Additional information for users from Russia and Belarus](#additional-information-for-users-from-russia-and-belarus)
5455

5556
## How to install
5657

@@ -68,6 +69,7 @@ If you are using `pre-commit-terraform` already or want to support its developme
6869
* [`infracost`](https://github.com/infracost/infracost) required for `infracost_breakdown` hook.
6970
* [`jq`](https://github.com/stedolan/jq) required for `infracost_breakdown` hook.
7071
* [`tfupdate`](https://github.com/minamijoyo/tfupdate) required for `tfupdate` hook.
72+
* [`hcledit`](https://github.com/minamijoyo/hcledit) required for `terraform_wrapper_module_for_each` hook.
7173

7274
<details><summary><b>Docker</b></summary><br>
7375

@@ -105,6 +107,7 @@ docker build -t pre-commit-terraform \
105107
--build-arg TFLINT_VERSION=0.31.0 \
106108
--build-arg TFSEC_VERSION=latest \
107109
--build-arg TFUPDATE_VERSION=latest \
110+
--build-arg HCLEDIT_VERSION=latest \
108111
.
109112
```
110113

@@ -116,7 +119,7 @@ Set `-e PRE_COMMIT_COLOR=never` to disable the color output in `pre-commit`.
116119
<details><summary><b>MacOS</b></summary><br>
117120

118121
```bash
119-
brew install pre-commit terraform-docs tflint tfsec checkov terrascan infracost tfupdate jq
122+
brew install pre-commit terraform-docs tflint tfsec checkov terrascan infracost tfupdate hcledit jq
120123
```
121124

122125
</details>
@@ -138,6 +141,7 @@ curl -L "$(curl -s https://api.github.com/repos/accurics/terrascan/releases/late
138141
sudo apt install -y jq && \
139142
curl -L "$(curl -s https://api.github.com/repos/infracost/infracost/releases/latest | grep -o -E -m 1 "https://.+?-linux-amd64.tar.gz")" > infracost.tgz && tar -xzf infracost.tgz && rm infracost.tgz && sudo mv infracost-linux-amd64 /usr/bin/infracost && infracost register
140143
curl -L "$(curl -s https://api.github.com/repos/minamijoyo/tfupdate/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.tar.gz")" > tfupdate.tar.gz && tar -xzf tfupdate.tar.gz tfupdate && rm tfupdate.tar.gz && sudo mv tfupdate /usr/bin/
144+
curl -L "$(curl -s https://api.github.com/repos/minamijoyo/hcledit/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.tar.gz")" > hcledit.tar.gz && tar -xzf hcledit.tar.gz hcledit && rm hcledit.tar.gz && sudo mv hcledit /usr/bin/
141145
```
142146

143147
</details>
@@ -158,6 +162,7 @@ curl -L "$(curl -s https://api.github.com/repos/aquasecurity/tfsec/releases/late
158162
sudo apt install -y jq && \
159163
curl -L "$(curl -s https://api.github.com/repos/infracost/infracost/releases/latest | grep -o -E -m 1 "https://.+?-linux-amd64.tar.gz")" > infracost.tgz && tar -xzf infracost.tgz && rm infracost.tgz && sudo mv infracost-linux-amd64 /usr/bin/infracost && infracost register
160164
curl -L "$(curl -s https://api.github.com/repos/minamijoyo/tfupdate/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.tar.gz")" > tfupdate.tar.gz && tar -xzf tfupdate.tar.gz tfupdate && rm tfupdate.tar.gz && sudo mv tfupdate /usr/bin/
165+
curl -L "$(curl -s https://api.github.com/repos/minamijoyo/hcledit/releases/latest | grep -o -E -m 1 "https://.+?_linux_amd64.tar.gz")" > hcledit.tar.gz && tar -xzf hcledit.tar.gz hcledit && rm hcledit.tar.gz && sudo mv hcledit /usr/bin/
161166
```
162167

163168
</details>
@@ -218,8 +223,8 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
218223

219224
<!-- markdownlint-disable no-inline-html -->
220225
| Hook name | Description | Dependencies<br><sup>[Install instructions here](#1-install-dependencies)</sup> |
221-
| ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
222-
| `checkov` and `terraform_checkov` | [checkov](https://github.com/bridgecrewio/checkov) static analysis of terraform templates to spot potential security issues. [Hook notes](#checkov-deprecated-and-terraform_checkov) | `checkov`<br>Ubuntu deps: `python3`, `python3-pip` |
226+
| ------------------------------------------------------ |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
227+
| `checkov` and `terraform_checkov` | [checkov](https://github.com/bridgecrewio/checkov) static analysis of terraform templates to spot potential security issues. [Hook notes](#checkov-deprecated-and-terraform_checkov) | `checkov`<br>Ubuntu deps: `python3`, `python3-pip` |
223228
| `infracost_breakdown` | Check how much your infra costs with [infracost](https://github.com/infracost/infracost). [Hook notes](#infracost_breakdown) | `infracost`, `jq`, [Infracost API key](https://www.infracost.io/docs/#2-get-api-key) |
224229
| `terraform_docs` | Inserts input and output documentation into `README.md`. Recommended. [Hook notes](#terraform_docs) | `terraform-docs` |
225230
| `terraform_docs_replace` | Runs `terraform-docs` and pipes the output directly to README.md. **DEPRECATED**, see [#248](https://github.com/antonbabenko/pre-commit-terraform/issues/248). [Hook notes](#terraform_docs_replace-deprecated) | `python3`, `terraform-docs` |
@@ -231,6 +236,7 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
231236
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) | - |
232237
| `terragrunt_fmt` | Reformat all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) to a canonical format. | `terragrunt` |
233238
| `terragrunt_validate` | Validates all [Terragrunt](https://github.com/gruntwork-io/terragrunt) configuration files (`*.hcl`) | `terragrunt` |
239+
| `terraform_wrapper_module_for_each` | Generates Terraform wrappers with `for_each` in module. [Hook notes](#terraform_wrapper_module_for_each) | `hcledit` |
234240
| `terrascan` | [terrascan](https://github.com/accurics/terrascan) Detect compliance and security violations. [Hook notes](#terrascan) | `terrascan` |
235241
| `tfupdate` | [tfupdate](https://github.com/minamijoyo/tfupdate) Update version constraints of Terraform core, providers, and modules. [Hook notes](#tfupdate) | `tfupdate` |
236242
<!-- markdownlint-enable no-inline-html -->
@@ -651,6 +657,27 @@ Example:
651657

652658
**Note:** The latter method will leave an "aliased-providers.tf.json" file in your repo. You will either want to automate a way to clean this up or add it to your `.gitignore` or both.
653659

660+
### terraform_wrapper_module_for_each
661+
662+
`terraform_wrapper_module_for_each` generates module wrappers for Terraform modules (useful for Terragrunt where `for_each` is not supported). When using this hook without arguments it will create wrappers for the root module and all modules available in "modules" directory.
663+
664+
You may want to customize some of the options:
665+
666+
1. `--module-dir=...` - Specify a single directory to process. Values: "." (means just root module), "modules/iam-user" (a single module), or empty (means include all submodules found in "modules/*").
667+
2. `--module-repo-org=...` - Module repository organization (e.g. "terraform-aws-modules").
668+
3. `--module-repo-shortname=...` - Short name of the repository (e.g. "s3-bucket").
669+
4. `--module-repo-provider=...` - Name of the repository provider (e.g. "aws" or "google").
670+
671+
Sample configuration:
672+
673+
```yaml
674+
- id: terraform_wrapper_module_for_each
675+
args:
676+
- --args=--module-dir=. # Process only root module
677+
- --args=--dry-run # No files will be created/updated
678+
- --args=--verbose # Verbose output
679+
```
680+
654681
### terrascan
655682

656683
1. `terrascan` supports custom arguments so you can pass supported flags like `--non-recursive` and `--policy-type` to disable recursive inspection and set the policy type respectively:
@@ -709,9 +736,7 @@ This repository is managed by [Anton Babenko](https://github.com/antonbabenko) w
709736

710737
MIT licensed. See [LICENSE](LICENSE) for full details.
711738

712-
### Additional terms of use for users from Russia and Belarus
713-
714-
By using the code provided in this repository you agree with the following:
739+
### Additional information for users from Russia and Belarus
715740

716741
* Russia has [illegally annexed Crimea in 2014](https://en.wikipedia.org/wiki/Annexation_of_Crimea_by_the_Russian_Federation) and [brought the war in Donbas](https://en.wikipedia.org/wiki/War_in_Donbas) followed by [full-scale invasion of Ukraine in 2022](https://en.wikipedia.org/wiki/2022_Russian_invasion_of_Ukraine).
717742
* Russia has brought sorrow and devastations to millions of Ukrainians, killed hundreds of innocent people, damaged thousands of buildings, and forced several million people to flee.

0 commit comments

Comments
 (0)