better deactivation (#10)

This commit is contained in:
Michael Quigley 2022-07-29 16:27:03 -04:00
parent 4080e219a3
commit d82b75f3b2
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 16 additions and 10 deletions

View File

@ -53,14 +53,15 @@ func (self *Store) FindServicesForAccount(accountId int, tx *sqlx.Tx) ([]*Servic
return svcs, nil return svcs, nil
} }
func (self *Store) DeactivateService(id int, tx *sqlx.Tx) error { func (self *Store) UpdateService(svc *Service, tx *sqlx.Tx) error {
stmt, err := tx.Prepare("update services set active=false where id = ?") sql := "update services set ziti_id = ?, endpoint = ?, active = ?, updated_at = strftime('%Y-%m-%d %H:%M:%f', 'now') where id = ?"
stmt, err := tx.Prepare(sql)
if err != nil { if err != nil {
return errors.Wrap(err, "error preparing services deactivate statement") return errors.Wrap(err, "error preparing services update statement")
} }
_, err = stmt.Exec(id) _, err = stmt.Exec(svc.ZitiId, svc.Endpoint, svc.Active, svc.Id)
if err != nil { if err != nil {
return errors.Wrap(err, "error executing services deactivate statement") return errors.Wrap(err, "error executing services update statement")
} }
return nil return nil
} }

View File

@ -59,19 +59,24 @@ func untunnelHandler(params tunnel.UntunnelParams, principal *rest_model_zrok.Pr
logrus.Errorf("error finding services for account: %v", err) logrus.Errorf("error finding services for account: %v", err)
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
} }
changed := false
for _, svc := range svcs { for _, svc := range svcs {
if svc.ZitiId == svcId { if svc.ZitiId == svcId {
if err := str.DeactivateService(svc.Id, tx); err != nil { svc.Active = false
if err := str.UpdateService(svc, tx); err != nil {
logrus.Errorf("error deactivating service '%v': %v", svcId, err) logrus.Errorf("error deactivating service '%v': %v", svcId, err)
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error())) return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
} }
if err := tx.Commit(); err != nil { changed = true
logrus.Errorf("error committing: %v", err)
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
logrus.Infof("deactivated service '%v'", svcId) logrus.Infof("deactivated service '%v'", svcId)
} }
} }
if changed {
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing: %v", err)
return tunnel.NewUntunnelInternalServerError().WithPayload(rest_model_zrok.ErrorMessage(err.Error()))
}
}
return tunnel.NewUntunnelOK() return tunnel.NewUntunnelOK()
} }