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 support for running terragrunt with opentofu #785

Merged
merged 2 commits into from
Jan 23, 2024

Conversation

TomerHeber
Copy link
Collaborator

Issue & Steps to Reproduce / Feature Request

resolves #784

Solution

  1. Updated the schema.
  2. Added special handling to support the unique behavior.
  3. Updated acceptance tests.

@@ -128,6 +130,10 @@ func (payload *TemplateCreatePayload) Invalidate() error {
return errors.New("must supply opentofu version")
}

if payload.TerragruntTfBinary != "" && payload.Type != "terragrunt" {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is to avoid setting TerragruntTfBinary without a template of type terragrunt.

@@ -239,6 +239,12 @@ func getTemplateSchema(prefix string) map[string]*schema.Schema {
ConflictsWith: allVCSAttributesBut("helm_chart_name", "is_helm_repository"),
RequiredWith: requiredWith("helm_chart_name"),
},
"terragrunt_tf_binary": {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could not set a deault value to "OpenTofu".
Default will only make sense if this was a "Terragrunt" template.
This is also an issue with existing templates that will use "Terrafrom" and not "OpenTofu".

I tried a few options. At the end I had to manually handle this unique use-case.
This makes the PR a little more complex than usual.

} else {
// No value was set - if it's a new template resource of type 'terragrunt' - default to 'opentofu'
if templateType == "terragrunt" && isNew {
payload.TerragruntTfBinary = "opentofu"
Copy link
Collaborator Author

@TomerHeber TomerHeber Jan 21, 2024

Choose a reason for hiding this comment

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

If it's a new terragrunt template send opentofu in the creation request


// If the user has set a value - use it.
if terragruntTfBinary := d.Get(terragruntTfBinaryKey).(string); terragruntTfBinary != "" {
payload.TerragruntTfBinary = terragruntTfBinary
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If the user manually configured terragrunt_tf_binary use it.

}

path, pathOk := d.GetOk(pathPrefix)
terragruntTfBinary := d.Get(terragruntTfBinaryPrefix).(string)

// If this value isn't set, ignore whatever is returned from the response.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a long explanation here. Hopefully it's clear enough.
Basically, if the user hasn't set anything in the schema ignore the response. This is to avoid drifts.

@@ -1899,9 +1899,11 @@ func TestUnitEnvironmentWithoutTemplateResource(t *testing.T) {
ErrorRegex: "RetryMeForDestroy.*",
},
},
Type: "terraform",
Type: "terragrunt",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This impacts an environment with a template.
Wanted to test that use-case as well. In this case the user explicitly mentioned here wants to use "terraform" instead of "opentofu".

IsGitlabEnterprise: true,
TerraformVersion: "0.12.24",
TerragruntVersion: "0.35.1",
TerragruntTfBinary: "opentofu",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In this case, user has set nothing. Defaults to opentofu when the template is created.

@TomerHeber TomerHeber requested a review from yaronya January 21, 2024 05:05
Copy link
Contributor

@Yossi-kerner Yossi-kerner left a comment

Choose a reason for hiding this comment

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

Looking good nice coding and comments really helped me understand what is going on

@github-actions github-actions bot added ready to merge PR approved - can be merged once the PR owner is ready and removed pending final review labels Jan 23, 2024
@TomerHeber TomerHeber merged commit be4500f into main Jan 23, 2024
4 checks passed
@TomerHeber TomerHeber deleted the feat-terragrunt-opentofu-#784 branch January 23, 2024 19:50
@kvendingoldo
Copy link

btw. you can also integrate tenv that support Terraform as well as OpenTofu (and Terragrunt :) ) in one tool. It allow you to simplify version management.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-client feature provider ready to merge PR approved - can be merged once the PR owner is ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for running terragrunt with opentofu
3 participants