Skip to content

Commit

Permalink
fix(terraform): dont add tags to data sources with the same name (#351)
Browse files Browse the repository at this point in the history
* dont add tags to data sources with the same name

* in one line

* add simple test he says
  • Loading branch information
JamesWoolfenden authored Mar 1, 2023
1 parent 7db8036 commit 02146dc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/terraform/structure/terraform_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var unsupportedTerraformBlocks = []string{
"aws_lb_listener_rule", // This resource does not support tags, although docs state otherwise.
"aws_cloudwatch_log_destination", // This resource does not support tags, although docs state otherwise.
"google_monitoring_notification_channel", //This resource uses labels for other purposes.
"aws_secretsmanager_secret_rotation", // This resource does not support tags, although tfschema states otherwise.
"aws_secretsmanager_secret_rotation", // This resource does not support tags, although tfschema states otherwise.
}

var taggableResourcesLock sync.RWMutex
Expand Down Expand Up @@ -150,6 +150,7 @@ func (p *TerraformParser) ParseFile(filePath string) ([]structure.IBlock, error)
}

syntaxBlocks := hclSyntaxFile.Body.(*hclsyntax.Body).Blocks

rawBlocks := hclFile.Body().Blocks()
parsedBlocks := make([]structure.IBlock, 0)
for i, block := range rawBlocks {
Expand Down Expand Up @@ -264,6 +265,12 @@ func (p *TerraformParser) modifyBlockTags(rawBlock *hclwrite.Block, parsedBlock
mergedTags := parsedBlock.MergeTags()
tagsAttributeName := parsedBlock.(*TerraformBlock).TagsAttributeName
tagsAttribute := rawBlock.Body().GetAttribute(tagsAttributeName)

//we don't add tags to data sources
if rawBlock.Type() == "data" {
return
}

if tagsAttribute == nil {
mergedTagsTokens := buildTagsTokens(mergedTags)
if mergedTagsTokens != nil {
Expand Down Expand Up @@ -542,7 +549,7 @@ func ExtractSubdirFromRemoteModuleSrc(raw string) string {
// we must remove before we start processing source string. We are using
// the fact that such double slashes always have : on the left.
parts := strings.Split(raw, "://")
parts = strings.Split(parts[len(parts) - 1], "//")
parts = strings.Split(parts[len(parts)-1], "//")

if len(parts) == 1 {
return ""
Expand Down
27 changes: 27 additions & 0 deletions src/terraform/structure/terraform_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,33 @@ func TestTerraformParser_Module(t *testing.T) {
assert.Equal(t, "aws_s3_bucket.test-bucket", blocks[0].GetResourceID())
})

t.Run("Test parsing of data source blocks", func(t *testing.T) {
p := &TerraformParser{}
p.Init("../../../tests/terraform/data", nil)
defer p.Close()
sourceFilePath := "../../../tests/terraform/data/main.tf"
expectedFileName := "../../../tests/terraform/data/expected.txt"
blocks, err := p.ParseFile(sourceFilePath)
if err != nil {
t.Fail()
}

mb := blocks[0]
mb.AddNewTags([]tags.ITag{
&tags.Tag{Key: tags.YorTraceTagKey, Value: "some-uuid"},
&tags.Tag{Key: "mock_tag", Value: "mock_value"},
})

resultFileName := "result.txt"
defer func() {
_ = os.Remove(resultFileName)
}()
_ = p.WriteFile(sourceFilePath, blocks, resultFileName)
resultStr, _ := os.ReadFile(resultFileName)
expectedStr, _ := os.ReadFile(expectedFileName)
assert.Equal(t, string(resultStr), string(expectedStr))
})

t.Run("Test parsing of unsupported resources", func(t *testing.T) {
p := &TerraformParser{}
p.Init("../../../tests/terraform/supported", nil)
Expand Down
13 changes: 13 additions & 0 deletions tests/terraform/data/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
data "aws_s3_bucket" "primary" {
count = var.create_bucket == true ? 0 : 1
bucket = "externally-created-bucket"
}

resource "aws_s3_bucket" "primary" {
count = var.create_bucket == true ? 1 : 0
bucket = "yor-bug-test-bucket"
tags = {
mock_tag = "mock_value"
yor_trace = "some-uuid"
}
}
9 changes: 9 additions & 0 deletions tests/terraform/data/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "aws_s3_bucket" "primary" {
count = var.create_bucket == true ? 0 : 1
bucket = "externally-created-bucket"
}

resource "aws_s3_bucket" "primary" {
count = var.create_bucket == true ? 1 : 0
bucket = "yor-bug-test-bucket"
}

0 comments on commit 02146dc

Please sign in to comment.