service cascade when processing environment disable (#52)

This commit is contained in:
Michael Quigley 2022-10-07 14:27:01 -04:00
parent c2bfa5ea09
commit e58440722d
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -5,6 +5,7 @@ import (
"github.com/jmoiron/sqlx"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/identity"
"github.com/openziti/edge/rest_management_api_client"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
@ -29,15 +30,19 @@ func (self *disableHandler) Handle(params identity.DisableParams, principal *res
logrus.Errorf("identity check failed: %v", err)
return identity.NewDisableUnauthorized()
}
if err := self.removeEnvironment(envId, tx); err != nil {
logrus.Errorf("error removing environment: %v", err)
return identity.NewDisableInternalServerError()
}
edge, err := edgeClient(self.cfg.Ziti)
if err != nil {
logrus.Errorf("error getting edge client: %v", err)
return identity.NewDisableInternalServerError()
}
if err := self.removeServicesForEnvironment(envId, tx, edge); err != nil {
logrus.Errorf("error removing services for environment: %v", err)
return identity.NewDisableInternalServerError()
}
if err := self.removeEnvironment(envId, tx); err != nil {
logrus.Errorf("error removing environment: %v", err)
return identity.NewDisableInternalServerError()
}
if err := deleteEdgeRouterPolicy(params.Body.Identity, edge); err != nil {
logrus.Errorf("error deleting edge router policy: %v", err)
return identity.NewDisableInternalServerError()
@ -65,6 +70,34 @@ func (self *disableHandler) checkZitiIdentity(id string, principal *rest_model_z
return -1, errors.Errorf("no such environment '%v'", id)
}
func (self *disableHandler) removeServicesForEnvironment(envId int, tx *sqlx.Tx, edge *rest_management_api_client.ZitiEdgeManagement) error {
svcs, err := str.FindServicesForEnvironment(envId, tx)
if err != nil {
return err
}
for _, svc := range svcs {
svcName := svc.ZrokServiceId
logrus.Infof("garbage collecting service '%v'", svcName)
if err := deleteServiceEdgeRouterPolicy(svcName, edge); err != nil {
logrus.Error(err)
}
if err := deleteServicePolicyDial(svcName, edge); err != nil {
logrus.Error(err)
}
if err := deleteServicePolicyBind(svcName, edge); err != nil {
logrus.Error(err)
}
if err := deleteConfig(svcName, edge); err != nil {
logrus.Error(err)
}
if err := deleteService(svc.ZitiServiceId, edge); err != nil {
logrus.Error(err)
}
logrus.Infof("removed service '%v'", svc.ZrokServiceId)
}
return nil
}
func (self *disableHandler) removeEnvironment(envId int, tx *sqlx.Tx) error {
if err := str.DeleteEnvironment(envId, tx); err != nil {
return errors.Wrapf(err, "error deleting environment '%d'", envId)