Skip to content

Commit f971285

Browse files
committed
fwserver: add list resources to GetMetadata
1 parent 16a2973 commit f971285

File tree

8 files changed

+434
-25
lines changed

8 files changed

+434
-25
lines changed

internal/fwserver/server.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/terraform-plugin-framework/function"
1515
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1616
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
17+
"github.com/hashicorp/terraform-plugin-framework/list"
1718
"github.com/hashicorp/terraform-plugin-framework/provider"
1819
"github.com/hashicorp/terraform-plugin-framework/resource"
1920
)
@@ -172,6 +173,20 @@ type Server struct {
172173
// Provider.Resources() method.
173174
resourceFuncs map[string]func() resource.Resource
174175

176+
// listResourceFuncs is the cached Resource List functions for RPCs that need to
177+
// access resource lists. If not found, it will be fetched from the
178+
// Provider.ListResources() method.
179+
listResourceFuncs map[string]func() list.ListResource
180+
181+
// listResourceTypesDiags is the cached Diagnostics obtained while populating
182+
// listResourceTypes. This is to ensure any warnings or errors are also
183+
// returned appropriately when fetching listResourceTypes.
184+
listResourceTypesDiags diag.Diagnostics
185+
186+
// listResourceTypesMutex is a mutex to protect concurrent listResourceTypes
187+
// access from race conditions.
188+
listResourceTypesMutex sync.Mutex
189+
175190
// resourceTypesDiags is the cached Diagnostics obtained while populating
176191
// resourceTypes. This is to ensure any warnings or errors are also
177192
// returned appropriately when fetching resourceTypes.

internal/fwserver/server_getmetadata.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type GetMetadataResponse struct {
2020
Diagnostics diag.Diagnostics
2121
EphemeralResources []EphemeralResourceMetadata
2222
Functions []FunctionMetadata
23+
ListResources []ListResourceMetadata
2324
Resources []ResourceMetadata
2425
ServerCapabilities *ServerCapabilities
2526
}
@@ -52,28 +53,39 @@ type ResourceMetadata struct {
5253
TypeName string
5354
}
5455

56+
// ListResourceMetadata is the framework server equivalent of the
57+
// tfprotov5.ListResourceMetadata and tfprotov6.ListResourceMetadata types.
58+
type ListResourceMetadata struct {
59+
// TypeName is the name of the list resource.
60+
TypeName string
61+
}
62+
5563
// GetMetadata implements the framework server GetMetadata RPC.
5664
func (s *Server) GetMetadata(ctx context.Context, req *GetMetadataRequest, resp *GetMetadataResponse) {
5765
resp.DataSources = []DataSourceMetadata{}
5866
resp.EphemeralResources = []EphemeralResourceMetadata{}
5967
resp.Functions = []FunctionMetadata{}
68+
resp.ListResources = []ListResourceMetadata{}
6069
resp.Resources = []ResourceMetadata{}
70+
6171
resp.ServerCapabilities = s.ServerCapabilities()
6272

6373
datasourceMetadatas, diags := s.DataSourceMetadatas(ctx)
64-
6574
resp.Diagnostics.Append(diags...)
6675

6776
ephemeralResourceMetadatas, diags := s.EphemeralResourceMetadatas(ctx)
68-
6977
resp.Diagnostics.Append(diags...)
7078

7179
functionMetadatas, diags := s.FunctionMetadatas(ctx)
72-
7380
resp.Diagnostics.Append(diags...)
7481

7582
resourceMetadatas, diags := s.ResourceMetadatas(ctx)
83+
resp.Diagnostics.Append(diags...)
7684

85+
// Metadata for list resources must be retrieved after metadata for managed
86+
// resources. Server.ListResourceMetadatas checks that each list resource
87+
// type nane matches a known managed Resource type name.
88+
listResourceMetadatas, diags := s.ListResourceMetadatas(ctx)
7789
resp.Diagnostics.Append(diags...)
7890

7991
if resp.Diagnostics.HasError() {
@@ -83,5 +95,6 @@ func (s *Server) GetMetadata(ctx context.Context, req *GetMetadataRequest, resp
8395
resp.DataSources = datasourceMetadatas
8496
resp.EphemeralResources = ephemeralResourceMetadatas
8597
resp.Functions = functionMetadatas
98+
resp.ListResources = listResourceMetadatas
8699
resp.Resources = resourceMetadatas
87100
}

0 commit comments

Comments
 (0)