Provisioning: set stable datasource UID with provisioning (#41769)

This commit is contained in:
Ryan McKinley
2021-11-18 01:18:08 +01:00
committed by GitHub
parent bf85ae44a2
commit 7ef05588ea
3 changed files with 25 additions and 1 deletions
@@ -2,6 +2,7 @@ package datasources
import (
"context"
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
@@ -35,6 +36,12 @@ func (cr *configReader) readConfig(ctx context.Context, path string) ([]*configs
}
if datasource != nil {
for _, ds := range datasource.Datasources {
if ds.UID == "" && ds.Name != "" {
ds.UID = safeUIDFromName(ds.Name)
}
}
datasources = append(datasources, datasource)
}
}
@@ -138,3 +145,10 @@ func (cr *configReader) validateAccessAndOrgID(ctx context.Context, ds *upsertDa
}
return nil
}
func safeUIDFromName(name string) string {
h := sha256.New()
_, _ = h.Write([]byte(name))
bs := h.Sum(nil)
return strings.ToUpper(fmt.Sprintf("P%x", bs[:8]))
}