Provisioning: Add validation for missing organisations in datasource, dashboard, and notifier configurations (#26601)

* Provisioning: Check that org. from config exists

Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>

* Apply suggestions from code review

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix some code after applying suggestions

Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>

* Add negative test case

Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Maksim Nabokikh
2020-07-30 12:59:12 +03:00
committed by GitHub
co-authored by Arve Knudsen
parent 3f21283655
commit ba2524cd88
8 changed files with 119 additions and 16 deletions
+20
View File
@@ -0,0 +1,20 @@
package utils
import (
"errors"
"fmt"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
)
func CheckOrgExists(orgID int64) error {
query := models.GetOrgByIdQuery{Id: orgID}
if err := bus.Dispatch(&query); err != nil {
if errors.Is(err, models.ErrOrgNotFound) {
return err
}
return fmt.Errorf("failed to check whether org. with the given ID exists: %w", err)
}
return nil
}
@@ -0,0 +1,29 @@
package utils
import (
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
. "github.com/smartystreets/goconvey/convey"
)
func TestCheckOrgExists(t *testing.T) {
Convey("with default org in database", t, func() {
sqlstore.InitTestDB(t)
defaultOrg := models.CreateOrgCommand{Name: "Main Org."}
err := sqlstore.CreateOrg(&defaultOrg)
So(err, ShouldBeNil)
Convey("default org exists", func() {
err := CheckOrgExists(defaultOrg.Result.Id)
So(err, ShouldBeNil)
})
Convey("other org doesn't exist", func() {
err := CheckOrgExists(defaultOrg.Result.Id + 1)
So(err, ShouldEqual, models.ErrOrgNotFound)
})
})
}