2023-03-27 17:34:29 +02:00
|
|
|
package limits
|
|
|
|
|
|
|
|
import (
|
2023-03-27 19:51:48 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
2023-03-27 17:34:29 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
2023-03-27 21:29:25 +02:00
|
|
|
"github.com/openziti/zrok/controller/emailUi"
|
2023-03-27 17:34:29 +02:00
|
|
|
"github.com/openziti/zrok/controller/store"
|
2023-03-27 21:29:25 +02:00
|
|
|
"github.com/pkg/errors"
|
2023-03-27 17:34:29 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type shareWarningAction struct {
|
|
|
|
str *store.Store
|
|
|
|
edge *rest_management_api_client.ZitiEdgeManagement
|
2023-03-27 21:29:25 +02:00
|
|
|
cfg *emailUi.Config
|
2023-03-27 17:34:29 +02:00
|
|
|
}
|
|
|
|
|
2023-03-27 21:29:25 +02:00
|
|
|
func newShareWarningAction(cfg *emailUi.Config, str *store.Store, edge *rest_management_api_client.ZitiEdgeManagement) *shareWarningAction {
|
|
|
|
return &shareWarningAction{str, edge, cfg}
|
2023-03-27 17:34:29 +02:00
|
|
|
}
|
|
|
|
|
2023-03-27 21:29:25 +02:00
|
|
|
func (a *shareWarningAction) HandleShare(shr *store.Share, rxBytes, txBytes int64, limit *BandwidthPerPeriod, trx *sqlx.Tx) error {
|
|
|
|
logrus.Infof("warning '%v'", shr.Token)
|
|
|
|
|
|
|
|
env, err := a.str.GetEnvironment(shr.EnvironmentId, trx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:39:42 +02:00
|
|
|
if env.AccountId != nil {
|
|
|
|
acct, err := a.str.GetAccount(*env.AccountId, trx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sendLimitWarningEmail(a.cfg, acct.Email, limit, rxBytes, txBytes); err != nil {
|
|
|
|
return errors.Wrapf(err, "error sending limit warning email to '%v'", acct.Email)
|
|
|
|
}
|
2023-03-27 21:29:25 +02:00
|
|
|
}
|
|
|
|
|
2023-03-27 17:34:29 +02:00
|
|
|
return nil
|
|
|
|
}
|