Skip to content

Commit

Permalink
feat: do not treat as traversal but treat as raw attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
tk3fftk committed Feb 6, 2024
1 parent d685318 commit 40fac71
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
27 changes: 9 additions & 18 deletions api/hcl_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
)

Expand Down Expand Up @@ -52,20 +51,8 @@ func (p HCLParser) ConcatFile(baseDir string, pathes []string) (*hclwrite.File,
}

func setBodyAttribute(target *hclwrite.Body, name string, attr *hclwrite.Attribute) (*hclwrite.Body, error) {
// Parse the attribute's tokens into an expression
// filename is used only for diagnostic messages. so it can be placeholder string.
expr, diags := hclsyntax.ParseExpression(attr.Expr().BuildTokens(nil).Bytes(), "overlays", hcl.InitialPos)
if diags.HasErrors() {
return nil, diags
}

// Evaluate the expression to get a cty.Value
val, diags := expr.Value(nil)
if diags.HasErrors() {
return nil, diags
}

target.SetAttributeValue(name, val)
tokens := attr.Expr().BuildTokens(nil)
target.SetAttributeRaw(name, tokens)

return target, nil
}
Expand Down Expand Up @@ -109,6 +96,7 @@ func mergeBlocks(base *hclwrite.Body, overlay *hclwrite.Body) (*hclwrite.Body, e
for _, overlayBlock := range overlayBlocks {
joinedLabel := strings.Join(overlayBlock.Labels(), "_")
blockType := overlayBlock.Type()
fmt.Printf("[debug] processing overlay blockType, joinedLabel: %v, %v\n", blockType, joinedLabel)
switch blockType {
case "provider", "resource", "data", "module", "terraform":
if tmpBlock, ok := tmpBlocks[blockType][joinedLabel]; ok {
Expand Down Expand Up @@ -149,8 +137,10 @@ func mergeBlocks(base *hclwrite.Body, overlay *hclwrite.Body) (*hclwrite.Body, e
base.AppendNewline()
}

for _, tmpBlock := range tmpBlocks {
for _, block := range tmpBlock {
for blockType, tmpBlock := range tmpBlocks {
fmt.Printf("[debug] processing blockType: %v\n", blockType)
for joinedLabel, block := range tmpBlock {
fmt.Printf("[debug] processing joinedLabel: %v\n", joinedLabel)
base.AppendBlock(block)
}
base.AppendNewline()
Expand Down Expand Up @@ -181,9 +171,10 @@ func mergeBlock(baseBlock *hclwrite.Block, overlayBlock *hclwrite.Block) (*hclwr
sort.Strings(sortedNames)

for _, name := range sortedNames {
fmt.Printf("[debug] processing name, value: %v, %v\n", name, tmpAttributes[name])
_, err := setBodyAttribute(resultBlockBody, name, tmpAttributes[name])
if err != nil {
return resultBlock, err
return nil, err
}
}

Expand Down
34 changes: 30 additions & 4 deletions api/hcl_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ resource "aws_s3_bucket" "bar" {
}

hclFile, err := parser.ConcatFile(dir, fileNames)
if (err != nil) || tt.wantErr {
if err != nil && !tt.wantErr {
t.Errorf("%q. ConcatFile() error = %v, wantErr %v", tt.name, err, tt.wantErr)
assert.Fail(t, "unexpected error")
} else {
assert.Equal(t, tt.expect, string(hclwrite.Format(hclFile.Bytes())))
}
assert.Equal(t, tt.expect, string(hclwrite.Format(hclFile.Bytes())))
})
}
}
Expand Down Expand Up @@ -139,6 +141,28 @@ func TestMergeFileBlocks(t *testing.T) {
values = ["hvm"]
}
}
`,
wantErr: false,
},
{
name: "data source and resource",
base: []string{"base/data_and_resource.tf"},
overlay: []string{"overlay/data_and_resource.tf"},
expect: `data "aws_ami" "ubuntu" {
most_recent = false
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
availability_zone = "ap-northeast-1a"
instance_type = "t3.large"
tags = {
Name = "HelloWorld"
}
}
`,
wantErr: false,
},
Expand All @@ -159,10 +183,12 @@ func TestMergeFileBlocks(t *testing.T) {
}

result, err := parser.MergeFileBlocks(baseHCL, overlayHCL)
if (err != nil) || tt.wantErr {
if err != nil && !tt.wantErr {
t.Errorf("MergeFileBlocks() error = %v, wantErr %v", err, tt.wantErr)
assert.Fail(t, "unexpected error")
} else {
assert.Equal(t, tt.expect, regexpFormatNewLines.ReplaceAllString(string(hclwrite.Format(result.Bytes())), "\n"))
}
assert.Equal(t, tt.expect, regexpFormatNewLines.ReplaceAllString(string(hclwrite.Format(result.Bytes())), "\n"))
})
}
}
15 changes: 15 additions & 0 deletions test/base/data_and_resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
data "aws_ami" "ubuntu" {
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}

resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"

tags = {
Name = "HelloWorld"
}
}
8 changes: 8 additions & 0 deletions test/overlay/data_and_resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data "aws_ami" "ubuntu" {
most_recent = false
}

resource "aws_instance" "web" {
instance_type = "t3.large"
availability_zone = "ap-northeast-1a"
}

0 comments on commit 40fac71

Please sign in to comment.