diff --git a/CHANGELOG.md b/CHANGELOG.md index 314708b..13da8a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 2.4.0 (Dec 16, 2024) + +IMPROVEMENTS: + +* Added the `trace` attribute containing the outputs of std.trace() function (fixed #13). + ## 2.3.2 (Oct 4, 2024) IMPROVEMENTS: diff --git a/docs/data-sources/file.md b/docs/data-sources/file.md index fbb91de..ccf5c02 100644 --- a/docs/data-sources/file.md +++ b/docs/data-sources/file.md @@ -47,3 +47,4 @@ Exactly one of `source` or `content` is required. The following attribute is exported: * `rendered` — Rendered JSON document. +* `trace` — Output of std.trace() function. diff --git a/private/provider/data_source_jsonnet_file.go b/private/provider/data_source_jsonnet_file.go index d0ddc42..2ca1ab1 100644 --- a/private/provider/data_source_jsonnet_file.go +++ b/private/provider/data_source_jsonnet_file.go @@ -10,13 +10,14 @@ import ( "context" "crypto/sha256" "encoding/hex" + "strings" + "github.com/google/go-jsonnet" "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/types" - "strings" ) var _ datasource.DataSource = &JsonnetFileDataSource{} @@ -58,6 +59,7 @@ type JsonnetFileDataSourceModel struct { TlaCode types.Map `tfsdk:"tla_code"` StringOutput types.Bool `tfsdk:"string_output"` Rendered types.String `tfsdk:"rendered"` + Trace types.String `tfsdk:"trace"` } func (d *JsonnetFileDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { @@ -111,6 +113,10 @@ func (d *JsonnetFileDataSource) Schema(_ context.Context, _ datasource.SchemaReq Computed: true, MarkdownDescription: "Rendered text.", }, + "trace": schema.StringAttribute{ + Computed: true, + MarkdownDescription: "Output of std.trace() function.", + }, }, } } @@ -143,7 +149,10 @@ func (d *JsonnetFileDataSource) Read(ctx context.Context, req datasource.ReadReq } } + traceOut := strings.Builder{} + vm.StringOutput = state.StringOutput.ValueBool() + vm.SetTraceOut(&traceOut) rendered, err := func() (string, error) { var jPaths []string @@ -169,6 +178,7 @@ func (d *JsonnetFileDataSource) Read(ctx context.Context, req datasource.ReadReq } state.Rendered = types.StringValue(rendered) + state.Trace = types.StringValue(traceOut.String()) state.Id = types.StringValue(hash(rendered)) resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) diff --git a/private/provider/data_source_jsonnet_file_test.go b/private/provider/data_source_jsonnet_file_test.go index 9aecb0d..511fb25 100644 --- a/private/provider/data_source_jsonnet_file_test.go +++ b/private/provider/data_source_jsonnet_file_test.go @@ -219,3 +219,45 @@ func TestDataSourceJsonnetFile_RenderFileWithLocalJPath(t *testing.T) { }, }) } + +func TestDataSourceJsonnetFile_RenderContentWithTrace(t *testing.T) { + expectedResult := `{ + "a": "rest" +} +` + resource.UnitTest(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: ` + data "jsonnet_file" "template" { + content = <