zrok/controller/frontendDetail.go
2023-05-17 13:53:18 -04:00

61 lines
1.9 KiB
Go

package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/metadata"
"github.com/sirupsen/logrus"
)
type getFrontendDetailHandler struct{}
func newGetFrontendDetailHandler() *getFrontendDetailHandler {
return &getFrontendDetailHandler{}
}
func (h *getFrontendDetailHandler) Handle(params metadata.GetFrontendDetailParams, principal *rest_model_zrok.Principal) middleware.Responder {
trx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return metadata.NewGetFrontendDetailInternalServerError()
}
defer func() { _ = trx.Rollback() }()
fe, err := str.GetFrontend(int(params.FeID), trx)
if err != nil {
logrus.Errorf("error finding share '%d': %v", params.FeID, err)
return metadata.NewGetFrontendDetailNotFound()
}
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), trx)
if err != nil {
logrus.Errorf("error finding environments for account '%v': %v", principal.Email, err)
return metadata.NewGetFrontendDetailInternalServerError()
}
found := false
if fe.EnvironmentId == nil {
logrus.Errorf("non owned environment '%d' for '%v'", fe.Id, principal.Email)
return metadata.NewGetFrontendDetailNotFound()
}
for _, env := range envs {
if *fe.EnvironmentId == env.Id {
found = true
break
}
}
if !found {
logrus.Errorf("environment not matched for frontend '%d' for account '%v'", fe.Id, principal.Email)
return metadata.NewGetFrontendDetailNotFound()
}
shr, err := str.GetShare(fe.Id, trx)
if err != nil {
logrus.Errorf("error getting share for frontend '%d': %v", fe.Id, err)
return metadata.NewGetFrontendDetailInternalServerError()
}
return metadata.NewGetFrontendDetailOK().WithPayload(&rest_model_zrok.Frontend{
ID: int64(fe.Id),
ShrToken: shr.Token,
ZID: fe.ZId,
CreatedAt: fe.CreatedAt.UnixMilli(),
UpdatedAt: fe.UpdatedAt.UnixMilli(),
})
}