2024-05-14 20:16:20 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2024-05-23 20:08:14 +02:00
|
|
|
"encoding/json"
|
2024-05-14 20:16:20 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LimitClass struct {
|
|
|
|
Model
|
2024-05-23 20:08:14 +02:00
|
|
|
LimitScope LimitScope
|
|
|
|
LimitAction LimitAction
|
|
|
|
ShareMode sdk.ShareMode
|
|
|
|
BackendMode sdk.BackendMode
|
|
|
|
Shares int
|
|
|
|
ReservedShares int
|
|
|
|
UniqueNames int
|
|
|
|
PeriodMinutes int
|
|
|
|
RxBytes int64
|
|
|
|
TxBytes int64
|
|
|
|
TotalBytes int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc LimitClass) String() string {
|
|
|
|
out, err := json.MarshalIndent(&lc, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
|
|
|
|
}
|
|
|
|
return string(out)
|
2024-05-14 20:16:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (str *Store) CreateLimitClass(lc *LimitClass, trx *sqlx.Tx) (int, error) {
|
|
|
|
stmt, err := trx.Prepare("insert into limit_classes (limit_scope, limit_action, share_mode, backend_mode, period_minutes, rx_bytes, tx_bytes, total_bytes) values ($1, $2, $3, $4, $5, $6, $7, $8) returning id")
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "error preparing limit_classes insert statement")
|
|
|
|
}
|
|
|
|
var id int
|
2024-05-31 21:18:07 +02:00
|
|
|
if err := stmt.QueryRow(lc.LimitScope, lc.LimitAction, lc.ShareMode, lc.BackendMode, lc.PeriodMinutes, lc.RxBytes, lc.TxBytes, lc.TotalBytes).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
|
|
|
|
}
|