grafana/pkg/api/quota.go

68 lines
1.7 KiB
Go
Raw Normal View History

package api
import (
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
2015-09-10 12:51:12 -05:00
"github.com/grafana/grafana/pkg/setting"
)
2018-03-07 10:54:50 -06:00
func GetOrgQuotas(c *m.ReqContext) Response {
2015-09-10 12:51:12 -05:00
if !setting.Quota.Enabled {
2018-03-22 16:13:46 -05:00
return Error(404, "Quotas not enabled", nil)
2015-09-10 12:51:12 -05:00
}
query := m.GetOrgQuotasQuery{OrgId: c.ParamsInt64(":orgId")}
if err := bus.Dispatch(&query); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to get org quotas", err)
}
2018-03-22 16:13:46 -05:00
return JSON(200, query.Result)
}
2018-03-07 10:54:50 -06:00
func UpdateOrgQuota(c *m.ReqContext, cmd m.UpdateOrgQuotaCmd) Response {
2015-09-10 12:51:12 -05:00
if !setting.Quota.Enabled {
2018-03-22 16:13:46 -05:00
return Error(404, "Quotas not enabled", nil)
2015-09-10 12:51:12 -05:00
}
cmd.OrgId = c.ParamsInt64(":orgId")
cmd.Target = c.Params(":target")
if _, ok := setting.Quota.Org.ToMap()[cmd.Target]; !ok {
2018-03-22 16:13:46 -05:00
return Error(404, "Invalid quota target", nil)
}
if err := bus.Dispatch(&cmd); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to update org quotas", err)
}
2018-03-22 16:13:46 -05:00
return Success("Organization quota updated")
}
2018-03-07 10:54:50 -06:00
func GetUserQuotas(c *m.ReqContext) Response {
if !setting.Quota.Enabled {
2018-03-22 16:13:46 -05:00
return Error(404, "Quotas not enabled", nil)
}
query := m.GetUserQuotasQuery{UserId: c.ParamsInt64(":id")}
2015-09-10 12:47:33 -05:00
if err := bus.Dispatch(&query); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to get org quotas", err)
2015-09-10 12:47:33 -05:00
}
2018-03-22 16:13:46 -05:00
return JSON(200, query.Result)
2015-09-10 12:47:33 -05:00
}
2018-03-07 10:54:50 -06:00
func UpdateUserQuota(c *m.ReqContext, cmd m.UpdateUserQuotaCmd) Response {
2015-09-10 12:51:12 -05:00
if !setting.Quota.Enabled {
2018-03-22 16:13:46 -05:00
return Error(404, "Quotas not enabled", nil)
2015-09-10 12:51:12 -05:00
}
cmd.UserId = c.ParamsInt64(":id")
cmd.Target = c.Params(":target")
2015-07-21 05:30:31 -05:00
if _, ok := setting.Quota.User.ToMap()[cmd.Target]; !ok {
2018-03-22 16:13:46 -05:00
return Error(404, "Invalid quota target", nil)
2015-07-21 05:30:31 -05:00
}
if err := bus.Dispatch(&cmd); err != nil {
2018-03-22 16:13:46 -05:00
return Error(500, "Failed to update org quotas", err)
}
2018-03-22 16:13:46 -05:00
return Success("Organization quota updated")
}