user_group.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/metrics"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/util"
  8. )
  9. // POST /api/user-groups
  10. func CreateUserGroup(c *middleware.Context, cmd m.CreateUserGroupCommand) Response {
  11. cmd.OrgId = c.OrgId
  12. if err := bus.Dispatch(&cmd); err != nil {
  13. if err == m.ErrUserGroupNameTaken {
  14. return ApiError(409, "User Group name taken", err)
  15. }
  16. return ApiError(500, "Failed to create User Group", err)
  17. }
  18. metrics.M_Api_UserGroup_Create.Inc(1)
  19. return Json(200, &util.DynMap{
  20. "userGroupId": cmd.Result.Id,
  21. "message": "User Group created",
  22. })
  23. }
  24. // PUT /api/user-groups/:userGroupId
  25. func UpdateUserGroup(c *middleware.Context, cmd m.UpdateUserGroupCommand) Response {
  26. cmd.Id = c.ParamsInt64(":userGroupId")
  27. if err := bus.Dispatch(&cmd); err != nil {
  28. if err == m.ErrUserGroupNameTaken {
  29. return ApiError(400, "User Group name taken", err)
  30. }
  31. return ApiError(500, "Failed to update User Group", err)
  32. }
  33. return ApiSuccess("User Group updated")
  34. }
  35. // DELETE /api/user-groups/:userGroupId
  36. func DeleteUserGroupById(c *middleware.Context) Response {
  37. if err := bus.Dispatch(&m.DeleteUserGroupCommand{Id: c.ParamsInt64(":userGroupId")}); err != nil {
  38. if err == m.ErrUserGroupNotFound {
  39. return ApiError(404, "Failed to delete User Group. ID not found", nil)
  40. }
  41. return ApiError(500, "Failed to update User Group", err)
  42. }
  43. return ApiSuccess("User Group deleted")
  44. }
  45. // GET /api/user-groups/search
  46. func SearchUserGroups(c *middleware.Context) Response {
  47. perPage := c.QueryInt("perpage")
  48. if perPage <= 0 {
  49. perPage = 1000
  50. }
  51. page := c.QueryInt("page")
  52. if page < 1 {
  53. page = 1
  54. }
  55. query := m.SearchUserGroupsQuery{
  56. Query: c.Query("query"),
  57. Name: c.Query("name"),
  58. Page: page,
  59. Limit: perPage,
  60. OrgId: c.OrgId,
  61. }
  62. if err := bus.Dispatch(&query); err != nil {
  63. return ApiError(500, "Failed to search User Groups", err)
  64. }
  65. query.Result.Page = page
  66. query.Result.PerPage = perPage
  67. return Json(200, query.Result)
  68. }
  69. // GET /api/user-groups/:userGroupId
  70. func GetUserGroupById(c *middleware.Context) Response {
  71. query := m.GetUserGroupByIdQuery{Id: c.ParamsInt64(":userGroupId")}
  72. if err := bus.Dispatch(&query); err != nil {
  73. if err == m.ErrUserGroupNotFound {
  74. return ApiError(404, "User Group not found", err)
  75. }
  76. return ApiError(500, "Failed to get User Group", err)
  77. }
  78. return Json(200, &query.Result)
  79. }