netmaker/controllers/limits.go

60 lines
1.8 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"
2022-09-25 19:11:26 +08:00
"github.com/gravitl/netmaker/servercfg"
2022-09-14 03:25:56 +08:00
)
// limit consts
const (
node_l = 0
networks_l = 1
users_l = 2
clients_l = 3
)
func checkFreeTierLimits(limit_choice int, next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var errorResponse = models.ErrorResponse{
Code: http.StatusUnauthorized, Message: "free tier limits exceeded on networks",
}
2022-09-25 19:11:26 +08:00
if logic.Free_Tier && servercfg.Is_EE { // check that free tier limits not exceeded
2022-09-14 03:25:56 +08:00
if limit_choice == networks_l {
currentNetworks, err := logic.GetNetworks()
2022-09-15 01:26:31 +08:00
if (err != nil && !database.IsEmptyRecord(err)) || len(currentNetworks) >= logic.Networks_Limit {
logic.ReturnErrorResponse(w, r, errorResponse)
2022-09-14 03:25:56 +08:00
return
}
} else if limit_choice == node_l {
nodes, err := logic.GetAllNodes()
2022-09-15 01:26:31 +08:00
if (err != nil && !database.IsEmptyRecord(err)) || len(nodes) >= logic.Node_Limit {
2022-09-14 03:25:56 +08:00
errorResponse.Message = "free tier limits exceeded on nodes"
2022-09-15 01:26:31 +08:00
logic.ReturnErrorResponse(w, r, errorResponse)
2022-09-14 03:25:56 +08:00
return
}
} else if limit_choice == users_l {
users, err := logic.GetUsers()
2022-09-15 01:26:31 +08:00
if (err != nil && !database.IsEmptyRecord(err)) || len(users) >= logic.Users_Limit {
2022-09-14 03:25:56 +08:00
errorResponse.Message = "free tier limits exceeded on users"
2022-09-15 01:26:31 +08:00
logic.ReturnErrorResponse(w, r, errorResponse)
2022-09-14 03:25:56 +08:00
return
}
} else if limit_choice == clients_l {
clients, err := logic.GetAllExtClients()
2022-09-15 01:26:31 +08:00
if (err != nil && !database.IsEmptyRecord(err)) || len(clients) >= logic.Clients_Limit {
2022-09-14 03:25:56 +08:00
errorResponse.Message = "free tier limits exceeded on external clients"
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)
}
}