Skip to content

Commit 7c5bf23

Browse files
committed
catch absence of 'config' as a terraform bug
1 parent 2276835 commit 7c5bf23

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

internal/plugin/grpc_provider.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,13 @@ func (p *GRPCProvider) ValidateListResourceConfig(r providers.ValidateListResour
363363
}
364364

365365
configSchema := listResourceSchema.Body.BlockTypes["config"]
366-
ty := configSchema.ImpliedType()
367-
config := cty.NullVal(ty)
368-
if r.Config.Type().HasAttribute("config") {
369-
config = r.Config.GetAttr("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
370369
}
371-
mp, err := msgpack.Marshal(config, ty)
370+
371+
config := r.Config.GetAttr("config")
372+
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
372373
if err != nil {
373374
resp.Diagnostics = resp.Diagnostics.Append(err)
374375
return resp
@@ -1325,10 +1326,12 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
13251326
}
13261327

13271328
configSchema := listResourceSchema.Body.BlockTypes["config"]
1328-
config := cty.NullVal(configSchema.ImpliedType())
1329-
if r.Config.Type().HasAttribute("config") {
1330-
config = r.Config.GetAttr("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"))
1331+
return resp
13311332
}
1333+
1334+
config := r.Config.GetAttr("config")
13321335
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
13331336
if err != nil {
13341337
resp.Diagnostics = resp.Diagnostics.Append(err)

internal/plugin/grpc_provider_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,16 @@ func TestGRPCProvider_ValidateListResourceConfig_OptionalCfg(t *testing.T) {
497497
sch := providerProtoSchema()
498498

499499
// mock the schema in a way that makes the config attributes optional
500-
sch.ListResourceSchemas["list"].Block.Attributes[0].Optional = true
501-
sch.ListResourceSchemas["list"].Block.Attributes[0].Required = false
502-
sch.ListResourceSchemas["list"].Block.BlockTypes[0].MinItems = 0
503-
sch.ListResourceSchemas["list"].Block.BlockTypes[0].MaxItems = 0
500+
listSchema := sch.ListResourceSchemas["list"].Block
501+
// filter_attr is optional
502+
listSchema.Attributes[0].Optional = true
503+
listSchema.Attributes[0].Required = false
504+
505+
// nested_filter is optional
506+
listSchema.BlockTypes[0].MinItems = 0
507+
listSchema.BlockTypes[0].MaxItems = 0
508+
509+
sch.ListResourceSchemas["list"].Block = listSchema
504510
// we always need a GetSchema method
505511
client.EXPECT().GetSchema(
506512
gomock.Any(),
@@ -523,10 +529,15 @@ func TestGRPCProvider_ValidateListResourceConfig_OptionalCfg(t *testing.T) {
523529
gomock.Any(),
524530
).Return(&proto.ValidateListResourceConfig_Response{}, nil)
525531

526-
cfg := hcl2shim.HCL2ValueFromConfigValue(map[string]interface{}{})
532+
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+
}
527538
resp := p.ValidateListResourceConfig(providers.ValidateListResourceConfigRequest{
528539
TypeName: "list",
529-
Config: cfg,
540+
Config: coercedCfg,
530541
})
531542
checkDiags(t, resp.Diagnostics)
532543
}

internal/plugin6/grpc_provider.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,14 @@ func (p *GRPCProvider) ValidateListResourceConfig(r providers.ValidateListResour
359359
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("unknown list resource type %q", r.TypeName))
360360
return resp
361361
}
362+
362363
configSchema := listResourceSchema.Body.BlockTypes["config"]
363-
config := cty.NullVal(configSchema.ImpliedType())
364-
if r.Config.Type().HasAttribute("config") {
365-
config = r.Config.GetAttr("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
366367
}
368+
369+
config := r.Config.GetAttr("config")
367370
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
368371
if err != nil {
369372
resp.Diagnostics = resp.Diagnostics.Append(err)
@@ -1319,10 +1322,12 @@ func (p *GRPCProvider) ListResource(r providers.ListResourceRequest) providers.L
13191322
}
13201323

13211324
configSchema := listResourceSchema.Body.BlockTypes["config"]
1322-
config := cty.NullVal(configSchema.ImpliedType())
1323-
if r.Config.Type().HasAttribute("config") {
1324-
config = r.Config.GetAttr("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"))
1327+
return resp
13251328
}
1329+
1330+
config := r.Config.GetAttr("config")
13261331
mp, err := msgpack.Marshal(config, configSchema.ImpliedType())
13271332
if err != nil {
13281333
resp.Diagnostics = resp.Diagnostics.Append(err)

internal/plugin6/grpc_provider_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,15 @@ func TestGRPCProvider_ValidateListResourceConfig_OptionalCfg(t *testing.T) {
522522
gomock.Any(),
523523
).Return(&proto.ValidateListResourceConfig_Response{}, nil)
524524

525+
converted := convert.ProtoToListSchema(sch.ListResourceSchemas["list"])
525526
cfg := hcl2shim.HCL2ValueFromConfigValue(map[string]interface{}{})
527+
coercedCfg, err := converted.Body.CoerceValue(cfg)
528+
if err != nil {
529+
t.Fatalf("failed to coerce config: %v", err)
530+
}
526531
resp := p.ValidateListResourceConfig(providers.ValidateListResourceConfigRequest{
527532
TypeName: "list",
528-
Config: cfg,
533+
Config: coercedCfg,
529534
})
530535
checkDiags(t, resp.Diagnostics)
531536
}

0 commit comments

Comments
 (0)