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 (
|
2023-08-09 01:47:49 +08:00
|
|
|
limitChoiceNetworks = iota
|
|
|
|
limitChoiceUsers
|
|
|
|
limitChoiceMachines
|
|
|
|
limitChoiceIngress
|
|
|
|
limitChoiceEgress
|
2022-09-14 03:25:56 +08:00
|
|
|
)
|
|
|
|
|
2023-08-09 01:47:49 +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{
|
2023-08-09 01:47:49 +08:00
|
|
|
Code: http.StatusForbidden, Message: "free tier limits exceeded on ",
|
2022-09-14 03:25:56 +08:00
|
|
|
}
|
|
|
|
|
2023-08-09 01:47:49 +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()
|
2023-08-09 01:47:49 +08:00
|
|
|
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
|
|
|
|
}
|
2023-08-09 01:47:49 +08:00
|
|
|
case limitChoiceUsers:
|
2022-09-14 03:25:56 +08:00
|
|
|
users, err := logic.GetUsers()
|
2023-08-09 01:47:49 +08:00
|
|
|
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
|
|
|
|
}
|
2023-08-09 01:47:49 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|