Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add terraform_validate support to init modules and providers if needed #301

Closed
wants to merge 4 commits into from

Conversation

jgrumboe
Copy link

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

This PR adds logic to terraform_validate to detect if module initialization is needed and run it. It's also possible now to force an initialization and/or provider upgrade by just adding the documented init-args to the hook call.

The new logic works as follows:
terraform providers is called on every run and used to detect if all needed modules are initialized correctly. If it succeeds it will pass -get=false to the terraform init call, which prevents modules to be re-downloaded. On the other hand, if terraform providers fails, this means modules or providers are not initialized and -get=truewill be passed to terraform init - a full initialization will happen.

Since terraform init will be called every time, the already existing --init-args feature will give us to additional benefits:

  • --init-args=-get=true will result in a forced initialization on every run
  • --init-args=-upgrade will allow to upgrade providers in addition to the modules if needed

Fixes #224

How has this code been tested

  1. You need to have terraform code which references modules and providers.
  2. Delete .terraform folder if present
  3. Change a module version in your code to some other existing version and stage the commit
  4. run pre-commit run terraform_validate and you see that it will initialize the .terraform folder newly and validate your code.
  5. Every re-run of pre-commit run terraform_validate will be faster, as long as no module version changes happen
  6. Again change a module version in your code to some other existing version and stage the commit
  7. run pre-commit run terraform_validate and you see that it will reinitialize for the new module

Repeat the above steps with changing provider version and setting the --init-args=-upgrade argument. You will notice that only providers or both are initialized if needed.

@jgrumboe jgrumboe changed the title feat: add support terraform_validate to init modules and providers if needed feat: Add support terraform_validate to init modules and providers if needed Dec 13, 2021
@jgrumboe jgrumboe changed the title feat: Add support terraform_validate to init modules and providers if needed feat: Add terraform_validate support to init modules and providers if needed Dec 13, 2021
@antonbabenko
Copy link
Owner

Looks pretty good. Let's see what @yermulnik or @MaxymVlasov have to say after they test it locally.

Copy link
Collaborator

@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch on using terraform providers to validate modules installation.
Code looks good to me.

README.md Outdated Show resolved Hide resolved
Co-authored-by: George L. Yermulnik <[email protected]>
@MaxymVlasov MaxymVlasov added estimate/1h Need 1 hour to be done feature New feature or request hook/terraform_validate Bash hook labels Dec 15, 2021
@MaxymVlasov MaxymVlasov self-assigned this Dec 15, 2021
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@@ -90,19 +90,20 @@ terraform_validate_() {

pushd "$(realpath "$path_uniq")" > /dev/null

if [[ ! -d .terraform ]]; then
Copy link
Collaborator

@MaxymVlasov MaxymVlasov Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should return that check and add check that checks that any --init-args provided.

Because, time execution increased up to 80-90% for:

repos:
- repo: [email protected]:jgrumboe/pre-commit-terraform.git
  rev: '4086982'
  hooks:
  - id: terraform_validate

compare with:

repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
  rev: v1.62.0
  hooks:
 - id: terraform_validate

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to run terraform init each time if it is not specified by arguments.
Or need to explicitly provide force-init: true etc. for that.

For now, it just looks like no needed speed violation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input.
I will try to come up with a better solution, which is fast as now when no modules/providers version changes were made in the code and still eliminates the need for manual deletion of the .terraform folder in case modules/providers version changes are made.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jgrumboe
Any updates? Do you need any help?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MaxymVlasov
Sorry, this one slipped out of my mind. I will have a look next week.
Thanks for the reminder.

@MaxymVlasov
Copy link
Collaborator

Close as too outdated. Simpler to create a new branch with specified changes than try to resolve conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
estimate/1h Need 1 hour to be done feature New feature or request hook/terraform_validate Bash hook
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Add argument like force-init to do init before terraform validate
4 participants