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
@@ -103,7 +103,7 @@ func TestIntegrationExistsEnabledByAccessToken(t *testing.T) {
|
||||
t.Run("ExistsEnabledByAccessToken will return true when at least one public dashboard has a matching access token", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
_, err := publicdashboardStore.Create(context.Background(), SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: true,
|
||||
Uid: "abc123",
|
||||
@@ -125,7 +125,7 @@ func TestIntegrationExistsEnabledByAccessToken(t *testing.T) {
|
||||
t.Run("ExistsEnabledByAccessToken will return false when IsEnabled=false", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
_, err := publicdashboardStore.Create(context.Background(), SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: false,
|
||||
Uid: "abc123",
|
||||
@@ -171,7 +171,7 @@ func TestIntegrationExistsEnabledByDashboardUid(t *testing.T) {
|
||||
t.Run("ExistsEnabledByDashboardUid Will return true when dashboard has at least one enabled public dashboard", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
_, err := publicdashboardStore.Create(context.Background(), SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: true,
|
||||
Uid: "abc123",
|
||||
@@ -193,7 +193,7 @@ func TestIntegrationExistsEnabledByDashboardUid(t *testing.T) {
|
||||
t.Run("ExistsEnabledByDashboardUid will return false when dashboard has public dashboards but they are not enabled", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
_, err := publicdashboardStore.Create(context.Background(), SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: false,
|
||||
Uid: "abc123",
|
||||
@@ -257,7 +257,7 @@ func TestIntegrationFindByDashboardUid(t *testing.T) {
|
||||
}
|
||||
|
||||
// insert test public dashboard
|
||||
err := publicdashboardStore.Save(context.Background(), cmd)
|
||||
_, err := publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
// retrieve from db
|
||||
@@ -320,7 +320,7 @@ func TestIntegrationFindByAccessToken(t *testing.T) {
|
||||
}
|
||||
|
||||
// insert test public dashboard
|
||||
err := publicdashboardStore.Save(context.Background(), cmd)
|
||||
_, err := publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
// retrieve from db
|
||||
@@ -338,7 +338,7 @@ func TestIntegrationFindByAccessToken(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationSavePublicDashboard(t *testing.T) {
|
||||
func TestIntegrationCreatePublicDashboard(t *testing.T) {
|
||||
var sqlStore db.DB
|
||||
var cfg *setting.Cfg
|
||||
var dashboardStore *dashboardsDB.DashboardStore
|
||||
@@ -357,7 +357,7 @@ func TestIntegrationSavePublicDashboard(t *testing.T) {
|
||||
|
||||
t.Run("saves new public dashboard", func(t *testing.T) {
|
||||
setup()
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
cmd := SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: true,
|
||||
AnnotationsEnabled: true,
|
||||
@@ -369,14 +369,14 @@ func TestIntegrationSavePublicDashboard(t *testing.T) {
|
||||
CreatedBy: 7,
|
||||
AccessToken: "NOTAREALUUID",
|
||||
},
|
||||
})
|
||||
}
|
||||
affectedRows, err := publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, affectedRows, 1)
|
||||
|
||||
pubdash, err := publicdashboardStore.FindByDashboardUid(context.Background(), savedDashboard.OrgId, savedDashboard.Uid)
|
||||
require.NoError(t, err)
|
||||
|
||||
// verify we have a valid uid
|
||||
assert.True(t, util.IsValidShortUID(pubdash.Uid))
|
||||
assert.Equal(t, pubdash.AccessToken, "NOTAREALUUID")
|
||||
|
||||
// verify we didn't update all dashboards
|
||||
pubdash2, err := publicdashboardStore.FindByDashboardUid(context.Background(), savedDashboard2.OrgId, savedDashboard2.Uid)
|
||||
@@ -386,7 +386,7 @@ func TestIntegrationSavePublicDashboard(t *testing.T) {
|
||||
|
||||
t.Run("guards from saving without dashboardUid", func(t *testing.T) {
|
||||
setup()
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
cmd := SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: true,
|
||||
Uid: "pubdash-uid",
|
||||
@@ -397,9 +397,11 @@ func TestIntegrationSavePublicDashboard(t *testing.T) {
|
||||
CreatedBy: 7,
|
||||
AccessToken: "NOTAREALUUID",
|
||||
},
|
||||
})
|
||||
}
|
||||
affectedRows, err := publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, err, dashboards.ErrDashboardIdentifierNotSet)
|
||||
assert.EqualValues(t, affectedRows, 0)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -423,7 +425,7 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) {
|
||||
setup()
|
||||
|
||||
pdUid := "asdf1234"
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
cmd := SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
Uid: pdUid,
|
||||
DashboardUid: savedDashboard.Uid,
|
||||
@@ -434,12 +436,14 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) {
|
||||
CreatedBy: 7,
|
||||
AccessToken: "NOTAREALUUID",
|
||||
},
|
||||
})
|
||||
}
|
||||
affectedRows, err := publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, affectedRows, 1)
|
||||
|
||||
// inserting two different public dashboards to test update works and only affect the desired pd by uid
|
||||
anotherPdUid := "anotherUid"
|
||||
err = publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
cmd = SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
Uid: anotherPdUid,
|
||||
DashboardUid: anotherSavedDashboard.Uid,
|
||||
@@ -450,8 +454,11 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) {
|
||||
CreatedBy: 7,
|
||||
AccessToken: "fakeaccesstoken",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
affectedRows, err = publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, affectedRows, 1)
|
||||
|
||||
updatedPublicDashboard := PublicDashboard{
|
||||
Uid: pdUid,
|
||||
@@ -463,11 +470,12 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) {
|
||||
UpdatedAt: time.Now().UTC().Round(time.Second),
|
||||
UpdatedBy: 8,
|
||||
}
|
||||
|
||||
// update initial record
|
||||
err = publicdashboardStore.Update(context.Background(), SavePublicDashboardCommand{
|
||||
PublicDashboard: updatedPublicDashboard,
|
||||
})
|
||||
cmd = SavePublicDashboardCommand{PublicDashboard: updatedPublicDashboard}
|
||||
rowsAffected, err := publicdashboardStore.Update(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, rowsAffected, 1)
|
||||
|
||||
// updated dashboard should have changed
|
||||
pdRetrieved, err := publicdashboardStore.FindByDashboardUid(context.Background(), savedDashboard.OrgId, savedDashboard.Uid)
|
||||
@@ -503,8 +511,7 @@ func TestIntegrationGetOrgIdByAccessToken(t *testing.T) {
|
||||
}
|
||||
t.Run("GetOrgIdByAccessToken will OrgId when enabled", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
cmd := SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: true,
|
||||
Uid: "abc123",
|
||||
@@ -514,7 +521,8 @@ func TestIntegrationGetOrgIdByAccessToken(t *testing.T) {
|
||||
CreatedBy: 7,
|
||||
AccessToken: "accessToken",
|
||||
},
|
||||
})
|
||||
}
|
||||
_, err := publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
orgId, err := publicdashboardStore.GetOrgIdByAccessToken(context.Background(), "accessToken")
|
||||
@@ -525,8 +533,7 @@ func TestIntegrationGetOrgIdByAccessToken(t *testing.T) {
|
||||
|
||||
t.Run("GetOrgIdByAccessToken will return 0 when IsEnabled=false", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.Save(context.Background(), SavePublicDashboardCommand{
|
||||
cmd := SavePublicDashboardCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: false,
|
||||
Uid: "abc123",
|
||||
@@ -536,8 +543,11 @@ func TestIntegrationGetOrgIdByAccessToken(t *testing.T) {
|
||||
CreatedBy: 7,
|
||||
AccessToken: "accessToken",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
_, err := publicdashboardStore.Create(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
orgId, err := publicdashboardStore.GetOrgIdByAccessToken(context.Background(), "accessToken")
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, savedDashboard.OrgId, orgId)
|
||||
@@ -634,8 +644,9 @@ func insertPublicDashboard(t *testing.T, publicdashboardStore *PublicDashboardSt
|
||||
},
|
||||
}
|
||||
|
||||
err = publicdashboardStore.Save(ctx, cmd)
|
||||
affectedRows, err := publicdashboardStore.Create(ctx, cmd)
|
||||
require.NoError(t, err)
|
||||
assert.EqualValues(t, affectedRows, 1)
|
||||
|
||||
pubdash, err := publicdashboardStore.Find(ctx, uid)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user