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

Add support for count/each value #1537

Merged
merged 1 commit into from
Oct 22, 2022
Merged

Conversation

wata727
Copy link
Member

@wata727 wata727 commented Oct 9, 2022

Fixes #1139

This PR adds support for count.index, each.key, and each.value references. Previously, expressions with these references were always evaluated as unknown:

# Before
resource "aws_instance" "main" {
  for_each = toset(["t2.micro", "m5.large"])

  instance_type = each.value # => ignored (unknown)
}

# After
resource "aws_instance" "main" {
  for_each = toset(["t2.micro", "m5.large"])

  instance_type = each.value # => "t2.micro" and "m5.large"
}

Because of this support, blocks returned by GetModuleContent will now be expanded. For example, if a resource with count = 2 is defined, 2 blocks will be returned, whereas previously a single block was returned.

resource "aws_instance" "main" {
  for_each = toset(["t2.micro", "m5.large"])

  instance_type = each.value
}
// Before
runner.GetModuleContent(...)
// => hclext.Blocks{
//  (*hclext.Block)(0xc00008a210)
// }

// After
runner.GetModuleContent(...)
// => hclext.Blocks{
//  (*hclext.Block)(0xc000014250)
//  (*hclext.Block)(0xc00009a050)
// }

Note that an error is now returned if count or for_each is null to follow Terraform semantics.

As an internal implementation, expressions containing count/for_each are evaluated when the block is expanded. The evaluated expression is returned to the plugin as a new expression wrapped around the value and resolved when EvaluateExpr is called. See also terraform-linters/tflint-plugin-sdk#205.

In order to use this feature, a plugin built with SDK v0.14+ is required. In earlier versions, blocks are expanded, but each.* and count.* are not evaluated.

ToDo

  • Move some functions to tflint-plugin-sdk
  • Support encoding/decoding EvaluatedExpr
  • Add unit tests
  • Add E2E tests
  • Add ExpandMode option instead of IncludeNotCreated
  • Revise documentaion
  • Revise GoDoc

@wata727 wata727 force-pushed the add_support_for_each_and_count branch 2 times, most recently from 8bc3b9e to cfa8f20 Compare October 10, 2022 16:17
@wata727 wata727 force-pushed the add_support_for_each_and_count branch 2 times, most recently from 1033e0a to 836b0c4 Compare October 12, 2022 17:01
@wata727 wata727 force-pushed the add_support_for_each_and_count branch 3 times, most recently from 80d6cb7 to c235cb1 Compare October 15, 2022 13:52
@wata727 wata727 force-pushed the add_support_for_each_and_count branch 3 times, most recently from a1402e9 to 9f55753 Compare October 15, 2022 18:51
@wata727 wata727 force-pushed the add_support_for_each_and_count branch from 9f55753 to d71268d Compare October 15, 2022 18:59
@wata727 wata727 marked this pull request as ready for review October 15, 2022 19:01
@wata727 wata727 merged commit ea78d58 into master Oct 22, 2022
@wata727 wata727 deleted the add_support_for_each_and_count branch October 22, 2022 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

TFLint skips expressions that reference "each"
1 participant