2015-09-18 14:36:58 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-01-27 15:50:36 +08:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2018-03-08 06:19:35 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2021-10-11 20:30:59 +08:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-09-18 14:36:58 +08:00
|
|
|
)
|
|
|
|
|
2019-02-12 04:12:01 +08:00
|
|
|
// Quota returns a function that returns a function used to call quotaservice based on target name
|
2022-02-10 19:42:06 +08:00
|
|
|
func Quota(quotaService quota.Service) func(string) web.Handler {
|
2021-08-25 21:11:22 +08:00
|
|
|
if quotaService == nil {
|
|
|
|
panic("quotaService is nil")
|
|
|
|
}
|
2019-02-12 04:12:01 +08:00
|
|
|
//https://open.spotify.com/track/7bZSoBEAEEUsGEuLOf94Jm?si=T1Tdju5qRSmmR0zph_6RBw fuuuuunky
|
2022-11-15 03:08:10 +08:00
|
|
|
return func(targetSrv string) web.Handler {
|
2023-01-27 15:50:36 +08:00
|
|
|
return func(c *contextmodel.ReqContext) {
|
2022-11-15 03:08:10 +08:00
|
|
|
limitReached, err := quotaService.QuotaReached(c, quota.TargetSrv(targetSrv))
|
2019-02-12 04:12:01 +08:00
|
|
|
if err != nil {
|
2020-12-16 02:09:04 +08:00
|
|
|
c.JsonApiErr(500, "Failed to get quota", err)
|
2019-02-12 04:12:01 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if limitReached {
|
2022-11-15 03:08:10 +08:00
|
|
|
c.JsonApiErr(403, fmt.Sprintf("%s Quota reached", targetSrv), nil)
|
2019-02-12 04:12:01 +08:00
|
|
|
return
|
|
|
|
}
|
2015-09-18 14:36:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|