mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Refactor quota service (#58643)
Chore: Refactor quota service (#57586) * Chore: refactore quota service * Apply suggestions from code review
This commit is contained in:
committed by
GitHub
parent
dd0d034796
commit
9855e74b92
@@ -6,10 +6,22 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
// swagger:route GET /org/quotas getCurrentOrg getCurrentOrgQuota
|
||||
//
|
||||
// Fetch Organization quota.
|
||||
//
|
||||
// If you are running Grafana Enterprise and have Fine-grained access control enabled, you need to have a permission with action `orgs.quotas:read` and scope `org:id:1` (orgIDScope).
|
||||
//
|
||||
// Responses:
|
||||
// 200: getQuotaResponse
|
||||
// 401: unauthorisedError
|
||||
// 403: forbiddenError
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) GetCurrentOrgQuotas(c *models.ReqContext) response.Response {
|
||||
return hs.getOrgQuotasHelper(c, c.OrgID)
|
||||
}
|
||||
@@ -29,22 +41,17 @@ func (hs *HTTPServer) GetCurrentOrgQuotas(c *models.ReqContext) response.Respons
|
||||
func (hs *HTTPServer) GetOrgQuotas(c *models.ReqContext) response.Response {
|
||||
orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
||||
return response.Err(quota.ErrBadRequest.Errorf("orgId is invalid: %w", err))
|
||||
}
|
||||
return hs.getOrgQuotasHelper(c, orgId)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) getOrgQuotasHelper(c *models.ReqContext, orgID int64) response.Response {
|
||||
if !hs.Cfg.Quota.Enabled {
|
||||
return response.Error(404, "Quotas not enabled", nil)
|
||||
q, err := hs.QuotaService.GetQuotasByScope(c.Req.Context(), quota.OrgScope, orgID)
|
||||
if err != nil {
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "failed to get quota", err)
|
||||
}
|
||||
query := models.GetOrgQuotasQuery{OrgId: orgID}
|
||||
|
||||
if err := hs.SQLStore.GetOrgQuotas(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Failed to get org quotas", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, query.Result)
|
||||
return response.JSON(http.StatusOK, q)
|
||||
}
|
||||
|
||||
// swagger:route PUT /orgs/{org_id}/quotas/{quota_target} orgs updateOrgQuota
|
||||
@@ -63,26 +70,19 @@ func (hs *HTTPServer) getOrgQuotasHelper(c *models.ReqContext, orgID int64) resp
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) UpdateOrgQuota(c *models.ReqContext) response.Response {
|
||||
cmd := models.UpdateOrgQuotaCmd{}
|
||||
cmd := quota.UpdateQuotaCmd{}
|
||||
var err error
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
return response.Err(quota.ErrBadRequest.Errorf("bad request data: %w", err))
|
||||
}
|
||||
if !hs.Cfg.Quota.Enabled {
|
||||
return response.Error(404, "Quotas not enabled", nil)
|
||||
}
|
||||
cmd.OrgId, err = strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
||||
cmd.OrgID, err = strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
||||
return response.Err(quota.ErrBadRequest.Errorf("orgId is invalid: %w", err))
|
||||
}
|
||||
cmd.Target = web.Params(c.Req)[":target"]
|
||||
|
||||
if _, ok := hs.Cfg.Quota.Org.ToMap()[cmd.Target]; !ok {
|
||||
return response.Error(404, "Invalid quota target", nil)
|
||||
}
|
||||
|
||||
if err := hs.SQLStore.UpdateOrgQuota(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "Failed to update org quotas", err)
|
||||
if err := hs.QuotaService.Update(c.Req.Context(), &cmd); err != nil {
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "Failed to update org quotas", err)
|
||||
}
|
||||
return response.Success("Organization quota updated")
|
||||
}
|
||||
@@ -114,22 +114,17 @@ func (hs *HTTPServer) UpdateOrgQuota(c *models.ReqContext) response.Response {
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) GetUserQuotas(c *models.ReqContext) response.Response {
|
||||
if !setting.Quota.Enabled {
|
||||
return response.Error(404, "Quotas not enabled", nil)
|
||||
}
|
||||
|
||||
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
return response.Err(quota.ErrBadRequest.Errorf("id is invalid: %w", err))
|
||||
}
|
||||
|
||||
query := models.GetUserQuotasQuery{UserId: id}
|
||||
|
||||
if err := hs.SQLStore.GetUserQuotas(c.Req.Context(), &query); err != nil {
|
||||
return response.Error(500, "Failed to get org quotas", err)
|
||||
q, err := hs.QuotaService.GetQuotasByScope(c.Req.Context(), quota.UserScope, id)
|
||||
if err != nil {
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "Failed to get org quotas", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, query.Result)
|
||||
return response.JSON(http.StatusOK, q)
|
||||
}
|
||||
|
||||
// swagger:route PUT /admin/users/{user_id}/quotas/{quota_target} admin_users updateUserQuota
|
||||
@@ -148,26 +143,19 @@ func (hs *HTTPServer) GetUserQuotas(c *models.ReqContext) response.Response {
|
||||
// 404: notFoundError
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) UpdateUserQuota(c *models.ReqContext) response.Response {
|
||||
cmd := models.UpdateUserQuotaCmd{}
|
||||
cmd := quota.UpdateQuotaCmd{}
|
||||
var err error
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
return response.Err(quota.ErrBadRequest.Errorf("bad request data: %w", err))
|
||||
}
|
||||
if !setting.Quota.Enabled {
|
||||
return response.Error(404, "Quotas not enabled", nil)
|
||||
}
|
||||
cmd.UserId, err = strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
cmd.UserID, err = strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
||||
if err != nil {
|
||||
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
||||
return response.Err(quota.ErrBadRequest.Errorf("id is invalid: %w", err))
|
||||
}
|
||||
cmd.Target = web.Params(c.Req)[":target"]
|
||||
|
||||
if _, ok := setting.Quota.User.ToMap()[cmd.Target]; !ok {
|
||||
return response.Error(404, "Invalid quota target", nil)
|
||||
}
|
||||
|
||||
if err := hs.SQLStore.UpdateUserQuota(c.Req.Context(), &cmd); err != nil {
|
||||
return response.Error(500, "Failed to update org quotas", err)
|
||||
if err := hs.QuotaService.Update(c.Req.Context(), &cmd); err != nil {
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "Failed to update org quotas", err)
|
||||
}
|
||||
return response.Success("Organization quota updated")
|
||||
}
|
||||
@@ -176,7 +164,7 @@ func (hs *HTTPServer) UpdateUserQuota(c *models.ReqContext) response.Response {
|
||||
type UpdateUserQuotaParams struct {
|
||||
// in:body
|
||||
// required:true
|
||||
Body models.UpdateUserQuotaCmd `json:"body"`
|
||||
Body quota.UpdateQuotaCmd `json:"body"`
|
||||
// in:path
|
||||
// required:true
|
||||
QuotaTarget string `json:"quota_target"`
|
||||
@@ -203,7 +191,7 @@ type GetOrgQuotaParams struct {
|
||||
type UpdateOrgQuotaParam struct {
|
||||
// in:body
|
||||
// required:true
|
||||
Body models.UpdateOrgQuotaCmd `json:"body"`
|
||||
Body quota.UpdateQuotaCmd `json:"body"`
|
||||
// in:path
|
||||
// required:true
|
||||
QuotaTarget string `json:"quota_target"`
|
||||
@@ -215,5 +203,5 @@ type UpdateOrgQuotaParam struct {
|
||||
// swagger:response getQuotaResponse
|
||||
type GetQuotaResponseResponse struct {
|
||||
// in:body
|
||||
Body []*models.UserQuotaDTO `json:"body"`
|
||||
Body []*quota.QuotaDTO `json:"body"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user