Skip to content

Commit

Permalink
fix parseTagAttribute func and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
EstyFridman1 committed Aug 6, 2024
1 parent 7877a68 commit 680fff0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
30 changes: 9 additions & 21 deletions src/terraform/structure/terraform_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package structure
import (
"encoding/json"
"fmt"
"github.com/genelet/determined/dethcl"
"os"
"path/filepath"
"reflect"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/bridgecrewio/yor/src/common/structure"
"github.com/bridgecrewio/yor/src/common/tagging/tags"
"github.com/bridgecrewio/yor/src/common/utils"
"github.com/genelet/determined/dethcl"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand Down Expand Up @@ -305,7 +305,7 @@ func (p *TerraformParser) modifyBlockTags(rawBlock *hclwrite.Block, parsedBlock
isMergeOpExists := false
isForOpExists := false
isRenderedAttribute := false
existingParsedTags, _ := p.parseTagAttribute(rawTagsTokens)
existingParsedTags := p.parseTagAttribute(rawTagsTokens)
for i, rawTagsToken := range rawTagsTokens {
tokenStr := string(rawTagsToken.Bytes)
if tokenStr == "merge" {
Expand Down Expand Up @@ -681,7 +681,7 @@ func (p *TerraformParser) getExistingTags(hclBlock *hclwrite.Block, tagsAttribut
// if tags exists in resource
isTaggable, _ = p.isBlockTaggable(hclBlock)
tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{})
parsedTags, _ := p.parseTagAttribute(tagsTokens)
parsedTags := p.parseTagAttribute(tagsTokens)
for key := range parsedTags {
iTag := tags.Init(key, parsedTags[key])
existingTags = append(existingTags, iTag)
Expand Down Expand Up @@ -815,7 +815,7 @@ func getUncloseBracketsCount(bracketsCounters map[hclsyntax.TokenType]int) int {
return sum
}

func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) (map[string]string, error) {
func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) map[string]string {
isForOpExists := false
for _, rawTagsToken := range tokens {
tokenStr := string(rawTagsToken.Bytes)
Expand All @@ -828,21 +828,9 @@ func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) (map[string]
hclData := new(Resource)
hclBytes := tokens.Bytes()
hclBytes = []byte(strings.Replace(string(hclBytes), "{", " tags= {", 1))

err := dethcl.Unmarshal([]byte(hclBytes), hclData)
if err != nil {
return nil, fmt.Errorf("failed to unmarshel data because %s", err)
}

tempHclData, err := dethcl.Marshal(hclData)
if err != nil {
return nil, fmt.Errorf("failed to marshel data because %s", err)

}
hclFile, diagnostics := hclwrite.ParseConfig(tempHclData, "", hcl.InitialPos)
if diagnostics != nil && diagnostics.HasErrors() {
return nil, fmt.Errorf("failed to convert to hclFile %s", diagnostics)
}
dethcl.Unmarshal((hclBytes), hclData)

Check failure on line 831 in src/terraform/structure/terraform_parser.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci-lint] reported by reviewdog 🐶 Error return value of `dethcl.Unmarshal` is not checked (errcheck) Raw Output: src/terraform/structure/terraform_parser.go:831:19: Error return value of `dethcl.Unmarshal` is not checked (errcheck) dethcl.Unmarshal((hclBytes), hclData) ^
tempHclData, _ := dethcl.Marshal(hclData)
hclFile, _ := hclwrite.ParseConfig(tempHclData, "", hcl.InitialPos)
tagsAttribute := hclFile.Body().GetAttribute("tags")
tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{})
tokens = tagsTokens
Expand Down Expand Up @@ -871,7 +859,7 @@ func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) (map[string]
parsedTags[key] = value
}

return parsedTags, nil
return parsedTags
}

func (p *TerraformParser) getClient(providerName string) tfschema.Client {
Expand Down Expand Up @@ -925,7 +913,7 @@ func (p *TerraformParser) getModuleTags(hclBlock *hclwrite.Block, tagsAttributeN
// if tags exists in module
isTaggable = true
tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{})
parsedTags, _ := p.parseTagAttribute(tagsTokens)
parsedTags := p.parseTagAttribute(tagsTokens)
for key := range parsedTags {
iTag := tags.Init(key, parsedTags[key])
existingTags = append(existingTags, iTag)
Expand Down
15 changes: 14 additions & 1 deletion src/terraform/structure/terraform_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ func TestTerraformParser_SkipResourceByComment(t *testing.T) {
}
assert.Empty(t, p.GetSkipResourcesByComment())
})
t.Run("One resource with skip comment, only that resource added to skipResourcesByComment slice", func(t *testing.T) {
// Initialize TerraformParser and parse file with one resource containing skip tag
p := &TerraformParser{}
p.Init("../../../tests/terraform/skipComment/", nil)
defer p.Close()
filePath := "../../../tests/terraform/skipComment/skipOne.tf"
_, err := p.ParseFile(filePath)
if err != nil {
t.Errorf("failed to read hcl file because %s", err)
}
exceptedSkipResources := []string{"aws_instance.example_instance"}
assert.Equal(t, exceptedSkipResources, p.GetSkipResourcesByComment())
})

}

Expand All @@ -68,7 +81,7 @@ func TestParseTagAttribute(t *testing.T) {
tagsAttribute := hclBlock.Body().GetAttribute(tagsAttributeName)
if tagsAttribute != nil {
tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{})
parsedTags, _ := parser.parseTagAttribute(tagsTokens)
parsedTags := parser.parseTagAttribute(tagsTokens)
if block.GetResourceName() == "bucket_var_tags" {
assert.Equal(t, parsedTags, expectedTags)
}
Expand Down

0 comments on commit 680fff0

Please sign in to comment.