Skip to content

Commit 7f3191d

Browse files
committed
fwserver.ListResourceSchema++
1 parent eecfd76 commit 7f3191d

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

internal/fwserver/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ type Server struct {
125125
// access from race conditions.
126126
listResourceFuncsMutex sync.Mutex
127127

128+
// listResourceSchemas is the cached ListResource Schemas for RPCs that
129+
// need to convert configuration data from the protocol. If not found, it
130+
// will be fetched from the [list.ListResource.ListResourceConfigSchema]
131+
// method.
132+
listResourceSchemas map[string]fwschema.Schema
133+
134+
// listResourceSchemasMutex is a mutex to protect concurrent
135+
// listResourceSchemas access from race conditions.
136+
listResourceSchemasMutex sync.RWMutex
137+
128138
// providerSchema is the cached Provider Schema for RPCs that need to
129139
// convert configuration data from the protocol. If not found, it will be
130140
// fetched from the Provider.GetSchema() method.

internal/fwserver/server_listresources.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,48 @@ func (s *Server) ListResourceMetadatas(ctx context.Context) ([]ListResourceMetad
117117
return listResourceMetadatas, diags
118118
}
119119

120+
// ListResourceSchema returns the ListResource Schema for the given type name and
121+
// caches the result for later ListResource operations.
120122
func (s *Server) ListResourceSchema(ctx context.Context, typeName string) (fwschema.Schema, diag.Diagnostics) {
121-
schemas, _ := s.ListResourceSchemas(ctx)
122-
schema, ok := schemas[typeName]
123-
if !ok {
124-
return nil, diag.Diagnostics{
125-
diag.NewErrorDiagnostic(
126-
"ListResource Schema Not Found",
127-
fmt.Sprintf("No ListResource schema was found for type %q.", typeName),
128-
),
129-
}
123+
s.listResourceSchemasMutex.RLock()
124+
listResourceSchema, ok := s.listResourceSchemas[typeName]
125+
s.listResourceSchemasMutex.RUnlock()
126+
127+
if ok {
128+
return listResourceSchema, nil
129+
}
130+
131+
var diags diag.Diagnostics
132+
133+
listResource, listResourceDiags := s.ListResourceType(ctx, typeName)
134+
diags.Append(listResourceDiags...)
135+
if diags.HasError() {
136+
return nil, diags
137+
}
138+
139+
schemaReq := list.ListResourceSchemaRequest{}
140+
schemaResp := list.ListResourceSchemaResponse{}
141+
142+
logging.FrameworkTrace(ctx, "Calling provider defined ListResourceConfigSchema method", map[string]interface{}{logging.KeyListResourceType: typeName})
143+
listResource.ListResourceConfigSchema(ctx, schemaReq, &schemaResp)
144+
logging.FrameworkTrace(ctx, "Called provider defined ListResourceConfigSchema method", map[string]interface{}{logging.KeyListResourceType: typeName})
145+
146+
diags.Append(schemaResp.Diagnostics...)
147+
if diags.HasError() {
148+
return schemaResp.Schema, diags
149+
}
150+
151+
s.listResourceSchemasMutex.Lock()
152+
153+
if s.listResourceSchemas == nil {
154+
s.listResourceSchemas = make(map[string]fwschema.Schema)
130155
}
131156

132-
return schema, nil
157+
s.listResourceSchemas[typeName] = schemaResp.Schema
158+
159+
s.listResourceSchemasMutex.Unlock()
160+
161+
return schemaResp.Schema, diags
133162
}
134163

135164
// ListResourceSchemas returns a map of ListResource Schemas for the

0 commit comments

Comments
 (0)