mirror of
https://github.com/grafana/grafana.git
synced 2026-08-11 05:34:53 -05:00
K8s: Add basic query service (#80325)
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Get the default API group name for from a plugin ID
|
||||
// NOTE: this is a work in progress, and may change without notice
|
||||
func GetDatasourceGroupNameFromPluginID(pluginId string) (string, error) {
|
||||
if pluginId == "" {
|
||||
return "", fmt.Errorf("bad pluginID (empty)")
|
||||
}
|
||||
parts := strings.Split(pluginId, "-")
|
||||
if len(parts) == 1 {
|
||||
return fmt.Sprintf("%s.datasource.grafana.app", parts[0]), nil
|
||||
}
|
||||
|
||||
last := parts[len(parts)-1]
|
||||
if last != "datasource" {
|
||||
return "", fmt.Errorf("bad pluginID (%s)", pluginId)
|
||||
}
|
||||
if parts[0] == "grafana" {
|
||||
parts = parts[1:] // strip the first value
|
||||
}
|
||||
return fmt.Sprintf("%s.datasource.grafana.app", strings.Join(parts[:len(parts)-1], "-")), nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUtils(t *testing.T) {
|
||||
// multiple flavors of the same idea
|
||||
require.Equal(t, "tempo.datasource.grafana.app", getIDIgnoreError("tempo"))
|
||||
require.Equal(t, "tempo.datasource.grafana.app", getIDIgnoreError("grafana-tempo-datasource"))
|
||||
require.Equal(t, "tempo.datasource.grafana.app", getIDIgnoreError("tempo-datasource"))
|
||||
|
||||
// Multiple dashes in the name
|
||||
require.Equal(t, "org-name.datasource.grafana.app", getIDIgnoreError("org-name-datasource"))
|
||||
require.Equal(t, "org-name-more.datasource.grafana.app", getIDIgnoreError("org-name-more-datasource"))
|
||||
require.Equal(t, "org-name-more-more.datasource.grafana.app", getIDIgnoreError("org-name-more-more-datasource"))
|
||||
|
||||
require.Error(t, getErrorIgnoreValue("graph-panel"))
|
||||
require.Error(t, getErrorIgnoreValue("anything-notdatasource"))
|
||||
}
|
||||
|
||||
func getIDIgnoreError(id string) string {
|
||||
v, _ := GetDatasourceGroupNameFromPluginID(id)
|
||||
return v
|
||||
}
|
||||
|
||||
func getErrorIgnoreValue(id string) error {
|
||||
_, err := GetDatasourceGroupNameFromPluginID(id)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user