quota.go 1.83 KB
Newer Older
1 2 3 4 5 6
package api

import (
	"github.com/grafana/grafana/pkg/bus"
	"github.com/grafana/grafana/pkg/middleware"
	m "github.com/grafana/grafana/pkg/models"
7
	"github.com/grafana/grafana/pkg/setting"
8 9 10
)

func GetOrgQuotas(c *middleware.Context) Response {
11 12 13
	if !setting.Quota.Enabled {
		return ApiError(404, "Quotas not enabled", nil)
	}
woodsaj committed
14
	query := m.GetOrgQuotasQuery{OrgId: c.ParamsInt64(":orgId")}
15 16 17 18 19 20 21 22

	if err := bus.Dispatch(&query); err != nil {
		return ApiError(500, "Failed to get org quotas", err)
	}

	return Json(200, query.Result)
}

woodsaj committed
23
func UpdateOrgQuota(c *middleware.Context, cmd m.UpdateOrgQuotaCmd) Response {
24 25 26
	if !setting.Quota.Enabled {
		return ApiError(404, "Quotas not enabled", nil)
	}
woodsaj committed
27 28 29
	cmd.OrgId = c.ParamsInt64(":orgId")
	cmd.Target = c.Params(":target")

30
	if _, ok := setting.Quota.Org.ToMap()[cmd.Target]; !ok {
woodsaj committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
		return ApiError(404, "Invalid quota target", nil)
	}

	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to update org quotas", err)
	}
	return ApiSuccess("Organization quota updated")
}

func GetUserQuotas(c *middleware.Context) Response {
	if !setting.Quota.Enabled {
		return ApiError(404, "Quotas not enabled", nil)
	}
	query := m.GetUserQuotasQuery{UserId: c.ParamsInt64(":id")}
woodsaj committed
45 46

	if err := bus.Dispatch(&query); err != nil {
woodsaj committed
47
		return ApiError(500, "Failed to get org quotas", err)
woodsaj committed
48 49 50 51 52
	}

	return Json(200, query.Result)
}

woodsaj committed
53
func UpdateUserQuota(c *middleware.Context, cmd m.UpdateUserQuotaCmd) Response {
54 55 56
	if !setting.Quota.Enabled {
		return ApiError(404, "Quotas not enabled", nil)
	}
woodsaj committed
57 58
	cmd.UserId = c.ParamsInt64(":id")
	cmd.Target = c.Params(":target")
59

60
	if _, ok := setting.Quota.User.ToMap()[cmd.Target]; !ok {
61 62 63
		return ApiError(404, "Invalid quota target", nil)
	}

64 65 66 67 68
	if err := bus.Dispatch(&cmd); err != nil {
		return ApiError(500, "Failed to update org quotas", err)
	}
	return ApiSuccess("Organization quota updated")
}