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

[FEATURE] Do not evaluate lets block if condition is false in generate_file and generate_hcl #1636

Open
g13013 opened this issue Apr 16, 2024 · 4 comments
Labels
enhancement New feature or request planned

Comments

@g13013
Copy link

g13013 commented Apr 16, 2024

Is your feature request related to a problem? Please describe.
The current behavior of the lets block is that it's evaluated all the time regardless of the condition result, this leads errors in some scenarios when lets block depends on whether the condition is true or false, the problem becomes more annoying when the lets bloc have many variables that depend on the same condition

example

generate_hcl 'some_file.tf' {
  condition = length(global.data.vnets) > 0 && global.someOtherCondition

  lets: { // Repeated condition
    data   = global.someOtherCondition ?  existantValue ? nonExistantValue
    data2 = global.someOtherCondition ? existantValue2 ? nonExistantValue2
  }
}

Describe the solution you'd like
The lets block or any other block should not be evaluated if the condition is false

generate_hcl 'some_file.tf' {
  condition = length(global.data.vnets) > 0 && global.someOtherCondition

  lets: {
    data   = existantValue
    data2 = existantValue2
  }
}
@g13013 g13013 added the enhancement New feature or request label Apr 16, 2024
@g13013 g13013 changed the title [FEATURE] prioritize condition over lets in generate_file and generate_hcl [FEATURE] do not evaluate lets block if condition is false in generate_file and generate_hcl Apr 16, 2024
@g13013 g13013 changed the title [FEATURE] do not evaluate lets block if condition is false in generate_file and generate_hcl [FEATURE] Do not evaluate lets block if condition is false in generate_file and generate_hcl Apr 16, 2024
@i4ki
Copy link
Contributor

i4ki commented Apr 16, 2024

Hi @g13013

We are aware of this issue. About your proposed solution, what if the condition depends on any let variable? Would it makes sense to only evaluate the condition and any dependent variable?

In our experience, usually the condition can get complex then being able to create temporary let variables would be good.

@mariux mariux added the planned label Apr 17, 2024
@mariux
Copy link
Contributor

mariux commented Apr 17, 2024

We are planning to improve the evaluation of variables, so we only evaluate needed parts.

@g13013
Copy link
Author

g13013 commented Apr 17, 2024

@i4ki

We are aware of this issue. About your proposed solution, what if the condition depends on any let variable? Would it makes sense to only evaluate the condition and any dependent variable?

Even better, it makes a lot of sense and it would awesome to have this !

@bart-braidwell
Copy link

First, it should evaluate the condition, then if all conditions are met, it should start evaluating/generating lets variables.
In other cases, I end up with all stacks trying to generate lets for the same template, even stack is not using it.

for example, if I am using an external terraform state file as a data source inside the lets variables:

- /stacks/level_0/security/iam
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:8,40-109: lets eval: unknown variable namespace: data
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:8,111-180: lets eval: unknown variable namespace: data
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:9,26-95: lets eval: unknown variable namespace: data
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:9,97-166: lets eval: unknown variable namespace: data
- /stacks/level_1/rds
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:8,40-109: lets eval: unknown variable namespace: data
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:8,111-180: lets eval: unknown variable namespace: data
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:9,26-95: lets eval: unknown variable namespace: data
	error: /terramate/templates/level_1/eks/config/eks_config.tm.hcl:9,97-166: lets eval: unknown variable namespace: data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request planned
Projects
None yet
Development

No branches or pull requests

4 participants