From 83ab21f00ccdf5ac12ac47a62b692a68422954b6 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Fri, 10 Mar 2023 14:25:29 -0500 Subject: [PATCH] roughed in limits model that incorporates bandwidth limit specs (#235) --- controller/config.go | 17 +++-------- controller/enable.go | 7 +++-- controller/limits/model.go | 51 ++++++++++++++++++++++++++++++++ controller/share.go | 7 +++-- controller/zrokEdgeSdk/client.go | 4 +-- 5 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 controller/limits/model.go diff --git a/controller/config.go b/controller/config.go index df68e46e..7be959f0 100644 --- a/controller/config.go +++ b/controller/config.go @@ -1,6 +1,7 @@ package controller import ( + "github.com/openziti/zrok/controller/limits" "github.com/openziti/zrok/controller/zrokEdgeSdk" "time" @@ -17,12 +18,12 @@ type Config struct { Endpoint *EndpointConfig Email *EmailConfig Influx *InfluxConfig - Limits *LimitsConfig + Limits *limits.Config Maintenance *MaintenanceConfig Registration *RegistrationConfig ResetPassword *ResetPasswordConfig Store *store.Config - Ziti *zrokEdgeSdk.ZitiConfig + Ziti *zrokEdgeSdk.Config } type AdminConfig struct { @@ -76,19 +77,9 @@ type ResetPasswordMaintenanceConfig struct { BatchLimit int } -const Unlimited = -1 - -type LimitsConfig struct { - Environments int - Shares int -} - func DefaultConfig() *Config { return &Config{ - Limits: &LimitsConfig{ - Environments: Unlimited, - Shares: Unlimited, - }, + Limits: limits.DefaultConfig(), Maintenance: &MaintenanceConfig{ ResetPassword: &ResetPasswordMaintenanceConfig{ ExpirationTimeout: time.Minute * 15, diff --git a/controller/enable.go b/controller/enable.go index 0b321e11..1025c562 100644 --- a/controller/enable.go +++ b/controller/enable.go @@ -5,6 +5,7 @@ import ( "encoding/json" "github.com/go-openapi/runtime/middleware" "github.com/jmoiron/sqlx" + "github.com/openziti/zrok/controller/limits" "github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/rest_model_zrok" @@ -14,10 +15,10 @@ import ( ) type enableHandler struct { - cfg *LimitsConfig + cfg *limits.Config } -func newEnableHandler(cfg *LimitsConfig) *enableHandler { +func newEnableHandler(cfg *limits.Config) *enableHandler { return &enableHandler{cfg: cfg} } @@ -100,7 +101,7 @@ func (h *enableHandler) Handle(params environment.EnableParams, principal *rest_ } func (h *enableHandler) checkLimits(principal *rest_model_zrok.Principal, tx *sqlx.Tx) error { - if !principal.Limitless && h.cfg.Environments > Unlimited { + if !principal.Limitless && h.cfg.Environments > limits.Unlimited { envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx) if err != nil { return errors.Errorf("unable to find environments for account '%v': %v", principal.Email, err) diff --git a/controller/limits/model.go b/controller/limits/model.go new file mode 100644 index 00000000..7622005e --- /dev/null +++ b/controller/limits/model.go @@ -0,0 +1,51 @@ +package limits + +import "time" + +const Unlimited = -1 + +type Config struct { + Environments int + Shares int + Bandwidth *BandwidthConfig +} + +type BandwidthConfig struct { + PerAccount *BandwidthPerPeriod + PerEnvironment *BandwidthPerPeriod + PerShare *BandwidthPerPeriod +} + +type BandwidthPerPeriod struct { + Period time.Duration + Rx int64 + Tx int64 + Total int64 +} + +func DefaultConfig() *Config { + return &Config{ + Environments: Unlimited, + Shares: Unlimited, + Bandwidth: &BandwidthConfig{ + PerAccount: &BandwidthPerPeriod{ + Period: 365 * (24 * time.Hour), + Rx: Unlimited, + Tx: Unlimited, + Total: Unlimited, + }, + PerEnvironment: &BandwidthPerPeriod{ + Period: 365 * (24 * time.Hour), + Rx: Unlimited, + Tx: Unlimited, + Total: Unlimited, + }, + PerShare: &BandwidthPerPeriod{ + Period: 365 * (24 * time.Hour), + Rx: Unlimited, + Tx: Unlimited, + Total: Unlimited, + }, + }, + } +} diff --git a/controller/share.go b/controller/share.go index a79965ab..a25b604b 100644 --- a/controller/share.go +++ b/controller/share.go @@ -3,6 +3,7 @@ package controller import ( "github.com/go-openapi/runtime/middleware" "github.com/jmoiron/sqlx" + "github.com/openziti/zrok/controller/limits" "github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/rest_model_zrok" @@ -12,10 +13,10 @@ import ( ) type shareHandler struct { - cfg *LimitsConfig + cfg *limits.Config } -func newShareHandler(cfg *LimitsConfig) *shareHandler { +func newShareHandler(cfg *limits.Config) *shareHandler { return &shareHandler{cfg: cfg} } @@ -144,7 +145,7 @@ func (h *shareHandler) Handle(params share.ShareParams, principal *rest_model_zr } func (h *shareHandler) checkLimits(principal *rest_model_zrok.Principal, envs []*store.Environment, tx *sqlx.Tx) error { - if !principal.Limitless && h.cfg.Shares > Unlimited { + if !principal.Limitless && h.cfg.Shares > limits.Unlimited { total := 0 for i := range envs { shrs, err := str.FindSharesForEnvironment(envs[i].Id, tx) diff --git a/controller/zrokEdgeSdk/client.go b/controller/zrokEdgeSdk/client.go index ace4b123..e1890835 100644 --- a/controller/zrokEdgeSdk/client.go +++ b/controller/zrokEdgeSdk/client.go @@ -6,13 +6,13 @@ import ( "github.com/openziti/edge/rest_util" ) -type ZitiConfig struct { +type Config struct { ApiEndpoint string Username string Password string `cf:"+secret"` } -func Client(cfg *ZitiConfig) (*rest_management_api_client.ZitiEdgeManagement, error) { +func Client(cfg *Config) (*rest_management_api_client.ZitiEdgeManagement, error) { caCerts, err := rest_util.GetControllerWellKnownCas(cfg.ApiEndpoint) if err != nil { return nil, err