Skip to content

Commit e6b545a

Browse files
committed
add generate resource config rpc
1 parent b38198f commit e6b545a

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

tf5muxserver/diagnostics.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func ephemeralResourceMissingError(typeName string) *tfprotov5.Diagnostic {
6868
}
6969
}
7070

71+
func generateResourceConfigMissingError(typeName string) *tfprotov5.Diagnostic {
72+
return &tfprotov5.Diagnostic{
73+
Severity: tfprotov5.DiagnosticSeverityError,
74+
Summary: "Generate Resource Config Not Implemented",
75+
Detail: "The combined provider does not implement the requested generate resource config for the resource type. " +
76+
"This is always an issue in the provider implementation and should be reported to the provider developers.\n\n" +
77+
"Missing generate resource config for resource type: " + typeName,
78+
}
79+
}
80+
7181
func listResourceDuplicateError(typeName string) *tfprotov5.Diagnostic {
7282
return &tfprotov5.Diagnostic{
7383
Severity: tfprotov5.DiagnosticSeverityError,

tf5muxserver/mux_server.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ type muxServer struct {
2929
// Routing for ephemeral resource types
3030
ephemeralResources map[string]tfprotov5.ProviderServer
3131

32+
// Routing for generating resource configuration
33+
generateResourceConfig map[string]tfprotov5.ProviderServer
34+
3235
// Routing for list resource types
3336
listResources map[string]tfprotov5.ProviderServer
3437

@@ -65,6 +68,10 @@ func (s *muxServer) ProviderServer() tfprotov5.ProviderServer {
6568
return s
6669
}
6770

71+
func (s *muxServer) ProviderServers() []tfprotov5.ProviderServer {
72+
return s.servers
73+
}
74+
6875
func (s *muxServer) getActionServer(ctx context.Context, actionType string) (tfprotov5.ProviderServer, []*tfprotov5.Diagnostic, error) {
6976
s.serverDiscoveryMutex.RLock()
7077
server, ok := s.actions[actionType]
@@ -170,6 +177,41 @@ func (s *muxServer) getEphemeralResourceServer(ctx context.Context, typeName str
170177
return server, s.serverDiscoveryDiagnostics, nil
171178
}
172179

180+
func (s *muxServer) getGenerateResourceConfigServer(ctx context.Context, typeName string) (tfprotov5.ProviderServer, []*tfprotov5.Diagnostic, error) {
181+
s.serverDiscoveryMutex.RLock()
182+
server, ok := s.resources[typeName]
183+
discoveryComplete := s.serverDiscoveryComplete
184+
s.serverDiscoveryMutex.RUnlock()
185+
186+
if discoveryComplete {
187+
if ok {
188+
return server, s.serverDiscoveryDiagnostics, nil
189+
}
190+
191+
return nil, []*tfprotov5.Diagnostic{
192+
generateResourceConfigMissingError(typeName),
193+
}, nil
194+
}
195+
196+
err := s.serverDiscovery(ctx)
197+
198+
if err != nil || diagnosticsHasError(s.serverDiscoveryDiagnostics) {
199+
return nil, s.serverDiscoveryDiagnostics, err
200+
}
201+
202+
s.serverDiscoveryMutex.RLock()
203+
server, ok = s.resources[typeName]
204+
s.serverDiscoveryMutex.RUnlock()
205+
206+
if !ok {
207+
return nil, []*tfprotov5.Diagnostic{
208+
generateResourceConfigMissingError(typeName),
209+
}, nil
210+
}
211+
212+
return server, s.serverDiscoveryDiagnostics, nil
213+
}
214+
173215
func (s *muxServer) getListResourceServer(ctx context.Context, typeName string) (tfprotov5.ProviderServer, []*tfprotov5.Diagnostic, error) {
174216
s.serverDiscoveryMutex.RLock()
175217
server, ok := s.listResources[typeName]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tf5muxserver
2+
3+
import (
4+
"context"
5+
//"fmt"
6+
7+
//"github.com/hashicorp/terraform-plugin-framework/providerserver"
8+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
9+
"github.com/hashicorp/terraform-plugin-mux/internal/logging"
10+
)
11+
12+
func (s *muxServer) GenerateResourceConfig(ctx context.Context, req *tfprotov5.GenerateResourceConfigRequest) (*tfprotov5.GenerateResourceConfigResponse, error) {
13+
rpc := "GenerateResourceConfig"
14+
ctx = logging.InitContext(ctx)
15+
ctx = logging.RpcContext(ctx, rpc)
16+
17+
server, diags, err := s.getGenerateResourceConfigServer(ctx, req.TypeName)
18+
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
// If there is an error diagnostic, return it directly
24+
if diagnosticsHasError(diags) {
25+
return &tfprotov5.GenerateResourceConfigResponse{
26+
Diagnostics: diags,
27+
}, nil
28+
}
29+
30+
return server.GenerateResourceConfig(ctx, req)
31+
}

0 commit comments

Comments
 (0)