mirror of
https://github.com/grafana/grafana.git
synced 2025-02-10 23:55:47 -06:00
implement updateQuota function
This commit is contained in:
parent
c238130842
commit
3d4d822528
@ -19,6 +19,11 @@ func GetOrgQuotas(c *middleware.Context) Response {
|
||||
func UpdateOrgQuota(c *middleware.Context, cmd m.UpdateQuotaCmd) Response {
|
||||
cmd.OrgId = c.ParamsInt64(":orgId")
|
||||
cmd.Target = m.QuotaTarget(c.Params(":target"))
|
||||
|
||||
if !cmd.Target.IsValid() {
|
||||
return ApiError(404, "Invalid quota target", nil)
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&cmd); err != nil {
|
||||
return ApiError(500, "Failed to update org quotas", err)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"time"
|
||||
@ -16,6 +17,13 @@ const (
|
||||
QUOTA_COLLECTOR QuotaTarget = "collector"
|
||||
)
|
||||
|
||||
var ErrInvalidQuotaTarget = errors.New("Invalid quota target")
|
||||
|
||||
func (q QuotaTarget) IsValid() bool {
|
||||
_, ok := DefaultQuotas[q]
|
||||
return ok
|
||||
}
|
||||
|
||||
// defaults are set from settings package.
|
||||
var DefaultQuotas map[QuotaTarget]int64
|
||||
|
||||
@ -64,6 +72,9 @@ type UpdateQuotaCmd struct {
|
||||
}
|
||||
|
||||
func QuotaReached(org_id int64, target QuotaTarget) (bool, error) {
|
||||
if !target.IsValid() {
|
||||
return true, ErrInvalidQuotaTarget
|
||||
}
|
||||
query := GetQuotaByTargetQuery{OrgId: org_id, Target: target}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return true, err
|
||||
|
@ -86,5 +86,29 @@ func GetQuotas(query *m.GetQuotasQuery) error {
|
||||
}
|
||||
|
||||
func UpdateQuota(cmd *m.UpdateQuotaCmd) error {
|
||||
return nil
|
||||
return inTransaction2(func(sess *session) error {
|
||||
//Check if quota is already defined in the DB
|
||||
quota := m.Quota{
|
||||
Target: cmd.Target,
|
||||
OrgId: cmd.OrgId,
|
||||
}
|
||||
has, err := sess.Get(quota)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
quota.Limit = cmd.Limit
|
||||
if has == false {
|
||||
//No quota in the DB for this target, so create a new one.
|
||||
if _, err := sess.Insert("a); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
//update existing quota entry in the DB.
|
||||
if _, err := sess.Id(quota.Id).Update("a); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user