mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Expose queryconvert endpoint (#93656)
This commit is contained in:
parent
177965704d
commit
225600a08b
137
pkg/registry/apis/datasource/queryconvert.go
Normal file
137
pkg/registry/apis/datasource/queryconvert.go
Normal file
@ -0,0 +1,137 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
|
||||
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
type pluginClientConversion interface {
|
||||
backend.ConversionHandler
|
||||
}
|
||||
|
||||
type queryConvertREST struct {
|
||||
client pluginClientConversion
|
||||
contextProvider PluginContextWrapper
|
||||
}
|
||||
|
||||
var (
|
||||
_ rest.Storage = (*queryConvertREST)(nil)
|
||||
_ rest.Connecter = (*queryConvertREST)(nil)
|
||||
_ rest.Scoper = (*queryConvertREST)(nil)
|
||||
_ rest.SingularNameProvider = (*queryConvertREST)(nil)
|
||||
)
|
||||
|
||||
func registerQueryConvert(client pluginClientConversion, contextProvider PluginContextWrapper, storage map[string]rest.Storage) {
|
||||
store := &queryConvertREST{
|
||||
client: client,
|
||||
contextProvider: contextProvider,
|
||||
}
|
||||
storage["queryconvert"] = store
|
||||
}
|
||||
|
||||
func (r *queryConvertREST) New() runtime.Object {
|
||||
return &query.QueryDataRequest{}
|
||||
}
|
||||
|
||||
func (r *queryConvertREST) Destroy() {}
|
||||
|
||||
func (r *queryConvertREST) NamespaceScoped() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *queryConvertREST) GetSingularName() string {
|
||||
return "queryconvert"
|
||||
}
|
||||
|
||||
func (r *queryConvertREST) ConnectMethods() []string {
|
||||
return []string{"POST"}
|
||||
}
|
||||
|
||||
func (r *queryConvertREST) NewConnectOptions() (runtime.Object, bool, string) {
|
||||
return nil, false, "" // true means you can use the trailing path as a variable
|
||||
}
|
||||
|
||||
func (r *queryConvertREST) convertQueryDataRequest(ctx context.Context, req *http.Request) (*query.QueryDataRequest, error) {
|
||||
dqr := data.QueryDataRequest{}
|
||||
err := web.Bind(req, &dqr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ds := dqr.Queries[0].Datasource
|
||||
pluginCtx, err := r.contextProvider.PluginContextForDataSource(ctx, &backend.DataSourceInstanceSettings{
|
||||
Type: ds.Type,
|
||||
UID: ds.UID,
|
||||
APIVersion: ds.APIVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx = backend.WithGrafanaConfig(ctx, pluginCtx.GrafanaConfig)
|
||||
raw, err := json.Marshal(dqr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal: %w", err)
|
||||
}
|
||||
convertRequest := &backend.ConversionRequest{
|
||||
PluginContext: pluginCtx,
|
||||
Objects: []backend.RawObject{
|
||||
{
|
||||
Raw: raw,
|
||||
ContentType: "application/json",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
convertResponse, err := r.client.ConvertObjects(ctx, convertRequest)
|
||||
if err != nil {
|
||||
if convertResponse != nil && convertResponse.Result != nil {
|
||||
return nil, fmt.Errorf("conversion failed. Err: %w. Result: %s", err, convertResponse.Result.Message)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
qr := &query.QueryDataRequest{}
|
||||
for _, obj := range convertResponse.Objects {
|
||||
if obj.ContentType != "application/json" {
|
||||
return nil, fmt.Errorf("unexpected content type: %s", obj.ContentType)
|
||||
}
|
||||
q := &data.DataQuery{}
|
||||
err = json.Unmarshal(obj.Raw, q)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unmarshal: %w", err)
|
||||
}
|
||||
qr.Queries = append(qr.Queries, *q)
|
||||
}
|
||||
|
||||
return qr, nil
|
||||
}
|
||||
|
||||
func (r *queryConvertREST) Connect(ctx context.Context, name string, _ runtime.Object, responder rest.Responder) (http.Handler, error) {
|
||||
// See: /pkg/services/apiserver/builder/helper.go#L34
|
||||
// The name is set with a rewriter hack
|
||||
if name != "name" {
|
||||
return nil, errors.NewNotFound(schema.GroupResource{}, name)
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
r, err := r.convertQueryDataRequest(ctx, req)
|
||||
if err != nil {
|
||||
responder.Error(err)
|
||||
return
|
||||
}
|
||||
responder.Object(http.StatusOK, r)
|
||||
}), nil
|
||||
}
|
90
pkg/registry/apis/datasource/queryconvert_test.go
Normal file
90
pkg/registry/apis/datasource/queryconvert_test.go
Normal file
@ -0,0 +1,90 @@
|
||||
package datasource
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
func TestSubQueryConvertConnect(t *testing.T) {
|
||||
originReq := `{"from":"","to":"","queries":[{"refId":"A","datasource":{"type":"","uid":"dsuid"},"rawSql":"SELECT * FROM table"}]}`
|
||||
converted := `{"refId":"A","datasource":{"type":"","uid":"dsuid"},"SQL":"SELECT * FROM table"}`
|
||||
convertedReq := `{"from":"","to":"","queries":[` + converted + `]}`
|
||||
|
||||
sqr := queryConvertREST{
|
||||
client: mockConvertClient{
|
||||
t: t,
|
||||
expectedInput: backend.RawObject{Raw: []byte(originReq), ContentType: "application/json"},
|
||||
convertObject: backend.RawObject{Raw: []byte(converted), ContentType: "application/json"},
|
||||
},
|
||||
contextProvider: mockContextProvider{},
|
||||
}
|
||||
rr := httptest.NewRecorder()
|
||||
mr := &mockResponderConvert{
|
||||
writer: rr,
|
||||
}
|
||||
handler, err := sqr.Connect(context.Background(), "name", nil, mr)
|
||||
require.NoError(t, err)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", bytes.NewReader([]byte(originReq)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
handler.ServeHTTP(rr, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, rr.Code)
|
||||
require.Contains(t, rr.Body.String(), convertedReq)
|
||||
}
|
||||
|
||||
type mockConvertClient struct {
|
||||
t *testing.T
|
||||
expectedInput backend.RawObject
|
||||
convertObject backend.RawObject
|
||||
}
|
||||
|
||||
func (m mockConvertClient) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m mockConvertClient) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m mockConvertClient) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m mockConvertClient) ConvertObjects(ctx context.Context, req *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
require.Equal(m.t, string(m.expectedInput.Raw), string(req.Objects[0].Raw))
|
||||
return &backend.ConversionResponse{
|
||||
Objects: []backend.RawObject{m.convertObject},
|
||||
}, nil
|
||||
}
|
||||
|
||||
type mockResponderConvert struct {
|
||||
writer http.ResponseWriter
|
||||
}
|
||||
|
||||
// Object writes the provided object to the response. Invoking this method multiple times is undefined.
|
||||
func (m mockResponderConvert) Object(statusCode int, obj runtime.Object) {
|
||||
m.writer.WriteHeader(statusCode)
|
||||
err := json.NewEncoder(m.writer).Encode(obj)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Error writes the provided error to the response. This method may only be invoked once.
|
||||
func (m mockResponderConvert) Error(err error) {
|
||||
m.writer.WriteHeader(http.StatusInternalServerError)
|
||||
errStr := err.Error()
|
||||
_, err = m.writer.Write([]byte(errStr))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
@ -104,6 +104,7 @@ type PluginClient interface {
|
||||
backend.QueryDataHandler
|
||||
backend.CheckHealthHandler
|
||||
backend.CallResourceHandler
|
||||
backend.ConversionHandler
|
||||
}
|
||||
|
||||
func NewDataSourceAPIBuilder(
|
||||
@ -225,6 +226,11 @@ func (b *DataSourceAPIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver
|
||||
|
||||
// Register hardcoded query schemas
|
||||
err := queryschema.RegisterQueryTypes(b.queryTypes, storage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
registerQueryConvert(b.client, b.contextProvider, storage)
|
||||
|
||||
apiGroupInfo.VersionedResourcesStorageMap[conn.GroupVersion().Version] = storage
|
||||
return err
|
||||
|
@ -71,6 +71,10 @@ func (m mockClient) CheckHealth(ctx context.Context, req *backend.CheckHealthReq
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m mockClient) ConvertObjects(ctx context.Context, req *backend.ConversionRequest) (*backend.ConversionResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type mockResponder struct {
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,12 @@ var PathRewriters = []filters.PathRewriter{
|
||||
return matches[1] + "/name" // connector requires a name
|
||||
},
|
||||
},
|
||||
{
|
||||
Pattern: regexp.MustCompile(`(/apis/.*/v0alpha1/namespaces/.*/queryconvert$)`),
|
||||
ReplaceFunc: func(matches []string) string {
|
||||
return matches[1] + "/name" // connector requires a name
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func getDefaultBuildHandlerChainFunc(builders []APIGroupBuilder) BuildHandlerChainFunc {
|
||||
|
@ -2,6 +2,7 @@ package dashboards
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@ -42,7 +43,7 @@ func TestIntegrationTestDatasource(t *testing.T) {
|
||||
|
||||
t.Run("Check discovery client", func(t *testing.T) {
|
||||
disco := helper.GetGroupVersionInfoJSON("testdata.datasource.grafana.app")
|
||||
// fmt.Printf("%s", disco)
|
||||
fmt.Printf("%s", disco)
|
||||
|
||||
require.JSONEq(t, `[
|
||||
{
|
||||
@ -103,7 +104,20 @@ func TestIntegrationTestDatasource(t *testing.T) {
|
||||
"get",
|
||||
"list"
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"resource": "queryconvert",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "QueryDataRequest",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"singularResource": "queryconvert",
|
||||
"verbs": [
|
||||
"create"
|
||||
]
|
||||
}
|
||||
],
|
||||
"version": "v0alpha1"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user