publicdashboards: split create/update api paths (#57940)
This PR splits the create and update paths for public dashboards and includes assorted refactors toward a proper REST API. Additionally, we removed the concept of a "public dashboard config" in favor of "public dashboard" Co-authored-by: juanicabanas <juan.cabanas@grafana.com> Co-authored-by: Ezequiel Victorero <ezequiel.victorero@grafana.com>
This commit is contained in:
co-authored by
juanicabanas
Ezequiel Victorero
parent
0367f61bb3
commit
6fcc5b42c0
@@ -66,11 +66,15 @@ func (d *PublicDashboardStoreImpl) FindDashboard(ctx context.Context, orgId int6
|
||||
return err
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !found {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return dashboard, err
|
||||
return dashboard, nil
|
||||
}
|
||||
|
||||
// Find Returns public dashboard by Uid or nil if not found
|
||||
@@ -80,10 +84,10 @@ func (d *PublicDashboardStoreImpl) Find(ctx context.Context, uid string) (*Publi
|
||||
}
|
||||
|
||||
var found bool
|
||||
pdRes := &PublicDashboard{Uid: uid}
|
||||
publicDashboard := &PublicDashboard{Uid: uid}
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var err error
|
||||
found, err = sess.Get(pdRes)
|
||||
found, err = sess.Get(publicDashboard)
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -95,7 +99,7 @@ func (d *PublicDashboardStoreImpl) Find(ctx context.Context, uid string) (*Publi
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return pdRes, err
|
||||
return publicDashboard, nil
|
||||
}
|
||||
|
||||
// FindByAccessToken Returns public dashboard by access token or nil if not found
|
||||
@@ -105,10 +109,10 @@ func (d *PublicDashboardStoreImpl) FindByAccessToken(ctx context.Context, access
|
||||
}
|
||||
|
||||
var found bool
|
||||
pdRes := &PublicDashboard{AccessToken: accessToken}
|
||||
publicDashboard := &PublicDashboard{AccessToken: accessToken}
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var err error
|
||||
found, err = sess.Get(pdRes)
|
||||
found, err = sess.Get(publicDashboard)
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -120,7 +124,7 @@ func (d *PublicDashboardStoreImpl) FindByAccessToken(ctx context.Context, access
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return pdRes, err
|
||||
return publicDashboard, nil
|
||||
}
|
||||
|
||||
// FindByDashboardUid Retrieves public dashboard by dashboard uid or nil if not found
|
||||
@@ -128,7 +132,6 @@ func (d *PublicDashboardStoreImpl) FindByDashboardUid(ctx context.Context, orgId
|
||||
if dashboardUid == "" || orgId == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var found bool
|
||||
publicDashboard := &PublicDashboard{OrgId: orgId, DashboardUid: dashboardUid}
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
@@ -149,67 +152,7 @@ func (d *PublicDashboardStoreImpl) FindByDashboardUid(ctx context.Context, orgId
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return publicDashboard, err
|
||||
}
|
||||
|
||||
// Save Persists public dashboard
|
||||
func (d *PublicDashboardStoreImpl) Save(ctx context.Context, cmd SavePublicDashboardCommand) error {
|
||||
if cmd.PublicDashboard.DashboardUid == "" {
|
||||
return dashboards.ErrDashboardIdentifierNotSet
|
||||
}
|
||||
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
_, err := sess.UseBool("is_enabled").Insert(&cmd.PublicDashboard)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Update updates existing public dashboard
|
||||
func (d *PublicDashboardStoreImpl) Update(ctx context.Context, cmd SavePublicDashboardCommand) error {
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
timeSettingsJSON, err := json.Marshal(cmd.PublicDashboard.TimeSettings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = sess.Exec("UPDATE dashboard_public SET is_enabled = ?, annotations_enabled = ?, time_settings = ?, updated_by = ?, updated_at = ? WHERE uid = ?",
|
||||
cmd.PublicDashboard.IsEnabled,
|
||||
cmd.PublicDashboard.AnnotationsEnabled,
|
||||
string(timeSettingsJSON),
|
||||
cmd.PublicDashboard.UpdatedBy,
|
||||
cmd.PublicDashboard.UpdatedAt.UTC().Format("2006-01-02 15:04:05"),
|
||||
cmd.PublicDashboard.Uid)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *PublicDashboardStoreImpl) Delete(ctx context.Context, orgId int64, uid string) (int64, error) {
|
||||
dashboard := &PublicDashboard{OrgId: orgId, Uid: uid}
|
||||
var affectedRows int64
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var err error
|
||||
affectedRows, err = sess.Delete(dashboard)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return affectedRows, err
|
||||
return publicDashboard, nil
|
||||
}
|
||||
|
||||
// ExistsEnabledByDashboardUid Responds true if there is an enabled public dashboard for a dashboard uid
|
||||
@@ -264,3 +207,62 @@ func (d *PublicDashboardStoreImpl) GetOrgIdByAccessToken(ctx context.Context, ac
|
||||
|
||||
return orgId, err
|
||||
}
|
||||
|
||||
// Creates a public dashboard
|
||||
func (d *PublicDashboardStoreImpl) Create(ctx context.Context, cmd SavePublicDashboardCommand) (int64, error) {
|
||||
if cmd.PublicDashboard.DashboardUid == "" {
|
||||
return 0, dashboards.ErrDashboardIdentifierNotSet
|
||||
}
|
||||
|
||||
var affectedRows int64
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var err error
|
||||
affectedRows, err = sess.UseBool("is_enabled").Insert(&cmd.PublicDashboard)
|
||||
return err
|
||||
})
|
||||
|
||||
return affectedRows, err
|
||||
}
|
||||
|
||||
// Updates existing public dashboard
|
||||
func (d *PublicDashboardStoreImpl) Update(ctx context.Context, cmd SavePublicDashboardCommand) (int64, error) {
|
||||
var affectedRows int64
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
timeSettingsJSON, err := json.Marshal(cmd.PublicDashboard.TimeSettings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sqlResult, err := sess.Exec("UPDATE dashboard_public SET is_enabled = ?, annotations_enabled = ?, time_settings = ?, updated_by = ?, updated_at = ? WHERE uid = ?",
|
||||
cmd.PublicDashboard.IsEnabled,
|
||||
cmd.PublicDashboard.AnnotationsEnabled,
|
||||
string(timeSettingsJSON),
|
||||
cmd.PublicDashboard.UpdatedBy,
|
||||
cmd.PublicDashboard.UpdatedAt.UTC().Format("2006-01-02 15:04:05"),
|
||||
cmd.PublicDashboard.Uid)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
affectedRows, err = sqlResult.RowsAffected()
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
return affectedRows, err
|
||||
}
|
||||
|
||||
// Deletes a public dashboard
|
||||
func (d *PublicDashboardStoreImpl) Delete(ctx context.Context, orgId int64, uid string) (int64, error) {
|
||||
dashboard := &PublicDashboard{OrgId: orgId, Uid: uid}
|
||||
var affectedRows int64
|
||||
err := d.sqlStore.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var err error
|
||||
affectedRows, err = sess.Delete(dashboard)
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
return affectedRows, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user