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"
|
|
|
|
"github.com/openziti/zrok/controller/store"
|
2023-03-27 19:51:48 +02:00
|
|
|
"github.com/openziti/zrok/controller/zrokEdgeSdk"
|
|
|
|
"github.com/pkg/errors"
|
2023-03-27 17:34:29 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type shareRelaxAction struct {
|
|
|
|
str *store.Store
|
|
|
|
edge *rest_management_api_client.ZitiEdgeManagement
|
|
|
|
}
|
|
|
|
|
|
|
|
func newShareRelaxAction(str *store.Store, edge *rest_management_api_client.ZitiEdgeManagement) *shareRelaxAction {
|
|
|
|
return &shareRelaxAction{str, edge}
|
|
|
|
}
|
|
|
|
|
2023-03-27 19:51:48 +02:00
|
|
|
func (a *shareRelaxAction) HandleShare(s *store.Share, rxBytes, txBytes int64, limit *BandwidthPerPeriod, trx *sqlx.Tx) error {
|
2023-03-27 17:34:29 +02:00
|
|
|
logrus.Infof("relaxing '%v'", s.Token)
|
2023-03-27 19:51:48 +02:00
|
|
|
|
|
|
|
if s.ShareMode == "public" {
|
|
|
|
env, err := a.str.GetEnvironment(s.EnvironmentId, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error finding environment")
|
|
|
|
}
|
|
|
|
|
|
|
|
fe, err := a.str.FindFrontendPubliclyNamed(*s.FrontendSelection, trx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error finding frontend name '%v' for '%v'", *s.FrontendSelection, s.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := zrokEdgeSdk.CreateServicePolicyDial(env.ZId+"-"+s.ZId+"-dial", s.ZId, []string{fe.ZId}, zrokEdgeSdk.ZrokShareTags(s.Token).SubTags, a.edge); err != nil {
|
|
|
|
return errors.Wrapf(err, "error creating dial service policy for '%v'", s.Token)
|
|
|
|
}
|
|
|
|
logrus.Infof("added dial service policy for '%v'", s.Token)
|
|
|
|
|
|
|
|
} else if s.ShareMode == "private" {
|
|
|
|
return errors.New("share relax for private share not implemented")
|
|
|
|
}
|
|
|
|
|
2023-03-27 17:34:29 +02:00
|
|
|
return nil
|
|
|
|
}
|