Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
954b002
Initial work on Query command for List Resource
rainkwan Jul 21, 2025
99e9c88
Added helper/resource parts for query
rainkwan Jul 23, 2025
870fff0
Moved files
rainkwan Jul 23, 2025
ede917c
But what if we comment out print statements?
rainkwan Jul 24, 2025
06d2573
What if we are more specific and say `.tfquery.hcl`
rainkwan Jul 30, 2025
236218d
Updating to version 1.14 for query_test.go
rainkwan Jul 31, 2025
49004cd
Scaffolding for QueryCheck
rainkwan Jul 31, 2025
086d38c
Outlining the marshalling for filling in terraform-json.Query
rainkwan Aug 4, 2025
cb0e717
Update testing_new.go
rainkwan Jul 31, 2025
f3be28d
Update helper/resource/query/query_test.go
rainkwan Aug 4, 2025
b5c37ca
Update internal/plugintest/working_dir.go
rainkwan Aug 4, 2025
896310e
Update internal/teststep/file.go
rainkwan Aug 4, 2025
116911f
Updating branch
rainkwan Aug 4, 2025
fef3bb6
Working on using unmarshalled buffer instead of tfjson
rainkwan Aug 18, 2025
a8e37e2
Removed tests that get skipped for version
rainkwan Aug 20, 2025
f59ae5d
pass reattach info into QueryJSON call
stephybun Aug 22, 2025
280f10b
unmarshal found list result
stephybun Aug 25, 2025
549962f
change tfversion checks to skip below `1.14`
SBGoods Aug 26, 2025
a46b2dc
try unmarshalling query results and traversing using `tfjsonpath`
SBGoods Aug 26, 2025
6953c7f
Updated go mod to latest
rainkwan Sep 2, 2025
95081f8
Changes from pairing
rainkwan Sep 2, 2025
884dfff
Updated terraform exec with new draft and initial query test in helpe…
rainkwan Sep 3, 2025
d531120
Updated to add error handling for returned terraform exec boolean
rainkwan Sep 3, 2025
d1418a8
Pushing for up to dateness
rainkwan Sep 4, 2025
22e003c
updating providerserver.go
rainkwan Sep 4, 2025
53ecea9
Updated the query test to return actual identity and start to impleme…
rainkwan Sep 9, 2025
5cbd9f7
fixed test so expect_identity.go runs
rainkwan Sep 9, 2025
b6e6c9c
Refactored expect_identity.go so it works for multiple identity attri…
rainkwan Sep 11, 2025
a6c11c2
Changes by sgoods
rainkwan Sep 11, 2025
3d77c5b
Updated error messages
rainkwan Sep 11, 2025
fd46aa7
Updated error messages
rainkwan Sep 11, 2025
02d6caa
Updated comment
rainkwan Sep 11, 2025
3331b2b
add contains query check for checking if a given resource exists in t…
stephybun Sep 12, 2025
c465a33
update CheckQueryRequest to provide ListResourceFoundData and ListCom…
stephybun Sep 12, 2025
10dd78c
add expect known value check
stephybun Sep 16, 2025
50430ce
Merge branch 'main' into rk/list-resource-query-check
rainkwan Sep 16, 2025
2858c75
Updated comment
rainkwan Sep 16, 2025
9e1d041
Ran make generate
rainkwan Sep 16, 2025
3da6e28
Fixed lintier
rainkwan Sep 16, 2025
695a600
Fixed lintier
rainkwan Sep 16, 2025
0bebb21
update terraform-json and terraform-exec dependencies and update quer…
stephybun Sep 17, 2025
e5bd162
remove redundant return statement and skip over nil messages
stephybun Sep 17, 2025
cca1521
fix query test
stephybun Sep 17, 2025
79b2189
review comments
stephybun Sep 18, 2025
460e176
update query test
stephybun Sep 18, 2025
e7db617
minor fixes
stephybun Sep 18, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require (
github.com/hashicorp/hc-install v0.9.2
github.com/hashicorp/hcl/v2 v2.24.0
github.com/hashicorp/logutils v1.0.0
github.com/hashicorp/terraform-exec v0.23.1-0.20250717072919-061a850a52d2
github.com/hashicorp/terraform-json v0.26.0
github.com/hashicorp/terraform-exec v0.24.0
github.com/hashicorp/terraform-json v0.27.2
github.com/hashicorp/terraform-plugin-go v0.29.0-beta.1
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQx
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.23.1-0.20250717072919-061a850a52d2 h1:90fcAqw0Qmv4vY7zL4jEKgKarHmOnNN6SjTY68eLKGA=
github.com/hashicorp/terraform-exec v0.23.1-0.20250717072919-061a850a52d2/go.mod h1:8D3RLLpzAZdhT9jvALYz1KHyGU4OvI73I1o0+01QJxA=
github.com/hashicorp/terraform-json v0.26.0 h1:+BnJavhRH+oyNWPnfzrfQwVWCZBFMvjdiH2Vi38Udz4=
github.com/hashicorp/terraform-json v0.26.0/go.mod h1:eyWCeC3nrZamyrKLFnrvwpc3LQPIJsx8hWHQ/nu2/v4=
github.com/hashicorp/terraform-exec v0.24.0 h1:mL0xlk9H5g2bn0pPF6JQZk5YlByqSqrO5VoaNtAf8OE=
github.com/hashicorp/terraform-exec v0.24.0/go.mod h1:lluc/rDYfAhYdslLJQg3J0oDqo88oGQAdHR+wDqFvo4=
github.com/hashicorp/terraform-json v0.27.2 h1:BwGuzM6iUPqf9JYM/Z4AF1OJ5VVJEEzoKST/tRDBJKU=
github.com/hashicorp/terraform-json v0.27.2/go.mod h1:GzPLJ1PLdUG5xL6xn1OXWIjteQRT2CNT9o/6A9mi9hE=
github.com/hashicorp/terraform-plugin-go v0.29.0-beta.1 h1:xeHlRQYev3iMXwX2W7+D1bSfLRBs9jojZXqE6hmNxMI=
github.com/hashicorp/terraform-plugin-go v0.29.0-beta.1/go.mod h1:5pww/UULn9C2tItq6o5sbScEkJxBUt9X9kI4DkeRsIw=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
91 changes: 91 additions & 0 deletions helper/resource/query/examplecloud_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package query_test

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list"
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
)

