From bd5aaa061abf658de621fb3b94c0fafe0bbf5cd0 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 14 May 2024 14:16:20 -0400 Subject: [PATCH] the very basic store implementation for limit_classes, applied_limit_classes (#606) --- controller/store/appliedLimitClass.go | 40 +++++++++++++++++++++++++++ controller/store/limitClass.go | 31 +++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 controller/store/appliedLimitClass.go create mode 100644 controller/store/limitClass.go diff --git a/controller/store/appliedLimitClass.go b/controller/store/appliedLimitClass.go new file mode 100644 index 00000000..56c62aa2 --- /dev/null +++ b/controller/store/appliedLimitClass.go @@ -0,0 +1,40 @@ +package store + +import ( + "github.com/jmoiron/sqlx" + "github.com/pkg/errors" +) + +type AppliedLimitClass struct { + Model + AccountId int + LimitClassId int +} + +func (str *Store) ApplyLimitClass(lc *AppliedLimitClass, trx *sqlx.Tx) (int, error) { + stmt, err := trx.Prepare("insert into applied_limit_classes (account_id, limit_class_id) values ($1, $2) returning id") + if err != nil { + return 0, errors.Wrap(err, "error preparing applied_limit_classes insert statement") + } + var id int + if err := stmt.QueryRow(lc.AccountId, lc.LimitClassId).Scan(&id); err != nil { + return 0, errors.Wrap(err, "error executing applied_limit_classes insert statement") + } + return id, nil +} + +func (str *Store) FindLimitClassesForAccount(acctId int, trx *sqlx.Tx) ([]*LimitClass, error) { + rows, err := trx.Queryx("select limit_classes.* from applied_limit_classes, limit_classes where applied_limit_classes.account_id = $1 and applied_limit_classes.limit_class_id = limit_classes.id", acctId) + if err != nil { + return nil, errors.Wrap(err, "error finding limit classes for account") + } + var lcs []*LimitClass + for rows.Next() { + lc := &LimitClass{} + if err := rows.StructScan(&lc); err != nil { + return nil, errors.Wrap(err, "error scanning limit_classes") + } + lcs = append(lcs, lc) + } + return lcs, nil +} diff --git a/controller/store/limitClass.go b/controller/store/limitClass.go new file mode 100644 index 00000000..c79805eb --- /dev/null +++ b/controller/store/limitClass.go @@ -0,0 +1,31 @@ +package store + +import ( + "github.com/jmoiron/sqlx" + "github.com/openziti/zrok/sdk/golang/sdk" + "github.com/pkg/errors" +) + +type LimitClass struct { + Model + LimitScope LimitScope + LimitAction LimitAction + ShareMode sdk.ShareMode + BackendMode sdk.BackendMode + PeriodMinutes int + RxBytes int64 + TxBytes int64 + TotalBytes int64 +} + +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 + if err := stmt.QueryRow(lc.LimitScope, lc.LimitAction, lc.ShareMode, lc.BackendMode, lc.PeriodMinutes).Scan(&id); err != nil { + return 0, errors.Wrap(err, "error executing limit_classes insert statement") + } + return id, nil +}