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

View File

@ -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)

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 (
"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)

View File

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