Populate missing UID of provisioned data source only for new records (#42999)

* defer updating empty UID to insert command
* change logging to use the command
This commit is contained in:
Yuriy Tseretyan
2021-12-13 14:14:39 -05:00
committed by GitHub
parent b63595b47f
commit 35b0067650
5 changed files with 70 additions and 38 deletions
@@ -2,17 +2,17 @@ package datasources
import (
"context"
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/provisioning/utils"
"gopkg.in/yaml.v2"
)
type configReader struct {
@@ -36,12 +36,6 @@ 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)
}
}
@@ -145,10 +139,3 @@ 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]))
}