mirror of
https://github.com/grafana/grafana.git
synced 2026-08-14 07:04:57 -05:00
Provisioning: Allows specifying uid for datasource and use that in derived fields (#23585)
* Add uid to datasource * Fix uid passing when provisioning * Better error handling and Uid column type change * Fix test and strict null error counts * Add backend tests * Add tests * Fix strict null checks * Update test * Improve tests * Update pkg/services/sqlstore/datasource.go Co-Authored-By: Arve Knudsen <arve.knudsen@gmail.com> * Variable rename Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
co-authored by
Arve Knudsen
parent
d5f8d976f0
commit
e5dd7efdee
@@ -161,7 +161,7 @@ func TestDatasourceAsConfig(t *testing.T) {
|
||||
|
||||
So(dsCfg.APIVersion, ShouldEqual, 1)
|
||||
|
||||
validateDatasource(dsCfg)
|
||||
validateDatasourceV1(dsCfg)
|
||||
validateDeleteDatasources(dsCfg)
|
||||
|
||||
dsCount := 0
|
||||
@@ -231,6 +231,12 @@ func validateDatasource(dsCfg *configs) {
|
||||
So(ds.SecureJSONData["tlsClientKey"], ShouldEqual, "ZkN4aG1aNkja/gKAB1wlnKFIsy2SRDq4slrM0A==")
|
||||
}
|
||||
|
||||
func validateDatasourceV1(dsCfg *configs) {
|
||||
validateDatasource(dsCfg)
|
||||
ds := dsCfg.Datasources[0]
|
||||
So(ds.UID, ShouldEqual, "test_uid")
|
||||
}
|
||||
|
||||
type fakeRepository struct {
|
||||
inserted []*models.AddDataSourceCommand
|
||||
deleted []*models.DeleteDataSourceByNameCommand
|
||||
|
||||
@@ -50,13 +50,13 @@ func (dc *DatasourceProvisioner) apply(cfg *configs) error {
|
||||
}
|
||||
|
||||
if err == models.ErrDataSourceNotFound {
|
||||
dc.log.Info("inserting datasource from configuration ", "name", ds.Name)
|
||||
dc.log.Info("inserting datasource from configuration ", "name", ds.Name, "uid", ds.UID)
|
||||
insertCmd := createInsertCommand(ds)
|
||||
if err := bus.Dispatch(insertCmd); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
dc.log.Debug("updating datasource from configuration", "name", ds.Name)
|
||||
dc.log.Debug("updating datasource from configuration", "name", ds.Name, "uid", ds.UID)
|
||||
updateCmd := createUpdateCommand(ds, cmd.Result.Id)
|
||||
if err := bus.Dispatch(updateCmd); err != nil {
|
||||
return err
|
||||
|
||||
+1
@@ -24,6 +24,7 @@ datasources:
|
||||
tlsClientKey: "ZkN4aG1aNkja/gKAB1wlnKFIsy2SRDq4slrM0A=="
|
||||
editable: true
|
||||
version: 10
|
||||
uid: "test_uid"
|
||||
|
||||
deleteDatasources:
|
||||
- name: old-graphite3
|
||||
|
||||
@@ -43,6 +43,7 @@ type upsertDataSourceFromConfig struct {
|
||||
JSONData map[string]interface{}
|
||||
SecureJSONData map[string]string
|
||||
Editable bool
|
||||
UID string
|
||||
}
|
||||
|
||||
type configsV0 struct {
|
||||
@@ -108,6 +109,7 @@ type upsertDataSourceFromConfigV1 struct {
|
||||
JSONData values.JSONValue `json:"jsonData" yaml:"jsonData"`
|
||||
SecureJSONData values.StringMapValue `json:"secureJsonData" yaml:"secureJsonData"`
|
||||
Editable values.BoolValue `json:"editable" yaml:"editable"`
|
||||
UID values.StringValue `json:"uid" yaml:"uid"`
|
||||
}
|
||||
|
||||
func (cfg *configsV1) mapToDatasourceFromConfig(apiVersion int64) *configs {
|
||||
@@ -138,6 +140,7 @@ func (cfg *configsV1) mapToDatasourceFromConfig(apiVersion int64) *configs {
|
||||
SecureJSONData: ds.SecureJSONData.Value(),
|
||||
Editable: ds.Editable.Value(),
|
||||
Version: ds.Version.Value(),
|
||||
UID: ds.UID.Value(),
|
||||
})
|
||||
|
||||
// Using Raw value for the warnings here so that even if it uses env interpolation and the env var is empty
|
||||
@@ -234,6 +237,7 @@ func createInsertCommand(ds *upsertDataSourceFromConfig) *models.AddDataSourceCo
|
||||
JsonData: jsonData,
|
||||
SecureJsonData: ds.SecureJSONData,
|
||||
ReadOnly: !ds.Editable,
|
||||
Uid: ds.UID,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,6 +251,7 @@ func createUpdateCommand(ds *upsertDataSourceFromConfig, id int64) *models.Updat
|
||||
|
||||
return &models.UpdateDataSourceCommand{
|
||||
Id: id,
|
||||
Uid: ds.UID,
|
||||
OrgId: ds.OrgID,
|
||||
Name: ds.Name,
|
||||
Type: ds.Type,
|
||||
|
||||
Reference in New Issue
Block a user