Provisioning: set stable datasource UID with provisioning (#41769)
This commit is contained in:
@@ -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]))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user