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