2024-05-14 20:16:20 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2024-06-05 21:20:33 +02:00
|
|
|
"fmt"
|
2024-05-14 20:16:20 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2024-06-05 21:20:33 +02:00
|
|
|
const Unlimited = -1
|
|
|
|
|
2024-06-06 19:49:36 +02:00
|
|
|
type BaseLimitClass interface {
|
2024-06-05 18:56:27 +02:00
|
|
|
IsGlobal() bool
|
|
|
|
GetLimitClassId() int
|
2024-06-06 19:49:36 +02:00
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceCountClass interface {
|
|
|
|
BaseLimitClass
|
2024-06-05 18:56:27 +02:00
|
|
|
GetEnvironments() int
|
|
|
|
GetShares() int
|
|
|
|
GetReservedShares() int
|
|
|
|
GetUniqueNames() int
|
|
|
|
}
|
|
|
|
|
2024-06-03 20:01:20 +02:00
|
|
|
type BandwidthClass interface {
|
2024-06-06 19:49:36 +02:00
|
|
|
BaseLimitClass
|
|
|
|
IsScoped() bool
|
2024-06-03 20:01:20 +02:00
|
|
|
GetBackendMode() sdk.BackendMode
|
|
|
|
GetPeriodMinutes() int
|
|
|
|
GetRxBytes() int64
|
|
|
|
GetTxBytes() int64
|
|
|
|
GetTotalBytes() int64
|
|
|
|
GetLimitAction() LimitAction
|
|
|
|
}
|
|
|
|
|
2024-05-14 20:16:20 +02:00
|
|
|
type LimitClass struct {
|
|
|
|
Model
|
2024-06-05 18:56:27 +02:00
|
|
|
ShareMode *sdk.ShareMode
|
|
|
|
BackendMode *sdk.BackendMode
|
2024-06-03 19:37:32 +02:00
|
|
|
Environments int
|
2024-05-23 20:08:14 +02:00
|
|
|
Shares int
|
|
|
|
ReservedShares int
|
|
|
|
UniqueNames int
|
|
|
|
PeriodMinutes int
|
|
|
|
RxBytes int64
|
|
|
|
TxBytes int64
|
|
|
|
TotalBytes int64
|
2024-06-03 19:37:32 +02:00
|
|
|
LimitAction LimitAction
|
2024-05-23 20:08:14 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) IsGlobal() bool {
|
2024-06-03 20:01:20 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-06 19:49:36 +02:00
|
|
|
func (lc LimitClass) IsScoped() bool {
|
|
|
|
return lc.BackendMode != nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetLimitClassId() int {
|
|
|
|
return lc.Id
|
|
|
|
}
|
|
|
|
|
2024-06-05 21:20:33 +02:00
|
|
|
func (lc LimitClass) GetEnvironments() int {
|
|
|
|
return lc.Environments
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc LimitClass) GetShares() int {
|
|
|
|
return lc.Shares
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc LimitClass) GetReservedShares() int {
|
|
|
|
return lc.ReservedShares
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc LimitClass) GetUniqueNames() int {
|
|
|
|
return lc.UniqueNames
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetShareMode() sdk.ShareMode {
|
2024-06-05 18:56:27 +02:00
|
|
|
if lc.ShareMode == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *lc.ShareMode
|
2024-06-03 20:01:20 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetBackendMode() sdk.BackendMode {
|
2024-06-05 18:56:27 +02:00
|
|
|
if lc.BackendMode == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *lc.BackendMode
|
2024-06-03 20:01:20 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetPeriodMinutes() int {
|
2024-06-03 20:01:20 +02:00
|
|
|
return lc.PeriodMinutes
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetRxBytes() int64 {
|
2024-06-03 20:01:20 +02:00
|
|
|
return lc.RxBytes
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetTxBytes() int64 {
|
2024-06-03 20:01:20 +02:00
|
|
|
return lc.TxBytes
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetTotalBytes() int64 {
|
2024-06-03 20:01:20 +02:00
|
|
|
return lc.TotalBytes
|
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
func (lc LimitClass) GetLimitAction() LimitAction {
|
2024-06-03 20:01:20 +02:00
|
|
|
return lc.LimitAction
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:08:14 +02:00
|
|
|
func (lc LimitClass) String() string {
|
2024-06-06 19:49:36 +02:00
|
|
|
out := fmt.Sprintf("LimitClass<id: %d", lc.Id)
|
2024-06-05 21:20:33 +02:00
|
|
|
if lc.ShareMode != nil {
|
|
|
|
out += fmt.Sprintf(", shareMode: '%s'", *lc.ShareMode)
|
|
|
|
}
|
|
|
|
if lc.BackendMode != nil {
|
|
|
|
out += fmt.Sprintf(", backendMode: '%s'", *lc.BackendMode)
|
|
|
|
}
|
|
|
|
if lc.Environments > Unlimited {
|
|
|
|
out += fmt.Sprintf(", environments: %d", lc.Environments)
|
|
|
|
}
|
|
|
|
if lc.Shares > Unlimited {
|
|
|
|
out += fmt.Sprintf(", shares: %d", lc.Shares)
|
|
|
|
}
|
|
|
|
if lc.ReservedShares > Unlimited {
|
|
|
|
out += fmt.Sprintf(", reservedShares: %d", lc.ReservedShares)
|
|
|
|
}
|
|
|
|
if lc.UniqueNames > Unlimited {
|
|
|
|
out += fmt.Sprintf(", uniqueNames: %d", lc.UniqueNames)
|
|
|
|
}
|
|
|
|
if lc.RxBytes > Unlimited || lc.TxBytes > Unlimited || lc.TotalBytes > Unlimited {
|
|
|
|
out += fmt.Sprintf(", periodMinutes: %d", lc.PeriodMinutes)
|
|
|
|
}
|
|
|
|
if lc.RxBytes > Unlimited {
|
|
|
|
out += fmt.Sprintf(", rxBytes: %d", lc.RxBytes)
|
|
|
|
}
|
|
|
|
if lc.TxBytes > Unlimited {
|
|
|
|
out += fmt.Sprintf(", txBytes: %d", lc.TxBytes)
|
|
|
|
}
|
|
|
|
if lc.TotalBytes > Unlimited {
|
|
|
|
out += fmt.Sprintf(", totalBytes: %d", lc.TotalBytes)
|
2024-05-23 20:08:14 +02:00
|
|
|
}
|
2024-06-05 21:20:33 +02:00
|
|
|
out += fmt.Sprintf(", limitAction: '%v'>", lc.LimitAction)
|
|
|
|
return out
|
2024-05-14 20:16:20 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 20:06:44 +02:00
|
|
|
var _ BandwidthClass = (*LimitClass)(nil)
|
|
|
|
|
2024-05-14 20:16:20 +02:00
|
|
|
func (str *Store) CreateLimitClass(lc *LimitClass, trx *sqlx.Tx) (int, error) {
|
2024-06-03 19:37:32 +02:00
|
|
|
stmt, err := trx.Prepare("insert into limit_classes (share_mode, backend_mode, environments, shares, reserved_shares, unique_names, period_minutes, rx_bytes, tx_bytes, total_bytes, limit_action) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) returning id")
|
2024-05-14 20:16:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "error preparing limit_classes insert statement")
|
|
|
|
}
|
|
|
|
var id int
|
2024-06-03 19:37:32 +02:00
|
|
|
if err := stmt.QueryRow(lc.ShareMode, lc.BackendMode, lc.Environments, lc.Shares, lc.ReservedShares, lc.UniqueNames, lc.PeriodMinutes, lc.RxBytes, lc.TxBytes, lc.TotalBytes, lc.LimitAction).Scan(&id); err != nil {
|
2024-05-14 20:16:20 +02:00
|
|
|
return 0, errors.Wrap(err, "error executing limit_classes insert statement")
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
2024-06-04 20:06:44 +02:00
|
|
|
|
|
|
|
func (str *Store) GetLimitClass(lcId int, trx *sqlx.Tx) (*LimitClass, error) {
|
|
|
|
lc := &LimitClass{}
|
|
|
|
if err := trx.QueryRowx("select * from limit_classes where id = $1", lcId).StructScan(lc); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error selecting limit_class by id")
|
|
|
|
}
|
|
|
|
return lc, nil
|
|
|
|
}
|