func examplecloudListResource() testprovider.ListResource {
return testprovider.ListResource{
IncludeResource: true,
SchemaResponse: &list.SchemaResponse{
Schema: &tfprotov6.Schema{
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "id",
Type: tftypes.String,
Computed: true,
},
},
},
},
},
ListResultsStream: &list.ListResultsStream{
Results: func(push func(list.ListResult) bool) {
push(list.ListResult{
Resource: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
"name": tftypes.NewValue(tftypes.String, "somevalue"),
},
)),
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue1"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
},
)),
})
push(list.ListResult{
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue2"),
"location": tftypes.NewValue(tftypes.String, "westeurope2"),
},
)),
})
push(list.ListResult{
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"location": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue3"),
"location": tftypes.NewValue(tftypes.String, "westeurope3"),
},
)),
})
},
},
}
}
23 changes: 17 additions & 6 deletions helper/resource/query/examplecloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ func examplecloudResource() testprovider.Resource {
NewIdentity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"id": tftypes.String,
"location": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "somelocation"),
},
)),
},
Expand All @@ -57,11 +59,13 @@ func examplecloudResource() testprovider.Resource {
NewIdentity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"id": tftypes.String,
"location": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope"),
},
)),
},
Expand All @@ -83,11 +87,13 @@ func examplecloudResource() testprovider.Resource {
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"id": tftypes.String,
"id": tftypes.String,
"location": tftypes.String,
},
},
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
"location": tftypes.NewValue(tftypes.String, "westeurope/somevalue"),
},
)),
},
Expand All @@ -111,6 +117,11 @@ func examplecloudResource() testprovider.Resource {
Type: tftypes.String,
RequiredForImport: true,
},
{
Name: "location",
Type: tftypes.String,
RequiredForImport: true,
},
},
},
},
Expand Down
52 changes: 52 additions & 0 deletions helper/resource/query/query_checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package query

import (
"context"
"errors"
"fmt"

tfjson "github.com/hashicorp/terraform-json"
"github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-testing/querycheck"
)

func RunQueryChecks(ctx context.Context, t testing.T, query []tfjson.LogMsg, queryChecks []querycheck.QueryResultCheck) error {
t.Helper()

var result []error

if query == nil {
result = append(result, fmt.Errorf("no query results found"))
}

found := make([]tfjson.ListResourceFoundData, 0)
summary := tfjson.ListCompleteData{}

for _, msg := range query {
switch v := msg.(type) {
case tfjson.ListResourceFoundMessage:
found = append(found, v.ListResourceFound)
case tfjson.ListCompleteMessage:
summary = v.ListComplete
// TODO diagnostics and errors?
default:
continue
}
}

for _, queryCheck := range queryChecks {
resp := querycheck.CheckQueryResponse{}
queryCheck.CheckQuery(ctx, querycheck.CheckQueryRequest{
Query: found,
QuerySummary: &summary,
}, &resp)

result = append(result, resp.Error)
}

return errors.Join(result...)
}
22 changes: 22 additions & 0 deletions helper/resource/query/query_checks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package query

import (
"context"

"github.com/hashicorp/terraform-plugin-testing/querycheck"
)

var _ querycheck.QueryResultCheck = &queryCheckSpy{}

type queryCheckSpy struct {
err error
called bool
}
Comment on lines +14 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Doesn't look like this is used anywhere, perhaps it will eventually?


func (s *queryCheckSpy) CheckQuery(ctx context.Context, req querycheck.CheckQueryRequest, resp *querycheck.CheckQueryResponse) {
s.called = true
resp.Error = s.err
}
Loading