roughed in limits model that incorporates bandwidth limit specs (#235)

This commit is contained in:
Michael Quigley 2023-03-10 14:25:29 -05:00
parent 86380d1836
commit 83ab21f00c
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
5 changed files with 65 additions and 21 deletions

View File

@ -1,6 +1,7 @@
package controller package controller
import ( import (
"github.com/openziti/zrok/controller/limits"
"github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/controller/zrokEdgeSdk"
"time" "time"
@ -17,12 +18,12 @@ type Config struct {
Endpoint *EndpointConfig Endpoint *EndpointConfig
Email *EmailConfig Email *EmailConfig
Influx *InfluxConfig Influx *InfluxConfig
Limits *LimitsConfig Limits *limits.Config
Maintenance *MaintenanceConfig Maintenance *MaintenanceConfig
Registration *RegistrationConfig Registration *RegistrationConfig
ResetPassword *ResetPasswordConfig ResetPassword *ResetPasswordConfig
Store *store.Config Store *store.Config
Ziti *zrokEdgeSdk.ZitiConfig Ziti *zrokEdgeSdk.Config
} }
type AdminConfig struct { type AdminConfig struct {
@ -76,19 +77,9 @@ type ResetPasswordMaintenanceConfig struct {
BatchLimit int BatchLimit int
} }
const Unlimited = -1
type LimitsConfig struct {
Environments int
Shares int
}
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{ return &Config{
Limits: &LimitsConfig{ Limits: limits.DefaultConfig(),
Environments: Unlimited,
Shares: Unlimited,
},
Maintenance: &MaintenanceConfig{ Maintenance: &MaintenanceConfig{
ResetPassword: &ResetPasswordMaintenanceConfig{ ResetPassword: &ResetPasswordMaintenanceConfig{
ExpirationTimeout: time.Minute * 15, ExpirationTimeout: time.Minute * 15,

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/middleware"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/openziti/zrok/controller/limits"
"github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/controller/zrokEdgeSdk"
"github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_model_zrok"
@ -14,10 +15,10 @@ import (
) )
type enableHandler struct { type enableHandler struct {
cfg *LimitsConfig cfg *limits.Config
} }
func newEnableHandler(cfg *LimitsConfig) *enableHandler { func newEnableHandler(cfg *limits.Config) *enableHandler {
return &enableHandler{cfg: cfg} 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 { 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) envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil { if err != nil {
return errors.Errorf("unable to find environments for account '%v': %v", principal.Email, err) return errors.Errorf("unable to find environments for account '%v': %v", principal.Email, err)

View File

@ -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,
},
},
}
}

View File

@ -3,6 +3,7 @@ package controller
import ( import (
"github.com/go-openapi/runtime/middleware" "github.com/go-openapi/runtime/middleware"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/openziti/zrok/controller/limits"
"github.com/openziti/zrok/controller/store" "github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/controller/zrokEdgeSdk" "github.com/openziti/zrok/controller/zrokEdgeSdk"
"github.com/openziti/zrok/rest_model_zrok" "github.com/openziti/zrok/rest_model_zrok"
@ -12,10 +13,10 @@ import (
) )
type shareHandler struct { type shareHandler struct {
cfg *LimitsConfig cfg *limits.Config
} }
func newShareHandler(cfg *LimitsConfig) *shareHandler { func newShareHandler(cfg *limits.Config) *shareHandler {
return &shareHandler{cfg: cfg} 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 { 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 total := 0
for i := range envs { for i := range envs {
shrs, err := str.FindSharesForEnvironment(envs[i].Id, tx) shrs, err := str.FindSharesForEnvironment(envs[i].Id, tx)

View File

@ -6,13 +6,13 @@ import (
"github.com/openziti/edge/rest_util" "github.com/openziti/edge/rest_util"
) )
type ZitiConfig struct { type Config struct {
ApiEndpoint string ApiEndpoint string
Username string Username string
Password string `cf:"+secret"` 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) caCerts, err := rest_util.GetControllerWellKnownCas(cfg.ApiEndpoint)
if err != nil { if err != nil {
return nil, err return nil, err