datasources: querier: add user to query (#109917)
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
dataapi "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
|
dataapi "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
|
||||||
|
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||||
queryapi "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
queryapi "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
||||||
"github.com/grafana/grafana/pkg/expr"
|
"github.com/grafana/grafana/pkg/expr"
|
||||||
"github.com/grafana/grafana/pkg/infra/log"
|
"github.com/grafana/grafana/pkg/infra/log"
|
||||||
@@ -45,6 +46,14 @@ func loadTestdataFrames(t *testing.T, filename string) *backend.QueryDataRespons
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mockUser struct {
|
||||||
|
identity.Requester
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mu mockUser) GetOrgID() int64 {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
func TestQueryAPI(t *testing.T) {
|
func TestQueryAPI(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -167,7 +176,9 @@ func TestQueryAPI(t *testing.T) {
|
|||||||
legacyDatasourceLookup: &mockLegacyDataSourceLookup{},
|
legacyDatasourceLookup: &mockLegacyDataSourceLookup{},
|
||||||
}
|
}
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "/some-path", bytes.NewReader([]byte(tc.queryJSON)))
|
reqCtx := identity.WithRequester(context.Background(), mockUser{})
|
||||||
|
|
||||||
|
req := httptest.NewRequestWithContext(reqCtx, http.MethodPost, "/some-path", bytes.NewReader([]byte(tc.queryJSON)))
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// Set optional headers
|
// Set optional headers
|
||||||
|
|||||||
@@ -234,7 +234,13 @@ func QueryData(ctx context.Context, log log.Logger, dscache datasources.CacheSer
|
|||||||
headers: headers,
|
headers: headers,
|
||||||
concurrentQueryLimit: 16, // TODO: make it configurable
|
concurrentQueryLimit: 16, // TODO: make it configurable
|
||||||
}
|
}
|
||||||
return s.QueryDataNew(ctx, nil, false, reqDTO)
|
|
||||||
|
user, err := identity.GetRequester(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.QueryDataNew(ctx, user, false, reqDTO)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleExpressions handles queries when there is an expression.
|
// handleExpressions handles queries when there is an expression.
|
||||||
|
|||||||
Reference in New Issue
Block a user