mirror of
https://github.com/gravitl/netmaker.git
synced 2024-11-15 06:05:27 +08:00
8ce7da2ce9
* Rename var * Rename consts and use iota * Use switch instead of repeated else if * Rename limits related vars * Introduce new free tier limits * Measure new limits and report on license validation * Separate usage and limits, have new ones * Don't check for hosts and clients limits, but for machines instead * Error on egress creation @ free tier w/ internet gateways * Remove clients and hosts limit from code * Rename var * Rename consts and use iota * Use switch instead of repeated else if * Rename limits related vars * Introduce new free tier limits * Measure new limits and report on license validation * Separate usage and limits, have new ones * Don't check for hosts and clients limits, but for machines instead * Error on egress creation @ free tier w/ internet gateways * Remove clients and hosts limit from code
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
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
|
|
)
|
|
|
|
func checkFreeTierLimits(limitChoice int, next http.Handler) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var errorResponse = models.ErrorResponse{
|
|
Code: http.StatusForbidden, Message: "free tier limits exceeded on ",
|
|
}
|
|
|
|
if logic.FreeTier { // check that free tier limits not exceeded
|
|
switch limitChoice {
|
|
case limitChoiceNetworks:
|
|
currentNetworks, err := logic.GetNetworks()
|
|
if (err != nil && !database.IsEmptyRecord(err)) ||
|
|
len(currentNetworks) >= logic.NetworksLimit {
|
|
errorResponse.Message += "networks"
|
|
logic.ReturnErrorResponse(w, r, errorResponse)
|
|
return
|
|
}
|
|
case limitChoiceUsers:
|
|
users, err := logic.GetUsers()
|
|
if (err != nil && !database.IsEmptyRecord(err)) ||
|
|
len(users) >= logic.UsersLimit {
|
|
errorResponse.Message += "users"
|
|
logic.ReturnErrorResponse(w, r, errorResponse)
|
|
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"
|
|
logic.ReturnErrorResponse(w, r, errorResponse)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
}
|
|
}
|