2022-12-02 19:21:05 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/go-openapi/runtime/middleware"
|
2023-01-13 21:01:34 +01:00
|
|
|
"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{}
|
|
|
|
}
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
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)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUpdateShareInternalServerError()
|
2022-12-02 19:21:05 +01:00
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
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)
|
2023-01-04 19:43:37 +01:00
|
|
|
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)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUpdateShareInternalServerError()
|
2022-12-02 19:21:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
envFound := false
|
|
|
|
for _, senv := range senvs {
|
2023-03-10 16:57:59 +01:00
|
|
|
if !senv.Deleted && senv.Id == sshr.Id {
|
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)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUpdateShareNotFound()
|
2022-12-02 19:21:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:13:50 +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)
|
2023-01-04 19:43:37 +01:00
|
|
|
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)
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUpdateShareInternalServerError()
|
2022-12-02 19:21:05 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 19:43:37 +01:00
|
|
|
return share.NewUpdateShareOK()
|
2022-12-02 19:21:05 +01:00
|
|
|
}
|