2015-07-20 07:51:27 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2021-11-29 03:18:01 -06:00
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-09-10 12:51:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-07-20 07:51:27 -05:00
|
|
|
)
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
func (hs *HTTPServer) GetCurrentOrgQuotas(c *models.ReqContext) response.Response {
|
|
|
|
return hs.getOrgQuotasHelper(c, c.OrgId)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) GetOrgQuotas(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
|
|
|
}
|
|
|
|
return hs.getOrgQuotasHelper(c, orgId)
|
2021-10-27 06:13:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (hs *HTTPServer) getOrgQuotasHelper(c *models.ReqContext, orgID int64) response.Response {
|
|
|
|
if !hs.Cfg.Quota.Enabled {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Quotas not enabled", nil)
|
2015-09-10 12:51:12 -05:00
|
|
|
}
|
2021-10-27 06:13:59 -05:00
|
|
|
query := models.GetOrgQuotasQuery{OrgId: orgID}
|
2015-07-20 07:51:27 -05:00
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
if err := hs.SQLStore.GetOrgQuotas(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get org quotas", err)
|
2015-07-20 07:51:27 -05:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, query.Result)
|
2015-07-20 07:51:27 -05:00
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) UpdateOrgQuota(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.UpdateOrgQuotaCmd{}
|
2022-01-14 10:55:57 -06:00
|
|
|
var err error
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2021-10-27 06:13:59 -05:00
|
|
|
if !hs.Cfg.Quota.Enabled {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Quotas not enabled", nil)
|
2015-09-10 12:51:12 -05:00
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
cmd.OrgId, err = strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
|
|
|
}
|
2021-10-11 07:30:59 -05:00
|
|
|
cmd.Target = web.Params(c.Req)[":target"]
|
2015-09-11 10:17:10 -05:00
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
if _, ok := hs.Cfg.Quota.Org.ToMap()[cmd.Target]; !ok {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Invalid quota target", nil)
|
2015-09-11 10:17:10 -05:00
|
|
|
}
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
if err := hs.SQLStore.UpdateOrgQuota(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update org quotas", err)
|
2015-09-11 10:17:10 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Organization quota updated")
|
2015-09-11 10:17:10 -05:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:20:20 -06:00
|
|
|
func (hs *HTTPServer) GetUserQuotas(c *models.ReqContext) response.Response {
|
2015-09-11 10:17:10 -05:00
|
|
|
if !setting.Quota.Enabled {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Quotas not enabled", nil)
|
2015-09-11 10:17:10 -05:00
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
|
|
|
|
id, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := models.GetUserQuotasQuery{UserId: id}
|
2015-09-10 12:47:33 -05:00
|
|
|
|
2022-02-03 02:20:20 -06:00
|
|
|
if err := hs.SQLStore.GetUserQuotas(c.Req.Context(), &query); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to get org quotas", err)
|
2015-09-10 12:47:33 -05:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, query.Result)
|
2015-09-10 12:47:33 -05:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:20:20 -06:00
|
|
|
func (hs *HTTPServer) UpdateUserQuota(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
cmd := models.UpdateUserQuotaCmd{}
|
2022-01-14 10:55:57 -06:00
|
|
|
var err error
|
2021-11-29 03:18:01 -06:00
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2015-09-10 12:51:12 -05:00
|
|
|
if !setting.Quota.Enabled {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Quotas not enabled", nil)
|
2015-09-10 12:51:12 -05:00
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
cmd.UserId, err = strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2021-10-11 07:30:59 -05:00
|
|
|
cmd.Target = web.Params(c.Req)[":target"]
|
2015-07-21 05:30:31 -05:00
|
|
|
|
2015-09-15 07:31:58 -05:00
|
|
|
if _, ok := setting.Quota.User.ToMap()[cmd.Target]; !ok {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, "Invalid quota target", nil)
|
2015-07-21 05:30:31 -05:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:20:20 -06:00
|
|
|
if err := hs.SQLStore.UpdateUserQuota(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update org quotas", err)
|
2015-07-20 07:51:27 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Organization quota updated")
|
2015-07-20 07:51:27 -05:00
|
|
|
}
|