-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query conversion handler #1078
Merged
Merged
Query conversion handler #1078
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad1d39b
Add QueryConversionHandler
andresmgot 4511493
auto handling
andresmgot 047a393
update comments
andresmgot 83edae4
Draft: QueryConversionHandler
andresmgot 9145499
Merge branch 'main' into queryConversionHandler
andresmgot afac625
review
andresmgot 85d585a
review and tests
andresmgot d5913e3
update comment
andresmgot 3cdfad6
comment
andresmgot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Would be good to document/note that this is optional (just for the sake of knowing not to skip nil checks if anyone is going to change any code here)