Skip to content

Commit 954b002

Browse files
committed
Initial work on Query command for List Resource
1 parent 8047bdc commit 954b002

File tree

16 files changed

+325
-33
lines changed

16 files changed

+325
-33
lines changed

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ require (
1313
github.com/hashicorp/hc-install v0.9.2
1414
github.com/hashicorp/hcl/v2 v2.24.0
1515
github.com/hashicorp/logutils v1.0.0
16-
github.com/hashicorp/terraform-exec v0.23.0
16+
github.com/hashicorp/terraform-exec v0.23.1-0.20250717072919-061a850a52d2
1717
github.com/hashicorp/terraform-json v0.25.0
18-
github.com/hashicorp/terraform-plugin-go v0.28.0
18+
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1
1919
github.com/hashicorp/terraform-plugin-log v0.9.0
2020
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0
2121
github.com/mitchellh/go-testing-interface v1.14.1
@@ -36,7 +36,7 @@ require (
3636
github.com/hashicorp/go-multierror v1.1.1 // indirect
3737
github.com/hashicorp/go-plugin v1.6.3 // indirect
3838
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
39-
github.com/hashicorp/terraform-registry-address v0.2.5 // indirect
39+
github.com/hashicorp/terraform-registry-address v0.3.0 // indirect
4040
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
4141
github.com/hashicorp/yamux v0.1.1 // indirect
4242
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -56,7 +56,7 @@ require (
5656
golang.org/x/text v0.27.0 // indirect
5757
golang.org/x/tools v0.34.0 // indirect
5858
google.golang.org/appengine v1.6.8 // indirect
59-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a // indirect
60-
google.golang.org/grpc v1.72.1 // indirect
59+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 // indirect
60+
google.golang.org/grpc v1.73.0 // indirect
6161
google.golang.org/protobuf v1.36.6 // indirect
6262
)

go.sum

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQx
7676
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
7777
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
7878
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
79-
github.com/hashicorp/terraform-exec v0.23.0 h1:MUiBM1s0CNlRFsCLJuM5wXZrzA3MnPYEsiXmzATMW/I=
80-
github.com/hashicorp/terraform-exec v0.23.0/go.mod h1:mA+qnx1R8eePycfwKkCRk3Wy65mwInvlpAeOwmA7vlY=
79+
github.com/hashicorp/terraform-exec v0.23.1-0.20250717072919-061a850a52d2 h1:90fcAqw0Qmv4vY7zL4jEKgKarHmOnNN6SjTY68eLKGA=
80+
github.com/hashicorp/terraform-exec v0.23.1-0.20250717072919-061a850a52d2/go.mod h1:8D3RLLpzAZdhT9jvALYz1KHyGU4OvI73I1o0+01QJxA=
8181
github.com/hashicorp/terraform-json v0.25.0 h1:rmNqc/CIfcWawGiwXmRuiXJKEiJu1ntGoxseG1hLhoQ=
8282
github.com/hashicorp/terraform-json v0.25.0/go.mod h1:sMKS8fiRDX4rVlR6EJUMudg1WcanxCMoWwTLkgZP/vc=
83-
github.com/hashicorp/terraform-plugin-go v0.28.0 h1:zJmu2UDwhVN0J+J20RE5huiF3XXlTYVIleaevHZgKPA=
84-
github.com/hashicorp/terraform-plugin-go v0.28.0/go.mod h1:FDa2Bb3uumkTGSkTFpWSOwWJDwA7bf3vdP3ltLDTH6o=
83+
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1 h1:ZId6oWG8VTKhz207quE/Xh8a3HuoLtM/QkcSSypekIQ=
84+
github.com/hashicorp/terraform-plugin-go v0.29.0-alpha.1/go.mod h1:hL//wLEfYo0YVt0TC/VLzia/ADQQto3HEm4/jX2gkdY=
8585
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
8686
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
8787
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0 h1:NFPMacTrY/IdcIcnUB+7hsore1ZaRWU9cnB6jFoBnIM=
8888
github.com/hashicorp/terraform-plugin-sdk/v2 v2.37.0/go.mod h1:QYmYnLfsosrxjCnGY1p9c7Zj6n9thnEE+7RObeYs3fA=
89-
github.com/hashicorp/terraform-registry-address v0.2.5 h1:2GTftHqmUhVOeuu9CW3kwDkRe4pcBDq0uuK5VJngU1M=
90-
github.com/hashicorp/terraform-registry-address v0.2.5/go.mod h1:PpzXWINwB5kuVS5CA7m1+eO2f1jKb5ZDIxrOPfpnGkg=
89+
github.com/hashicorp/terraform-registry-address v0.3.0 h1:HMpK3nqaGFPS9VmgRXrJL/dzHNdheGVKk5k7VlFxzCo=
90+
github.com/hashicorp/terraform-registry-address v0.3.0/go.mod h1:jRGCMiLaY9zii3GLC7hqpSnwhfnCN5yzvY0hh4iCGbM=
9191
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
9292
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
9393
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
@@ -152,16 +152,16 @@ github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6
152152
github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM=
153153
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
154154
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
155-
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
156-
go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI=
157-
go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ=
158-
go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE=
159-
go.opentelemetry.io/otel/sdk v1.34.0 h1:95zS4k/2GOy069d321O8jWgYsW3MzVV+KuSPKp7Wr1A=
160-
go.opentelemetry.io/otel/sdk v1.34.0/go.mod h1:0e/pNiaMAqaykJGKbi+tSjWfNNHMTxoC9qANsCzbyxU=
161-
go.opentelemetry.io/otel/sdk/metric v1.34.0 h1:5CeK9ujjbFVL5c1PhLuStg1wxA7vQv7ce1EK0Gyvahk=
162-
go.opentelemetry.io/otel/sdk/metric v1.34.0/go.mod h1:jQ/r8Ze28zRKoNRdkjCZxfs6YvBTG1+YIqyFVFYec5w=
163-
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
164-
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
155+
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
156+
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
157+
go.opentelemetry.io/otel/metric v1.35.0 h1:0znxYu2SNyuMSQT4Y9WDWej0VpcsxkuklLa4/siN90M=
158+
go.opentelemetry.io/otel/metric v1.35.0/go.mod h1:nKVFgxBZ2fReX6IlyW28MgZojkoAkJGaE8CpgeAU3oE=
159+
go.opentelemetry.io/otel/sdk v1.35.0 h1:iPctf8iprVySXSKJffSS79eOjl9pvxV9ZqOWT0QejKY=
160+
go.opentelemetry.io/otel/sdk v1.35.0/go.mod h1:+ga1bZliga3DxJ3CQGg3updiaAJoNECOgJREo9KHGQg=
161+
go.opentelemetry.io/otel/sdk/metric v1.35.0 h1:1RriWBmCKgkeHEhM7a2uMjMUfP7MsOF5JpUCaEqEI9o=
162+
go.opentelemetry.io/otel/sdk/metric v1.35.0/go.mod h1:is6XYCUMpcKi+ZsOvfluY5YstFnhW0BidkR+gL+qN+w=
163+
go.opentelemetry.io/otel/trace v1.35.0 h1:dPpEfJu1sDIqruz7BHFG3c7528f6ddfSWfFDVt/xgMs=
164+
go.opentelemetry.io/otel/trace v1.35.0/go.mod h1:WUk7DtFp1Aw2MkvqGdwiXYDZZNvA/1J8o6xRXLrIkyc=
165165
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
166166
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
167167
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
@@ -214,10 +214,10 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
214214
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
215215
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
216216
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
217-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a h1:51aaUVRocpvUOSQKM6Q7VuoaktNIaMCLuhZB6DKksq4=
218-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250218202821-56aae31c358a/go.mod h1:uRxBH1mhmO8PGhU89cMcHaXKZqO+OfakD8QQO0oYwlQ=
219-
google.golang.org/grpc v1.72.1 h1:HR03wO6eyZ7lknl75XlxABNVLLFc2PAb6mHlYh756mA=
220-
google.golang.org/grpc v1.72.1/go.mod h1:wH5Aktxcg25y1I3w7H69nHfXdOG3UiadoBtjh3izSDM=
217+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 h1:e0AIkUUhxyBKh6ssZNrAMeqhA7RKUj42346d1y02i2g=
218+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463/go.mod h1:qQ0YXyHHx3XkvlzUtpXDkS29lDSafHMZBAZDc03LQ3A=
219+
google.golang.org/grpc v1.73.0 h1:VIWSmpI2MegBtTuFt5/JWy2oXxtjJ/e89Z70ImfD2ok=
220+
google.golang.org/grpc v1.73.0/go.mod h1:50sbHOUqWoCQGI8V2HQLJM0B+LMlIUjNSZmow7EVBQc=
221221
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
222222
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
223223
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=

helper/resource/testing.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,9 @@ type TestStep struct {
835835
// for performing import testing where the prior TestStep configuration
836836
// contained a provider outside the one under test.
837837
ExternalProviders map[string]ExternalProvider
838+
839+
// If true, the test step will run the query command
840+
Query bool
838841
}
839842

840843
// ConfigPlanChecks defines the different points in a Config TestStep when plan checks can be run.

internal/plugintest/working_dir.go

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package plugintest
55

66
import (
7+
"bytes"
78
"context"
89
"fmt"
910
"io"
@@ -21,6 +22,7 @@ import (
2122
const (
2223
ConfigFileName = "terraform_plugin_test.tf"
2324
PlanFileName = "tfplan"
25+
QueryFileName = "terraform_plugin_test.tfquery.hcl"
2426
)
2527

2628
// WorkingDir represents a distinct working directory that can be used for
@@ -37,6 +39,10 @@ type WorkingDir struct {
3739
// was stored; empty until SetConfig is called.
3840
configFilename string
3941

42+
// queryFilename is the full filename where the latest query configuration
43+
// was stored; empty until SetQuery is called.
44+
queryFilename string
45+
4046
// tf is the instance of tfexec.Terraform used for running Terraform commands
4147
tf *tfexec.Terraform
4248

@@ -101,7 +107,7 @@ func (wd *WorkingDir) SetConfig(ctx context.Context, cfg teststep.Config, vars c
101107

102108
for _, file := range fi {
103109
if file.Mode().IsRegular() {
104-
if filepath.Ext(file.Name()) == ".tf" || filepath.Ext(file.Name()) == ".json" {
110+
if filepath.Ext(file.Name()) == ".tf" || filepath.Ext(file.Name()) == ".json" || filepath.Ext(file.Name()) == ".hcl" {
105111
err = os.Remove(filepath.Join(d.Name(), file.Name()))
106112

107113
if err != nil && !os.IsNotExist(err) {
@@ -151,6 +157,80 @@ func (wd *WorkingDir) SetConfig(ctx context.Context, cfg teststep.Config, vars c
151157
return nil
152158
}
153159

160+
// SetQuery sets a new query configuration for the working directory.
161+
//
162+
// This must be called at least once before any call to Init or Query Destroy
163+
// to establish the query configuration. Any previously-set configuration is
164+
// discarded and any saved plan is cleared.
165+
func (wd *WorkingDir) SetQuery(ctx context.Context, cfg teststep.Config, vars config.Variables) error {
166+
// Remove old config and variables files first
167+
d, err := os.Open(wd.baseDir)
168+
169+
if err != nil {
170+
return err
171+
}
172+
173+
defer d.Close()
174+
175+
fi, err := d.Readdir(-1)
176+
177+
if err != nil {
178+
return err
179+
}
180+
181+
for _, file := range fi {
182+
if file.Mode().IsRegular() {
183+
if filepath.Ext(file.Name()) == ".warioform" || filepath.Ext(file.Name()) == ".json" || filepath.Ext(file.Name()) == ".hcl" {
184+
err = os.Remove(filepath.Join(d.Name(), file.Name()))
185+
186+
if err != nil && !os.IsNotExist(err) {
187+
return err
188+
}
189+
}
190+
}
191+
}
192+
193+
logging.HelperResourceTrace(ctx, "Setting Terraform query configuration", map[string]any{logging.KeyTestTerraformConfiguration: cfg})
194+
195+
outFilename := filepath.Join(wd.baseDir, QueryFileName)
196+
197+
// This file has to be written otherwise wd.Init() will return an error.
198+
err = os.WriteFile(outFilename, nil, 0700)
199+
200+
if err != nil {
201+
return err
202+
}
203+
204+
// wd.configFilename must be set otherwise wd.Init() will return an error.
205+
wd.queryFilename = outFilename
206+
wd.configFilename = outFilename
207+
208+
// Write configuration
209+
if cfg != nil {
210+
err = cfg.WriteQuery(ctx, wd.baseDir)
211+
212+
if err != nil {
213+
return err
214+
}
215+
}
216+
217+
//Write configuration variables
218+
err = vars.Write(wd.baseDir)
219+
220+
if err != nil {
221+
return err
222+
}
223+
224+
// Changing configuration invalidates any saved plan.
225+
err = wd.ClearPlan(ctx)
226+
227+
if err != nil {
228+
return err
229+
}
230+
231+
return nil
232+
}
233+
154234
// ClearState deletes any Terraform state present in the working directory.
155235
//
156236
// Any remote objects tracked by the state are not destroyed first, so this
@@ -444,3 +524,21 @@ func (wd *WorkingDir) Schemas(ctx context.Context) (*tfjson.ProviderSchemas, err
444524

445525
return providerSchemas, err
446526
}
527+
528+
func (wd *WorkingDir) Query(ctx context.Context) ([]string, error) {
529+
logging.HelperResourceTrace(ctx, "Calling Terraform CLI providers query command")
530+
531+
// Query the provider using the Terraform CLI function
532+
var buffer bytes.Buffer
533+
err := wd.tf.QueryJSON(context.Background(), &buffer)
534+
535+
if err != nil {
536+
return nil, fmt.Errorf("error running terraform query command: %w", err)
537+
}
538+
539+
logging.HelperResourceTrace(ctx, "Called Terraform CLI providers query command")
540+
541+
output := buffer.String()
542+
543+
return []string{output}, nil
544+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package testprovider
5+
6+
import (
7+
"context"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list"
10+
)
11+
12+
var _ list.ListResource = ListResource{}
13+
14+
type ListResource struct {
15+
SchemaResponse *list.SchemaResponse
16+
ListResultsStream *list.ListResultsStream
17+
ValidateListConfigResponse *list.ValidateListConfigResponse
18+
}
19+
20+
func (r ListResource) Schema(ctx context.Context, req list.SchemaRequest, resp *list.SchemaResponse) {
21+
if r.SchemaResponse != nil {
22+
resp.Diagnostics = r.SchemaResponse.Diagnostics
23+
resp.Schema = r.SchemaResponse.Schema
24+
}
25+
}
26+
func (r ListResource) List(ctx context.Context, req list.ListRequest, stream *list.ListResultsStream) {
27+
stream.Results = r.ListResultsStream.Results
28+
}

internal/testing/testprovider/provider.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1010
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource"
11+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list"
1112
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/provider"
1213
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
1314
)
@@ -19,6 +20,7 @@ var _ provider.Provider = Provider{}
1920
type Provider struct {
2021
ConfigureResponse *provider.ConfigureResponse
2122
DataSources map[string]DataSource
23+
ListResources map[string]ListResource
2224
Resources map[string]Resource
2325
SchemaResponse *provider.SchemaResponse
2426
StopResponse *provider.StopResponse
@@ -41,6 +43,16 @@ func (p Provider) DataSourcesMap() map[string]datasource.DataSource {
4143
return datasources
4244
}
4345

46+
func (p Provider) ListResourcesMap() map[string]list.ListResource {
47+
listResources := make(map[string]list.ListResource, len(p.ListResources))
48+
49+
for typeName, d := range p.ListResources {
50+
listResources[typeName] = d
51+
}
52+
53+
return listResources
54+
}
55+
4456
func (p Provider) ResourcesMap() map[string]resource.Resource {
4557
resources := make(map[string]resource.Resource, len(p.Resources))
4658

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package list
5+
6+
import (
7+
"context"
8+
"iter"
9+
10+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
11+
)
12+
13+
type ListResource interface {
14+
Schema(context.Context, SchemaRequest, *SchemaResponse)
15+
List(context.Context, ListRequest, *ListResultsStream)
16+
}
17+
18+
type ListRequest struct {
19+
}
20+
21+
type ListResultsStream struct {
22+
Results iter.Seq[ListResult]
23+
}
24+
25+
type ListResult struct {
26+
}
27+
28+
type ValidateListConfigResponse struct {
29+
}
30+
31+
type SchemaRequest struct{}
32+
33+
type SchemaResponse struct {
34+
Diagnostics []*tfprotov6.Diagnostic
35+
Schema *tfprotov6.Schema
36+
}

internal/testing/testsdk/provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import (
99
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1010
"github.com/hashicorp/terraform-plugin-go/tftypes"
1111
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/datasource"
12+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list"
1213
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
1314
)
1415

1516
type Provider interface {
1617
Configure(context.Context, ConfigureRequest, *ConfigureResponse)
1718
DataSourcesMap() map[string]datasource.DataSource
19+
ListResourcesMap() map[string]list.ListResource
1820
ResourcesMap() map[string]resource.Resource
1921
Schema(context.Context, SchemaRequest, *SchemaResponse)
2022
Stop(context.Context, StopRequest, *StopResponse)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package providerserver
5+
6+
import (
7+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
8+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list"
9+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/provider"
10+
)
11+
12+
func ProviderListResource(p provider.Provider, typeName string) (list.ListResource, *tfprotov6.Diagnostic) {
13+
r, ok := p.ListResourcesMap()[typeName]
14+
15+
if !ok {
16+
return nil, &tfprotov6.Diagnostic{
17+
Severity: tfprotov6.DiagnosticSeverityError,
18+
Summary: "Missing List Resource Type",
19+
Detail: "The provider does not define the list resource type: " + typeName,
20+
}
21+
}
22+
23+
return r, nil
24+
}

0 commit comments

Comments
 (0)