ds-querier: parse datasourceUid correctly (#95629)
* ds-querier: parse datasourceUid correctly Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com> * make test case smaller --------- Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com>
This commit is contained in:
co-authored by
Sarah Zinger
parent
6f74c19d75
commit
3adc7c8771
@@ -80,7 +80,7 @@ func (p *queryParser) parseRequest(ctx context.Context, input *query.QueryDataRe
|
||||
return rsp, MakePublicQueryError(q.RefID, "multiple queries with same refId")
|
||||
}
|
||||
|
||||
ds, err := p.getValidDataSourceRef(ctx, q.Datasource, q.DatasourceID)
|
||||
ds, err := p.getValidDataSourceRef(ctx, q)
|
||||
if err != nil {
|
||||
return rsp, err
|
||||
}
|
||||
@@ -194,16 +194,30 @@ func getTimeRangeForQuery(parentTimerange, queryTimerange *data.TimeRange) data.
|
||||
}
|
||||
}
|
||||
|
||||
func (p *queryParser) getValidDataSourceRef(ctx context.Context, ds *data.DataSourceRef, id int64) (*data.DataSourceRef, error) {
|
||||
func (p *queryParser) getValidDataSourceRef(ctx context.Context, dataQuery data.DataQuery) (*data.DataSourceRef, error) {
|
||||
ds := dataQuery.Datasource
|
||||
id := dataQuery.DatasourceID
|
||||
|
||||
if ds == nil {
|
||||
if id == 0 {
|
||||
return nil, fmt.Errorf("missing datasource reference or id")
|
||||
return nil, NewErrorWithRefID(dataQuery.RefID, fmt.Errorf("missing datasource reference or id"))
|
||||
}
|
||||
|
||||
if p.legacy == nil {
|
||||
return nil, fmt.Errorf("legacy datasource lookup unsupported (id:%d)", id)
|
||||
}
|
||||
return p.legacy.GetDataSourceFromDeprecatedFields(ctx, "", id)
|
||||
}
|
||||
|
||||
if ds.UID[0] == '$' {
|
||||
uid, ok := dataQuery.Get("datasourceUid")
|
||||
if ok {
|
||||
return p.legacy.GetDataSourceFromDeprecatedFields(ctx, uid.(string), 0) // uid can be name in this scenario
|
||||
} else {
|
||||
return nil, NewErrorWithRefID(dataQuery.RefID, fmt.Errorf("missing datasource reference or id"))
|
||||
}
|
||||
}
|
||||
|
||||
if ds.Type == "" {
|
||||
if ds.UID == "" {
|
||||
return nil, fmt.Errorf("missing name/uid in data source reference")
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"description": "datasource uid is variable",
|
||||
"input": {
|
||||
"from": "now-6",
|
||||
"to": "now",
|
||||
"queries": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "",
|
||||
"uid": "${datasource}"
|
||||
},
|
||||
"datasourceUid": "XYX",
|
||||
"expr": "2"
|
||||
}
|
||||
]
|
||||
},
|
||||
"expect": {
|
||||
"requests": [
|
||||
{
|
||||
"pluginId": "plugin-bbb",
|
||||
"uid": "XYX",
|
||||
"request": {
|
||||
"from": "now-6",
|
||||
"to": "now",
|
||||
"queries": [
|
||||
{
|
||||
"refId": "A",
|
||||
"datasource": {
|
||||
"type": "",
|
||||
"uid": "${datasource}"
|
||||
},
|
||||
"datasourceUid": "XYX",
|
||||
"expr": "2"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user