netmaker/controllers/limits.go

76 lines
2.1 KiB
Go
Raw Normal View History

2022-09-14 03:25:56 +08:00
package controller
import (
"net/http"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
)
// limit consts
const (
limitChoiceNetworks = iota
limitChoiceUsers
limitChoiceMachines
limitChoiceIngress
limitChoiceEgress
2022-09-14 03:25:56 +08:00
)
func checkFreeTierLimits(limitChoice int, next http.Handler) http.HandlerFunc {
2022-09-14 03:25:56 +08:00
return func(w http.ResponseWriter, r *http.Request) {
var errorResponse = models.ErrorResponse{
Code: http.StatusForbidden, Message: "free tier limits exceeded on ",
2022-09-14 03:25:56 +08:00
}
if logic.FreeTier { // check that free tier limits not exceeded
switch limitChoice {
case limitChoiceNetworks:
2022-09-14 03:25:56 +08:00
currentNetworks, err := logic.GetNetworks()
if (err != nil && !database.IsEmptyRecord(err)) ||
len(currentNetworks) >= logic.NetworksLimit {
errorResponse.Message += "networks"
2022-09-15 01:26:31 +08:00
logic.ReturnErrorResponse(w, r, errorResponse)
2022-09-14 03:25:56 +08:00
return
}
case limitChoiceUsers:
2022-09-14 03:25:56 +08:00
users, err := logic.GetUsers()
if (err != nil && !database.IsEmptyRecord(err)) ||
len(users) >= logic.UsersLimit {
errorResponse.Message += "users"
2022-09-15 01:26:31 +08:00
logic.ReturnErrorResponse(w, r, errorResponse)
2022-09-14 03:25:56 +08:00
return
}
case limitChoiceMachines:
hosts, hErr := logic.GetAllHosts()
clients, cErr := logic.GetAllExtClients()
if (hErr != nil && !database.IsEmptyRecord(hErr)) ||
(cErr != nil && !database.IsEmptyRecord(cErr)) ||
len(hosts)+len(clients) >= logic.MachinesLimit {
errorResponse.Message += "machines"
logic.ReturnErrorResponse(w, r, errorResponse)
return
}
case limitChoiceIngress:
ingresses, err := logic.GetAllIngresses()
if (err != nil && !database.IsEmptyRecord(err)) ||
len(ingresses) >= logic.IngressesLimit {
errorResponse.Message += "ingresses"
logic.ReturnErrorResponse(w, r, errorResponse)
return
}
case limitChoiceEgress:
egresses, err := logic.GetAllEgresses()
if (err != nil && !database.IsEmptyRecord(err)) ||
len(egresses) >= logic.EgressesLimit {
errorResponse.Message += "egresses"
2022-09-15 01:26:31 +08:00
logic.ReturnErrorResponse(w, r, errorResponse)
2022-09-14 03:25:56 +08:00
return
}
}
}
next.ServeHTTP(w, r)
}
}