Skip to content

Commit 296359b

Browse files
committed
add serialize helper func for debugging
1 parent c0d2df6 commit 296359b

File tree

3 files changed

+84
-18
lines changed

3 files changed

+84
-18
lines changed

hclext/serialize.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package hclext
2+
3+
import (
4+
"bytes"
5+
6+
"github.com/aquasecurity/trivy/pkg/iac/terraform"
7+
"github.com/hashicorp/hcl/v2/hclwrite"
8+
)
9+
10+
// Serialize makes a best effort to serialize the blocks back to HCL.
11+
// This is not guaranteed to be the same as the original HCL, and should
12+
// only be used for debugging.
13+
func Serialize(blocks terraform.Blocks) string {
14+
var buf bytes.Buffer
15+
16+
f := hclwrite.NewFile()
17+
for _, b := range blocks {
18+
f.Body().AppendBlock(hclwriteBlock(b))
19+
}
20+
21+
_, _ = f.WriteTo(&buf)
22+
return buf.String()
23+
}
24+
25+
func hclwriteBlock(block *terraform.Block) *hclwrite.Block {
26+
b := hclwrite.NewBlock(block.TypeLabel(), block.Labels())
27+
for _, attr := range block.Attributes() {
28+
// If the value is "null", it would be better to write the
29+
// underlying expression. That gets a bit complicated with
30+
// hclwrite, so just keep them as null for now.
31+
b.Body().SetAttributeValue(attr.Name(), attr.Value())
32+
}
33+
34+
for _, subBlock := range block.AllBlocks() {
35+
b.Body().AppendBlock(hclwriteBlock(subBlock))
36+
}
37+
38+
return b
39+
}

preview_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,11 @@ func Test_Extract(t *testing.T) {
306306
Owner: types.WorkspaceOwner{},
307307
},
308308
unknownTags: []string{},
309-
params: map[string]assertParam{},
309+
params: map[string]assertParam{
310+
"ref": ap().value("Index 2").
311+
optVals("Index 0", "Index 1", "Index 2"),
312+
"ref_count": ap().value("Index 2"),
313+
},
310314
},
311315
{
312316
name: "defexpression",

testdata/count/main.tf

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
11
terraform {
22
required_providers {
33
coder = {
4-
source = "coder/coder"
4+
source = "coder/coder"
5+
}
6+
null = {
7+
source = "hashicorp/null"
8+
version = "3.2.2"
59
}
610
}
711
}
812

9-
data "coder_parameter" "one" {
10-
count = 1
11-
name = "one"
12-
type = "number"
13-
default = 1
13+
data "null_data_source" "exists" {
14+
# for_each = toset(["one", "two", "three"])
15+
count = 3
16+
inputs = {
17+
foo = "Index ${count.index}"
18+
}
1419
}
1520

16-
data "coder_parameter" "two" {
17-
count = data.coder_parameter.one[0].value
18-
name = "two"
19-
type = "string"
20-
default = "two"
21+
data "coder_parameter" "ref" {
22+
name = "ref"
23+
type = "string"
24+
default = data.null_data_source.exists[2].inputs.foo
25+
count = 1
26+
27+
option {
28+
name = "exists 1"
29+
value = data.null_data_source.exists[0].inputs.foo
30+
}
31+
option {
32+
name = "exists 2"
33+
value = data.null_data_source.exists[1].inputs.foo
34+
}
35+
option {
36+
name = "exists 3"
37+
value = data.null_data_source.exists[2].inputs.foo
38+
}
2139
}
2240

23-
data "coder_parameter" "three" {
24-
count = 1
25-
name = "three"
26-
type = "number"
27-
default = data.coder_parameter.one[0].value
28-
}
41+
data "coder_parameter" "ref_count" {
42+
name = "ref_count"
43+
type = "string"
44+
default = data.coder_parameter.ref[0].default
45+
count = 1
46+
47+
option {
48+
name = "Only"
49+
value = data.coder_parameter.ref[0].default
50+
}
51+
}

0 commit comments

Comments
 (0)