zrok/controller/updateShare.go

64 lines
1.8 KiB
Go
Raw Permalink Normal View History

2022-12-02 19:21:05 +01:00
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/share"
2022-12-02 19:21:05 +01:00
"github.com/sirupsen/logrus"
)
type updateShareHandler struct{}
func newUpdateShareHandler() *updateShareHandler {
return &updateShareHandler{}
}
func (h *updateShareHandler) Handle(params share.UpdateShareParams, principal *rest_model_zrok.Principal) middleware.Responder {
shrToken := params.Body.ShrToken
2022-12-02 19:21:05 +01:00
backendProxyEndpoint := params.Body.BackendProxyEndpoint
tx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return share.NewUpdateShareInternalServerError()
2022-12-02 19:21:05 +01:00
}
defer func() { _ = tx.Rollback() }()
sshr, err := str.FindShareWithToken(shrToken, tx)
2022-12-02 19:21:05 +01:00
if err != nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("share '%v' not found: %v", shrToken, err)
return share.NewUpdateShareNotFound()
2022-12-02 19:21:05 +01:00
}
senvs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil {
logrus.Errorf("error finding environments for account '%v': %v", principal.Email, err)
return share.NewUpdateShareInternalServerError()
2022-12-02 19:21:05 +01:00
}
envFound := false
for _, senv := range senvs {
2023-06-14 19:18:44 +02:00
if senv.Id == sshr.EnvironmentId {
2022-12-02 19:21:05 +01:00
envFound = true
break
}
}
if !envFound {
2023-01-04 20:21:23 +01:00
logrus.Errorf("environment not found for share '%v'", shrToken)
return share.NewUpdateShareNotFound()
2022-12-02 19:21:05 +01:00
}
sshr.BackendProxyEndpoint = &backendProxyEndpoint
if err := str.UpdateShare(sshr, tx); err != nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("error updating share '%v': %v", shrToken, err)
return share.NewUpdateShareInternalServerError()
2022-12-02 19:21:05 +01:00
}
if err := tx.Commit(); err != nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("error committing transaction for share '%v' update: %v", shrToken, err)
return share.NewUpdateShareInternalServerError()
2022-12-02 19:21:05 +01:00
}
return share.NewUpdateShareOK()
2022-12-02 19:21:05 +01:00
}