File tree Expand file tree Collapse file tree 3 files changed +84
-18
lines changed Expand file tree Collapse file tree 3 files changed +84
-18
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -306,7 +306,11 @@ func Test_Extract(t *testing.T) {
306
306
Owner : types.WorkspaceOwner {},
307
307
},
308
308
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
+ },
310
314
},
311
315
{
312
316
name : "defexpression" ,
Original file line number Diff line number Diff line change 1
1
terraform {
2
2
required_providers {
3
3
coder = {
4
- source = " coder/coder"
4
+ source = " coder/coder"
5
+ }
6
+ null = {
7
+ source = " hashicorp/null"
8
+ version = " 3.2.2"
5
9
}
6
10
}
7
11
}
8
12
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
+ }
14
19
}
15
20
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
+ }
21
39
}
22
40
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
+ }
You can’t perform that action at this time.
0 commit comments