fix ds test

This commit is contained in:
Haris Rozajac
2025-08-13 19:05:36 -06:00
parent 1f4c896e82
commit 09d59e9fbc
4 changed files with 49 additions and 19 deletions

View File

@ -1,11 +1,20 @@
package conversion package conversion
import (
dashv2alpha1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2alpha1"
)
// getDefaultDatasourceType gets the default datasource type using the conversion module's datasource provider // getDefaultDatasourceType gets the default datasource type using the conversion module's datasource provider
func getDefaultDatasourceType() string { func getDefaultDatasourceRef() dashv2alpha1.DashboardDataSourceRef {
defaultGrafanaUID := "-- Grafana --"
defaultGrafanaType := "grafana"
// Get the datasource info from the conversion module's provider // Get the datasource info from the conversion module's provider
dsProvider := GetDataSourceProvider() dsProvider := GetDataSourceProvider()
if dsProvider == nil { if dsProvider == nil {
return "grafana" return dashv2alpha1.DashboardDataSourceRef{
Uid: &defaultGrafanaUID,
Type: &defaultGrafanaType,
}
} }
dsInfo := dsProvider.GetDataSourceInfo() dsInfo := dsProvider.GetDataSourceInfo()
@ -13,18 +22,24 @@ func getDefaultDatasourceType() string {
// Find the default datasource // Find the default datasource
for _, ds := range dsInfo { for _, ds := range dsInfo {
if ds.Default { if ds.Default {
return ds.Type return dashv2alpha1.DashboardDataSourceRef{
Uid: &ds.UID,
Type: &ds.Type,
}
} }
} }
// If no default datasource is found, return "grafana" as fallback // If no default datasource is found, return "grafana" as fallback
return "grafana" return dashv2alpha1.DashboardDataSourceRef{
Uid: &defaultGrafanaUID,
Type: &defaultGrafanaType,
}
} }
// getDatasourceTypeByUID gets the datasource type by UID using the conversion module's datasource provider // getDatasourceTypeByUID gets the datasource type by UID using the conversion module's datasource provider
func getDatasourceTypeByUID(uid string) string { func getDatasourceTypeByUID(uid string) string {
if uid == "" { if uid == "" {
return getDefaultDatasourceType() return *getDefaultDatasourceRef().Uid
} }
// Get the datasource info from the conversion module's provider // Get the datasource info from the conversion module's provider
@ -43,5 +58,5 @@ func getDatasourceTypeByUID(uid string) string {
} }
// If not found, return the default // If not found, return the default
return getDefaultDatasourceType() return *getDefaultDatasourceRef().Uid
} }

View File

@ -49,6 +49,10 @@
"kind": "prometheus", "kind": "prometheus",
"spec": {} "spec": {}
}, },
"datasource": {
"type": "prometheus",
"uid": "default-ds-uid"
},
"refId": "A", "refId": "A",
"hidden": false "hidden": false
} }

View File

@ -51,6 +51,9 @@
"kind": "DataQuery", "kind": "DataQuery",
"group": "prometheus", "group": "prometheus",
"version": "v0", "version": "v0",
"datasource": {
"name": "default-ds-uid"
},
"spec": {} "spec": {}
}, },
"refId": "A", "refId": "A",

View File

