Skip to content

Commit df53161

Browse files
committed
query: extract config block before passing to the provider request
1 parent 7c5bf23 commit df53161

14 files changed

+110
-192
lines changed

internal/command/query_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,15 +375,13 @@ func queryFixtureProvider() *testing_provider.MockProvider {
375375
// Check the config to determine what kind of response to return
376376
wholeConfigMap := request.Config.AsValueMap()
377377

378-
configMap := wholeConfigMap["config"]
379-
380378
// For empty results test case
381-
ami, ok := configMap.AsValueMap()["ami"]
379+
ami, ok := wholeConfigMap["ami"]
382380
if ok && ami.AsString() == "ami-nonexistent" {
383381
return providers.ListResourceResponse{
384382
Result: cty.ObjectVal(map[string]cty.Value{
385383
"data": cty.ListValEmpty(cty.DynamicPseudoType),
386-
"config": configMap,
384+
"config": request.Config,
387385
}),
388386
}
389387
}
@@ -414,7 +412,7 @@ func queryFixtureProvider() *testing_provider.MockProvider {
414412
"display_name": cty.StringVal("Test Instance 2"),
415413
}),
416414
}),
417-
"config": configMap,
415+
"config": request.Config,
418416
}),
419417
}
420418
case "test_database":
@@ -432,14 +430,14 @@ func queryFixtureProvider() *testing_provider.MockProvider {
432430
"display_name": cty.StringVal("Test Database 1"),
433431
}),
434432
}),
435-
"config": configMap,
433+
"config": request.Config,
436434
}),
437435
}
438436
default:
439437
return providers.ListResourceResponse{
440438
Result: cty.ObjectVal(map[string]cty.Value{
441439
"data": cty.ListVal([]cty.Value{}),
442-
"config": configMap,
440+
"config": request.Config,
443441
}),
444442
}
445443
}

