Commit 7a5464fe by Agnès Toulet Committed by GitHub

Chore: use Wrap for all admin API (#26673)

* Chore: use Wrap for all admin API

* API: fix admin tests
parent ec76d69b
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
) )
func AdminGetSettings(c *models.ReqContext) { func AdminGetSettings(c *models.ReqContext) Response {
settings := make(map[string]interface{}) settings := make(map[string]interface{})
for _, section := range setting.Raw.Sections() { for _, section := range setting.Raw.Sections() {
...@@ -35,16 +35,15 @@ func AdminGetSettings(c *models.ReqContext) { ...@@ -35,16 +35,15 @@ func AdminGetSettings(c *models.ReqContext) {
} }
} }
c.JSON(200, settings) return JSON(200, settings)
} }
func AdminGetStats(c *models.ReqContext) { func AdminGetStats(c *models.ReqContext) Response {
statsQuery := models.GetAdminStatsQuery{} statsQuery := models.GetAdminStatsQuery{}
if err := bus.Dispatch(&statsQuery); err != nil { if err := bus.Dispatch(&statsQuery); err != nil {
c.JsonApiErr(500, "Failed to get admin stats from database", err) return Error(500, "Failed to get admin stats from database", err)
return
} }
c.JSON(200, statsQuery.Result) return JSON(200, statsQuery.Result)
} }
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
func AdminCreateUser(c *models.ReqContext, form dtos.AdminCreateUserForm) { func AdminCreateUser(c *models.ReqContext, form dtos.AdminCreateUserForm) Response {
cmd := models.CreateUserCommand{ cmd := models.CreateUserCommand{
Login: form.Login, Login: form.Login,
Email: form.Email, Email: form.Email,
...@@ -20,24 +20,20 @@ func AdminCreateUser(c *models.ReqContext, form dtos.AdminCreateUserForm) { ...@@ -20,24 +20,20 @@ func AdminCreateUser(c *models.ReqContext, form dtos.AdminCreateUserForm) {
if len(cmd.Login) == 0 { if len(cmd.Login) == 0 {
cmd.Login = cmd.Email cmd.Login = cmd.Email
if len(cmd.Login) == 0 { if len(cmd.Login) == 0 {
c.JsonApiErr(400, "Validation error, need specify either username or email", nil) return Error(400, "Validation error, need specify either username or email", nil)
return
} }
} }
if len(cmd.Password) < 4 { if len(cmd.Password) < 4 {
c.JsonApiErr(400, "Password is missing or too short", nil) return Error(400, "Password is missing or too short", nil)
return
} }
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
if err == models.ErrOrgNotFound { if err == models.ErrOrgNotFound {
c.JsonApiErr(400, models.ErrOrgNotFound.Error(), nil) return Error(400, models.ErrOrgNotFound.Error(), nil)
return
} }
c.JsonApiErr(500, "failed to create user", err) return Error(500, "failed to create user", err)
return
} }
metrics.MApiAdminUserCreate.Inc() metrics.MApiAdminUserCreate.Inc()
...@@ -49,28 +45,25 @@ func AdminCreateUser(c *models.ReqContext, form dtos.AdminCreateUserForm) { ...@@ -49,28 +45,25 @@ func AdminCreateUser(c *models.ReqContext, form dtos.AdminCreateUserForm) {
Id: user.Id, Id: user.Id,
} }
c.JSON(200, result) return JSON(200, result)
} }
func AdminUpdateUserPassword(c *models.ReqContext, form dtos.AdminUpdateUserPasswordForm) { func AdminUpdateUserPassword(c *models.ReqContext, form dtos.AdminUpdateUserPasswordForm) Response {
userID := c.ParamsInt64(":id") userID := c.ParamsInt64(":id")
if len(form.Password) < 4 { if len(form.Password) < 4 {
c.JsonApiErr(400, "New password too short", nil) return Error(400, "New password too short", nil)
return
} }
userQuery := models.GetUserByIdQuery{Id: userID} userQuery := models.GetUserByIdQuery{Id: userID}
if err := bus.Dispatch(&userQuery); err != nil { if err := bus.Dispatch(&userQuery); err != nil {
c.JsonApiErr(500, "Could not read user from database", err) return Error(500, "Could not read user from database", err)
return
} }
passwordHashed, err := util.EncodePassword(form.Password, userQuery.Result.Salt) passwordHashed, err := util.EncodePassword(form.Password, userQuery.Result.Salt)
if err != nil { if err != nil {
c.JsonApiErr(500, "Could not encode password", err) return Error(500, "Could not encode password", err)
return
} }
cmd := models.ChangeUserPasswordCommand{ cmd := models.ChangeUserPasswordCommand{
...@@ -79,15 +72,14 @@ func AdminUpdateUserPassword(c *models.ReqContext, form dtos.AdminUpdateUserPass ...@@ -79,15 +72,14 @@ func AdminUpdateUserPassword(c *models.ReqContext, form dtos.AdminUpdateUserPass
} }
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
c.JsonApiErr(500, "Failed to update user password", err) return Error(500, "Failed to update user password", err)
return
} }
c.JsonOK("User password updated") return Success("User password updated")
} }
// PUT /api/admin/users/:id/permissions // PUT /api/admin/users/:id/permissions
func AdminUpdateUserPermissions(c *models.ReqContext, form dtos.AdminUpdateUserPermissionsForm) { func AdminUpdateUserPermissions(c *models.ReqContext, form dtos.AdminUpdateUserPermissionsForm) Response {
userID := c.ParamsInt64(":id") userID := c.ParamsInt64(":id")
cmd := models.UpdateUserPermissionsCommand{ cmd := models.UpdateUserPermissionsCommand{
...@@ -97,32 +89,28 @@ func AdminUpdateUserPermissions(c *models.ReqContext, form dtos.AdminUpdateUserP ...@@ -97,32 +89,28 @@ func AdminUpdateUserPermissions(c *models.ReqContext, form dtos.AdminUpdateUserP
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
if err == models.ErrLastGrafanaAdmin { if err == models.ErrLastGrafanaAdmin {
c.JsonApiErr(400, models.ErrLastGrafanaAdmin.Error(), nil) return Error(400, models.ErrLastGrafanaAdmin.Error(), nil)
return
} }
c.JsonApiErr(500, "Failed to update user permissions", err) return Error(500, "Failed to update user permissions", err)
return
} }
c.JsonOK("User permissions updated") return Success("User permissions updated")
} }
func AdminDeleteUser(c *models.ReqContext) { func AdminDeleteUser(c *models.ReqContext) Response {
userID := c.ParamsInt64(":id") userID := c.ParamsInt64(":id")
cmd := models.DeleteUserCommand{UserId: userID} cmd := models.DeleteUserCommand{UserId: userID}
if err := bus.Dispatch(&cmd); err != nil { if err := bus.Dispatch(&cmd); err != nil {
if err == models.ErrUserNotFound { if err == models.ErrUserNotFound {
c.JsonApiErr(404, models.ErrUserNotFound.Error(), nil) return Error(404, models.ErrUserNotFound.Error(), nil)
return
} }
c.JsonApiErr(500, "Failed to delete user", err) return Error(500, "Failed to delete user", err)
return
} }
c.JsonOK("User deleted") return Success("User deleted")
} }
// POST /api/admin/users/:id/disable // POST /api/admin/users/:id/disable
......
...@@ -266,13 +266,13 @@ func putAdminScenario(desc string, url string, routePattern string, role models. ...@@ -266,13 +266,13 @@ func putAdminScenario(desc string, url string, routePattern string, role models.
defer bus.ClearBusHandlers() defer bus.ClearBusHandlers()
sc := setupScenarioContext(url) sc := setupScenarioContext(url)
sc.defaultHandler = Wrap(func(c *models.ReqContext) { sc.defaultHandler = Wrap(func(c *models.ReqContext) Response {
sc.context = c sc.context = c
sc.context.UserId = TestUserID sc.context.UserId = TestUserID
sc.context.OrgId = TestOrgID sc.context.OrgId = TestOrgID
sc.context.OrgRole = role sc.context.OrgRole = role
AdminUpdateUserPermissions(c, cmd) return AdminUpdateUserPermissions(c, cmd)
}) })
sc.m.Put(routePattern, sc.defaultHandler) sc.m.Put(routePattern, sc.defaultHandler)
...@@ -396,11 +396,11 @@ func adminDeleteUserScenario(desc string, url string, routePattern string, fn sc ...@@ -396,11 +396,11 @@ func adminDeleteUserScenario(desc string, url string, routePattern string, fn sc
defer bus.ClearBusHandlers() defer bus.ClearBusHandlers()
sc := setupScenarioContext(url) sc := setupScenarioContext(url)
sc.defaultHandler = Wrap(func(c *models.ReqContext) { sc.defaultHandler = Wrap(func(c *models.ReqContext) Response {
sc.context = c sc.context = c
sc.context.UserId = TestUserID sc.context.UserId = TestUserID
AdminDeleteUser(c) return AdminDeleteUser(c)
}) })
sc.m.Delete(routePattern, sc.defaultHandler) sc.m.Delete(routePattern, sc.defaultHandler)
...@@ -414,11 +414,11 @@ func adminCreateUserScenario(desc string, url string, routePattern string, cmd d ...@@ -414,11 +414,11 @@ func adminCreateUserScenario(desc string, url string, routePattern string, cmd d
defer bus.ClearBusHandlers() defer bus.ClearBusHandlers()
sc := setupScenarioContext(url) sc := setupScenarioContext(url)
sc.defaultHandler = Wrap(func(c *models.ReqContext) { sc.defaultHandler = Wrap(func(c *models.ReqContext) Response {
sc.context = c sc.context = c
sc.context.UserId = TestUserID sc.context.UserId = TestUserID
AdminCreateUser(c, cmd) return AdminCreateUser(c, cmd)
}) })
sc.m.Post(routePattern, sc.defaultHandler) sc.m.Post(routePattern, sc.defaultHandler)
......
...@@ -387,16 +387,16 @@ func (hs *HTTPServer) registerRoutes() { ...@@ -387,16 +387,16 @@ func (hs *HTTPServer) registerRoutes() {
// admin api // admin api
r.Group("/api/admin", func(adminRoute routing.RouteRegister) { r.Group("/api/admin", func(adminRoute routing.RouteRegister) {
adminRoute.Get("/settings", AdminGetSettings) adminRoute.Get("/settings", Wrap(AdminGetSettings))
adminRoute.Post("/users", bind(dtos.AdminCreateUserForm{}), AdminCreateUser) adminRoute.Post("/users", bind(dtos.AdminCreateUserForm{}), Wrap(AdminCreateUser))
adminRoute.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), AdminUpdateUserPassword) adminRoute.Put("/users/:id/password", bind(dtos.AdminUpdateUserPasswordForm{}), Wrap(AdminUpdateUserPassword))
adminRoute.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), AdminUpdateUserPermissions) adminRoute.Put("/users/:id/permissions", bind(dtos.AdminUpdateUserPermissionsForm{}), Wrap(AdminUpdateUserPermissions))
adminRoute.Delete("/users/:id", AdminDeleteUser) adminRoute.Delete("/users/:id", Wrap(AdminDeleteUser))
adminRoute.Post("/users/:id/disable", Wrap(hs.AdminDisableUser)) adminRoute.Post("/users/:id/disable", Wrap(hs.AdminDisableUser))
adminRoute.Post("/users/:id/enable", Wrap(AdminEnableUser)) adminRoute.Post("/users/:id/enable", Wrap(AdminEnableUser))
adminRoute.Get("/users/:id/quotas", Wrap(GetUserQuotas)) adminRoute.Get("/users/:id/quotas", Wrap(GetUserQuotas))
adminRoute.Put("/users/:id/quotas/:target", bind(models.UpdateUserQuotaCmd{}), Wrap(UpdateUserQuota)) adminRoute.Put("/users/:id/quotas/:target", bind(models.UpdateUserQuotaCmd{}), Wrap(UpdateUserQuota))
adminRoute.Get("/stats", AdminGetStats) adminRoute.Get("/stats", Wrap(AdminGetStats))
adminRoute.Post("/pause-all-alerts", bind(dtos.PauseAllAlertsCommand{}), Wrap(PauseAllAlerts)) adminRoute.Post("/pause-all-alerts", bind(dtos.PauseAllAlertsCommand{}), Wrap(PauseAllAlerts))
adminRoute.Post("/users/:id/logout", Wrap(hs.AdminLogoutUser)) adminRoute.Post("/users/:id/logout", Wrap(hs.AdminLogoutUser))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment