2015-07-20 07:51:27 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2015-07-20 07:51:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
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 {
|
|
|
|
return hs.getOrgQuotasHelper(c, c.ParamsInt64(":orgId"))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, 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{}
|
|
|
|
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
|
|
|
}
|
2015-09-11 10:17:10 -05:00
|
|
|
cmd.OrgId = c.ParamsInt64(":orgId")
|
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
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
func 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
|
|
|
}
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetUserQuotasQuery{UserId: c.ParamsInt64(":id")}
|
2015-09-10 12:47:33 -05:00
|
|
|
|
2021-09-20 10:05:30 -05:00
|
|
|
if err := bus.DispatchCtx(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
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.JSON(200, query.Result)
|
2015-09-10 12:47:33 -05:00
|
|
|
}
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func UpdateUserQuota(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.UpdateUserQuotaCmd{}
|
|
|
|
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
|
|
|
}
|
2015-09-11 10:17:10 -05:00
|
|
|
cmd.UserId = c.ParamsInt64(":id")
|
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
|
|
|
}
|
|
|
|
|
2021-09-20 10:05:30 -05:00
|
|
|
if err := bus.DispatchCtx(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
|
|
|
}
|