internal/command/views/query_operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (v *QueryOperationHuman) Plan(plan *plans.Plan, schemas *terraform.Schemas)
6666
for _, query := range plan.Changes.Queries {
6767
pSchema := schemas.ProviderSchema(query.ProviderAddr.Provider)
6868
addr := query.Addr
69-
schema := pSchema.ListResourceTypes[addr.Resource.Resource.Type]
69+
schema := pSchema.SchemaForListResourceType(addr.Resource.Resource.Type)
7070

7171
results, err := query.Decode(schema)
7272
if err != nil {

internal/configs/query_file.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,10 @@ func decodeQueryListBlock(block *hcl.Block) (*Resource, hcl.Diagnostics) {
167167
})
168168
diags = append(diags, contentDiags...)
169169

170-
var configBlock hcl.Body
171170
for _, block := range content.Blocks {
172171
switch block.Type {
173172
case "config":
174-
if configBlock != nil {
173+
if r.List.Config != nil {
175174
diags = diags.Append(&hcl.Diagnostic{
176175
Severity: hcl.DiagError,
177176
Summary: "Duplicate config block",
@@ -180,7 +179,7 @@ func decodeQueryListBlock(block *hcl.Block) (*Resource, hcl.Diagnostics) {
180179
})
181180
continue
182181
}
183-
configBlock = block.Body
182+
r.List.Config = block.Body
184183
default:
185184
// Should not get here because the above should cover all
186185
// block types declared in the schema.

internal/configs/resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ type ManagedResource struct {
7070
}
7171

7272
type ListResource struct {
73+
// Config is the "config" block from the list resource configuration.
74+
Config hcl.Body
75+
7376
// By default, the results of a list resource only include the identities of
7477
// the discovered resources. If the user specifies "include_resources = true",
7578
// then the provider should include the resource data in the result.

internal/plans/changes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func (c *Changes) Encode(schemas *schemarepo.Schemas) (*ChangesSrc, error) {
8686
return changesSrc, fmt.Errorf("Changes.Encode: missing provider %s for %s", qi.ProviderAddr, qi.Addr)
8787
}
8888

89-
schema := p.ListResourceTypes[qi.Addr.Resource.Resource.Type]
90-
if schema.Body == nil {
89+
schema := p.SchemaForListResourceType(qi.Addr.Resource.Resource.Type)
90+
if schema.IsNil() {
9191
return changesSrc, fmt.Errorf("Changes.Encode: missing schema for %s", qi.Addr)
9292
}
9393
rcs, err := qi.Encode(schema)
@@ -308,8 +308,8 @@ func (qi *QueryInstance) DeepCopy() *QueryInstance {
308308
return &ret
309309
}
310310

311-
func (rc *QueryInstance) Encode(schema providers.Schema) (*QueryInstanceSrc, error) {
312-
results, err := NewDynamicValue(rc.Results.Value, schema.Body.ImpliedType())
311+
func (rc *QueryInstance) Encode(schema providers.ListResourceSchema) (*QueryInstanceSrc, error) {
312+
results, err := NewDynamicValue(rc.Results.Value, schema.FullSchema.ImpliedType())
313313
if err != nil {
314314
return nil, err
315315
}

internal/plans/changes_src.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ func (c *ChangesSrc) Decode(schemas *schemarepo.Schemas) (*Changes, error) {
150150
if !ok {
151151
return nil, fmt.Errorf("ChangesSrc.Decode: missing provider %s for %s", qis.ProviderAddr, qis.Addr)
152152
}
153-
schema := p.ListResourceTypes[qis.Addr.Resource.Resource.Type]
153+
schema := p.SchemaForListResourceType(qis.Addr.Resource.Resource.Type)
154154

155-
if schema.Body == nil {
155+
if schema.IsNil() {
156156
return nil, fmt.Errorf("ChangesSrc.Decode: missing schema for %s", qis.Addr)
157157
}
158158

@@ -210,8 +210,8 @@ type QueryInstanceSrc struct {
210210
Generated genconfig.ImportGroup
211211
}
212212

213-
func (qis *QueryInstanceSrc) Decode(schema providers.Schema) (*QueryInstance, error) {
214-
query, err := qis.Results.Decode(schema.Body.ImpliedType())
213+
func (qis *QueryInstanceSrc) Decode(schema providers.ListResourceSchema) (*QueryInstance, error) {
214+
query, err := qis.Results.Decode(schema.FullSchema.ImpliedType())
215215
if err != nil {
216216
return nil, err
217217
}

internal/plugin/grpc_provider.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -356,20 +356,13 @@ func (p *GRPCProvider) ValidateListResourceConfig(r providers.ValidateListResour
356356
return resp
357357
}
358358

359-
listResourceSchema, ok := schema.ListResourceTypes[r.TypeName]
360-
if !ok {
359+
listResourceSchema := schema.SchemaForListResourceType(r.TypeName)
360+
if listResourceSchema.IsNil() {
361361
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown list resource type %q", r.TypeName))
362362
return resp
363363
}
364364

365-
configSchema := listResourceSchema.Body.BlockTypes["config"]
366-
if !r.Config.Type().HasAttribute("config") {
367-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("missing required attribute \"config\"; this is a bug in Terraform - please report it"))
368-
return resp
369-
}
370-
371-
config := r.Config.GetAttr("config")
372-
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
365+
mp, err := msgpack.Marshal(r.Config, listResourceSchema.ConfigSchema.ImpliedType())
373366
if err != nil {
374367
resp.Diagnostics = resp.Diagnostics.Append(err)
375368
return resp
@@ -1313,26 +1306,19 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
13131306
return resp
13141307
}
13151308

1316-
listResourceSchema, ok := schema.ListResourceTypes[r.TypeName]
1317-
if !ok {
1318-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown list resource type %q", r.TypeName))
1319-
return resp
1320-
}
1321-
13221309
resourceSchema, ok := schema.ResourceTypes[r.TypeName]
13231310
if !ok || resourceSchema.Identity == nil {
13241311
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("identity schema not found for resource type %s", r.TypeName))
13251312
return resp
13261313
}
13271314

1328-
configSchema := listResourceSchema.Body.BlockTypes["config"]
1329-
if !r.Config.Type().HasAttribute("config") {
1330-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("missing required attribute \"config\"; this is a bug in Terraform - please report it"))
1315+
listResourceSchema := schema.SchemaForListResourceType(r.TypeName)
1316+
if listResourceSchema.IsNil() {
1317+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown list resource type %q", r.TypeName))
13311318
return resp
13321319
}
13331320

1334-
config := r.Config.GetAttr("config")
1335-
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
1321+
mp, err := msgpack.Marshal(r.Config, listResourceSchema.ConfigSchema.ImpliedType())
13361322
if err != nil {
13371323
resp.Diagnostics = resp.Diagnostics.Append(err)
13381324
return resp
@@ -1431,7 +1417,7 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
14311417
// and the elements of the result of a list resource instance (list.aws_instance.test.data[index])
14321418
resp.Result = cty.ObjectVal(map[string]cty.Value{
14331419
"data": cty.TupleVal(results),
1434-
"config": config,
1420+
"config": r.Config,
14351421
})
14361422
return resp
14371423
}

internal/plugin/grpc_provider_test.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func TestGRPCProvider_ValidateListResourceConfig(t *testing.T) {
483483
gomock.Any(),
484484
).Return(&proto.ValidateListResourceConfig_Response{}, nil)
485485

486-
cfg := hcl2shim.HCL2ValueFromConfigValue(map[string]interface{}{"config": map[string]interface{}{"filter_attr": "value", "nested_filter": map[string]interface{}{"nested_attr": "value"}}})
486+
cfg := hcl2shim.HCL2ValueFromConfigValue(map[string]any{"filter_attr": "value", "nested_filter": map[string]any{"nested_attr": "value"}})
487487
resp := p.ValidateListResourceConfig(providers.ValidateListResourceConfigRequest{
488488
TypeName: "list",
489489
Config: cfg,
@@ -530,14 +530,10 @@ func TestGRPCProvider_ValidateListResourceConfig_OptionalCfg(t *testing.T) {
530530
).Return(&proto.ValidateListResourceConfig_Response{}, nil)
531531

532532
converted := convert.ProtoToListSchema(sch.ListResourceSchemas["list"])
533-
cfg := hcl2shim.HCL2ValueFromConfigValue(map[string]any{})
534-
coercedCfg, err := converted.Body.CoerceValue(cfg)
535-
if err != nil {
536-
t.Fatalf("failed to coerce config: %v", err)
537-
}
533+
cfg := converted.Body.BlockTypes["config"].Block.EmptyValue()
538534
resp := p.ValidateListResourceConfig(providers.ValidateListResourceConfigRequest{
539535
TypeName: "list",
540-
Config: coercedCfg,
536+
Config: cfg,
541537
})
542538
checkDiags(t, resp.Diagnostics)
543539
}
@@ -1702,11 +1698,9 @@ func TestGRPCProvider_ListResource(t *testing.T) {
17021698

17031699
// Create the request
17041700
configVal := cty.ObjectVal(map[string]cty.Value{
1705-
"config": cty.ObjectVal(map[string]cty.Value{
1706-
"filter_attr": cty.StringVal("filter-value"),
1707-
"nested_filter": cty.ObjectVal(map[string]cty.Value{
1708-
"nested_attr": cty.StringVal("value"),
1709-
}),
1701+
"filter_attr": cty.StringVal("filter-value"),
1702+
"nested_filter": cty.ObjectVal(map[string]cty.Value{
1703+
"nested_attr": cty.StringVal("value"),
17101704
}),
17111705
})
17121706
request := providers.ListResourceRequest{
@@ -1787,11 +1781,9 @@ func TestGRPCProvider_ListResource_Error(t *testing.T) {
17871781
).Return(nil, fmt.Errorf("provider error"))
17881782

17891783
configVal := cty.ObjectVal(map[string]cty.Value{
1790-
"config": cty.ObjectVal(map[string]cty.Value{
1791-
"filter_attr": cty.StringVal("filter-value"),
1792-
"nested_filter": cty.ObjectVal(map[string]cty.Value{
1793-
"nested_attr": cty.StringVal("value"),
1794-
}),
1784+
"filter_attr": cty.StringVal("filter-value"),
1785+
"nested_filter": cty.ObjectVal(map[string]cty.Value{
1786+
"nested_attr": cty.StringVal("value"),
17951787
}),
17961788
})
17971789
request := providers.ListResourceRequest{
@@ -1805,11 +1797,9 @@ func TestGRPCProvider_ListResource_Error(t *testing.T) {
18051797

18061798
func TestGRPCProvider_ListResource_Diagnostics(t *testing.T) {
18071799
configVal := cty.ObjectVal(map[string]cty.Value{
1808-
"config": cty.ObjectVal(map[string]cty.Value{
1809-
"filter_attr": cty.StringVal("filter-value"),
1810-
"nested_filter": cty.ObjectVal(map[string]cty.Value{
1811-
"nested_attr": cty.StringVal("value"),
1812-
}),
1800+
"filter_attr": cty.StringVal("filter-value"),
1801+
"nested_filter": cty.ObjectVal(map[string]cty.Value{
1802+
"nested_attr": cty.StringVal("value"),
18131803
}),
18141804
})
18151805
request := providers.ListResourceRequest{
@@ -2071,11 +2061,9 @@ func TestGRPCProvider_ListResource_Limit(t *testing.T) {
20712061

20722062
// Create the request
20732063
configVal := cty.ObjectVal(map[string]cty.Value{
2074-
"config": cty.ObjectVal(map[string]cty.Value{
2075-
"filter_attr": cty.StringVal("filter-value"),
2076-
"nested_filter": cty.ObjectVal(map[string]cty.Value{
2077-
"nested_attr": cty.StringVal("value"),
2078-
}),
2064+
"filter_attr": cty.StringVal("filter-value"),
2065+
"nested_filter": cty.ObjectVal(map[string]cty.Value{
2066+
"nested_attr": cty.StringVal("value"),
20792067
}),
20802068
})
20812069
request := providers.ListResourceRequest{

internal/plugin6/grpc_provider.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -354,20 +354,13 @@ func (p *GRPCProvider) ValidateListResourceConfig(r providers.ValidateListResour
354354
return resp
355355
}
356356

357-
listResourceSchema, ok := schema.ListResourceTypes[r.TypeName]
358-
if !ok {
357+
listResourceSchema := schema.SchemaForListResourceType(r.TypeName)
358+
if listResourceSchema.IsNil() {
359359
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown list resource type %q", r.TypeName))
360360
return resp
361361
}
362362

363-
configSchema := listResourceSchema.Body.BlockTypes["config"]
364-
if !r.Config.Type().HasAttribute("config") {
365-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("missing required attribute \"config\"; this is a bug in Terraform - please report it"))
366-
return resp
367-
}
368-
369-
config := r.Config.GetAttr("config")
370-
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
363+
mp, err := msgpack.Marshal(r.Config, listResourceSchema.ConfigSchema.ImpliedType())
371364
if err != nil {
372365
resp.Diagnostics = resp.Diagnostics.Append(err)
373366
return resp
@@ -1309,26 +1302,19 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
13091302
return resp
13101303
}
13111304

1312-
listResourceSchema, ok := schema.ListResourceTypes[r.TypeName]
1313-
if !ok {
1314-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown list resource type %q", r.TypeName))
1315-
return resp
1316-
}
1317-
13181305
resourceSchema, ok := schema.ResourceTypes[r.TypeName]
13191306
if !ok || resourceSchema.Identity == nil {
13201307
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("identity schema not found for resource type %s", r.TypeName))
13211308
return resp
13221309
}
13231310

1324-
configSchema := listResourceSchema.Body.BlockTypes["config"]
1325-
if !r.Config.Type().HasAttribute("config") {
1326-
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("missing required attribute \"config\"; this is a bug in Terraform - please report it"))
1311+
listResourceSchema := schema.SchemaForListResourceType(r.TypeName)
1312+
if listResourceSchema.IsNil() {
1313+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown list resource type %q", r.TypeName))
13271314
return resp
13281315
}
13291316

1330-
config := r.Config.GetAttr("config")
1331-
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
1317+
mp, err := msgpack.Marshal(r.Config, listResourceSchema.ConfigSchema.ImpliedType())
13321318
if err != nil {
13331319
resp.Diagnostics = resp.Diagnostics.Append(err)
13341320
return resp
@@ -1427,7 +1413,7 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
14271413
// and the elements of the result of a list resource instance (list.aws_instance.test.data[index])
14281414
resp.Result = cty.ObjectVal(map[string]cty.Value{
14291415
"data": cty.TupleVal(results),
1430-
"config": config,
1416+
"config": r.Config,
14311417
})
14321418
return resp
14331419
}

0 commit comments

Comments
 (0)