|
|
@ -1,12 +1,13 @@ |
|
|
|
package resource |
|
|
|
package resource |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"context" |
|
|
|
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/fullstorydev/grpchan" |
|
|
|
"github.com/fullstorydev/grpchan" |
|
|
|
"github.com/fullstorydev/grpchan/inprocgrpc" |
|
|
|
|
|
|
|
"github.com/grafana/authlib/authn" |
|
|
|
|
|
|
|
grpcUtils "github.com/grafana/grafana/pkg/storage/unified/resource/grpc" |
|
|
|
grpcUtils "github.com/grafana/grafana/pkg/storage/unified/resource/grpc" |
|
|
|
grpcAuth "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" |
|
|
|
|
|
|
|
"google.golang.org/grpc" |
|
|
|
"google.golang.org/grpc" |
|
|
|
|
|
|
|
"google.golang.org/grpc/metadata" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type ResourceClient interface { |
|
|
|
type ResourceClient interface { |
|
|
@ -31,17 +32,133 @@ func NewResourceClient(channel *grpc.ClientConn) ResourceClient { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewLocalResourceClient(server ResourceStoreServer) ResourceStoreClient { |
|
|
|
func NewLocalResourceClient(server ResourceServer) ResourceStoreClient { |
|
|
|
channel := &inprocgrpc.Channel{} |
|
|
|
return &localResourceClient{server} |
|
|
|
auth := authn.LocalAuthenticator{} |
|
|
|
} |
|
|
|
channel.RegisterService( |
|
|
|
|
|
|
|
grpchan.InterceptServer( |
|
|
|
var _ ResourceClient = &localResourceClient{} |
|
|
|
&ResourceStore_ServiceDesc, |
|
|
|
|
|
|
|
grpcAuth.UnaryServerInterceptor(auth.Authenticate), |
|
|
|
type localResourceClient struct { |
|
|
|
grpcAuth.StreamServerInterceptor(auth.Authenticate), |
|
|
|
server ResourceServer |
|
|
|
), |
|
|
|
} |
|
|
|
server, // Implements all the things
|
|
|
|
|
|
|
|
) |
|
|
|
// Create implements ResourceClient.
|
|
|
|
cc := grpchan.InterceptClientConn(channel, grpcUtils.UnaryClientInterceptor, grpcUtils.StreamClientInterceptor) |
|
|
|
func (c *localResourceClient) Create(ctx context.Context, in *CreateRequest, opts ...grpc.CallOption) (*CreateResponse, error) { |
|
|
|
return NewResourceStoreClient(cc) |
|
|
|
return c.server.Create(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Delete implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) Delete(ctx context.Context, in *DeleteRequest, opts ...grpc.CallOption) (*DeleteResponse, error) { |
|
|
|
|
|
|
|
return c.server.Delete(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// History implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) History(ctx context.Context, in *HistoryRequest, opts ...grpc.CallOption) (*HistoryResponse, error) { |
|
|
|
|
|
|
|
return c.server.History(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// IsHealthy implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) IsHealthy(ctx context.Context, in *HealthCheckRequest, opts ...grpc.CallOption) (*HealthCheckResponse, error) { |
|
|
|
|
|
|
|
return c.server.IsHealthy(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// List implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) List(ctx context.Context, in *ListRequest, opts ...grpc.CallOption) (*ListResponse, error) { |
|
|
|
|
|
|
|
return c.server.List(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Origin implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) Origin(ctx context.Context, in *OriginRequest, opts ...grpc.CallOption) (*OriginResponse, error) { |
|
|
|
|
|
|
|
return c.server.Origin(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) Read(ctx context.Context, in *ReadRequest, opts ...grpc.CallOption) (*ReadResponse, error) { |
|
|
|
|
|
|
|
return c.server.Read(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) Update(ctx context.Context, in *UpdateRequest, opts ...grpc.CallOption) (*UpdateResponse, error) { |
|
|
|
|
|
|
|
return c.server.Update(ctx, in) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Watch implements ResourceClient.
|
|
|
|
|
|
|
|
func (c *localResourceClient) Watch(ctx context.Context, in *WatchRequest, opts ...grpc.CallOption) (ResourceStore_WatchClient, error) { |
|
|
|
|
|
|
|
stream := &localWatchStream{ |
|
|
|
|
|
|
|
ctx: ctx, |
|
|
|
|
|
|
|
events: make(chan *WatchEvent), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
err := c.server.Watch(in, stream) |
|
|
|
|
|
|
|
return stream, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _ ResourceStore_WatchClient = &localWatchStream{} |
|
|
|
|
|
|
|
var _ ResourceStore_WatchServer = &localWatchStream{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type localWatchStream struct { |
|
|
|
|
|
|
|
ctx context.Context |
|
|
|
|
|
|
|
events chan *WatchEvent |
|
|
|
|
|
|
|
closed bool |
|
|
|
|
|
|
|
trailer metadata.MD |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send implements ResourceStore_WatchServer.
|
|
|
|
|
|
|
|
func (s *localWatchStream) Send(e *WatchEvent) error { |
|
|
|
|
|
|
|
if s.closed { |
|
|
|
|
|
|
|
return fmt.Errorf("stream is already closed") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
s.events <- e |
|
|
|
|
|
|
|
return nil // check if the channel is OK?
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Recv implements ResourceStore_WatchClient.
|
|
|
|
|
|
|
|
func (s *localWatchStream) Recv() (*WatchEvent, error) { |
|
|
|
|
|
|
|
e := <-s.events |
|
|
|
|
|
|
|
return e, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Context implements ResourceStore_WatchClient.
|
|
|
|
|
|
|
|
func (s *localWatchStream) Context() context.Context { |
|
|
|
|
|
|
|
return s.ctx |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CloseSend implements ResourceStore_WatchClient.
|
|
|
|
|
|
|
|
func (s *localWatchStream) CloseSend() error { |
|
|
|
|
|
|
|
s.closed = true |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SendHeader implements ResourceStore_WatchServer.
|
|
|
|
|
|
|
|
func (s *localWatchStream) SendHeader(metadata.MD) error { |
|
|
|
|
|
|
|
return fmt.Errorf("local stream") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetHeader implements ResourceStore_WatchServer.
|
|
|
|
|
|
|
|
func (s *localWatchStream) SetHeader(metadata.MD) error { |
|
|
|
|
|
|
|
return fmt.Errorf("local stream") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetTrailer implements ResourceStore_WatchServer.
|
|
|
|
|
|
|
|
func (s *localWatchStream) SetTrailer(v metadata.MD) { |
|
|
|
|
|
|
|
s.trailer = v |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Header implements ResourceStore_WatchClient.
|
|
|
|
|
|
|
|
func (s *localWatchStream) Header() (metadata.MD, error) { |
|
|
|
|
|
|
|
return metadata.MD{}, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RecvMsg implements ResourceStore_WatchClient.
|
|
|
|
|
|
|
|
func (s *localWatchStream) RecvMsg(m any) error { |
|
|
|
|
|
|
|
return fmt.Errorf("local stream") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SendMsg implements ResourceStore_WatchClient.
|
|
|
|
|
|
|
|
func (s *localWatchStream) SendMsg(m any) error { |
|
|
|
|
|
|
|
return fmt.Errorf("local stream") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Trailer implements ResourceStore_WatchClient.
|
|
|
|
|
|
|
|
func (s *localWatchStream) Trailer() metadata.MD { |
|
|
|
|
|
|
|
return s.trailer |
|
|
|
} |
|
|
|
} |
|
|
|