zrok/controller/share.go

98 lines
2.8 KiB
Go
Raw Normal View History

2022-07-26 22:21:49 +02:00
package controller
import (
"github.com/go-openapi/runtime/middleware"
2022-11-22 17:35:25 +01:00
"github.com/openziti-test-kitchen/zrok/controller/store"
2022-07-26 22:21:49 +02:00
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/service"
2022-11-22 17:35:25 +01:00
"github.com/sirupsen/logrus"
2022-07-26 22:21:49 +02:00
)
type shareHandler struct{}
func newShareHandler() *shareHandler {
return &shareHandler{}
}
func (h *shareHandler) Handle(params service.ShareParams, principal *rest_model_zrok.Principal) middleware.Responder {
2022-11-22 17:35:25 +01:00
tx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return service.NewShareInternalServerError()
}
defer func() { _ = tx.Rollback() }()
envZId := params.Body.ZID
envId := 0
if envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx); err == nil {
found := false
for _, env := range envs {
if env.ZId == envZId {
logrus.Debugf("found identity '%v' for user '%v'", envZId, principal.Email)
envId = env.Id
found = true
break
}
}
if !found {
logrus.Errorf("environment '%v' not found for user '%v'", envZId, principal.Email)
return service.NewShareUnauthorized()
}
} else {
logrus.Errorf("error finding environments for account '%v'", principal.Email)
return service.NewShareInternalServerError()
}
edge, err := edgeClient()
if err != nil {
logrus.Error(err)
return service.NewShareInternalServerError()
}
svcName, err := createServiceName()
if err != nil {
logrus.Error(err)
return service.NewShareInternalServerError()
}
var svcZId string
var frontendEndpoints []string
switch params.Body.ShareMode {
case "public":
2022-11-22 17:35:25 +01:00
svcZId, frontendEndpoints, err = newPublicResourceAllocator().Allocate(envZId, svcName, params, edge)
if err != nil {
logrus.Error(err)
return service.NewShareInternalServerError()
}
default:
2022-11-22 17:35:25 +01:00
logrus.Errorf("unknown share mode '%v", params.Body.ShareMode)
return service.NewShareInternalServerError()
2022-07-27 15:49:54 +02:00
}
2022-11-22 17:35:25 +01:00
logrus.Debugf("allocated service '%v'", svcName)
sid, err := str.CreateService(envId, &store.Service{
ZId: svcZId,
Name: svcName,
2022-11-22 17:39:00 +01:00
ShareMode: params.Body.ShareMode,
BackendMode: params.Body.BackendMode,
2022-11-22 17:35:25 +01:00
FrontendEndpoint: &frontendEndpoints[0],
BackendProxyEndpoint: &params.Body.BackendProxyEndpoint,
}, tx)
if err != nil {
logrus.Errorf("error creating service record: %v", err)
_ = tx.Rollback()
return service.NewShareInternalServerError()
}
if err := tx.Commit(); err != nil {
logrus.Errorf("error committing service record: %v", err)
return service.NewShareInternalServerError()
}
logrus.Infof("recorded service '%v' with id '%v' for '%v'", svcName, sid, principal.Email)
return service.NewShareCreated().WithPayload(&rest_model_zrok.ShareResponse{
FrontendProxyEndpoint: frontendEndpoints[0],
SvcName: svcName,
})
2022-09-15 21:18:37 +02:00
}