mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
provisioning: simplify db query
the GetProvisionedDashboardQuery wasent used for anything else than check if a dashboard is provisioned or not. So we simplified this query to make it more maintainable.
This commit is contained in:
@@ -102,13 +102,13 @@ func GetDashboard(c *m.ReqContext) Response {
|
|||||||
meta.FolderUrl = query.Result.GetUrl()
|
meta.FolderUrl = query.Result.GetUrl()
|
||||||
}
|
}
|
||||||
|
|
||||||
dpQuery := &m.GetProvisionedDashboardByDashboardId{DashboardId: dash.Id}
|
isDashboardProvisioned := &m.IsDashboardProvisionedQuery{DashboardId: dash.Id}
|
||||||
err = bus.Dispatch(dpQuery)
|
err = bus.Dispatch(isDashboardProvisioned)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Error(500, "Error while checking if dashboard is provisioned", err)
|
return Error(500, "Error while checking if dashboard is provisioned", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dpQuery.Result != nil {
|
if isDashboardProvisioned.Result {
|
||||||
meta.CanEdit = true
|
meta.CanEdit = true
|
||||||
meta.Provisioned = true
|
meta.Provisioned = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
|
||||||
query.Result = nil
|
query.Result = false
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -197,8 +197,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
fakeDash.HasAcl = true
|
fakeDash.HasAcl = true
|
||||||
setting.ViewersCanEdit = false
|
setting.ViewersCanEdit = false
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
|
||||||
query.Result = nil
|
query.Result = false
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -635,8 +635,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
dashTwo.FolderId = 3
|
dashTwo.FolderId = 3
|
||||||
dashTwo.HasAcl = false
|
dashTwo.HasAcl = false
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
|
||||||
query.Result = nil
|
query.Result = false
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -766,8 +766,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
|
||||||
query.Result = nil
|
query.Result = false
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ var (
|
|||||||
ErrDashboardInvalidUid = errors.New("uid contains illegal characters")
|
ErrDashboardInvalidUid = errors.New("uid contains illegal characters")
|
||||||
ErrDashboardUidToLong = errors.New("uid to long. max 40 characters")
|
ErrDashboardUidToLong = errors.New("uid to long. max 40 characters")
|
||||||
ErrDashboardCannotSaveProvisionedDashboard = errors.New("Cannot save provisioned dashboard")
|
ErrDashboardCannotSaveProvisionedDashboard = errors.New("Cannot save provisioned dashboard")
|
||||||
ErrDashboardProvisioningDoesNotExist = errors.New("Dashboard provisioning does not exist")
|
//ErrDashboardProvisioningDoesNotExist = errors.New("Dashboard provisioning does not exist")
|
||||||
RootFolderName = "General"
|
RootFolderName = "General"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -319,10 +319,10 @@ type GetDashboardSlugByIdQuery struct {
|
|||||||
Result string
|
Result string
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetProvisionedDashboardByDashboardId struct {
|
type IsDashboardProvisionedQuery struct {
|
||||||
DashboardId int64
|
DashboardId int64
|
||||||
|
|
||||||
Result *DashboardProvisioning
|
Result bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetProvisionedDashboardDataQuery struct {
|
type GetProvisionedDashboardDataQuery struct {
|
||||||
|
|||||||
@@ -93,11 +93,17 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := dr.validateDashboardIsNotProvisioned(dash.Id)
|
isDashboardProvisioned := &models.IsDashboardProvisionedQuery{DashboardId: dash.Id}
|
||||||
|
err := bus.Dispatch(isDashboardProvisioned)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isDashboardProvisioned.Result {
|
||||||
|
return nil, models.ErrDashboardCannotSaveProvisionedDashboard
|
||||||
|
}
|
||||||
|
|
||||||
validateBeforeSaveCmd := models.ValidateDashboardBeforeSaveCommand{
|
validateBeforeSaveCmd := models.ValidateDashboardBeforeSaveCommand{
|
||||||
OrgId: dto.OrgId,
|
OrgId: dto.OrgId,
|
||||||
Dashboard: dash,
|
Dashboard: dash,
|
||||||
@@ -134,23 +140,6 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
|
|||||||
return cmd, nil
|
return cmd, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dr *dashboardServiceImpl) validateDashboardIsNotProvisioned(dashboardId int64) error {
|
|
||||||
dpQuery := &models.GetProvisionedDashboardByDashboardId{DashboardId: dashboardId}
|
|
||||||
err := bus.Dispatch(dpQuery)
|
|
||||||
|
|
||||||
// provisioned dashboards cannot be saved. So we can only save
|
|
||||||
// this dashboard if ErrDashboardProvisioningDoesNotExist is returned
|
|
||||||
if err != nil && err != models.ErrDashboardProvisioningDoesNotExist {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
return models.ErrDashboardCannotSaveProvisionedDashboard
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dr *dashboardServiceImpl) updateAlerting(cmd *models.SaveDashboardCommand, dto *SaveDashboardDTO) error {
|
func (dr *dashboardServiceImpl) updateAlerting(cmd *models.SaveDashboardCommand, dto *SaveDashboardDTO) error {
|
||||||
alertCmd := models.UpdateDashboardAlertsCommand{
|
alertCmd := models.UpdateDashboardAlertsCommand{
|
||||||
OrgId: dto.OrgId,
|
OrgId: dto.OrgId,
|
||||||
|
|||||||
@@ -56,8 +56,9 @@ func TestDashboardService(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
|
||||||
return models.ErrDashboardProvisioningDoesNotExist
|
cmd.Result = false
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
@@ -84,8 +85,8 @@ func TestDashboardService(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("Should return validation error if dashboard is provisioned", func() {
|
Convey("Should return validation error if dashboard is provisioned", func() {
|
||||||
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
|
||||||
cmd.Result = &models.DashboardProvisioning{}
|
cmd.Result = true
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -105,8 +106,9 @@ func TestDashboardService(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("Should return validation error if alert data is invalid", func() {
|
Convey("Should return validation error if alert data is invalid", func() {
|
||||||
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
|
||||||
return models.ErrDashboardProvisioningDoesNotExist
|
cmd.Result = false
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(cmd *models.ValidateDashboardAlertsCommand) error {
|
bus.AddHandler("test", func(cmd *models.ValidateDashboardAlertsCommand) error {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type DashboardExtras struct {
|
|||||||
Value string
|
Value string
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetProvisionedDataByDashboardId(cmd *models.GetProvisionedDashboardByDashboardId) error {
|
func GetProvisionedDataByDashboardId(cmd *models.IsDashboardProvisionedQuery) error {
|
||||||
result := &models.DashboardProvisioning{}
|
result := &models.DashboardProvisioning{}
|
||||||
|
|
||||||
exist, err := x.Where("dashboard_id = ?", cmd.DashboardId).Get(result)
|
exist, err := x.Where("dashboard_id = ?", cmd.DashboardId).Get(result)
|
||||||
@@ -26,11 +26,7 @@ func GetProvisionedDataByDashboardId(cmd *models.GetProvisionedDashboardByDashbo
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exist {
|
cmd.Result = exist
|
||||||
return models.ErrDashboardProvisioningDoesNotExist
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.Result = result
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,20 +52,20 @@ func TestDashboardProvisioningTest(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Convey("Can query for one provisioned dashboard", func() {
|
Convey("Can query for one provisioned dashboard", func() {
|
||||||
query := &models.GetProvisionedDashboardByDashboardId{DashboardId: cmd.Result.Id}
|
query := &models.IsDashboardProvisionedQuery{DashboardId: cmd.Result.Id}
|
||||||
|
|
||||||
err := GetProvisionedDataByDashboardId(query)
|
err := GetProvisionedDataByDashboardId(query)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
So(query.Result.DashboardId, ShouldEqual, cmd.Result.Id)
|
So(query.Result, ShouldBeTrue)
|
||||||
So(query.Result.Updated, ShouldEqual, now.Unix())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Can query for one provisioned dashboard2", func() {
|
Convey("Can query for none provisioned dashboard", func() {
|
||||||
query := &models.GetProvisionedDashboardByDashboardId{DashboardId: 3000}
|
query := &models.IsDashboardProvisionedQuery{DashboardId: 3000}
|
||||||
|
|
||||||
err := GetProvisionedDataByDashboardId(query)
|
err := GetProvisionedDataByDashboardId(query)
|
||||||
So(err, ShouldEqual, models.ErrDashboardProvisioningDoesNotExist)
|
So(err, ShouldBeNil)
|
||||||
|
So(query.Result, ShouldBeFalse)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -27,8 +27,9 @@ func TestIntegratedDashboardService(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
|
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
|
||||||
return models.ErrDashboardProvisioningDoesNotExist
|
cmd.Result = false
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
savedFolder := saveTestFolder("Saved folder", testOrgId)
|
savedFolder := saveTestFolder("Saved folder", testOrgId)
|
||||||
|
|||||||
Reference in New Issue
Block a user