Skip to content

Commit 9f7f784

Browse files
committed
add a schemas method for list
1 parent bef8cef commit 9f7f784

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

internal/fwserver/server_listresources.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,14 @@ func (s *Server) ListResourceFuncs(ctx context.Context) (map[string]func() list.
8585
continue
8686
}
8787

88+
schemasResp := list.SchemaResponse{}
89+
if listResourceWithSchemas, ok := listResource.(list.ListResourceWithProtoSchemas); ok {
90+
listResourceWithSchemas.Schemas(ctx, &schemasResp)
91+
}
92+
8893
resourceFuncs, _ := s.ResourceFuncs(ctx)
8994
if _, ok := resourceFuncs[typeName]; !ok {
90-
if metadataResp.ProtoV5Schema == nil || metadataResp.ProtoV5IdentitySchema == nil {
95+
if schemasResp.ProtoV5Schema == nil || schemasResp.ProtoV5IdentitySchema == nil {
9196
s.listResourceFuncsDiags.AddError(
9297
"ListResource Type Defined without a Matching Managed Resource Type",
9398
fmt.Sprintf("The %s ListResource type name was returned, but no matching managed Resource type was defined. ", typeName)+

internal/proto5server/server_listresource.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
1111
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
1212
"github.com/hashicorp/terraform-plugin-framework/internal/toproto5"
13-
"github.com/hashicorp/terraform-plugin-framework/resource"
13+
"github.com/hashicorp/terraform-plugin-framework/list"
1414
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1515
)
1616

@@ -48,20 +48,22 @@ func (s *Server) ListResource(ctx context.Context, protoReq *tfprotov5.ListResou
4848
return ListRequestErrorDiagnostics(ctx, allDiags...)
4949
}
5050

51-
metadataResp := resource.MetadataResponse{}
52-
listResource.Metadata(ctx, resource.MetadataRequest{}, &metadataResp)
53-
5451
req := &fwserver.ListRequest{
5552
Config: config,
5653
ListResource: listResource,
5754
IncludeResource: protoReq.IncludeResource,
5855
Limit: protoReq.Limit,
5956
}
6057

58+
schemaResp := list.SchemaResponse{}
59+
if listResourceWithProtoSchemas, ok := listResource.(list.ListResourceWithProtoSchemas); ok {
60+
listResourceWithProtoSchemas.Schemas(ctx, &schemaResp)
61+
}
62+
6163
// There's validation in ListResources that ensures both are set if either is provided so it should be sufficient to only nil check Identity
62-
if metadataResp.ProtoV5IdentitySchema != nil {
63-
req.ResourceSchema, _ = fromproto5.ResourceSchema(ctx, metadataResp.ProtoV5Schema())
64-
req.ResourceIdentitySchema, _ = fromproto5.IdentitySchema(ctx, metadataResp.ProtoV5IdentitySchema())
64+
if schemaResp.ProtoV5IdentitySchema != nil {
65+
req.ResourceSchema, _ = fromproto5.ResourceSchema(ctx, schemaResp.ProtoV5Schema())
66+
req.ResourceIdentitySchema, _ = fromproto5.IdentitySchema(ctx, schemaResp.ProtoV5IdentitySchema())
6567
} else {
6668
req.ResourceSchema, diags = s.FrameworkServer.ResourceSchema(ctx, protoReq.TypeName)
6769
allDiags.Append(diags...)

list/list_resource.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1212
"github.com/hashicorp/terraform-plugin-framework/resource"
1313
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
14+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1415
"github.com/hashicorp/terraform-plugin-go/tftypes"
1516
)
1617

@@ -41,6 +42,16 @@ type ListResource interface {
4142
List(context.Context, ListRequest, *ListResultsStream)
4243
}
4344

45+
// ListResourceWithProtoSchemas is an interface type that extends ListResource to include a method
46+
// which allows provider developers to supply the ProtoV5 representations of resource and resource identity
47+
// schemas. This is necessary if list functionality is being used with a legacy resource.
48+
type ListResourceWithProtoSchemas interface {
49+
ListResource
50+
51+
// Schemas is called to provide the ProtoV5 representations of the resource and resource identity schemas.
52+
Schemas(context.Context, *SchemaResponse)
53+
}
54+
4455
// ListResourceWithConfigure is an interface type that extends ListResource to include a method
4556
// which the framework will automatically call so provider developers have the
4657
// opportunity to setup any necessary provider-level data or clients.
@@ -182,6 +193,20 @@ type ListResult struct {
182193
Diagnostics diag.Diagnostics
183194
}
184195

196+
// SchemaResponse represents a response that is populated by the Schemas method
197+
// and is used to pass along the ProtoV5 representations of the resource and resource identity schemas.
198+
type SchemaResponse struct {
199+
// ProtoV5IdentitySchema is the ProtoV5 representation of the resource identity
200+
// schema. This should only be supplied if framework functionality is being used
201+
// with a legacy resource. Currently, this only applies to list.
202+
ProtoV5IdentitySchema func() *tfprotov5.ResourceIdentitySchema
203+
204+
// ProtoV5Schema is the ProtoV5 representation of the resource schema
205+
// This should only be supplied if framework functionality is being used
206+
// with a legacy resource. Currently, this only applies to list.
207+
ProtoV5Schema func() *tfprotov5.Schema
208+
}
209+
185210
// ValidateConfigRequest represents a request to validate the configuration of
186211
// a list resource. An instance of this request struct is supplied as an
187212
// argument to the [ListResourceWithValidateConfig.ValidateListResourceConfig]

resource/metadata.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
package resource
55

6-
import "github.com/hashicorp/terraform-plugin-go/tfprotov5"
7-
86
// MetadataRequest represents a request for the Resource to return metadata,
97
// such as its type name. An instance of this request struct is supplied as
108
// an argument to the Resource type Metadata method.
@@ -27,16 +25,6 @@ type MetadataResponse struct {
2725
// ResourceBehavior is used to control framework-specific logic when
2826
// interacting with this resource.
2927
ResourceBehavior ResourceBehavior
30-
31-
// ProtoV5IdentitySchema is the ProtoV5 representation of the resource identity
32-
// schema. This should only be supplied if framework functionality is being used
33-
// with a legacy resource. Currently, this only applies to list.
34-
ProtoV5IdentitySchema func() *tfprotov5.ResourceIdentitySchema
35-
36-
// ProtoV5Schema is the ProtoV5 representation of the resource schema
37-
// This should only be supplied if framework functionality is being used
38-
// with a legacy resource. Currently, this only applies to list.
39-
ProtoV5Schema func() *tfprotov5.Schema
4028
}
4129

4230
// ResourceBehavior controls framework-specific logic when interacting

0 commit comments

Comments
 (0)