-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0008ea0
commit 97a7441
Showing
7 changed files
with
161 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package backend | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConvertObjects(t *testing.T) { | ||
t.Run("converts a raw object", func(t *testing.T) { | ||
input := []byte(`{"foo":"bar"}`) | ||
expected := []byte(`{"baz":"qux"}`) | ||
|
||
a := newConversionSDKAdapter(ConvertObjectsFunc(func(_ context.Context, r *ConversionRequest) (*ConversionResponse, error) { | ||
require.Equal(t, input, r.Objects[0].Raw) | ||
return &ConversionResponse{ | ||
Objects: []RawObject{{Raw: expected}}, | ||
}, nil | ||
}), nil) | ||
res, err := a.ConvertObjects(context.Background(), &pluginv2.ConversionRequest{ | ||
PluginContext: &pluginv2.PluginContext{}, | ||
TargetVersion: &pluginv2.GroupVersion{}, | ||
Objects: []*pluginv2.RawObject{{Raw: input}}, | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, []*pluginv2.RawObject{{Raw: expected}}, res.Objects) | ||
}) | ||
|
||
t.Run("converts a query data request", func(t *testing.T) { | ||
input := []byte(`{"queries":[{"JSON":"foo"}]}`) | ||
|
||
a := newConversionSDKAdapter(nil, ConvertQueryFunc(func(_ context.Context, r *QueryDataRequest) (*QueryConversionResponse, error) { | ||
require.Equal(t, `"foo"`, string(r.Queries[0].JSON)) | ||
return &QueryConversionResponse{ | ||
Queries: []any{DataQuery{JSON: []byte(`"bar"`)}}, | ||
}, nil | ||
})) | ||
res, err := a.ConvertObjects(context.Background(), &pluginv2.ConversionRequest{ | ||
PluginContext: &pluginv2.PluginContext{}, | ||
TargetVersion: &pluginv2.GroupVersion{}, | ||
Objects: []*pluginv2.RawObject{{Raw: input, ContentType: "application/json"}}, | ||
}) | ||
require.NoError(t, err) | ||
require.Contains(t, string(res.Objects[0].Raw), `"JSON":"bar"`) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package backend | ||
|
||
import "context" | ||
|
||
// QueryConversionHandler is an EXPERIMENTAL service that allows converting queries between versions | ||
|
||
type QueryConversionHandler interface { | ||
// ConvertQuery is called to covert queries between different versions | ||
ConvertQueryDataRequest(ctx context.Context, req *QueryDataRequest) (*QueryConversionResponse, error) | ||
} | ||
|
||
type ConvertQueryFunc func(ctx context.Context, req *QueryDataRequest) (*QueryConversionResponse, error) | ||
|
||
func (fn ConvertQueryFunc) ConvertQueryDataRequest(ctx context.Context, req *QueryDataRequest) (*QueryConversionResponse, error) { | ||
return fn(ctx, req) | ||
} | ||
|
||
type QueryConversionResponse struct { | ||
// Converted queries. It should extend v0alpha1.Query | ||
Queries []any | ||
// Result contains extra details into why an conversion request was denied. | ||
// +optional | ||
Result *StatusResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters