Commit 58cfb236 by bergquist

retry uid generation

parent 9aa488c0
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/util"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
...@@ -19,6 +20,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) { ...@@ -19,6 +20,7 @@ func TestMiddlewareDashboardRedirect(t *testing.T) {
fakeDash.Id = 1 fakeDash.Id = 1
fakeDash.FolderId = 1 fakeDash.FolderId = 1
fakeDash.HasAcl = false fakeDash.HasAcl = false
fakeDash.Uid = util.GenerateShortUid()
bus.AddHandler("test", func(query *m.GetDashboardQuery) error { bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
query.Result = fakeDash query.Result = fakeDash
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"github.com/gosimple/slug" "github.com/gosimple/slug"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
) )
// Typed errors // Typed errors
...@@ -23,6 +22,7 @@ var ( ...@@ -23,6 +22,7 @@ var (
ErrDashboardFolderCannotHaveParent = errors.New("A Dashboard Folder cannot be added to another folder") ErrDashboardFolderCannotHaveParent = errors.New("A Dashboard Folder cannot be added to another folder")
ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard") ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard")
ErrDashboardFailedToUpdateAlertData = errors.New("Failed to save alert data") ErrDashboardFailedToUpdateAlertData = errors.New("Failed to save alert data")
ErrDashboardFailedGenerateUniqueUid = errors.New("Failed to generate unique dashboard uid.")
) )
type UpdatePluginDashboardError struct { type UpdatePluginDashboardError struct {
...@@ -66,7 +66,6 @@ type Dashboard struct { ...@@ -66,7 +66,6 @@ type Dashboard struct {
// NewDashboard creates a new dashboard // NewDashboard creates a new dashboard
func NewDashboard(title string) *Dashboard { func NewDashboard(title string) *Dashboard {
dash := &Dashboard{} dash := &Dashboard{}
dash.Uid = util.GenerateShortUid()
dash.Data = simplejson.New() dash.Data = simplejson.New()
dash.Data.Set("title", title) dash.Data.Set("title", title)
dash.Title = title dash.Title = title
...@@ -115,9 +114,6 @@ func NewDashboardFromJson(data *simplejson.Json) *Dashboard { ...@@ -115,9 +114,6 @@ func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
if uid, err := dash.Data.Get("uid").String(); err == nil { if uid, err := dash.Data.Get("uid").String(); err == nil {
dash.Uid = uid dash.Uid = uid
} else {
dash.Uid = util.GenerateShortUid()
dash.Data.Set("uid", dash.Uid)
} }
return dash return dash
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/metrics"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search" "github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/util"
) )
func init() { func init() {
...@@ -20,6 +21,8 @@ func init() { ...@@ -20,6 +21,8 @@ func init() {
bus.AddHandler("sql", GetDashboardsByPluginId) bus.AddHandler("sql", GetDashboardsByPluginId)
} }
var generateNewUid func() string = util.GenerateShortUid
func SaveDashboard(cmd *m.SaveDashboardCommand) error { func SaveDashboard(cmd *m.SaveDashboardCommand) error {
return inTransaction(func(sess *DBSession) error { return inTransaction(func(sess *DBSession) error {
dash := cmd.GetDashboardModel() dash := cmd.GetDashboardModel()
...@@ -27,7 +30,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { ...@@ -27,7 +30,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
// try get existing dashboard // try get existing dashboard
var existing m.Dashboard var existing m.Dashboard
if dash.Id > 0 { if dash.Id != 0 {
dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing) dashWithIdExists, err := sess.Where("id=? AND org_id=?", dash.Id, dash.OrgId).Get(&existing)
if err != nil { if err != nil {
return err return err
...@@ -49,8 +52,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { ...@@ -49,8 +52,7 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
if existing.PluginId != "" && cmd.Overwrite == false { if existing.PluginId != "" && cmd.Overwrite == false {
return m.UpdatePluginDashboardError{PluginId: existing.PluginId} return m.UpdatePluginDashboardError{PluginId: existing.PluginId}
} }
} } else if dash.Uid != "" {
var sameUid m.Dashboard var sameUid m.Dashboard
sameUidExists, err := sess.Where("org_id=? AND uid=?", dash.OrgId, dash.Uid).Get(&sameUid) sameUidExists, err := sess.Where("org_id=? AND uid=?", dash.OrgId, dash.Uid).Get(&sameUid)
if err != nil { if err != nil {
...@@ -68,8 +70,18 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { ...@@ -68,8 +70,18 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
} }
} }
} }
}
if dash.Uid == "" {
uid, err := generateNewDashboardUid(sess, dash.OrgId)
if err != nil {
return err
}
dash.Uid = uid
dash.Data.Set("uid", uid)
}
err = guaranteeDashboardNameIsUniqueInFolder(sess, dash) err := guaranteeDashboardNameIsUniqueInFolder(sess, dash)
if err != nil { if err != nil {
return err return err
} }
...@@ -144,6 +156,22 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error { ...@@ -144,6 +156,22 @@ func SaveDashboard(cmd *m.SaveDashboardCommand) error {
return err return err
}) })
} }
func generateNewDashboardUid(sess *DBSession, orgId int64) (string, error) {
for i := 0; i < 3; i++ {
uid := generateNewUid()
exists, err := sess.Where("org_id=? AND uid=?", orgId, uid).Get(&m.Dashboard{})
if err != nil {
return "", err
}
if !exists {
return uid, nil
}
}
return "", m.ErrDashboardFailedGenerateUniqueUid
}
func guaranteeDashboardNameIsUniqueInFolder(sess *DBSession, dash *m.Dashboard) error { func guaranteeDashboardNameIsUniqueInFolder(sess *DBSession, dash *m.Dashboard) error {
var sameNameInFolder m.Dashboard var sameNameInFolder m.Dashboard
......
...@@ -5,12 +5,12 @@ import ( ...@@ -5,12 +5,12 @@ import (
"testing" "testing"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
. "github.com/smartystreets/goconvey/convey"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search" "github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
. "github.com/smartystreets/goconvey/convey"
) )
func TestDashboardDataAccess(t *testing.T) { func TestDashboardDataAccess(t *testing.T) {
...@@ -338,6 +338,30 @@ func TestDashboardDataAccess(t *testing.T) { ...@@ -338,6 +338,30 @@ func TestDashboardDataAccess(t *testing.T) {
So(err, ShouldBeNil) So(err, ShouldBeNil)
}) })
Convey("Should retry generation of uid once if it fails.", func() {
timesCalled := 0
generateNewUid = func() string {
timesCalled += 1
if timesCalled <= 2 {
return savedDash.Uid
} else {
return util.GenerateShortUid()
}
}
cmd := m.SaveDashboardCommand{
OrgId: 1,
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"title": "new dash 12334",
"tags": []interface{}{},
}),
}
err := SaveDashboard(&cmd)
So(err, ShouldBeNil)
generateNewUid = util.GenerateShortUid
})
Convey("Should be able to update dashboard and remove folderId", func() { Convey("Should be able to update dashboard and remove folderId", func() {
cmd := m.SaveDashboardCommand{ cmd := m.SaveDashboardCommand{
OrgId: 1, OrgId: 1,
......
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