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

Fix unable to parse each.value object issue #359

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/terraform/structure/terraform_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const DataBlockType = "data"
const LocalBlockType = "local"
const VarBlockType = "var"
const VariableBlockType = "variable"
const EachBlockType = "each"

var SupportedBlockTypes = []string{ResourceBlockType, ModuleBlockType, VariableBlockType}

Expand Down
2 changes: 1 addition & 1 deletion src/terraform/structure/terraform_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (p *TerraformParser) modifyBlockTags(rawBlock *hclwrite.Block, parsedBlock
isMergeOpExists = true
break
}
if i == 0 && utils.InSlice([]string{VarBlockType, LocalBlockType, ModuleBlockType, DataBlockType}, tokenStr) {
if i == 0 && utils.InSlice([]string{VarBlockType, LocalBlockType, ModuleBlockType, DataBlockType, EachBlockType}, tokenStr) {
isRenderedAttribute = true
break
}
Expand Down
56 changes: 55 additions & 1 deletion src/terraform/structure/terraform_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,61 @@ func TestTerraformParser_Module(t *testing.T) {
assert.True(t, block.IsBlockTaggable(), fmt.Sprintf("Block %v should be taggable", block.GetResourceID()))
}
})

t.Run("Parse a file assigns tags from `for_each` object, tag its blocks, and write them to the file", func(t *testing.T) {
rootDir := "../../../tests/terraform/resources/for_each"
filePath := "../../../tests/terraform/resources/for_each/main.tf"
originFileBytes, _ := os.ReadFile(filePath)
defer func() {
_ = os.WriteFile(filePath, originFileBytes, 0644)
}()
p := &TerraformParser{}
c2cTagGroup := &code2cloud.TagGroup{}
c2cTagGroup.InitTagGroup("", nil, nil)
p.Init(rootDir, nil)
writeFilePath := "../../../tests/terraform/resources/for_each/main.tf"
writeFileBytes, _ := os.ReadFile(writeFilePath)
defer func() {
_ = os.WriteFile(writeFilePath, writeFileBytes, 0644)
}()
parsedBlocks, err := p.ParseFile(filePath)
if err != nil {
t.Errorf("failed to read hcl file because %s", err)
}

for _, block := range parsedBlocks {
if block.IsBlockTaggable() {
_ = c2cTagGroup.CreateTagsForBlock(block)
} else {
assert.Fail(t, fmt.Sprintf("Block %v should be taggable!", block.GetResourceID()))
}
}

err = p.WriteFile(filePath, parsedBlocks, writeFilePath)
if err != nil {
t.Error(err)
}
parsedTaggedFileTags, err := p.ParseFile(writeFilePath)
if err != nil {
t.Error(err)
}

for _, block := range parsedTaggedFileTags {
if block.IsBlockTaggable() {
isYorTagExists := false
yorTagKey := tags.YorTraceTagKey
for _, tag := range block.GetExistingTags() {
if tag.GetKey() == yorTagKey || strings.ReplaceAll(tag.GetKey(), `"`, "") == yorTagKey {
isYorTagExists = true
}
assert.NotEqualf(t, "kubernetes.io/cluster/$${local.prefix}", tag.GetKey(), "Bad tag exists!")
}
if !isYorTagExists {
t.Errorf("tag not found on merged block %v", yorTagKey)
}
}
}
})
}

func TestExtractProviderFromModuleSrc(t *testing.T) {
Expand Down Expand Up @@ -553,7 +608,6 @@ func TestExtractProviderFromModuleSrc(t *testing.T) {
}
}


func TestExtractSubdirFromRemoteModuleSrc(t *testing.T) {
tests := []struct {
name string
Expand Down
20 changes: 20 additions & 0 deletions tests/terraform/resources/for_each/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
locals {
subnets = {
"us-east-1" = {
cidr_block = "10.10.10.10/24"
tags = {
location = "us-east-1a"
}
}
}
}

resource "aws_subnet" "eks_subnet" {
for_each = local.subnets

vpc_id = var.vpc_id
cidr_block = each.value.cidr_block
availability_zone = each.key
map_public_ip_on_launch = true
tags = each.value.tags
}