Commit 949484b9 by bergquist

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.
parent 0f9b5924
......@@ -102,13 +102,13 @@ func GetDashboard(c *m.ReqContext) Response {
meta.FolderUrl = query.Result.GetUrl()
}
dpQuery := &m.GetProvisionedDashboardByDashboardId{DashboardId: dash.Id}
err = bus.Dispatch(dpQuery)
isDashboardProvisioned := &m.IsDashboardProvisionedQuery{DashboardId: dash.Id}
err = bus.Dispatch(isDashboardProvisioned)
if err != nil {
return Error(500, "Error while checking if dashboard is provisioned", err)
}
if dpQuery.Result != nil {
if isDashboardProvisioned.Result {
meta.CanEdit = true
meta.Provisioned = true
}
......
......@@ -42,8 +42,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
return nil
})
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
query.Result = nil
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
query.Result = false
return nil
})
......@@ -197,8 +197,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
fakeDash.HasAcl = true
setting.ViewersCanEdit = false
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
query.Result = nil
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
query.Result = false
return nil
})
......@@ -635,8 +635,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
dashTwo.FolderId = 3
dashTwo.HasAcl = false
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
query.Result = nil
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
query.Result = false
return nil
})
......@@ -766,8 +766,8 @@ func TestDashboardApiEndpoint(t *testing.T) {
return nil
})
bus.AddHandler("test", func(query *m.GetProvisionedDashboardByDashboardId) error {
query.Result = nil
bus.AddHandler("test", func(query *m.IsDashboardProvisionedQuery) error {
query.Result = false
return nil
})
......
......@@ -33,8 +33,8 @@ var (
ErrDashboardInvalidUid = errors.New("uid contains illegal characters")
ErrDashboardUidToLong = errors.New("uid to long. max 40 characters")
ErrDashboardCannotSaveProvisionedDashboard = errors.New("Cannot save provisioned dashboard")
ErrDashboardProvisioningDoesNotExist = errors.New("Dashboard provisioning does not exist")
RootFolderName = "General"
//ErrDashboardProvisioningDoesNotExist = errors.New("Dashboard provisioning does not exist")
RootFolderName = "General"
)
type UpdatePluginDashboardError struct {
......@@ -319,10 +319,10 @@ type GetDashboardSlugByIdQuery struct {
Result string
}
type GetProvisionedDashboardByDashboardId struct {
type IsDashboardProvisionedQuery struct {
DashboardId int64
Result *DashboardProvisioning
Result bool
}
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 {
return nil, err
}
if isDashboardProvisioned.Result {
return nil, models.ErrDashboardCannotSaveProvisionedDashboard
}
validateBeforeSaveCmd := models.ValidateDashboardBeforeSaveCommand{
OrgId: dto.OrgId,
Dashboard: dash,
......@@ -134,23 +140,6 @@ func (dr *dashboardServiceImpl) buildSaveDashboardCommand(dto *SaveDashboardDTO,
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 {
alertCmd := models.UpdateDashboardAlertsCommand{
OrgId: dto.OrgId,
......
......@@ -56,8 +56,9 @@ func TestDashboardService(t *testing.T) {
return nil
})
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
return models.ErrDashboardProvisioningDoesNotExist
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
cmd.Result = false
return nil
})
testCases := []struct {
......@@ -84,8 +85,8 @@ func TestDashboardService(t *testing.T) {
})
Convey("Should return validation error if dashboard is provisioned", func() {
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
cmd.Result = &models.DashboardProvisioning{}
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
cmd.Result = true
return nil
})
......@@ -105,8 +106,9 @@ func TestDashboardService(t *testing.T) {
})
Convey("Should return validation error if alert data is invalid", func() {
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
return models.ErrDashboardProvisioningDoesNotExist
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
cmd.Result = false
return nil
})
bus.AddHandler("test", func(cmd *models.ValidateDashboardAlertsCommand) error {
......
......@@ -18,7 +18,7 @@ type DashboardExtras struct {
Value string
}
func GetProvisionedDataByDashboardId(cmd *models.GetProvisionedDashboardByDashboardId) error {
func GetProvisionedDataByDashboardId(cmd *models.IsDashboardProvisionedQuery) error {
result := &models.DashboardProvisioning{}
exist, err := x.Where("dashboard_id = ?", cmd.DashboardId).Get(result)
......@@ -26,11 +26,7 @@ func GetProvisionedDataByDashboardId(cmd *models.GetProvisionedDashboardByDashbo
return err
}
if !exist {
return models.ErrDashboardProvisioningDoesNotExist
}
cmd.Result = result
cmd.Result = exist
return nil
}
......
......@@ -52,20 +52,20 @@ func TestDashboardProvisioningTest(t *testing.T) {
})
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)
So(err, ShouldBeNil)
So(query.Result.DashboardId, ShouldEqual, cmd.Result.Id)
So(query.Result.Updated, ShouldEqual, now.Unix())
So(query.Result, ShouldBeTrue)
})
Convey("Can query for one provisioned dashboard2", func() {
query := &models.GetProvisionedDashboardByDashboardId{DashboardId: 3000}
Convey("Can query for none provisioned dashboard", func() {
query := &models.IsDashboardProvisionedQuery{DashboardId: 3000}
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
})
bus.AddHandler("test", func(cmd *models.GetProvisionedDashboardByDashboardId) error {
return models.ErrDashboardProvisioningDoesNotExist
bus.AddHandler("test", func(cmd *models.IsDashboardProvisionedQuery) error {
cmd.Result = false
return nil
})
savedFolder := saveTestFolder("Saved folder", testOrgId)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment