@@ -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.
120122func (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