Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tf5muxserver/mux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ type muxServer struct {

// Underlying servers for requests that should be handled by all servers
servers []tfprotov5.ProviderServer

// interceptors []tfprotov5.Interceptor
interceptors []Interceptor
}

type Interceptor struct {
BeforeListResource func(context.Context, *tfprotov5.ListResourceRequest) context.Context
}

// ProviderServer is a function compatible with tf6server.Serve.
Expand Down Expand Up @@ -426,3 +433,48 @@ func NewMuxServer(_ context.Context, servers ...func() tfprotov5.ProviderServer)

return &result, nil
}

type Option func(*muxServer)

func Servers(servers ...func() tfprotov5.ProviderServer) Option {
return func(mux *muxServer) {
for _, server := range servers {
mux.servers = append(mux.servers, server())
}
}
}

func Interceptors(interceptors ...Interceptor) Option {
return func(mux *muxServer) {
mux.interceptors = append(mux.interceptors, interceptors...)
}
}

// NewMuxServerWithOptions returns a muxed server that will route gRPC requests between
// tfprotov5.ProviderServers specified. The GetProviderSchema method of each
// is called to verify that the overall muxed server is compatible by ensuring:
//
// - All provider schemas exactly match
// - All provider meta schemas exactly match
// - Only one provider implements each managed resource
// - Only one provider implements each data source
// - Only one provider implements each function
// - Only one provider implements each ephemeral resource
// - Only one provider implements each list resource
// - Only one provider implements each resource identity
func NewMuxServerWithOptions(_ context.Context, options ...Option) (*muxServer, error) {
result := muxServer{
dataSources: make(map[string]tfprotov5.ProviderServer),
ephemeralResources: make(map[string]tfprotov5.ProviderServer),
listResources: make(map[string]tfprotov5.ProviderServer),
functions: make(map[string]tfprotov5.ProviderServer),
resources: make(map[string]tfprotov5.ProviderServer),
resourceCapabilities: make(map[string]*tfprotov5.ServerCapabilities),
}

for _, option := range options {
option(&result)
}

return &result, nil
}
7 changes: 7 additions & 0 deletions tf5muxserver/mux_server_ListResource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ func (s *muxServer) ListResource(ctx context.Context, req *tfprotov5.ListResourc
ctx = logging.InitContext(ctx)
ctx = logging.RpcContext(ctx, rpc)

for _, i := range s.interceptors {
if i.BeforeListResource == nil {
continue
}
ctx = i.BeforeListResource(ctx, req)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx = i.BeforeListResource(ctx, req)
ctx = i.Before(ctx, ListResource, req)

}

server, diags, err := s.getListResourceServer(ctx, req.TypeName)

if err != nil {
Expand Down
Loading