mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
API: Fix storing dashboard with static UID (#43861)
* API: Fix storing dashboard with static UID
This commit is contained in:
committed by
GitHub
parent
ed5c664e4a
commit
963ae4ef87
@@ -323,13 +323,13 @@ func (hs *HTTPServer) postDashboard(c *models.ReqContext, cmd models.SaveDashboa
|
|||||||
if dash.Id != 0 {
|
if dash.Id != 0 {
|
||||||
data, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id)
|
data, err := svc.GetProvisionedDashboardDataByDashboardID(dash.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return response.Error(500, "Error while checking if dashboard is provisioned", err)
|
return response.Error(500, "Error while checking if dashboard is provisioned using ID", err)
|
||||||
}
|
}
|
||||||
provisioningData = data
|
provisioningData = data
|
||||||
} else if dash.Uid != "" {
|
} else if dash.Uid != "" {
|
||||||
data, err := svc.GetProvisionedDashboardDataByDashboardUID(dash.OrgId, dash.Uid)
|
data, err := svc.GetProvisionedDashboardDataByDashboardUID(dash.OrgId, dash.Uid)
|
||||||
if err != nil && !errors.Is(err, models.ErrProvisionedDashboardNotFound) {
|
if err != nil && (!errors.Is(err, models.ErrProvisionedDashboardNotFound) && !errors.Is(err, models.ErrDashboardNotFound)) {
|
||||||
return response.Error(500, "Error while checking if dashboard is provisioned", err)
|
return response.Error(500, "Error while checking if dashboard is provisioned using UID", err)
|
||||||
}
|
}
|
||||||
provisioningData = data
|
provisioningData = data
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd models.CreateUserCom
|
|||||||
return u.Id
|
return u.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProvisionioningDashboards(t *testing.T) {
|
func TestUpdatingProvisionionedDashboards(t *testing.T) {
|
||||||
// Setup Grafana and its Database
|
// Setup Grafana and its Database
|
||||||
dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
|
dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
|
||||||
DisableAnonymous: true,
|
DisableAnonymous: true,
|
||||||
@@ -161,14 +161,31 @@ providers:
|
|||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
dashboardData string
|
dashboardData string
|
||||||
|
expStatus int
|
||||||
|
expErrReason string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "when updating provisioned dashboard using ID it should fail",
|
desc: "when updating provisioned dashboard using ID it should fail",
|
||||||
dashboardData: fmt.Sprintf(`{"title":"just testing", "id": %d, "version": 1}`, dashboardID),
|
dashboardData: fmt.Sprintf(`{"title":"just testing", "id": %d, "version": 1}`, dashboardID),
|
||||||
|
expStatus: http.StatusBadRequest,
|
||||||
|
expErrReason: models.ErrDashboardCannotSaveProvisionedDashboard.Reason,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "when updating provisioned dashboard using UID is should fail",
|
desc: "when updating provisioned dashboard using UID it should fail",
|
||||||
dashboardData: fmt.Sprintf(`{"title":"just testing", "uid": %q, "version": 1}`, dashboardUID),
|
dashboardData: fmt.Sprintf(`{"title":"just testing", "uid": %q, "version": 1}`, dashboardUID),
|
||||||
|
expStatus: http.StatusBadRequest,
|
||||||
|
expErrReason: models.ErrDashboardCannotSaveProvisionedDashboard.Reason,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "when updating dashboard using unknown ID, it should fail",
|
||||||
|
dashboardData: `{"title":"just testing", "id": 42, "version": 1}`,
|
||||||
|
expStatus: http.StatusNotFound,
|
||||||
|
expErrReason: models.ErrDashboardNotFound.Reason,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "when updating dashboard using unknown UID, it should succeed",
|
||||||
|
dashboardData: `{"title":"just testing", "uid": "unknown", "version": 1}`,
|
||||||
|
expStatus: http.StatusOK,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
@@ -186,17 +203,20 @@ providers:
|
|||||||
// nolint:gosec
|
// nolint:gosec
|
||||||
resp, err := http.Post(u, "application/json", buf)
|
resp, err := http.Post(u, "application/json", buf)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
assert.Equal(t, tc.expStatus, resp.StatusCode)
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
err := resp.Body.Close()
|
err := resp.Body.Close()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
})
|
})
|
||||||
|
if tc.expErrReason == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
dashboardErr := &errorResponseBody{}
|
dashboardErr := &errorResponseBody{}
|
||||||
err = json.Unmarshal(b, dashboardErr)
|
err = json.Unmarshal(b, dashboardErr)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, models.ErrDashboardCannotSaveProvisionedDashboard.Reason, dashboardErr.Message)
|
assert.Equal(t, tc.expErrReason, dashboardErr.Message)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user