mirror of https://github.com/grafana/grafana
Plugins: Add API for creating pluginv2 proto client (#77492)
* first pass * remove client from plugin * fix wire * update * undo import change * add opts * add check * tidy * re-use logic * rollback changespull/77408/head
parent
d624a5d490
commit
19cd7dbae1
@ -0,0 +1,123 @@ |
||||
package grpcplugin |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
|
||||
"google.golang.org/grpc" |
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2" |
||||
|
||||
"github.com/grafana/grafana/pkg/plugins/log" |
||||
) |
||||
|
||||
var ( |
||||
errClientNotStarted = errors.New("plugin client has not been started") |
||||
) |
||||
|
||||
var _ ProtoClient = (*protoClient)(nil) |
||||
|
||||
type ProtoClient interface { |
||||
pluginv2.DataClient |
||||
pluginv2.ResourceClient |
||||
pluginv2.DiagnosticsClient |
||||
pluginv2.StreamClient |
||||
|
||||
PluginID() string |
||||
Logger() log.Logger |
||||
Start(context.Context) error |
||||
Stop(context.Context) error |
||||
} |
||||
|
||||
type protoClient struct { |
||||
plugin *grpcPlugin |
||||
} |
||||
|
||||
type ProtoClientOpts struct { |
||||
PluginID string |
||||
ExecutablePath string |
||||
ExecutableArgs []string |
||||
Env []string |
||||
Logger log.Logger |
||||
} |
||||
|
||||
func NewProtoClient(opts ProtoClientOpts) (ProtoClient, error) { |
||||
p := newGrpcPlugin( |
||||
PluginDescriptor{ |
||||
pluginID: opts.PluginID, |
||||
managed: true, |
||||
executablePath: opts.ExecutablePath, |
||||
executableArgs: opts.ExecutableArgs, |
||||
versionedPlugins: pluginSet, |
||||
}, |
||||
opts.Logger, |
||||
func() []string { return opts.Env }, |
||||
) |
||||
|
||||
return &protoClient{plugin: p}, nil |
||||
} |
||||
|
||||
func (r *protoClient) PluginID() string { |
||||
return r.plugin.descriptor.pluginID |
||||
} |
||||
|
||||
func (r *protoClient) Logger() log.Logger { |
||||
return r.plugin.logger |
||||
} |
||||
|
||||
func (r *protoClient) Start(ctx context.Context) error { |
||||
return r.plugin.Start(ctx) |
||||
} |
||||
|
||||
func (r *protoClient) Stop(ctx context.Context) error { |
||||
return r.plugin.Stop(ctx) |
||||
} |
||||
|
||||
func (r *protoClient) QueryData(ctx context.Context, in *pluginv2.QueryDataRequest, opts ...grpc.CallOption) (*pluginv2.QueryDataResponse, error) { |
||||
if r.plugin.pluginClient == nil { |
||||
return nil, errClientNotStarted |
||||
} |
||||
return r.plugin.pluginClient.DataClient.QueryData(ctx, in, opts...) |
||||
} |
||||
|
||||
func (r *protoClient) CallResource(ctx context.Context, in *pluginv2.CallResourceRequest, opts ...grpc.CallOption) (pluginv2.Resource_CallResourceClient, error) { |
||||
if r.plugin.pluginClient == nil { |
||||
return nil, errClientNotStarted |
||||
} |
||||
return r.plugin.pluginClient.ResourceClient.CallResource(ctx, in, opts...) |
||||
} |
||||
|
||||
func (r *protoClient) CheckHealth(ctx context.Context, in *pluginv2.CheckHealthRequest, opts ...grpc.CallOption) (*pluginv2.CheckHealthResponse, error) { |
||||
if r.plugin.pluginClient == nil { |
||||
return nil, errClientNotStarted |
||||
} |
||||
return r.plugin.pluginClient.DiagnosticsClient.CheckHealth(ctx, in, opts...) |
||||
} |
||||
|
||||
func (r *protoClient) CollectMetrics(ctx context.Context, in *pluginv2.CollectMetricsRequest, opts ...grpc.CallOption) (*pluginv2.CollectMetricsResponse, error) { |
||||
if r.plugin.pluginClient == nil { |
||||
return nil, errClientNotStarted |
||||
} |
||||
return r.plugin.pluginClient.DiagnosticsClient.CollectMetrics(ctx, in, opts...) |
||||
} |
||||
|
||||
func (r *protoClient) SubscribeStream(ctx context.Context, in *pluginv2.SubscribeStreamRequest, opts ...grpc.CallOption) (*pluginv2.SubscribeStreamResponse, error) { |
||||
if r.plugin.pluginClient == nil { |
||||
return nil, errClientNotStarted |
||||
} |
||||
return r.plugin.pluginClient.StreamClient.SubscribeStream(ctx, in, opts...) |
||||
} |
||||
|
||||
func (r *protoClient) RunStream(ctx context.Context, in *pluginv2.RunStreamRequest, opts ...grpc.CallOption) (pluginv2.Stream_RunStreamClient, error) { |
||||
if r.plugin.pluginClient == nil { |
||||
return nil, errClientNotStarted |
||||
} |
||||
return r.plugin.pluginClient.StreamClient.RunStream(ctx, in, opts...) |
||||
} |
||||
|
||||
func (r *protoClient) PublishStream(ctx context.Context, in *pluginv2.PublishStreamRequest, opts ...grpc.CallOption) (*pluginv2.PublishStreamResponse, error) { |
||||
if r.plugin.pluginClient == nil { |
||||
return nil, errClientNotStarted |
||||
} |
||||
return r.plugin.pluginClient.StreamClient.PublishStream(ctx, in, opts...) |
||||
} |
Loading…
Reference in new issue