mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Rename Id to ID for annotation models (#62886)
* Rename Id to ID for annotation models * Add xorm tags * Rename Id to ID for API key models * Add xorm tags
This commit is contained in:
parent
a0602a2a78
commit
982939111b
@ -35,12 +35,12 @@ func (hs *HTTPServer) GetAnnotations(c *contextmodel.ReqContext) response.Respon
|
||||
query := &annotations.ItemQuery{
|
||||
From: c.QueryInt64("from"),
|
||||
To: c.QueryInt64("to"),
|
||||
OrgId: c.OrgID,
|
||||
UserId: c.QueryInt64("userId"),
|
||||
AlertId: c.QueryInt64("alertId"),
|
||||
DashboardId: c.QueryInt64("dashboardId"),
|
||||
DashboardUid: c.Query("dashboardUID"),
|
||||
PanelId: c.QueryInt64("panelId"),
|
||||
OrgID: c.OrgID,
|
||||
UserID: c.QueryInt64("userId"),
|
||||
AlertID: c.QueryInt64("alertId"),
|
||||
DashboardID: c.QueryInt64("dashboardId"),
|
||||
DashboardUID: c.Query("dashboardUID"),
|
||||
PanelID: c.QueryInt64("panelId"),
|
||||
Limit: c.QueryInt64("limit"),
|
||||
Tags: c.QueryStrings("tags"),
|
||||
Type: c.Query("type"),
|
||||
@ -49,8 +49,8 @@ func (hs *HTTPServer) GetAnnotations(c *contextmodel.ReqContext) response.Respon
|
||||
}
|
||||
|
||||
// When dashboard UID present in the request, we ignore dashboard ID
|
||||
if query.DashboardUid != "" {
|
||||
dq := dashboards.GetDashboardQuery{UID: query.DashboardUid, OrgID: c.OrgID}
|
||||
if query.DashboardUID != "" {
|
||||
dq := dashboards.GetDashboardQuery{UID: query.DashboardUID, OrgID: c.OrgID}
|
||||
dqResult, err := hs.DashboardService.GetDashboard(c.Req.Context(), &dq)
|
||||
if err != nil {
|
||||
if hs.Features.IsEnabled(featuremgmt.FlagDashboardsFromStorage) {
|
||||
@ -59,7 +59,7 @@ func (hs *HTTPServer) GetAnnotations(c *contextmodel.ReqContext) response.Respon
|
||||
return response.Error(http.StatusBadRequest, "Invalid dashboard UID in annotation request", err)
|
||||
}
|
||||
} else {
|
||||
query.DashboardId = dqResult.ID
|
||||
query.DashboardID = dqResult.ID
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,18 +72,18 @@ func (hs *HTTPServer) GetAnnotations(c *contextmodel.ReqContext) response.Respon
|
||||
dashboardCache := make(map[int64]*string)
|
||||
for _, item := range items {
|
||||
if item.Email != "" {
|
||||
item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
|
||||
item.AvatarURL = dtos.GetGravatarUrl(item.Email)
|
||||
}
|
||||
|
||||
if item.DashboardId != 0 {
|
||||
if val, ok := dashboardCache[item.DashboardId]; ok {
|
||||
if item.DashboardID != 0 {
|
||||
if val, ok := dashboardCache[item.DashboardID]; ok {
|
||||
item.DashboardUID = val
|
||||
} else {
|
||||
query := dashboards.GetDashboardQuery{ID: item.DashboardId, OrgID: c.OrgID}
|
||||
query := dashboards.GetDashboardQuery{ID: item.DashboardID, OrgID: c.OrgID}
|
||||
queryResult, err := hs.DashboardService.GetDashboard(c.Req.Context(), &query)
|
||||
if err == nil && queryResult != nil {
|
||||
item.DashboardUID = &queryResult.UID
|
||||
dashboardCache[item.DashboardId] = &queryResult.UID
|
||||
dashboardCache[item.DashboardID] = &queryResult.UID
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -139,10 +139,10 @@ func (hs *HTTPServer) PostAnnotation(c *contextmodel.ReqContext) response.Respon
|
||||
}
|
||||
|
||||
item := annotations.Item{
|
||||
OrgId: c.OrgID,
|
||||
UserId: c.UserID,
|
||||
DashboardId: cmd.DashboardId,
|
||||
PanelId: cmd.PanelId,
|
||||
OrgID: c.OrgID,
|
||||
UserID: c.UserID,
|
||||
DashboardID: cmd.DashboardId,
|
||||
PanelID: cmd.PanelId,
|
||||
Epoch: cmd.Time,
|
||||
EpochEnd: cmd.TimeEnd,
|
||||
Text: cmd.Text,
|
||||
@ -157,7 +157,7 @@ func (hs *HTTPServer) PostAnnotation(c *contextmodel.ReqContext) response.Respon
|
||||
return response.ErrOrFallback(500, "Failed to save annotation", err)
|
||||
}
|
||||
|
||||
startID := item.Id
|
||||
startID := item.ID
|
||||
|
||||
return response.JSON(http.StatusOK, util.DynMap{
|
||||
"message": "Annotation added",
|
||||
@ -221,8 +221,8 @@ func (hs *HTTPServer) PostGraphiteAnnotation(c *contextmodel.ReqContext) respons
|
||||
}
|
||||
|
||||
item := annotations.Item{
|
||||
OrgId: c.OrgID,
|
||||
UserId: c.UserID,
|
||||
OrgID: c.OrgID,
|
||||
UserID: c.UserID,
|
||||
Epoch: cmd.When * 1000,
|
||||
Text: text,
|
||||
Tags: tagsArray,
|
||||
@ -234,7 +234,7 @@ func (hs *HTTPServer) PostGraphiteAnnotation(c *contextmodel.ReqContext) respons
|
||||
|
||||
return response.JSON(http.StatusOK, util.DynMap{
|
||||
"message": "Graphite annotation added",
|
||||
"id": item.Id,
|
||||
"id": item.ID,
|
||||
})
|
||||
}
|
||||
|
||||
@ -271,9 +271,9 @@ func (hs *HTTPServer) UpdateAnnotation(c *contextmodel.ReqContext) response.Resp
|
||||
}
|
||||
|
||||
item := annotations.Item{
|
||||
OrgId: c.OrgID,
|
||||
UserId: c.UserID,
|
||||
Id: annotationID,
|
||||
OrgID: c.OrgID,
|
||||
UserID: c.UserID,
|
||||
ID: annotationID,
|
||||
Epoch: cmd.Time,
|
||||
EpochEnd: cmd.TimeEnd,
|
||||
Text: cmd.Text,
|
||||
@ -326,9 +326,9 @@ func (hs *HTTPServer) PatchAnnotation(c *contextmodel.ReqContext) response.Respo
|
||||
}
|
||||
|
||||
existing := annotations.Item{
|
||||
OrgId: c.OrgID,
|
||||
UserId: c.UserID,
|
||||
Id: annotationID,
|
||||
OrgID: c.OrgID,
|
||||
UserID: c.UserID,
|
||||
ID: annotationID,
|
||||
Epoch: annotation.Time,
|
||||
EpochEnd: annotation.TimeEnd,
|
||||
Text: annotation.Text,
|
||||
@ -403,17 +403,17 @@ func (hs *HTTPServer) MassDeleteAnnotations(c *contextmodel.ReqContext) response
|
||||
if respErr != nil {
|
||||
return respErr
|
||||
}
|
||||
dashboardId = annotation.DashboardId
|
||||
dashboardId = annotation.DashboardID
|
||||
deleteParams = &annotations.DeleteParams{
|
||||
OrgId: c.OrgID,
|
||||
Id: cmd.AnnotationId,
|
||||
OrgID: c.OrgID,
|
||||
ID: cmd.AnnotationId,
|
||||
}
|
||||
} else {
|
||||
dashboardId = cmd.DashboardId
|
||||
deleteParams = &annotations.DeleteParams{
|
||||
OrgId: c.OrgID,
|
||||
DashboardId: cmd.DashboardId,
|
||||
PanelId: cmd.PanelId,
|
||||
OrgID: c.OrgID,
|
||||
DashboardID: cmd.DashboardId,
|
||||
PanelID: cmd.PanelId,
|
||||
}
|
||||
}
|
||||
|
||||
@ -423,10 +423,10 @@ func (hs *HTTPServer) MassDeleteAnnotations(c *contextmodel.ReqContext) response
|
||||
}
|
||||
} else { // legacy permissions
|
||||
deleteParams = &annotations.DeleteParams{
|
||||
OrgId: c.OrgID,
|
||||
Id: cmd.AnnotationId,
|
||||
DashboardId: cmd.DashboardId,
|
||||
PanelId: cmd.PanelId,
|
||||
OrgID: c.OrgID,
|
||||
ID: cmd.AnnotationId,
|
||||
DashboardID: cmd.DashboardId,
|
||||
PanelID: cmd.PanelId,
|
||||
}
|
||||
}
|
||||
|
||||
@ -459,7 +459,7 @@ func (hs *HTTPServer) GetAnnotationByID(c *contextmodel.ReqContext) response.Res
|
||||
}
|
||||
|
||||
if annotation.Email != "" {
|
||||
annotation.AvatarUrl = dtos.GetGravatarUrl(annotation.Email)
|
||||
annotation.AvatarURL = dtos.GetGravatarUrl(annotation.Email)
|
||||
}
|
||||
|
||||
return response.JSON(200, annotation)
|
||||
@ -492,8 +492,8 @@ func (hs *HTTPServer) DeleteAnnotationByID(c *contextmodel.ReqContext) response.
|
||||
}
|
||||
|
||||
err = hs.annotationsRepo.Delete(c.Req.Context(), &annotations.DeleteParams{
|
||||
OrgId: c.OrgID,
|
||||
Id: annotationID,
|
||||
OrgID: c.OrgID,
|
||||
ID: annotationID,
|
||||
})
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to delete annotation", err)
|
||||
@ -504,7 +504,7 @@ func (hs *HTTPServer) DeleteAnnotationByID(c *contextmodel.ReqContext) response.
|
||||
|
||||
func (hs *HTTPServer) canSaveAnnotation(c *contextmodel.ReqContext, annotation *annotations.ItemDTO) (bool, error) {
|
||||
if annotation.GetType() == annotations.Dashboard {
|
||||
return canEditDashboard(c, annotation.DashboardId)
|
||||
return canEditDashboard(c, annotation.DashboardID)
|
||||
} else {
|
||||
if hs.AccessControl.IsDisabled() {
|
||||
return c.SignedInUser.HasRole(org.RoleEditor), nil
|
||||
@ -528,8 +528,8 @@ func canEditDashboard(c *contextmodel.ReqContext, dashboardID int64) (bool, erro
|
||||
|
||||
func findAnnotationByID(ctx context.Context, repo annotations.Repository, annotationID int64, user *user.SignedInUser) (*annotations.ItemDTO, response.Response) {
|
||||
query := &annotations.ItemQuery{
|
||||
AnnotationId: annotationID,
|
||||
OrgId: user.OrgID,
|
||||
AnnotationID: annotationID,
|
||||
OrgID: user.OrgID,
|
||||
SignedInUser: user,
|
||||
}
|
||||
items, err := repo.Find(ctx, query)
|
||||
|
@ -595,8 +595,8 @@ func TestAPI_Annotations_AccessControl(t *testing.T) {
|
||||
server := SetupAPITestServer(t, func(hs *HTTPServer) {
|
||||
hs.Cfg = setting.NewCfg()
|
||||
repo := annotationstest.NewFakeAnnotationsRepo()
|
||||
_ = repo.Save(context.Background(), &annotations.Item{Id: 1, DashboardId: 0})
|
||||
_ = repo.Save(context.Background(), &annotations.Item{Id: 2, DashboardId: 1})
|
||||
_ = repo.Save(context.Background(), &annotations.Item{ID: 1, DashboardID: 0})
|
||||
_ = repo.Save(context.Background(), &annotations.Item{ID: 2, DashboardID: 1})
|
||||
hs.annotationsRepo = repo
|
||||
hs.AccessControl = acimpl.ProvideAccessControl(hs.Cfg)
|
||||
hs.AccessControl.RegisterScopeAttributeResolver(AnnotationTypeScopeResolver(hs.annotationsRepo))
|
||||
@ -649,8 +649,8 @@ func TestService_AnnotationTypeScopeResolver(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
dashboardAnnotation := annotations.Item{Id: 1, DashboardId: 1}
|
||||
organizationAnnotation := annotations.Item{Id: 2}
|
||||
dashboardAnnotation := annotations.Item{ID: 1, DashboardID: 1}
|
||||
organizationAnnotation := annotations.Item{ID: 2}
|
||||
|
||||
fakeAnnoRepo := annotationstest.NewFakeAnnotationsRepo()
|
||||
_ = fakeAnnoRepo.Save(context.Background(), &dashboardAnnotation)
|
||||
|
@ -27,7 +27,7 @@ import (
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) GetAPIKeys(c *contextmodel.ReqContext) response.Response {
|
||||
query := apikey.GetApiKeysQuery{OrgId: c.OrgID, User: c.SignedInUser, IncludeExpired: c.QueryBool("includeExpired")}
|
||||
query := apikey.GetApiKeysQuery{OrgID: c.OrgID, User: c.SignedInUser, IncludeExpired: c.QueryBool("includeExpired")}
|
||||
|
||||
if err := hs.apiKeyService.GetAPIKeys(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Failed to list api keys", err)
|
||||
@ -36,14 +36,14 @@ func (hs *HTTPServer) GetAPIKeys(c *contextmodel.ReqContext) response.Response {
|
||||
ids := map[string]bool{}
|
||||
result := make([]*dtos.ApiKeyDTO, len(query.Result))
|
||||
for i, t := range query.Result {
|
||||
ids[strconv.FormatInt(t.Id, 10)] = true
|
||||
ids[strconv.FormatInt(t.ID, 10)] = true
|
||||
var expiration *time.Time = nil
|
||||
if t.Expires != nil {
|
||||
v := time.Unix(*t.Expires, 0)
|
||||
expiration = &v
|
||||
}
|
||||
result[i] = &dtos.ApiKeyDTO{
|
||||
Id: t.Id,
|
||||
ID: t.ID,
|
||||
Name: t.Name,
|
||||
Role: t.Role,
|
||||
Expiration: expiration,
|
||||
@ -53,7 +53,7 @@ func (hs *HTTPServer) GetAPIKeys(c *contextmodel.ReqContext) response.Response {
|
||||
metadata := hs.getMultiAccessControlMetadata(c, c.OrgID, "apikeys:id", ids)
|
||||
if len(metadata) > 0 {
|
||||
for _, key := range result {
|
||||
key.AccessControl = metadata[strconv.FormatInt(key.Id, 10)]
|
||||
key.AccessControl = metadata[strconv.FormatInt(key.ID, 10)]
|
||||
}
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ func (hs *HTTPServer) DeleteAPIKey(c *contextmodel.ReqContext) response.Response
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
}
|
||||
|
||||
cmd := &apikey.DeleteCommand{Id: id, OrgId: c.OrgID}
|
||||
cmd := &apikey.DeleteCommand{ID: id, OrgID: c.OrgID}
|
||||
err = hs.apiKeyService.DeleteApiKey(c.Req.Context(), cmd)
|
||||
if err != nil {
|
||||
var status int
|
||||
@ -125,9 +125,9 @@ func (hs *HTTPServer) AddAPIKey(c *contextmodel.ReqContext) response.Response {
|
||||
}
|
||||
}
|
||||
|
||||
cmd.OrgId = c.OrgID
|
||||
cmd.OrgID = c.OrgID
|
||||
|
||||
newKeyInfo, err := apikeygen.New(cmd.OrgId, cmd.Name)
|
||||
newKeyInfo, err := apikeygen.New(cmd.OrgID, cmd.Name)
|
||||
if err != nil {
|
||||
return response.Error(500, "Generating API key failed", err)
|
||||
}
|
||||
@ -144,7 +144,7 @@ func (hs *HTTPServer) AddAPIKey(c *contextmodel.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
result := &dtos.NewApiKeyResult{
|
||||
ID: cmd.Result.Id,
|
||||
ID: cmd.Result.ID,
|
||||
Name: cmd.Result.Name,
|
||||
Key: newKeyInfo.ClientSecret,
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ type NewApiKeyResult struct {
|
||||
}
|
||||
|
||||
type ApiKeyDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Role org.RoleType `json:"role"`
|
||||
Expiration *time.Time `json:"expiration,omitempty"`
|
||||
|
@ -30,7 +30,7 @@ func TestMiddlewareBasicAuth(t *testing.T) {
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgId: orgID, Role: org.RoleEditor, Key: keyhash}
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgID: orgID, Role: org.RoleEditor, Key: keyhash}
|
||||
|
||||
authHeader := util.GetBasicAuthHeader("api_key", "eyJrIjoidjVuQXdwTWFmRlA2em5hUzR1cmhkV0RMUzU1MTFNNDIiLCJuIjoiYXNkIiwiaWQiOjF9")
|
||||
sc.fakeReq("GET", "/").withAuthorizationHeader(authHeader).exec()
|
||||
|
@ -226,7 +226,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgId: orgID, Role: org.RoleEditor, Key: keyhash}
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgID: orgID, Role: org.RoleEditor, Key: keyhash}
|
||||
|
||||
sc.fakeReq("GET", "/").withValidApiKey().exec()
|
||||
|
||||
@ -242,7 +242,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
keyhash, err := util.EncodePassword("v5nAwpMafFP6znaS4urhdWDLS5511M42", "asd")
|
||||
require.NoError(t, err)
|
||||
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgId: orgID, Role: org.RoleEditor, Key: keyhash}
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgID: orgID, Role: org.RoleEditor, Key: keyhash}
|
||||
|
||||
sc.fakeReq("GET", "/").withValidApiKey().exec()
|
||||
|
||||
@ -299,7 +299,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
|
||||
middlewareScenario(t, "Valid API key, but does not match DB hash", func(t *testing.T, sc *scenarioContext) {
|
||||
const keyhash = "Something_not_matching"
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgId: 12, Role: org.RoleEditor, Key: keyhash}
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgID: 12, Role: org.RoleEditor, Key: keyhash}
|
||||
|
||||
sc.fakeReq("GET", "/").withValidApiKey().exec()
|
||||
|
||||
@ -314,7 +314,7 @@ func TestMiddlewareContext(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
expires := sc.contextHandler.GetTime().Add(-1 * time.Second).Unix()
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgId: 12, Role: org.RoleEditor, Key: keyhash, Expires: &expires}
|
||||
sc.apiKeyService.ExpectedAPIKey = &apikey.APIKey{OrgID: 12, Role: org.RoleEditor, Key: keyhash, Expires: &expires}
|
||||
|
||||
sc.fakeReq("GET", "/").withValidApiKey().exec()
|
||||
|
||||
|
@ -84,10 +84,10 @@ func (handler *defaultResultHandler) handle(evalContext *EvalContext) error {
|
||||
|
||||
// save annotation
|
||||
item := annotations.Item{
|
||||
OrgId: evalContext.Rule.OrgID,
|
||||
DashboardId: evalContext.Rule.DashboardID,
|
||||
PanelId: evalContext.Rule.PanelID,
|
||||
AlertId: evalContext.Rule.ID,
|
||||
OrgID: evalContext.Rule.OrgID,
|
||||
DashboardID: evalContext.Rule.DashboardID,
|
||||
PanelID: evalContext.Rule.PanelID,
|
||||
AlertID: evalContext.Rule.ID,
|
||||
Text: "",
|
||||
NewState: string(evalContext.Rule.State),
|
||||
PrevState: string(evalContext.PrevAlertState),
|
||||
|
@ -124,11 +124,11 @@ func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
|
||||
|
||||
// create some test annotations
|
||||
a := annotations.Item{
|
||||
DashboardId: 1,
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
PanelId: 1,
|
||||
AlertId: 10,
|
||||
DashboardID: 1,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
PanelID: 1,
|
||||
AlertID: 10,
|
||||
Text: "",
|
||||
Created: time.Now().AddDate(-10, 0, -10).UnixNano() / int64(time.Millisecond),
|
||||
}
|
||||
@ -139,7 +139,7 @@ func TestOldAnnotationsAreDeletedFirst(t *testing.T) {
|
||||
_, err = sess.Insert(a)
|
||||
require.NoError(t, err, "cannot insert annotation")
|
||||
|
||||
a.AlertId = 20
|
||||
a.AlertID = 20
|
||||
_, err = sess.Insert(a)
|
||||
require.NoError(t, err, "cannot insert annotation")
|
||||
|
||||
@ -195,23 +195,23 @@ func createTestAnnotations(t *testing.T, store db.DB, expectedCount int, oldAnno
|
||||
|
||||
for i := 0; i < expectedCount; i++ {
|
||||
a := &annotations.Item{
|
||||
DashboardId: 1,
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
PanelId: 1,
|
||||
DashboardID: 1,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
PanelID: 1,
|
||||
Text: "",
|
||||
}
|
||||
|
||||
// mark every third as an API annotation
|
||||
// that does not belong to a dashboard
|
||||
if i%3 == 1 {
|
||||
a.DashboardId = 0
|
||||
a.DashboardID = 0
|
||||
}
|
||||
|
||||
// mark every third annotation as an alert annotation
|
||||
if i%3 == 0 {
|
||||
a.AlertId = 10
|
||||
a.DashboardId = 2
|
||||
a.AlertID = 10
|
||||
a.DashboardID = 2
|
||||
}
|
||||
|
||||
// create epoch as int annotations.go line 40
|
||||
@ -229,7 +229,7 @@ func createTestAnnotations(t *testing.T, store db.DB, expectedCount int, oldAnno
|
||||
// mimick the SQL annotation Save logic by writing records to the annotation_tag table
|
||||
// we need to ensure they get deleted when we clean up annotations
|
||||
for tagID := range []int{1, 2} {
|
||||
_, err = sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", a.Id, tagID)
|
||||
_, err = sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", a.ID, tagID)
|
||||
require.NoError(t, err, "should be able to save annotation tag ID", err)
|
||||
}
|
||||
return err
|
||||
|
@ -131,7 +131,7 @@ func (r *xormRepositoryImpl) synchronizeTags(ctx context.Context, item *annotati
|
||||
return err
|
||||
}
|
||||
for _, tag := range tags {
|
||||
if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", item.Id, tag.Id); err != nil {
|
||||
if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", item.ID, tag.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -148,7 +148,7 @@ func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item)
|
||||
)
|
||||
existing := new(annotations.Item)
|
||||
|
||||
isExist, err = sess.Table("annotation").Where("id=? AND org_id=?", item.Id, item.OrgId).Get(existing)
|
||||
isExist, err = sess.Table("annotation").Where("id=? AND org_id=?", item.ID, item.OrgID).Get(existing)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -176,11 +176,11 @@ func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := sess.Exec("DELETE FROM annotation_tag WHERE annotation_id = ?", existing.Id); err != nil {
|
||||
if _, err := sess.Exec("DELETE FROM annotation_tag WHERE annotation_id = ?", existing.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, tag := range tags {
|
||||
if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", existing.Id, tag.Id); err != nil {
|
||||
if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", existing.ID, tag.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -192,7 +192,7 @@ func (r *xormRepositoryImpl) Update(ctx context.Context, item *annotations.Item)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = sess.Table("annotation").ID(existing.Id).Cols("epoch", "text", "epoch_end", "updated", "tags", "data").Update(existing)
|
||||
_, err = sess.Table("annotation").ID(existing.ID).Cols("epoch", "text", "epoch_end", "updated", "tags", "data").Update(existing)
|
||||
return err
|
||||
})
|
||||
}
|
||||
@ -228,32 +228,32 @@ func (r *xormRepositoryImpl) Get(ctx context.Context, query *annotations.ItemQue
|
||||
`)
|
||||
|
||||
sql.WriteString(`WHERE a.org_id = ?`)
|
||||
params = append(params, query.OrgId)
|
||||
params = append(params, query.OrgID)
|
||||
|
||||
if query.AnnotationId != 0 {
|
||||
if query.AnnotationID != 0 {
|
||||
// fmt.Print("annotation query")
|
||||
sql.WriteString(` AND a.id = ?`)
|
||||
params = append(params, query.AnnotationId)
|
||||
params = append(params, query.AnnotationID)
|
||||
}
|
||||
|
||||
if query.AlertId != 0 {
|
||||
if query.AlertID != 0 {
|
||||
sql.WriteString(` AND a.alert_id = ?`)
|
||||
params = append(params, query.AlertId)
|
||||
params = append(params, query.AlertID)
|
||||
}
|
||||
|
||||
if query.DashboardId != 0 {
|
||||
if query.DashboardID != 0 {
|
||||
sql.WriteString(` AND a.dashboard_id = ?`)
|
||||
params = append(params, query.DashboardId)
|
||||
params = append(params, query.DashboardID)
|
||||
}
|
||||
|
||||
if query.PanelId != 0 {
|
||||
if query.PanelID != 0 {
|
||||
sql.WriteString(` AND a.panel_id = ?`)
|
||||
params = append(params, query.PanelId)
|
||||
params = append(params, query.PanelID)
|
||||
}
|
||||
|
||||
if query.UserId != 0 {
|
||||
if query.UserID != 0 {
|
||||
sql.WriteString(` AND a.user_id = ?`)
|
||||
params = append(params, query.UserId)
|
||||
params = append(params, query.UserID)
|
||||
}
|
||||
|
||||
if query.From > 0 && query.To > 0 {
|
||||
@ -363,27 +363,27 @@ func (r *xormRepositoryImpl) Delete(ctx context.Context, params *annotations.Del
|
||||
annoTagSQL string
|
||||
)
|
||||
|
||||
r.log.Info("delete", "orgId", params.OrgId)
|
||||
if params.Id != 0 {
|
||||
r.log.Info("delete", "orgId", params.OrgID)
|
||||
if params.ID != 0 {
|
||||
annoTagSQL = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE id = ? AND org_id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE id = ? AND org_id = ?"
|
||||
|
||||
if _, err := sess.Exec(annoTagSQL, params.Id, params.OrgId); err != nil {
|
||||
if _, err := sess.Exec(annoTagSQL, params.ID, params.OrgID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := sess.Exec(sql, params.Id, params.OrgId); err != nil {
|
||||
if _, err := sess.Exec(sql, params.ID, params.OrgID); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
annoTagSQL = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE dashboard_id = ? AND panel_id = ? AND org_id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ? AND org_id = ?"
|
||||
|
||||
if _, err := sess.Exec(annoTagSQL, params.DashboardId, params.PanelId, params.OrgId); err != nil {
|
||||
if _, err := sess.Exec(annoTagSQL, params.DashboardID, params.PanelID, params.OrgID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := sess.Exec(sql, params.DashboardId, params.PanelId, params.OrgId); err != nil {
|
||||
if _, err := sess.Exec(sql, params.DashboardID, params.PanelID, params.OrgID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -82,9 +82,9 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
annotation := &annotations.Item{
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
DashboardId: dashboard.ID,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
DashboardID: dashboard.ID,
|
||||
Text: "hello",
|
||||
Type: "alert",
|
||||
Epoch: 10,
|
||||
@ -93,13 +93,13 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
}
|
||||
err = repo.Add(context.Background(), annotation)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, annotation.Id, int64(0))
|
||||
assert.Greater(t, annotation.ID, int64(0))
|
||||
assert.Equal(t, annotation.Epoch, annotation.EpochEnd)
|
||||
|
||||
annotation2 := &annotations.Item{
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
DashboardId: dashboard2.ID,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
DashboardID: dashboard2.ID,
|
||||
Text: "hello",
|
||||
Type: "alert",
|
||||
Epoch: 21, // Should swap epoch & epochEnd
|
||||
@ -108,13 +108,13 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
}
|
||||
err = repo.Add(context.Background(), annotation2)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, annotation2.Id, int64(0))
|
||||
assert.Greater(t, annotation2.ID, int64(0))
|
||||
assert.Equal(t, int64(20), annotation2.Epoch)
|
||||
assert.Equal(t, int64(21), annotation2.EpochEnd)
|
||||
|
||||
organizationAnnotation1 := &annotations.Item{
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
Text: "deploy",
|
||||
Type: "",
|
||||
Epoch: 15,
|
||||
@ -122,11 +122,11 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
}
|
||||
err = repo.Add(context.Background(), organizationAnnotation1)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, organizationAnnotation1.Id, int64(0))
|
||||
assert.Greater(t, organizationAnnotation1.ID, int64(0))
|
||||
|
||||
globalAnnotation2 := &annotations.Item{
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
Text: "rollback",
|
||||
Type: "",
|
||||
Epoch: 17,
|
||||
@ -134,11 +134,11 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
}
|
||||
err = repo.Add(context.Background(), globalAnnotation2)
|
||||
require.NoError(t, err)
|
||||
assert.Greater(t, globalAnnotation2.Id, int64(0))
|
||||
assert.Greater(t, globalAnnotation2.ID, int64(0))
|
||||
t.Run("Can query for annotation by dashboard id", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: dashboard.ID,
|
||||
OrgID: 1,
|
||||
DashboardID: dashboard.ID,
|
||||
From: 0,
|
||||
To: 15,
|
||||
SignedInUser: testUser,
|
||||
@ -155,8 +155,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
})
|
||||
|
||||
badAnnotation := &annotations.Item{
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
Text: "rollback",
|
||||
Type: "",
|
||||
Epoch: 17,
|
||||
@ -171,7 +171,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items := make([]annotations.Item, count)
|
||||
for i := 0; i < count; i++ {
|
||||
items[i] = annotations.Item{
|
||||
OrgId: 100,
|
||||
OrgID: 100,
|
||||
Type: "batch",
|
||||
Epoch: 12,
|
||||
}
|
||||
@ -180,7 +180,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
err := repo.AddMany(context.Background(), items)
|
||||
|
||||
require.NoError(t, err)
|
||||
query := &annotations.ItemQuery{OrgId: 100, SignedInUser: testUser}
|
||||
query := &annotations.ItemQuery{OrgID: 100, SignedInUser: testUser}
|
||||
inserted, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, inserted, count)
|
||||
@ -196,7 +196,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items := make([]annotations.Item, count)
|
||||
for i := 0; i < count; i++ {
|
||||
items[i] = annotations.Item{
|
||||
OrgId: 101,
|
||||
OrgID: 101,
|
||||
Type: "batch",
|
||||
Epoch: 12,
|
||||
}
|
||||
@ -206,7 +206,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
err := repo.AddMany(context.Background(), items)
|
||||
|
||||
require.NoError(t, err)
|
||||
query := &annotations.ItemQuery{OrgId: 101, SignedInUser: testUser}
|
||||
query := &annotations.ItemQuery{OrgID: 101, SignedInUser: testUser}
|
||||
inserted, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, inserted, count)
|
||||
@ -214,19 +214,19 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Can query for annotation by id", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
AnnotationId: annotation2.Id,
|
||||
OrgID: 1,
|
||||
AnnotationID: annotation2.ID,
|
||||
SignedInUser: testUser,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, items, 1)
|
||||
assert.Equal(t, annotation2.Id, items[0].Id)
|
||||
assert.Equal(t, annotation2.ID, items[0].ID)
|
||||
})
|
||||
|
||||
t.Run("Should not find any when item is outside time range", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 12,
|
||||
To: 15,
|
||||
SignedInUser: testUser,
|
||||
@ -237,8 +237,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Should not find one when tag filter does not match", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 1,
|
||||
To: 15,
|
||||
Tags: []string{"asd"},
|
||||
@ -250,8 +250,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Should not find one when type filter does not match", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 1,
|
||||
To: 15,
|
||||
Type: "alert",
|
||||
@ -263,8 +263,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Should find one when all tag filters does match", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 1,
|
||||
To: 15, // this will exclude the second test annotation
|
||||
Tags: []string{"outage", "error"},
|
||||
@ -276,7 +276,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Should find two annotations using partial match", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
OrgID: 1,
|
||||
From: 1,
|
||||
To: 25,
|
||||
MatchAny: true,
|
||||
@ -289,8 +289,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Should find one when all key value tag filters does match", func(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 1,
|
||||
To: 15,
|
||||
Tags: []string{"type:outage", "server:server-1"},
|
||||
@ -302,8 +302,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Can update annotation and remove all tags", func(t *testing.T) {
|
||||
query := &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 0,
|
||||
To: 15,
|
||||
SignedInUser: testUser,
|
||||
@ -311,10 +311,10 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
annotationId := items[0].Id
|
||||
annotationId := items[0].ID
|
||||
err = repo.Update(context.Background(), &annotations.Item{
|
||||
Id: annotationId,
|
||||
OrgId: 1,
|
||||
ID: annotationId,
|
||||
OrgID: 1,
|
||||
Text: "something new",
|
||||
Tags: []string{},
|
||||
})
|
||||
@ -323,7 +323,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items, err = repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, annotationId, items[0].Id)
|
||||
assert.Equal(t, annotationId, items[0].ID)
|
||||
assert.Empty(t, items[0].Tags)
|
||||
assert.Equal(t, "something new", items[0].Text)
|
||||
data, err := items[0].Data.Map()
|
||||
@ -333,8 +333,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Can update annotation with new tags", func(t *testing.T) {
|
||||
query := &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 0,
|
||||
To: 15,
|
||||
SignedInUser: testUser,
|
||||
@ -342,10 +342,10 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
annotationId := items[0].Id
|
||||
annotationId := items[0].ID
|
||||
err = repo.Update(context.Background(), &annotations.Item{
|
||||
Id: annotationId,
|
||||
OrgId: 1,
|
||||
ID: annotationId,
|
||||
OrgID: 1,
|
||||
Text: "something new",
|
||||
Tags: []string{"newtag1", "newtag2"},
|
||||
})
|
||||
@ -354,7 +354,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items, err = repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, annotationId, items[0].Id)
|
||||
assert.Equal(t, annotationId, items[0].ID)
|
||||
assert.Equal(t, []string{"newtag1", "newtag2"}, items[0].Tags)
|
||||
assert.Equal(t, "something new", items[0].Text)
|
||||
assert.Greater(t, items[0].Updated, items[0].Created)
|
||||
@ -362,8 +362,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Can update annotations with data", func(t *testing.T) {
|
||||
query := &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 0,
|
||||
To: 15,
|
||||
SignedInUser: testUser,
|
||||
@ -371,11 +371,11 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
annotationId := items[0].Id
|
||||
annotationId := items[0].ID
|
||||
data := simplejson.NewFromAny(map[string]interface{}{"data": "I am a data", "data2": "I am also a data"})
|
||||
err = repo.Update(context.Background(), &annotations.Item{
|
||||
Id: annotationId,
|
||||
OrgId: 1,
|
||||
ID: annotationId,
|
||||
OrgID: 1,
|
||||
Text: "something new",
|
||||
Tags: []string{"newtag1", "newtag2"},
|
||||
Data: data,
|
||||
@ -385,7 +385,7 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items, err = repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, annotationId, items[0].Id)
|
||||
assert.Equal(t, annotationId, items[0].ID)
|
||||
assert.Equal(t, []string{"newtag1", "newtag2"}, items[0].Tags)
|
||||
assert.Equal(t, "something new", items[0].Text)
|
||||
assert.Greater(t, items[0].Updated, items[0].Created)
|
||||
@ -394,8 +394,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Can delete annotation", func(t *testing.T) {
|
||||
query := &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
From: 0,
|
||||
To: 15,
|
||||
SignedInUser: testUser,
|
||||
@ -403,8 +403,8 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
items, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
annotationId := items[0].Id
|
||||
err = repo.Delete(context.Background(), &annotations.DeleteParams{Id: annotationId, OrgId: 1})
|
||||
annotationId := items[0].ID
|
||||
err = repo.Delete(context.Background(), &annotations.DeleteParams{ID: annotationId, OrgID: 1})
|
||||
require.NoError(t, err)
|
||||
|
||||
items, err = repo.Get(context.Background(), query)
|
||||
@ -414,29 +414,29 @@ func TestIntegrationAnnotations(t *testing.T) {
|
||||
|
||||
t.Run("Can delete annotation using dashboard id and panel id", func(t *testing.T) {
|
||||
annotation3 := &annotations.Item{
|
||||
OrgId: 1,
|
||||
UserId: 1,
|
||||
DashboardId: dashboard2.ID,
|
||||
OrgID: 1,
|
||||
UserID: 1,
|
||||
DashboardID: dashboard2.ID,
|
||||
Text: "toBeDeletedWithPanelId",
|
||||
Type: "alert",
|
||||
Epoch: 11,
|
||||
Tags: []string{"test"},
|
||||
PanelId: 20,
|
||||
PanelID: 20,
|
||||
}
|
||||
err = repo.Add(context.Background(), annotation3)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
AnnotationId: annotation3.Id,
|
||||
OrgID: 1,
|
||||
AnnotationID: annotation3.ID,
|
||||
SignedInUser: testUser,
|
||||
}
|
||||
items, err := repo.Get(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
|
||||
dashboardId := items[0].DashboardId
|
||||
panelId := items[0].PanelId
|
||||
err = repo.Delete(context.Background(), &annotations.DeleteParams{DashboardId: dashboardId, PanelId: panelId, OrgId: 1})
|
||||
dashboardId := items[0].DashboardID
|
||||
panelId := items[0].PanelID
|
||||
err = repo.Delete(context.Background(), &annotations.DeleteParams{DashboardID: dashboardId, PanelID: panelId, OrgID: 1})
|
||||
require.NoError(t, err)
|
||||
|
||||
items, err = repo.Get(context.Background(), query)
|
||||
@ -523,16 +523,16 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
dash1Annotation := &annotations.Item{
|
||||
OrgId: 1,
|
||||
DashboardId: 1,
|
||||
OrgID: 1,
|
||||
DashboardID: 1,
|
||||
Epoch: 10,
|
||||
}
|
||||
err = repo.Add(context.Background(), dash1Annotation)
|
||||
require.NoError(t, err)
|
||||
|
||||
dash2Annotation := &annotations.Item{
|
||||
OrgId: 1,
|
||||
DashboardId: 2,
|
||||
OrgID: 1,
|
||||
DashboardID: 2,
|
||||
Epoch: 10,
|
||||
Tags: []string{"foo:bar"},
|
||||
}
|
||||
@ -540,7 +540,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
organizationAnnotation := &annotations.Item{
|
||||
OrgId: 1,
|
||||
OrgID: 1,
|
||||
Epoch: 10,
|
||||
}
|
||||
err = repo.Add(context.Background(), organizationAnnotation)
|
||||
@ -566,7 +566,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
accesscontrol.ActionAnnotationsRead: {accesscontrol.ScopeAnnotationsAll},
|
||||
dashboards.ActionDashboardsRead: {dashboards.ScopeDashboardsAll},
|
||||
},
|
||||
expectedAnnotationIds: []int64{dash1Annotation.Id, dash2Annotation.Id, organizationAnnotation.Id},
|
||||
expectedAnnotationIds: []int64{dash1Annotation.ID, dash2Annotation.ID, organizationAnnotation.ID},
|
||||
},
|
||||
{
|
||||
description: "Should find all dashboard annotations",
|
||||
@ -574,7 +574,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
accesscontrol.ActionAnnotationsRead: {accesscontrol.ScopeAnnotationsTypeDashboard},
|
||||
dashboards.ActionDashboardsRead: {dashboards.ScopeDashboardsAll},
|
||||
},
|
||||
expectedAnnotationIds: []int64{dash1Annotation.Id, dash2Annotation.Id},
|
||||
expectedAnnotationIds: []int64{dash1Annotation.ID, dash2Annotation.ID},
|
||||
},
|
||||
{
|
||||
description: "Should find only annotations from dashboards that user can read",
|
||||
@ -582,7 +582,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
accesscontrol.ActionAnnotationsRead: {accesscontrol.ScopeAnnotationsTypeDashboard},
|
||||
dashboards.ActionDashboardsRead: {fmt.Sprintf("dashboards:uid:%s", dash1UID)},
|
||||
},
|
||||
expectedAnnotationIds: []int64{dash1Annotation.Id},
|
||||
expectedAnnotationIds: []int64{dash1Annotation.ID},
|
||||
},
|
||||
{
|
||||
description: "Should find no annotations if user can't view dashboards or organization annotations",
|
||||
@ -597,7 +597,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
accesscontrol.ActionAnnotationsRead: {accesscontrol.ScopeAnnotationsTypeOrganization},
|
||||
dashboards.ActionDashboardsRead: {dashboards.ScopeDashboardsAll},
|
||||
},
|
||||
expectedAnnotationIds: []int64{organizationAnnotation.Id},
|
||||
expectedAnnotationIds: []int64{organizationAnnotation.ID},
|
||||
},
|
||||
{
|
||||
description: "Should error if user doesn't have annotation read permissions",
|
||||
@ -614,7 +614,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
setupRBACPermission(t, repo, role, user)
|
||||
|
||||
results, err := repo.Get(context.Background(), &annotations.ItemQuery{
|
||||
OrgId: 1,
|
||||
OrgID: 1,
|
||||
SignedInUser: user,
|
||||
})
|
||||
if tc.expectedError {
|
||||
@ -624,7 +624,7 @@ func TestIntegrationAnnotationListingWithRBAC(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, results, len(tc.expectedAnnotationIds))
|
||||
for _, r := range results {
|
||||
assert.Contains(t, tc.expectedAnnotationIds, r.Id)
|
||||
assert.Contains(t, tc.expectedAnnotationIds, r.ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -22,12 +22,12 @@ func (repo *fakeAnnotationsRepo) Delete(_ context.Context, params *annotations.D
|
||||
repo.mtx.Lock()
|
||||
defer repo.mtx.Unlock()
|
||||
|
||||
if params.Id != 0 {
|
||||
delete(repo.annotations, params.Id)
|
||||
if params.ID != 0 {
|
||||
delete(repo.annotations, params.ID)
|
||||
} else {
|
||||
for _, v := range repo.annotations {
|
||||
if params.DashboardId == v.DashboardId && params.PanelId == v.PanelId {
|
||||
delete(repo.annotations, v.Id)
|
||||
if params.DashboardID == v.DashboardID && params.PanelID == v.PanelID {
|
||||
delete(repo.annotations, v.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -39,10 +39,10 @@ func (repo *fakeAnnotationsRepo) Save(ctx context.Context, item *annotations.Ite
|
||||
repo.mtx.Lock()
|
||||
defer repo.mtx.Unlock()
|
||||
|
||||
if item.Id == 0 {
|
||||
item.Id = int64(len(repo.annotations) + 1)
|
||||
if item.ID == 0 {
|
||||
item.ID = int64(len(repo.annotations) + 1)
|
||||
}
|
||||
repo.annotations[item.Id] = *item
|
||||
repo.annotations[item.ID] = *item
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -52,10 +52,10 @@ func (repo *fakeAnnotationsRepo) SaveMany(ctx context.Context, items []annotatio
|
||||
defer repo.mtx.Unlock()
|
||||
|
||||
for _, i := range items {
|
||||
if i.Id == 0 {
|
||||
i.Id = int64(len(repo.annotations) + 1)
|
||||
if i.ID == 0 {
|
||||
i.ID = int64(len(repo.annotations) + 1)
|
||||
}
|
||||
repo.annotations[i.Id] = i
|
||||
repo.annotations[i.ID] = i
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -69,10 +69,10 @@ func (repo *fakeAnnotationsRepo) Find(_ context.Context, query *annotations.Item
|
||||
repo.mtx.Lock()
|
||||
defer repo.mtx.Unlock()
|
||||
|
||||
if annotation, has := repo.annotations[query.AnnotationId]; has {
|
||||
return []*annotations.ItemDTO{{Id: annotation.Id, DashboardId: annotation.DashboardId}}, nil
|
||||
if annotation, has := repo.annotations[query.AnnotationID]; has {
|
||||
return []*annotations.ItemDTO{{ID: annotation.ID, DashboardID: annotation.DashboardID}}, nil
|
||||
}
|
||||
annotations := []*annotations.ItemDTO{{Id: 1, DashboardId: 0}}
|
||||
annotations := []*annotations.ItemDTO{{ID: 1, DashboardID: 0}}
|
||||
return annotations, nil
|
||||
}
|
||||
|
||||
|
@ -6,15 +6,15 @@ import (
|
||||
)
|
||||
|
||||
type ItemQuery struct {
|
||||
OrgId int64 `json:"orgId"`
|
||||
OrgID int64 `json:"orgId"`
|
||||
From int64 `json:"from"`
|
||||
To int64 `json:"to"`
|
||||
UserId int64 `json:"userId"`
|
||||
AlertId int64 `json:"alertId"`
|
||||
DashboardId int64 `json:"dashboardId"`
|
||||
DashboardUid string `json:"dashboardUID"`
|
||||
PanelId int64 `json:"panelId"`
|
||||
AnnotationId int64 `json:"annotationId"`
|
||||
UserID int64 `json:"userId"`
|
||||
AlertID int64 `json:"alertId"`
|
||||
DashboardID int64 `json:"dashboardId"`
|
||||
DashboardUID string `json:"dashboardUID"`
|
||||
PanelID int64 `json:"panelId"`
|
||||
AnnotationID int64 `json:"annotationId"`
|
||||
Tags []string `json:"tags"`
|
||||
Type string `json:"type"`
|
||||
MatchAny bool `json:"matchAny"`
|
||||
@ -55,20 +55,20 @@ type GetAnnotationTagsResponse struct {
|
||||
}
|
||||
|
||||
type DeleteParams struct {
|
||||
OrgId int64
|
||||
Id int64
|
||||
DashboardId int64
|
||||
PanelId int64
|
||||
OrgID int64
|
||||
ID int64
|
||||
DashboardID int64
|
||||
PanelID int64
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Id int64 `json:"id" xorm:"pk autoincr 'id'"`
|
||||
OrgId int64 `json:"orgId"`
|
||||
UserId int64 `json:"userId"`
|
||||
DashboardId int64 `json:"dashboardId"`
|
||||
PanelId int64 `json:"panelId"`
|
||||
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
|
||||
OrgID int64 `json:"orgId" xorm:"org_id"`
|
||||
UserID int64 `json:"userId" xorm:"user_id"`
|
||||
DashboardID int64 `json:"dashboardId" xorm:"dashboard_id"`
|
||||
PanelID int64 `json:"panelId" xorm:"panel_id"`
|
||||
Text string `json:"text"`
|
||||
AlertId int64 `json:"alertId"`
|
||||
AlertID int64 `json:"alertId" xorm:"alert_id"`
|
||||
PrevState string `json:"prevState"`
|
||||
NewState string `json:"newState"`
|
||||
Epoch int64 `json:"epoch"`
|
||||
@ -88,13 +88,13 @@ func (i Item) TableName() string {
|
||||
}
|
||||
|
||||
type ItemDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
AlertId int64 `json:"alertId"`
|
||||
ID int64 `json:"id" xorm:"id"`
|
||||
AlertID int64 `json:"alertId" xorm:"alert_id"`
|
||||
AlertName string `json:"alertName"`
|
||||
DashboardId int64 `json:"dashboardId"`
|
||||
DashboardUID *string `json:"dashboardUID"`
|
||||
PanelId int64 `json:"panelId"`
|
||||
UserId int64 `json:"userId"`
|
||||
DashboardID int64 `json:"dashboardId" xorm:"dashboard_id"`
|
||||
DashboardUID *string `json:"dashboardUID" xorm:"dashboard_uid"`
|
||||
PanelID int64 `json:"panelId" xorm:"panel_id"`
|
||||
UserID int64 `json:"userId" xorm:"user_id"`
|
||||
NewState string `json:"newState"`
|
||||
PrevState string `json:"prevState"`
|
||||
Created int64 `json:"created"`
|
||||
@ -105,7 +105,7 @@ type ItemDTO struct {
|
||||
Tags []string `json:"tags"`
|
||||
Login string `json:"login"`
|
||||
Email string `json:"email"`
|
||||
AvatarUrl string `json:"avatarUrl"`
|
||||
AvatarURL string `json:"avatarUrl" xorm:"avatar_url"`
|
||||
Data *simplejson.Json `json:"data"`
|
||||
}
|
||||
|
||||
@ -128,7 +128,7 @@ func (a annotationType) String() string {
|
||||
}
|
||||
|
||||
func (annotation *ItemDTO) GetType() annotationType {
|
||||
if annotation.DashboardId != 0 {
|
||||
if annotation.DashboardID != 0 {
|
||||
return Dashboard
|
||||
}
|
||||
return Organization
|
||||
|
@ -26,10 +26,10 @@ func (ss *sqlxStore) GetAPIKeys(ctx context.Context, query *apikey.GetApiKeysQue
|
||||
|
||||
if query.IncludeExpired {
|
||||
where = append(where, "org_id=?")
|
||||
args = append(args, query.OrgId)
|
||||
args = append(args, query.OrgID)
|
||||
} else {
|
||||
where = append(where, "org_id=? and ( expires IS NULL or expires >= ?)")
|
||||
args = append(args, query.OrgId, timeNow().Unix())
|
||||
args = append(args, query.OrgID, timeNow().Unix())
|
||||
}
|
||||
|
||||
where = append(where, "service_account_id IS NULL")
|
||||
@ -64,7 +64,7 @@ func (ss *sqlxStore) GetAllAPIKeys(ctx context.Context, orgID int64) ([]*apikey.
|
||||
}
|
||||
|
||||
func (ss *sqlxStore) DeleteApiKey(ctx context.Context, cmd *apikey.DeleteCommand) error {
|
||||
res, err := ss.sess.Exec(ctx, "DELETE FROM api_key WHERE id=? and org_id=? and service_account_id IS NULL", cmd.Id, cmd.OrgId)
|
||||
res, err := ss.sess.Exec(ctx, "DELETE FROM api_key WHERE id=? and org_id=? and service_account_id IS NULL", cmd.ID, cmd.OrgID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -85,14 +85,14 @@ func (ss *sqlxStore) AddAPIKey(ctx context.Context, cmd *apikey.AddCommand) erro
|
||||
return apikey.ErrInvalidExpiration
|
||||
}
|
||||
|
||||
err := ss.GetApiKeyByName(ctx, &apikey.GetByNameQuery{OrgId: cmd.OrgId, KeyName: cmd.Name})
|
||||
err := ss.GetApiKeyByName(ctx, &apikey.GetByNameQuery{OrgID: cmd.OrgID, KeyName: cmd.Name})
|
||||
// If key with the same orgId and name already exist return err
|
||||
if !errors.Is(err, apikey.ErrInvalid) {
|
||||
return apikey.ErrDuplicate
|
||||
}
|
||||
isRevoked := false
|
||||
t := apikey.APIKey{
|
||||
OrgId: cmd.OrgId,
|
||||
OrgID: cmd.OrgID,
|
||||
Name: cmd.Name,
|
||||
Role: cmd.Role,
|
||||
Key: cmd.Key,
|
||||
@ -103,15 +103,15 @@ func (ss *sqlxStore) AddAPIKey(ctx context.Context, cmd *apikey.AddCommand) erro
|
||||
IsRevoked: &isRevoked,
|
||||
}
|
||||
|
||||
t.Id, err = ss.sess.ExecWithReturningId(ctx,
|
||||
`INSERT INTO api_key (org_id, name, role, "key", created, updated, expires, service_account_id, is_revoked) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, t.OrgId, t.Name, t.Role, t.Key, t.Created, t.Updated, t.Expires, t.ServiceAccountId, t.IsRevoked)
|
||||
t.ID, err = ss.sess.ExecWithReturningId(ctx,
|
||||
`INSERT INTO api_key (org_id, name, role, "key", created, updated, expires, service_account_id, is_revoked) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, t.OrgID, t.Name, t.Role, t.Key, t.Created, t.Updated, t.Expires, t.ServiceAccountId, t.IsRevoked)
|
||||
cmd.Result = &t
|
||||
return err
|
||||
}
|
||||
|
||||
func (ss *sqlxStore) GetApiKeyById(ctx context.Context, query *apikey.GetByIDQuery) error {
|
||||
var key apikey.APIKey
|
||||
err := ss.sess.Get(ctx, &key, "SELECT * FROM api_key WHERE id=?", query.ApiKeyId)
|
||||
err := ss.sess.Get(ctx, &key, "SELECT * FROM api_key WHERE id=?", query.ApiKeyID)
|
||||
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
||||
return apikey.ErrInvalid
|
||||
}
|
||||
@ -121,7 +121,7 @@ func (ss *sqlxStore) GetApiKeyById(ctx context.Context, query *apikey.GetByIDQue
|
||||
|
||||
func (ss *sqlxStore) GetApiKeyByName(ctx context.Context, query *apikey.GetByNameQuery) error {
|
||||
var key apikey.APIKey
|
||||
err := ss.sess.Get(ctx, &key, "SELECT * FROM api_key WHERE org_id=? AND name=?", query.OrgId, query.KeyName)
|
||||
err := ss.sess.Get(ctx, &key, "SELECT * FROM api_key WHERE org_id=? AND name=?", query.OrgID, query.KeyName)
|
||||
if err != nil && errors.Is(err, sql.ErrNoRows) {
|
||||
return apikey.ErrInvalid
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func seedApiKeys(t *testing.T, store store, num int) {
|
||||
err := store.AddAPIKey(context.Background(), &apikey.AddCommand{
|
||||
Name: fmt.Sprintf("key:%d", i),
|
||||
Key: fmt.Sprintf("key:%d", i),
|
||||
OrgId: 1,
|
||||
OrgID: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
@ -63,12 +63,12 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
ss := fn(db, db.Cfg)
|
||||
|
||||
t.Run("Given saved api key", func(t *testing.T) {
|
||||
cmd := apikey.AddCommand{OrgId: 1, Name: "hello", Key: "asd"}
|
||||
cmd := apikey.AddCommand{OrgID: 1, Name: "hello", Key: "asd"}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Should be able to get key by name", func(t *testing.T) {
|
||||
query := apikey.GetByNameQuery{KeyName: "hello", OrgId: 1}
|
||||
query := apikey.GetByNameQuery{KeyName: "hello", OrgID: 1}
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
|
||||
assert.Nil(t, err)
|
||||
@ -84,17 +84,17 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
t.Run("Should be able to delete key by id", func(t *testing.T) {
|
||||
key, err := ss.GetAPIKeyByHash(context.Background(), cmd.Key)
|
||||
assert.NoError(t, err)
|
||||
err = ss.DeleteApiKey(context.Background(), &apikey.DeleteCommand{Id: key.Id, OrgId: key.OrgId})
|
||||
err = ss.DeleteApiKey(context.Background(), &apikey.DeleteCommand{ID: key.ID, OrgID: key.OrgID})
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Add non expiring key", func(t *testing.T) {
|
||||
cmd := apikey.AddCommand{OrgId: 1, Name: "non-expiring", Key: "asd1", SecondsToLive: 0}
|
||||
cmd := apikey.AddCommand{OrgID: 1, Name: "non-expiring", Key: "asd1", SecondsToLive: 0}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
query := apikey.GetByNameQuery{KeyName: "non-expiring", OrgId: 1}
|
||||
query := apikey.GetByNameQuery{KeyName: "non-expiring", OrgID: 1}
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, query.Result.Expires)
|
||||
@ -102,11 +102,11 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
|
||||
t.Run("Add an expiring key", func(t *testing.T) {
|
||||
// expires in one hour
|
||||
cmd := apikey.AddCommand{OrgId: 1, Name: "expiring-in-an-hour", Key: "asd2", SecondsToLive: 3600}
|
||||
cmd := apikey.AddCommand{OrgID: 1, Name: "expiring-in-an-hour", Key: "asd2", SecondsToLive: 3600}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
query := apikey.GetByNameQuery{KeyName: "expiring-in-an-hour", OrgId: 1}
|
||||
query := apikey.GetByNameQuery{KeyName: "expiring-in-an-hour", OrgID: 1}
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@ -122,16 +122,16 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
|
||||
t.Run("Last Used At datetime update", func(t *testing.T) {
|
||||
// expires in one hour
|
||||
cmd := apikey.AddCommand{OrgId: 1, Name: "last-update-at", Key: "asd3", SecondsToLive: 3600}
|
||||
cmd := apikey.AddCommand{OrgID: 1, Name: "last-update-at", Key: "asd3", SecondsToLive: 3600}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Nil(t, cmd.Result.LastUsedAt)
|
||||
|
||||
err = ss.UpdateAPIKeyLastUsedDate(context.Background(), cmd.Result.Id)
|
||||
err = ss.UpdateAPIKeyLastUsedDate(context.Background(), cmd.Result.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
query := apikey.GetByNameQuery{KeyName: "last-update-at", OrgId: 1}
|
||||
query := apikey.GetByNameQuery{KeyName: "last-update-at", OrgID: 1}
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, query.Result.LastUsedAt)
|
||||
@ -139,28 +139,28 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
|
||||
t.Run("Add a key with negative lifespan", func(t *testing.T) {
|
||||
// expires in one day
|
||||
cmd := apikey.AddCommand{OrgId: 1, Name: "key-with-negative-lifespan", Key: "asd3", SecondsToLive: -3600}
|
||||
cmd := apikey.AddCommand{OrgID: 1, Name: "key-with-negative-lifespan", Key: "asd3", SecondsToLive: -3600}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.EqualError(t, err, apikey.ErrInvalidExpiration.Error())
|
||||
|
||||
query := apikey.GetByNameQuery{KeyName: "key-with-negative-lifespan", OrgId: 1}
|
||||
query := apikey.GetByNameQuery{KeyName: "key-with-negative-lifespan", OrgID: 1}
|
||||
err = ss.GetApiKeyByName(context.Background(), &query)
|
||||
assert.EqualError(t, err, "invalid API key")
|
||||
})
|
||||
|
||||
t.Run("Add keys", func(t *testing.T) {
|
||||
// never expires
|
||||
cmd := apikey.AddCommand{OrgId: 1, Name: "key1", Key: "key1", SecondsToLive: 0}
|
||||
cmd := apikey.AddCommand{OrgID: 1, Name: "key1", Key: "key1", SecondsToLive: 0}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// expires in 1s
|
||||
cmd = apikey.AddCommand{OrgId: 1, Name: "key2", Key: "key2", SecondsToLive: 1}
|
||||
cmd = apikey.AddCommand{OrgID: 1, Name: "key2", Key: "key2", SecondsToLive: 1}
|
||||
err = ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// expires in one hour
|
||||
cmd = apikey.AddCommand{OrgId: 1, Name: "key3", Key: "key3", SecondsToLive: 3600}
|
||||
cmd = apikey.AddCommand{OrgID: 1, Name: "key3", Key: "key3", SecondsToLive: 3600}
|
||||
err = ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@ -173,7 +173,7 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
1: {accesscontrol.ActionAPIKeyRead: []string{accesscontrol.ScopeAPIKeysAll}},
|
||||
},
|
||||
}
|
||||
query := apikey.GetApiKeysQuery{OrgId: 1, IncludeExpired: false, User: testUser}
|
||||
query := apikey.GetApiKeysQuery{OrgID: 1, IncludeExpired: false, User: testUser}
|
||||
err = ss.GetAPIKeys(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@ -183,7 +183,7 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
}
|
||||
}
|
||||
|
||||
query = apikey.GetApiKeysQuery{OrgId: 1, IncludeExpired: true, User: testUser}
|
||||
query = apikey.GetApiKeysQuery{OrgID: 1, IncludeExpired: true, User: testUser}
|
||||
err = ss.GetAPIKeys(context.Background(), &query)
|
||||
assert.Nil(t, err)
|
||||
|
||||
@ -202,7 +202,7 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
ss := fn(db, db.Cfg)
|
||||
|
||||
t.Run("Delete non-existing key should return error", func(t *testing.T) {
|
||||
cmd := apikey.DeleteCommand{Id: 1}
|
||||
cmd := apikey.DeleteCommand{ID: 1}
|
||||
err := ss.DeleteApiKey(context.Background(), &cmd)
|
||||
|
||||
assert.EqualError(t, err, apikey.ErrNotFound.Error())
|
||||
@ -210,12 +210,12 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
|
||||
t.Run("Testing API Duplicate Key Errors", func(t *testing.T) {
|
||||
t.Run("Given saved api key", func(t *testing.T) {
|
||||
cmd := apikey.AddCommand{OrgId: 0, Name: "duplicate", Key: "asd"}
|
||||
cmd := apikey.AddCommand{OrgID: 0, Name: "duplicate", Key: "asd"}
|
||||
err := ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.Nil(t, err)
|
||||
|
||||
t.Run("Add API Key with existing Org ID and Name", func(t *testing.T) {
|
||||
cmd := apikey.AddCommand{OrgId: 0, Name: "duplicate", Key: "asd"}
|
||||
cmd := apikey.AddCommand{OrgID: 0, Name: "duplicate", Key: "asd"}
|
||||
err = ss.AddAPIKey(context.Background(), &cmd)
|
||||
assert.EqualError(t, err, apikey.ErrDuplicate.Error())
|
||||
})
|
||||
@ -257,7 +257,7 @@ func testIntegrationApiKeyDataAccess(t *testing.T, fn getStore) {
|
||||
store := fn(db, db.Cfg)
|
||||
seedApiKeys(t, store, 10)
|
||||
|
||||
query := &apikey.GetApiKeysQuery{OrgId: 1, User: tt.user}
|
||||
query := &apikey.GetApiKeysQuery{OrgID: 1, User: tt.user}
|
||||
err := store.GetAPIKeys(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, query.Result, tt.expectedNumKeys)
|
||||
|
@ -30,11 +30,11 @@ func (ss *sqlStore) GetAPIKeys(ctx context.Context, query *apikey.GetApiKeysQuer
|
||||
|
||||
if query.IncludeExpired {
|
||||
sess = dbSession.Limit(100, 0).
|
||||
Where("org_id=?", query.OrgId).
|
||||
Where("org_id=?", query.OrgID).
|
||||
Asc("name")
|
||||
} else {
|
||||
sess = dbSession.Limit(100, 0).
|
||||
Where("org_id=? and ( expires IS NULL or expires >= ?)", query.OrgId, timeNow().Unix()).
|
||||
Where("org_id=? and ( expires IS NULL or expires >= ?)", query.OrgID, timeNow().Unix()).
|
||||
Asc("name")
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ func (ss *sqlStore) GetAllAPIKeys(ctx context.Context, orgID int64) ([]*apikey.A
|
||||
func (ss *sqlStore) DeleteApiKey(ctx context.Context, cmd *apikey.DeleteCommand) error {
|
||||
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
rawSQL := "DELETE FROM api_key WHERE id=? and org_id=? and service_account_id IS NULL"
|
||||
result, err := sess.Exec(rawSQL, cmd.Id, cmd.OrgId)
|
||||
result, err := sess.Exec(rawSQL, cmd.ID, cmd.OrgID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -84,7 +84,7 @@ func (ss *sqlStore) DeleteApiKey(ctx context.Context, cmd *apikey.DeleteCommand)
|
||||
|
||||
func (ss *sqlStore) AddAPIKey(ctx context.Context, cmd *apikey.AddCommand) error {
|
||||
return ss.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||
key := apikey.APIKey{OrgId: cmd.OrgId, Name: cmd.Name}
|
||||
key := apikey.APIKey{OrgID: cmd.OrgID, Name: cmd.Name}
|
||||
exists, _ := sess.Get(&key)
|
||||
if exists {
|
||||
return apikey.ErrDuplicate
|
||||
@ -101,7 +101,7 @@ func (ss *sqlStore) AddAPIKey(ctx context.Context, cmd *apikey.AddCommand) error
|
||||
|
||||
isRevoked := false
|
||||
t := apikey.APIKey{
|
||||
OrgId: cmd.OrgId,
|
||||
OrgID: cmd.OrgID,
|
||||
Name: cmd.Name,
|
||||
Role: cmd.Role,
|
||||
Key: cmd.Key,
|
||||
@ -123,7 +123,7 @@ func (ss *sqlStore) AddAPIKey(ctx context.Context, cmd *apikey.AddCommand) error
|
||||
func (ss *sqlStore) GetApiKeyById(ctx context.Context, query *apikey.GetByIDQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var key apikey.APIKey
|
||||
has, err := sess.ID(query.ApiKeyId).Get(&key)
|
||||
has, err := sess.ID(query.ApiKeyID).Get(&key)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
@ -139,7 +139,7 @@ func (ss *sqlStore) GetApiKeyById(ctx context.Context, query *apikey.GetByIDQuer
|
||||
func (ss *sqlStore) GetApiKeyByName(ctx context.Context, query *apikey.GetByNameQuery) error {
|
||||
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
var key apikey.APIKey
|
||||
has, err := sess.Where("org_id=? AND name=?", query.OrgId, query.KeyName).Get(&key)
|
||||
has, err := sess.Where("org_id=? AND name=?", query.OrgID, query.KeyName).Get(&key)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -17,8 +17,8 @@ var (
|
||||
)
|
||||
|
||||
type APIKey struct {
|
||||
Id int64 `db:"id"`
|
||||
OrgId int64 `db:"org_id"`
|
||||
ID int64 `db:"id" xorm:"pk autoincr 'id'"`
|
||||
OrgID int64 `db:"org_id" xorm:"org_id"`
|
||||
Name string `db:"name"`
|
||||
Key string `db:"key"`
|
||||
Role org.RoleType `db:"role"`
|
||||
@ -36,7 +36,7 @@ func (k APIKey) TableName() string { return "api_key" }
|
||||
type AddCommand struct {
|
||||
Name string `json:"name" binding:"Required"`
|
||||
Role org.RoleType `json:"role" binding:"Required"`
|
||||
OrgId int64 `json:"-"`
|
||||
OrgID int64 `json:"-" xorm:"org_id"`
|
||||
Key string `json:"-"`
|
||||
SecondsToLive int64 `json:"secondsToLive"`
|
||||
ServiceAccountID *int64 `json:"-"`
|
||||
@ -45,24 +45,24 @@ type AddCommand struct {
|
||||
}
|
||||
|
||||
type DeleteCommand struct {
|
||||
Id int64 `json:"id"`
|
||||
OrgId int64 `json:"-"`
|
||||
ID int64 `json:"id"`
|
||||
OrgID int64 `json:"-"`
|
||||
}
|
||||
|
||||
type GetApiKeysQuery struct {
|
||||
OrgId int64
|
||||
OrgID int64
|
||||
IncludeExpired bool
|
||||
User *user.SignedInUser
|
||||
Result []*APIKey
|
||||
}
|
||||
type GetByNameQuery struct {
|
||||
KeyName string
|
||||
OrgId int64
|
||||
OrgID int64
|
||||
Result *APIKey
|
||||
}
|
||||
|
||||
type GetByIDQuery struct {
|
||||
ApiKeyId int64
|
||||
ApiKeyID int64
|
||||
Result *APIKey
|
||||
}
|
||||
|
||||
|
@ -63,15 +63,15 @@ func (s *APIKey) Authenticate(ctx context.Context, r *authn.Request) (*authn.Ide
|
||||
// if the api key don't belong to a service account construct the identity and return it
|
||||
if apiKey.ServiceAccountId == nil || *apiKey.ServiceAccountId < 1 {
|
||||
return &authn.Identity{
|
||||
ID: authn.NamespacedID(authn.NamespaceAPIKey, apiKey.Id),
|
||||
OrgID: apiKey.OrgId,
|
||||
OrgRoles: map[int64]org.RoleType{apiKey.OrgId: apiKey.Role},
|
||||
ID: authn.NamespacedID(authn.NamespaceAPIKey, apiKey.ID),
|
||||
OrgID: apiKey.OrgID,
|
||||
OrgRoles: map[int64]org.RoleType{apiKey.OrgID: apiKey.Role},
|
||||
}, nil
|
||||
}
|
||||
|
||||
usr, err := s.userService.GetSignedInUserWithCacheCtx(ctx, &user.GetSignedInUserQuery{
|
||||
UserID: *apiKey.ServiceAccountId,
|
||||
OrgID: apiKey.OrgId,
|
||||
OrgID: apiKey.OrgID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -116,7 +116,7 @@ func (s *APIKey) getFromTokenLegacy(ctx context.Context, token string) (*apikey.
|
||||
}
|
||||
|
||||
// fetch key
|
||||
keyQuery := apikey.GetByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
|
||||
keyQuery := apikey.GetByNameQuery{KeyName: decoded.Name, OrgID: decoded.OrgId}
|
||||
if err := s.apiKeyService.GetApiKeyByName(ctx, &keyQuery); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -43,8 +43,8 @@ func TestAPIKey_Authenticate(t *testing.T) {
|
||||
},
|
||||
}},
|
||||
expectedKey: &apikey.APIKey{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
Key: hash,
|
||||
Role: org.RoleAdmin,
|
||||
},
|
||||
@ -62,8 +62,8 @@ func TestAPIKey_Authenticate(t *testing.T) {
|
||||
},
|
||||
}},
|
||||
expectedKey: &apikey.APIKey{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
Key: hash,
|
||||
ServiceAccountId: intPtr(1),
|
||||
},
|
||||
|
@ -73,11 +73,11 @@ func (c *PermissionChecker) CheckReadPermissions(ctx context.Context, orgId int6
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
items, err := c.annotationsRepo.Find(ctx, &annotations.ItemQuery{AnnotationId: annotationID, OrgId: orgId, SignedInUser: signedInUser})
|
||||
items, err := c.annotationsRepo.Find(ctx, &annotations.ItemQuery{AnnotationID: annotationID, OrgID: orgId, SignedInUser: signedInUser})
|
||||
if err != nil || len(items) != 1 {
|
||||
return false, nil
|
||||
}
|
||||
dashboardID := items[0].DashboardId
|
||||
dashboardID := items[0].DashboardID
|
||||
if dashboardID == 0 {
|
||||
return false, nil
|
||||
}
|
||||
@ -131,11 +131,11 @@ func (c *PermissionChecker) CheckWritePermissions(ctx context.Context, orgId int
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
items, err := c.annotationsRepo.Find(ctx, &annotations.ItemQuery{AnnotationId: annotationID, OrgId: orgId, SignedInUser: signedInUser})
|
||||
items, err := c.annotationsRepo.Find(ctx, &annotations.ItemQuery{AnnotationID: annotationID, OrgID: orgId, SignedInUser: signedInUser})
|
||||
if err != nil || len(items) != 1 {
|
||||
return false, nil
|
||||
}
|
||||
dashboardID := items[0].DashboardId
|
||||
dashboardID := items[0].DashboardID
|
||||
if dashboardID == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
@ -270,7 +270,7 @@ func (h *ContextHandler) getAPIKey(ctx context.Context, keyString string) (*apik
|
||||
}
|
||||
|
||||
// fetch key
|
||||
keyQuery := apikey.GetByNameQuery{KeyName: decoded.Name, OrgId: decoded.OrgId}
|
||||
keyQuery := apikey.GetByNameQuery{KeyName: decoded.Name, OrgID: decoded.OrgId}
|
||||
if err := h.apiKeyService.GetApiKeyByName(ctx, &keyQuery); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -357,15 +357,15 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *contextmodel.ReqConte
|
||||
if err := h.apiKeyService.UpdateAPIKeyLastUsedDate(context.Background(), id); err != nil {
|
||||
reqContext.Logger.Warn("failed to update last use date for api key", "id", id)
|
||||
}
|
||||
}(apiKey.Id)
|
||||
}(apiKey.ID)
|
||||
|
||||
if apiKey.ServiceAccountId == nil || *apiKey.ServiceAccountId < 1 { //There is no service account attached to the apikey
|
||||
// Use the old APIkey method. This provides backwards compatibility.
|
||||
// will probably have to be supported for a long time.
|
||||
reqContext.SignedInUser = &user.SignedInUser{}
|
||||
reqContext.OrgRole = apiKey.Role
|
||||
reqContext.ApiKeyID = apiKey.Id
|
||||
reqContext.OrgID = apiKey.OrgId
|
||||
reqContext.ApiKeyID = apiKey.ID
|
||||
reqContext.OrgID = apiKey.OrgID
|
||||
reqContext.IsSignedIn = true
|
||||
return true
|
||||
}
|
||||
@ -373,7 +373,7 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *contextmodel.ReqConte
|
||||
//There is a service account attached to the API key
|
||||
|
||||
//Use service account linked to API key as the signed in user
|
||||
querySignedInUser := user.GetSignedInUserQuery{UserID: *apiKey.ServiceAccountId, OrgID: apiKey.OrgId}
|
||||
querySignedInUser := user.GetSignedInUserQuery{UserID: *apiKey.ServiceAccountId, OrgID: apiKey.OrgID}
|
||||
querySignedInUserResult, err := h.userService.GetSignedInUserWithCacheCtx(reqContext.Req.Context(), &querySignedInUser)
|
||||
if err != nil {
|
||||
reqContext.Logger.Error(
|
||||
|
@ -98,7 +98,7 @@ func (a *authenticator) getSignedInUser(ctx context.Context, token string) (*use
|
||||
return nil, status.Error(codes.Unauthenticated, "api key does not have a service account")
|
||||
}
|
||||
|
||||
querySignedInUser := user.GetSignedInUserQuery{UserID: *apikey.ServiceAccountId, OrgID: apikey.OrgId}
|
||||
querySignedInUser := user.GetSignedInUserQuery{UserID: *apikey.ServiceAccountId, OrgID: apikey.OrgID}
|
||||
signedInUser, err := a.UserService.GetSignedInUserWithCacheCtx(ctx, &querySignedInUser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -22,8 +22,8 @@ func TestAuthenticator_Authenticate(t *testing.T) {
|
||||
serviceAccountId := int64(1)
|
||||
t.Run("accepts service api key with admin role", func(t *testing.T) {
|
||||
s := newFakeAPIKey(&apikey.APIKey{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
Key: "admin-api-key",
|
||||
Name: "Admin API Key",
|
||||
ServiceAccountId: &serviceAccountId,
|
||||
@ -38,8 +38,8 @@ func TestAuthenticator_Authenticate(t *testing.T) {
|
||||
|
||||
t.Run("rejects non-admin role", func(t *testing.T) {
|
||||
s := newFakeAPIKey(&apikey.APIKey{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
Key: "admin-api-key",
|
||||
Name: "Admin API Key",
|
||||
ServiceAccountId: &serviceAccountId,
|
||||
@ -54,8 +54,8 @@ func TestAuthenticator_Authenticate(t *testing.T) {
|
||||
|
||||
t.Run("removes auth header from context", func(t *testing.T) {
|
||||
s := newFakeAPIKey(&apikey.APIKey{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
Key: "admin-api-key",
|
||||
Name: "Admin API Key",
|
||||
ServiceAccountId: &serviceAccountId,
|
||||
@ -76,8 +76,8 @@ func TestAuthenticator_Authenticate(t *testing.T) {
|
||||
|
||||
t.Run("sets SignInUser", func(t *testing.T) {
|
||||
s := newFakeAPIKey(&apikey.APIKey{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
Key: "admin-api-key",
|
||||
Name: "Admin API Key",
|
||||
ServiceAccountId: &serviceAccountId,
|
||||
@ -94,8 +94,8 @@ func TestAuthenticator_Authenticate(t *testing.T) {
|
||||
|
||||
t.Run("sets SignInUser permissions", func(t *testing.T) {
|
||||
s := newFakeAPIKey(&apikey.APIKey{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
Key: "admin-api-key",
|
||||
Name: "Admin API Key",
|
||||
ServiceAccountId: &serviceAccountId,
|
||||
|
@ -79,8 +79,8 @@ func (h *AnnotationBackend) QueryStates(ctx context.Context, query ngmodels.Hist
|
||||
}
|
||||
|
||||
q := annotations.ItemQuery{
|
||||
AlertId: rq.Result.ID,
|
||||
OrgId: query.OrgID,
|
||||
AlertID: rq.Result.ID,
|
||||
OrgID: query.OrgID,
|
||||
From: query.From.Unix(),
|
||||
To: query.To.Unix(),
|
||||
SignedInUser: query.SignedInUser,
|
||||
@ -121,7 +121,7 @@ func (h *AnnotationBackend) QueryStates(ctx context.Context, query ngmodels.Hist
|
||||
for _, item := range items {
|
||||
data, err := json.Marshal(item.Data)
|
||||
if err != nil {
|
||||
logger.Error("Annotation service gave an annotation with unparseable data, skipping", "id", item.Id, "err", err)
|
||||
logger.Error("Annotation service gave an annotation with unparseable data, skipping", "id", item.ID, "err", err)
|
||||
continue
|
||||
}
|
||||
times = append(times, time.Unix(item.Time, 0))
|
||||
@ -151,8 +151,8 @@ func buildAnnotations(rule history_model.RuleMeta, states []state.StateTransitio
|
||||
annotationText, annotationData := buildAnnotationTextAndData(rule, state.State)
|
||||
|
||||
item := annotations.Item{
|
||||
AlertId: rule.ID,
|
||||
OrgId: state.OrgID,
|
||||
AlertID: rule.ID,
|
||||
OrgID: state.OrgID,
|
||||
PrevState: state.PreviousFormatted(),
|
||||
NewState: state.Formatted(),
|
||||
Text: annotationText,
|
||||
@ -174,8 +174,8 @@ func (h *AnnotationBackend) recordAnnotationsSync(ctx context.Context, panel *pa
|
||||
}
|
||||
|
||||
for i := range annotations {
|
||||
annotations[i].DashboardId = dashID
|
||||
annotations[i].PanelId = panel.panelID
|
||||
annotations[i].DashboardID = dashID
|
||||
annotations[i].PanelID = panel.panelID
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,9 +54,9 @@ func createTestAnnotationBackendSut(t *testing.T) *AnnotationBackend {
|
||||
|
||||
func createAnnotation() annotations.Item {
|
||||
return annotations.Item{
|
||||
Id: 1,
|
||||
OrgId: 1,
|
||||
AlertId: 1,
|
||||
ID: 1,
|
||||
OrgID: 1,
|
||||
AlertID: 1,
|
||||
Text: "MyAlert {a=b} - No data",
|
||||
Data: simplejson.New(),
|
||||
Epoch: time.Now().UnixNano() / int64(time.Millisecond),
|
||||
|
@ -43,16 +43,16 @@ func (pd *PublicDashboardServiceImpl) FindAnnotations(ctx context.Context, reqDT
|
||||
annoQuery := &annotations.ItemQuery{
|
||||
From: reqDTO.From,
|
||||
To: reqDTO.To,
|
||||
OrgId: dash.OrgID,
|
||||
DashboardId: dash.ID,
|
||||
DashboardUid: dash.UID,
|
||||
OrgID: dash.OrgID,
|
||||
DashboardID: dash.ID,
|
||||
DashboardUID: dash.UID,
|
||||
Limit: anno.Target.Limit,
|
||||
MatchAny: anno.Target.MatchAny,
|
||||
SignedInUser: anonymousUser,
|
||||
}
|
||||
|
||||
if anno.Target.Type == "tags" {
|
||||
annoQuery.DashboardId = 0
|
||||
annoQuery.DashboardID = 0
|
||||
annoQuery.Tags = anno.Target.Tags
|
||||
}
|
||||
|
||||
@ -63,8 +63,8 @@ func (pd *PublicDashboardServiceImpl) FindAnnotations(ctx context.Context, reqDT
|
||||
|
||||
for _, item := range annotationItems {
|
||||
event := models.AnnotationEvent{
|
||||
Id: item.Id,
|
||||
DashboardId: item.DashboardId,
|
||||
Id: item.ID,
|
||||
DashboardId: item.DashboardID,
|
||||
Tags: item.Tags,
|
||||
IsRegion: item.TimeEnd > 0 && item.Time != item.TimeEnd,
|
||||
Text: item.Text,
|
||||
@ -77,7 +77,7 @@ func (pd *PublicDashboardServiceImpl) FindAnnotations(ctx context.Context, reqDT
|
||||
// We want dashboard annotations to reference the panel they're for. If no panelId is provided, they'll show up on all panels
|
||||
// which is only intended for tag and org annotations.
|
||||
if anno.Type == "dashboard" {
|
||||
event.PanelId = item.PanelId
|
||||
event.PanelId = item.PanelID
|
||||
}
|
||||
|
||||
// We want events from tag queries to overwrite existing events
|
||||
|
@ -485,9 +485,9 @@ func TestGetAnnotations(t *testing.T) {
|
||||
|
||||
annotationsRepo.On("Find", mock.Anything, mock.Anything).Return([]*annotations.ItemDTO{
|
||||
{
|
||||
Id: 1,
|
||||
DashboardId: 1,
|
||||
PanelId: 1,
|
||||
ID: 1,
|
||||
DashboardID: 1,
|
||||
PanelID: 1,
|
||||
Tags: []string{"tag1"},
|
||||
TimeEnd: 2,
|
||||
Time: 2,
|
||||
@ -545,9 +545,9 @@ func TestGetAnnotations(t *testing.T) {
|
||||
|
||||
annotationsRepo.On("Find", mock.Anything, mock.Anything).Return([]*annotations.ItemDTO{
|
||||
{
|
||||
Id: 1,
|
||||
DashboardId: 1,
|
||||
PanelId: 1,
|
||||
ID: 1,
|
||||
DashboardID: 1,
|
||||
PanelID: 1,
|
||||
Tags: []string{},
|
||||
TimeEnd: 1,
|
||||
Time: 2,
|
||||
@ -617,9 +617,9 @@ func TestGetAnnotations(t *testing.T) {
|
||||
|
||||
annotationsRepo.On("Find", mock.Anything, mock.Anything).Return([]*annotations.ItemDTO{
|
||||
{
|
||||
Id: 1,
|
||||
DashboardId: 1,
|
||||
PanelId: 1,
|
||||
ID: 1,
|
||||
DashboardID: 1,
|
||||
PanelID: 1,
|
||||
Tags: []string{},
|
||||
TimeEnd: 1,
|
||||
Time: 2,
|
||||
|
@ -98,7 +98,7 @@ func (api *ServiceAccountsAPI) ListTokens(ctx *contextmodel.ReqContext) response
|
||||
}
|
||||
|
||||
result[i] = TokenDTO{
|
||||
Id: token.Id,
|
||||
Id: token.ID,
|
||||
Name: token.Name,
|
||||
Created: &token.Created,
|
||||
Expiration: expiration,
|
||||
@ -187,7 +187,7 @@ func (api *ServiceAccountsAPI) CreateToken(c *contextmodel.ReqContext) response.
|
||||
}
|
||||
|
||||
result := &dtos.NewApiKeyResult{
|
||||
ID: apiKey.Id,
|
||||
ID: apiKey.ID,
|
||||
Name: apiKey.Name,
|
||||
Key: newKeyInfo.ClientSecret,
|
||||
}
|
||||
|
@ -423,7 +423,7 @@ func (s *ServiceAccountsStoreImpl) MigrateApiKeysToServiceAccounts(ctx context.C
|
||||
s.log.Error("migating to service accounts failed with error", err)
|
||||
return err
|
||||
}
|
||||
s.log.Debug("API key converted to service account token", "keyId", key.Id)
|
||||
s.log.Debug("API key converted to service account token", "keyId", key.ID)
|
||||
}
|
||||
}
|
||||
if err := s.kvStore.Set(ctx, orgId, "serviceaccounts", "migrationStatus", "1"); err != nil {
|
||||
@ -441,7 +441,7 @@ func (s *ServiceAccountsStoreImpl) MigrateApiKey(ctx context.Context, orgId int6
|
||||
return fmt.Errorf("no API keys to convert found")
|
||||
}
|
||||
for _, key := range basicKeys {
|
||||
if keyId == key.Id {
|
||||
if keyId == key.ID {
|
||||
err := s.CreateServiceAccountFromApikey(ctx, key)
|
||||
if err != nil {
|
||||
s.log.Error("converting to service account failed with error", "keyId", keyId, "error", err)
|
||||
@ -455,9 +455,9 @@ func (s *ServiceAccountsStoreImpl) MigrateApiKey(ctx context.Context, orgId int6
|
||||
func (s *ServiceAccountsStoreImpl) CreateServiceAccountFromApikey(ctx context.Context, key *apikey.APIKey) error {
|
||||
prefix := "sa-autogen"
|
||||
cmd := user.CreateUserCommand{
|
||||
Login: fmt.Sprintf("%v-%v-%v", prefix, key.OrgId, key.Name),
|
||||
Login: fmt.Sprintf("%v-%v-%v", prefix, key.OrgID, key.Name),
|
||||
Name: fmt.Sprintf("%v-%v", prefix, key.Name),
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
DefaultOrgRole: string(key.Role),
|
||||
IsServiceAccount: true,
|
||||
}
|
||||
@ -468,7 +468,7 @@ func (s *ServiceAccountsStoreImpl) CreateServiceAccountFromApikey(ctx context.Co
|
||||
return fmt.Errorf("failed to create service account: %w", errCreateSA)
|
||||
}
|
||||
|
||||
if err := s.assignApiKeyToServiceAccount(sess, key.Id, newSA.ID); err != nil {
|
||||
if err := s.assignApiKeyToServiceAccount(sess, key.ID, newSA.ID); err != nil {
|
||||
return fmt.Errorf("failed to migrate API key to service account token: %w", err)
|
||||
}
|
||||
|
||||
@ -478,7 +478,7 @@ func (s *ServiceAccountsStoreImpl) CreateServiceAccountFromApikey(ctx context.Co
|
||||
|
||||
// RevertApiKey converts service account token to old API key
|
||||
func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64, keyId int64) error {
|
||||
query := apikey.GetByIDQuery{ApiKeyId: keyId}
|
||||
query := apikey.GetByIDQuery{ApiKeyID: keyId}
|
||||
if err := s.apiKeyService.GetApiKeyById(ctx, &query); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -493,7 +493,7 @@ func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64,
|
||||
}
|
||||
|
||||
tokens, err := s.ListTokens(ctx, &serviceaccounts.GetSATokensQuery{
|
||||
OrgID: &key.OrgId,
|
||||
OrgID: &key.OrgID,
|
||||
ServiceAccountID: key.ServiceAccountId,
|
||||
})
|
||||
if err != nil {
|
||||
@ -506,7 +506,7 @@ func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64,
|
||||
err = s.sqlStore.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
|
||||
user := user.User{}
|
||||
has, err := sess.Where(`org_id = ? and id = ? and is_service_account = ?`,
|
||||
key.OrgId, *key.ServiceAccountId, s.sqlStore.GetDialect().BooleanStr(true)).Get(&user)
|
||||
key.OrgID, *key.ServiceAccountId, s.sqlStore.GetDialect().BooleanStr(true)).Get(&user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -514,11 +514,11 @@ func (s *ServiceAccountsStoreImpl) RevertApiKey(ctx context.Context, saId int64,
|
||||
return serviceaccounts.ErrServiceAccountNotFound
|
||||
}
|
||||
// Detach API key from service account
|
||||
if err := s.detachApiKeyFromServiceAccount(sess, key.Id); err != nil {
|
||||
if err := s.detachApiKeyFromServiceAccount(sess, key.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
// Delete service account
|
||||
if err := s.deleteServiceAccount(sess, key.OrgId, *key.ServiceAccountId); err != nil {
|
||||
if err := s.deleteServiceAccount(sess, key.OrgID, *key.ServiceAccountId); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -180,14 +180,14 @@ func TestStore_MigrateApiKeys(t *testing.T) {
|
||||
_, err := store.orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "main"})
|
||||
require.NoError(t, err)
|
||||
key := tests.SetupApiKey(t, db, c.key)
|
||||
err = store.MigrateApiKey(context.Background(), key.OrgId, key.Id)
|
||||
err = store.MigrateApiKey(context.Background(), key.OrgID, key.ID)
|
||||
if c.expectedErr != nil {
|
||||
require.ErrorIs(t, err, c.expectedErr)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
q := serviceaccounts.SearchOrgServiceAccountsQuery{
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
Query: "",
|
||||
Page: 1,
|
||||
Limit: 50,
|
||||
@ -195,7 +195,7 @@ func TestStore_MigrateApiKeys(t *testing.T) {
|
||||
UserID: 1,
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{
|
||||
key.OrgId: {
|
||||
key.OrgID: {
|
||||
"serviceaccounts:read": {"serviceaccounts:id:*"},
|
||||
},
|
||||
},
|
||||
@ -208,7 +208,7 @@ func TestStore_MigrateApiKeys(t *testing.T) {
|
||||
require.Equal(t, string(key.Role), saMigrated.Role)
|
||||
|
||||
tokens, err := store.ListTokens(context.Background(), &serviceaccounts.GetSATokensQuery{
|
||||
OrgID: &key.OrgId,
|
||||
OrgID: &key.OrgID,
|
||||
ServiceAccountID: &saMigrated.Id,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
@ -350,7 +350,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
saId = rand.Int63()
|
||||
} else {
|
||||
q := serviceaccounts.SearchOrgServiceAccountsQuery{
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
Query: "",
|
||||
Page: 1,
|
||||
Limit: 50,
|
||||
@ -358,7 +358,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
UserID: 1,
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{
|
||||
key.OrgId: {
|
||||
key.OrgID: {
|
||||
"serviceaccounts:read": {"serviceaccounts:id:*"},
|
||||
},
|
||||
},
|
||||
@ -369,14 +369,14 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
saId = serviceAccounts.ServiceAccounts[0].Id
|
||||
}
|
||||
|
||||
err = store.RevertApiKey(context.Background(), saId, key.Id)
|
||||
err = store.RevertApiKey(context.Background(), saId, key.ID)
|
||||
|
||||
if c.expectedErr != nil {
|
||||
require.ErrorIs(t, err, c.expectedErr)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
q := serviceaccounts.SearchOrgServiceAccountsQuery{
|
||||
OrgID: key.OrgId,
|
||||
OrgID: key.OrgID,
|
||||
Query: "",
|
||||
Page: 1,
|
||||
Limit: 50,
|
||||
@ -384,7 +384,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
UserID: 1,
|
||||
OrgID: 1,
|
||||
Permissions: map[int64]map[string][]string{
|
||||
key.OrgId: {
|
||||
key.OrgID: {
|
||||
"serviceaccounts:read": {"serviceaccounts:id:*"},
|
||||
},
|
||||
},
|
||||
@ -400,7 +400,7 @@ func TestStore_RevertApiKey(t *testing.T) {
|
||||
require.Len(t, apiKeys, 1)
|
||||
apiKey := apiKeys[0]
|
||||
require.Equal(t, c.key.Name, apiKey.Name)
|
||||
require.Equal(t, c.key.OrgId, apiKey.OrgId)
|
||||
require.Equal(t, c.key.OrgId, apiKey.OrgID)
|
||||
require.Equal(t, c.key.Role, apiKey.Role)
|
||||
require.Equal(t, key.Key, apiKey.Key)
|
||||
// Api key should not be linked to service account
|
||||
|
@ -49,7 +49,7 @@ func (s *ServiceAccountsStoreImpl) AddServiceAccountToken(ctx context.Context, s
|
||||
addKeyCmd := &apikey.AddCommand{
|
||||
Name: cmd.Name,
|
||||
Role: org.RoleViewer,
|
||||
OrgId: cmd.OrgId,
|
||||
OrgID: cmd.OrgId,
|
||||
Key: cmd.Key,
|
||||
SecondsToLive: cmd.SecondsToLive,
|
||||
ServiceAccountID: &serviceAccountId,
|
||||
@ -107,7 +107,7 @@ func (s *ServiceAccountsStoreImpl) RevokeServiceAccountToken(ctx context.Context
|
||||
|
||||
// assignApiKeyToServiceAccount sets the API key service account ID
|
||||
func (s *ServiceAccountsStoreImpl) assignApiKeyToServiceAccount(sess *db.Session, apiKeyId int64, serviceAccountId int64) error {
|
||||
key := apikey.APIKey{Id: apiKeyId}
|
||||
key := apikey.APIKey{ID: apiKeyId}
|
||||
exists, err := sess.Get(&key)
|
||||
if err != nil {
|
||||
s.log.Warn("API key not loaded", "err", err)
|
||||
@ -119,7 +119,7 @@ func (s *ServiceAccountsStoreImpl) assignApiKeyToServiceAccount(sess *db.Session
|
||||
}
|
||||
key.ServiceAccountId = &serviceAccountId
|
||||
|
||||
if _, err := sess.ID(key.Id).Update(&key); err != nil {
|
||||
if _, err := sess.ID(key.ID).Update(&key); err != nil {
|
||||
s.log.Warn("Could not update api key", "err", err)
|
||||
return err
|
||||
}
|
||||
@ -129,7 +129,7 @@ func (s *ServiceAccountsStoreImpl) assignApiKeyToServiceAccount(sess *db.Session
|
||||
|
||||
// detachApiKeyFromServiceAccount converts service account token to old API key
|
||||
func (s *ServiceAccountsStoreImpl) detachApiKeyFromServiceAccount(sess *db.Session, apiKeyId int64) error {
|
||||
key := apikey.APIKey{Id: apiKeyId}
|
||||
key := apikey.APIKey{ID: apiKeyId}
|
||||
exists, err := sess.Get(&key)
|
||||
if err != nil {
|
||||
s.log.Warn("Cannot get API key", "err", err)
|
||||
@ -141,7 +141,7 @@ func (s *ServiceAccountsStoreImpl) detachApiKeyFromServiceAccount(sess *db.Sessi
|
||||
}
|
||||
key.ServiceAccountId = nil
|
||||
|
||||
if _, err := sess.ID(key.Id).AllCols().Update(&key); err != nil {
|
||||
if _, err := sess.ID(key.ID).AllCols().Update(&key); err != nil {
|
||||
s.log.Error("Could not update api key", "err", err)
|
||||
return err
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ func TestStore_RevokeServiceAccountToken(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Revoke SAT
|
||||
err = store.RevokeServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.Id)
|
||||
err = store.RevokeServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify against DB
|
||||
@ -153,14 +153,14 @@ func TestStore_DeleteServiceAccountToken(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Delete key from wrong service account
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID+2, newKey.Id)
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID+2, newKey.ID)
|
||||
require.Error(t, err)
|
||||
|
||||
// Delete key from wrong org
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID+2, sa.ID, newKey.Id)
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID+2, sa.ID, newKey.ID)
|
||||
require.Error(t, err)
|
||||
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.Id)
|
||||
err = store.DeleteServiceAccountToken(context.Background(), sa.OrgID, sa.ID, newKey.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify against DB
|
||||
|
@ -120,10 +120,10 @@ func (s *Service) CheckTokens(ctx context.Context) error {
|
||||
|
||||
if s.revoke {
|
||||
if err := s.store.RevokeServiceAccountToken(
|
||||
ctx, leakedToken.OrgId, *leakedToken.ServiceAccountId, leakedToken.Id); err != nil {
|
||||
ctx, leakedToken.OrgID, *leakedToken.ServiceAccountId, leakedToken.ID); err != nil {
|
||||
s.logger.Error("failed to delete leaked token. Revoke manually.",
|
||||
"error", err, "url", secretscanToken.URL, "reported_at", secretscanToken.ReportedAt,
|
||||
"token_id", leakedToken.Id, "token", leakedToken.Name, "org", leakedToken.OrgId,
|
||||
"token_id", leakedToken.ID, "token", leakedToken.Name, "org", leakedToken.OrgID,
|
||||
"serviceAccount", *leakedToken.ServiceAccountId)
|
||||
}
|
||||
}
|
||||
@ -136,7 +136,7 @@ func (s *Service) CheckTokens(ctx context.Context) error {
|
||||
|
||||
s.logger.Warn("found leaked token",
|
||||
"url", secretscanToken.URL, "reported_at", secretscanToken.ReportedAt,
|
||||
"token_id", leakedToken.Id, "token", leakedToken.Name, "org", leakedToken.OrgId,
|
||||
"token_id", leakedToken.ID, "token", leakedToken.Name, "org", leakedToken.OrgID,
|
||||
"serviceAccount", *leakedToken.ServiceAccountId, "revoked", s.revoke)
|
||||
}
|
||||
|
||||
|
@ -37,8 +37,8 @@ func TestService_CheckTokens(t *testing.T) {
|
||||
{
|
||||
desc: "one token leaked - no revoke, no notify",
|
||||
retrievedTokens: []apikey.APIKey{{
|
||||
Id: 1,
|
||||
OrgId: 2,
|
||||
ID: 1,
|
||||
OrgID: 2,
|
||||
Name: "test",
|
||||
Key: "test-hash-1",
|
||||
Role: "Viewer",
|
||||
@ -54,8 +54,8 @@ func TestService_CheckTokens(t *testing.T) {
|
||||
{
|
||||
desc: "one token leaked - revoke, no notify",
|
||||
retrievedTokens: []apikey.APIKey{{
|
||||
Id: 1,
|
||||
OrgId: 2,
|
||||
ID: 1,
|
||||
OrgID: 2,
|
||||
Name: "test",
|
||||
Key: "test-hash-1",
|
||||
Role: "Viewer",
|
||||
@ -71,8 +71,8 @@ func TestService_CheckTokens(t *testing.T) {
|
||||
{
|
||||
desc: "two tokens - one revoke, notify",
|
||||
retrievedTokens: []apikey.APIKey{{
|
||||
Id: 1,
|
||||
OrgId: 2,
|
||||
ID: 1,
|
||||
OrgID: 2,
|
||||
Name: "test",
|
||||
Key: "test-hash-1",
|
||||
Role: "Viewer",
|
||||
@ -80,8 +80,8 @@ func TestService_CheckTokens(t *testing.T) {
|
||||
ServiceAccountId: new(int64),
|
||||
IsRevoked: &falseBool,
|
||||
}, {
|
||||
Id: 2,
|
||||
OrgId: 4,
|
||||
ID: 2,
|
||||
OrgID: 4,
|
||||
Name: "test-2",
|
||||
Key: "test-hash-2",
|
||||
Role: "Viewer",
|
||||
@ -97,8 +97,8 @@ func TestService_CheckTokens(t *testing.T) {
|
||||
{
|
||||
desc: "one token already revoked should not be checked",
|
||||
retrievedTokens: []apikey.APIKey{{
|
||||
Id: 1,
|
||||
OrgId: 2,
|
||||
ID: 1,
|
||||
OrgID: 2,
|
||||
Name: "test",
|
||||
Key: "test-hash-1",
|
||||
Role: "Viewer",
|
||||
@ -114,8 +114,8 @@ func TestService_CheckTokens(t *testing.T) {
|
||||
{
|
||||
desc: "one token expired should not be checked",
|
||||
retrievedTokens: []apikey.APIKey{{
|
||||
Id: 1,
|
||||
OrgId: 2,
|
||||
ID: 1,
|
||||
OrgID: 2,
|
||||
Name: "test",
|
||||
Key: "test-hash-1",
|
||||
Role: "Viewer",
|
||||
|
@ -69,7 +69,7 @@ func SetupApiKey(t *testing.T, sqlStore *sqlstore.SQLStore, testKey TestApiKey)
|
||||
addKeyCmd := &apikey.AddCommand{
|
||||
Name: testKey.Name,
|
||||
Role: role,
|
||||
OrgId: testKey.OrgId,
|
||||
OrgID: testKey.OrgId,
|
||||
ServiceAccountID: testKey.ServiceAccountID,
|
||||
}
|
||||
|
||||
@ -90,7 +90,7 @@ func SetupApiKey(t *testing.T, sqlStore *sqlstore.SQLStore, testKey TestApiKey)
|
||||
// Force setting expires to time before now to make key expired
|
||||
var expires int64 = 1
|
||||
key := apikey.APIKey{Expires: &expires}
|
||||
rowsAffected, err := sess.ID(addKeyCmd.Result.Id).Update(&key)
|
||||
rowsAffected, err := sess.ID(addKeyCmd.Result.ID).Update(&key)
|
||||
require.Equal(t, int64(1), rowsAffected)
|
||||
return err
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user