Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a2b804

Browse files
committed
fixup! feat(tfparse): add support for built-in Terraform functions
1 parent 2eedbfc commit 7a2b804

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

coderd/templateversions_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,11 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
489489
"foo": "bar",
490490
"a": var.a,
491491
"b": data.coder_parameter.b.value,
492-
"test": try(null_resource.test.name, "whatever"),
492+
"test": pathexpand("~/file.txt"),
493493
}
494494
}`,
495495
},
496-
expectError: `Function calls not allowed; Functions may not be called here.`,
496+
expectError: `function "pathexpand" may not be used here`,
497497
},
498498
// We will allow coder_workspace_tags to set the scope on a template version import job
499499
// BUT the user ID will be ultimately determined by the API key in the scope.

provisioner/terraform/tfparse/funcs.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package tfparse
22

33
import (
4-
"fmt"
5-
64
"github.com/aquasecurity/trivy-iac/pkg/scanners/terraform/parser/funcs"
75
"github.com/hashicorp/hcl/v2/ext/tryfunc"
86
ctyyaml "github.com/zclconf/go-cty-yaml"
97
"github.com/zclconf/go-cty/cty"
108
"github.com/zclconf/go-cty/cty/function"
119
"github.com/zclconf/go-cty/cty/function/stdlib"
10+
"golang.org/x/xerrors"
1211
)
1312

1413
// Functions returns a set of functions that are safe to use in the context of
@@ -131,7 +130,8 @@ var (
131130
"filesha1": makeStubFunction("filesha1", cty.String, function.Parameter{Name: "path", Type: cty.String}),
132131
"filesha256": makeStubFunction("filesha256", cty.String, function.Parameter{Name: "path", Type: cty.String}),
133132
"filesha512": makeStubFunction("filesha512", cty.String, function.Parameter{Name: "path", Type: cty.String}),
134-
"pathexpand": makeStubFunction("pathexpand", cty.String, function.Parameter{Name: "path", Type: cty.String})}
133+
"pathexpand": makeStubFunction("pathexpand", cty.String, function.Parameter{Name: "path", Type: cty.String}),
134+
}
135135

136136
allFunctions = mergeMaps(safeFunctions, unsafeFileFunctions)
137137
)
@@ -155,8 +155,8 @@ func makeStubFunction(name string, returnType cty.Type, params ...function.Param
155155
var spec function.Spec
156156
spec.Params = params
157157
spec.Type = function.StaticReturnType(returnType)
158-
spec.Impl = func(args []cty.Value, retType cty.Type) (cty.Value, error) {
159-
return cty.UnknownVal(returnType), fmt.Errorf("function %q may not be used here", name)
158+
spec.Impl = func(_ []cty.Value, _ cty.Type) (cty.Value, error) {
159+
return cty.UnknownVal(returnType), xerrors.Errorf("function %q may not be used here", name)
160160
}
161161
return function.New(&spec)
162162
}

provisioner/terraform/tfparse/tfparse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ func Test_WorkspaceTagDefaultsFromFile(t *testing.T) {
482482
"cluster" = "${"devel"}${"opers"}"
483483
"region" = try(split(".", var.region)[1], "placeholder")
484484
"az" = try(split(".", data.coder_parameter.az.value)[1], "placeholder")
485-
"some_path" = pathexpand("file.txt")
485+
"some_path" = pathexpand("~/file.txt")
486486
}
487487
}`,
488488
},

0 commit comments

Comments
 (0)