@ -1025,10 +1025,10 @@ func buildQueryVariable(varMap map[string]interface{}, commonProps CommonVariabl
datasourceType = getDatasourceTypeByUID(datasourceUID) datasourceType = getDatasourceTypeByUID(datasourceUID)
} else if datasourceType == "" { } else if datasourceType == "" {
// If no UID and no type, use default // If no UID and no type, use default
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
} else { } else {
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
queryVar := &dashv2alpha1.DashboardQueryVariableKind{ queryVar := &dashv2alpha1.DashboardQueryVariableKind{
@ -1073,7 +1073,7 @@ func buildQueryVariable(varMap map[string]interface{}, commonProps CommonVariabl
// Datasource Variable // Datasource Variable
func buildDatasourceVariable(varMap map[string]interface{}, commonProps CommonVariableProperties) (dashv2alpha1.DashboardVariableKind, error) { func buildDatasourceVariable(varMap map[string]interface{}, commonProps CommonVariableProperties) (dashv2alpha1.DashboardVariableKind, error) {
pluginId := getDefaultDatasourceType() pluginId := *getDefaultDatasourceRef().Uid
if query := varMap["query"]; query != nil { if query := varMap["query"]; query != nil {
if queryStr, ok := query.(string); ok { if queryStr, ok := query.(string); ok {
pluginId = queryStr pluginId = queryStr
@ -1221,10 +1221,10 @@ func buildAdhocVariable(varMap map[string]interface{}, commonProps CommonVariabl
datasourceType = getDatasourceTypeByUID(datasourceUID) datasourceType = getDatasourceTypeByUID(datasourceUID)
} else if datasourceType == "" { } else if datasourceType == "" {
// If no UID and no type, use default // If no UID and no type, use default
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
} else { } else {
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
adhocVar := &dashv2alpha1.DashboardAdhocVariableKind{ adhocVar := &dashv2alpha1.DashboardAdhocVariableKind{
@ -1383,10 +1383,10 @@ func buildGroupByVariable(varMap map[string]interface{}, commonProps CommonVaria
datasourceType = getDatasourceTypeByUID(datasourceUID) datasourceType = getDatasourceTypeByUID(datasourceUID)
} else if datasourceType == "" { } else if datasourceType == "" {
// If no UID and no type, use default // If no UID and no type, use default
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
} else { } else {
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
groupByVar := &dashv2alpha1.DashboardGroupByVariableKind{ groupByVar := &dashv2alpha1.DashboardGroupByVariableKind{
@ -1460,7 +1460,7 @@ func buildAnnotationQuery(annotationMap map[string]interface{}) (dashv2alpha1.Da
datasourceType = getDatasourceTypeByUID(datasourceUID) datasourceType = getDatasourceTypeByUID(datasourceUID)
} else if datasourceType == "" { } else if datasourceType == "" {
// If no UID and no type, use default // If no UID and no type, use default
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
if datasourceUID != "" { if datasourceUID != "" {
@ -1470,7 +1470,7 @@ func buildAnnotationQuery(annotationMap map[string]interface{}) (dashv2alpha1.Da
} }
} }
} else { } else {
datasourceType = getDefaultDatasourceType() datasourceType = *getDefaultDatasourceRef().Type
} }
// Build the query from target // Build the query from target
@ -1589,7 +1589,7 @@ func transformPanelQueries(panelMap map[string]interface{}) []dashv2alpha1.Dashb
dsType = getDatasourceTypeByUID(dsUID) dsType = getDatasourceTypeByUID(dsUID)
} else if dsType == "" { } else if dsType == "" {
// If no UID and no type, use default // If no UID and no type, use default
dsType = getDefaultDatasourceType() dsType = *getDefaultDatasourceRef().Type
} }
panelDatasource = &dashv2alpha1.DashboardDataSourceRef{ panelDatasource = &dashv2alpha1.DashboardDataSourceRef{
@ -1622,10 +1622,10 @@ func transformSingleQuery(targetMap map[string]interface{}, panelDatasource *das
// If we have a UID, use it to get the correct type from the datasource service // If we have a UID, use it to get the correct type from the datasource service
if queryDatasourceUID != "" { if queryDatasourceUID != "" {
queryDatasourceType = getDatasourceTypeByUID(queryDatasourceUID) queryDatasourceType = *getDefaultDatasourceRef().Type
} else if queryDatasourceType == "" { } else if queryDatasourceType == "" {
// If no UID and no type, use default // If no UID and no type, use default
queryDatasourceType = getDefaultDatasourceType() queryDatasourceType = *getDefaultDatasourceRef().Type
} }
} else if panelDatasource != nil { } else if panelDatasource != nil {
if panelDatasource.Type != nil { if panelDatasource.Type != nil {
@ -1635,7 +1635,8 @@ func transformSingleQuery(targetMap map[string]interface{}, panelDatasource *das
queryDatasourceUID = *panelDatasource.Uid queryDatasourceUID = *panelDatasource.Uid
} }
} else { } else {
queryDatasourceType = getDefaultDatasourceType() queryDatasourceType = *getDefaultDatasourceRef().Type
queryDatasourceUID = *getDefaultDatasourceRef().Uid
} }
// Build query spec by excluding known fields // Build query spec by excluding known fields
@ -1656,6 +1657,13 @@ func transformSingleQuery(targetMap map[string]interface{}, panelDatasource *das
Query: buildDataQueryKind(querySpec, queryDatasourceType, queryDatasourceUID), Query: buildDataQueryKind(querySpec, queryDatasourceType, queryDatasourceUID),
} }
// // if panelQuerySpec.Query.Datasource is not set, set it to default datasource
// if panelQuerySpec.Query.Spec["datasource"] == nil {
// panelQuerySpec.Query.Spec["datasource"] = map[string]interface{}{
// "name": queryDatasourceUID,
// }
// }
// Only include datasource reference if UID is provided // Only include datasource reference if UID is provided
if queryDatasourceUID != "" { if queryDatasourceUID != "" {
panelQuerySpec.Datasource = &dashv2alpha1.DashboardDataSourceRef{ panelQuerySpec.Datasource = &dashv2alpha1.DashboardDataSourceRef{