Skip to content

Commit

Permalink
Output results of std.trace() function
Browse files Browse the repository at this point in the history
  • Loading branch information
alxrem committed Dec 16, 2024
1 parent 47157cf commit d261a12
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 11 additions & 1 deletion private/provider/data_source_jsonnet_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.",
},
},
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)...)
Expand Down
42 changes: 42 additions & 0 deletions private/provider/data_source_jsonnet_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<EOF
{
"a": std.trace("str", "rest")
}
EOF
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.jsonnet_file.template", "rendered", expectedResult),
resource.TestCheckResourceAttr("data.jsonnet_file.template", "trace", "TRACE: data:2 str\n"),
),
},
{
Config: `
data "jsonnet_file" "template" {
content = <<EOF
{
"a": "rest"
}
EOF
}
`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.jsonnet_file.template", "rendered", expectedResult),
resource.TestCheckResourceAttr("data.jsonnet_file.template", "trace", ""),
),
},
},
})
}

0 comments on commit d261a12

Please sign in to comment.