zrok/controller/shareDetail.go

78 lines
2.4 KiB
Go
Raw Normal View History

2022-12-22 21:09:37 +01:00
package controller
import (
"github.com/go-openapi/runtime/middleware"
"github.com/openziti-test-kitchen/zrok/controller/store"
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
"github.com/openziti-test-kitchen/zrok/rest_server_zrok/operations/metadata"
"github.com/sirupsen/logrus"
)
type shareDetailHandler struct{}
2022-12-22 21:09:37 +01:00
func newShareDetailHandler() *shareDetailHandler {
return &shareDetailHandler{}
2022-12-22 21:09:37 +01:00
}
func (h *shareDetailHandler) Handle(params metadata.GetShareDetailParams, principal *rest_model_zrok.Principal) middleware.Responder {
2022-12-22 21:09:37 +01:00
tx, err := str.Begin()
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
return metadata.NewGetShareDetailInternalServerError()
2022-12-22 21:09:37 +01:00
}
defer func() { _ = tx.Rollback() }()
shr, err := str.FindShareWithToken(params.ShrToken, tx)
2022-12-22 21:09:37 +01:00
if err != nil {
logrus.Errorf("error finding share '%v': %v", params.ShrToken, err)
return metadata.NewGetShareDetailNotFound()
2022-12-22 21:09:37 +01:00
}
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), tx)
if err != nil {
logrus.Errorf("error finding environments for account '%v': %v", principal.Email, err)
return metadata.NewGetShareDetailInternalServerError()
2022-12-22 21:09:37 +01:00
}
found := false
for _, env := range envs {
if shr.EnvironmentId == env.Id {
2022-12-22 21:09:37 +01:00
found = true
break
}
}
if !found {
2023-01-04 20:21:23 +01:00
logrus.Errorf("environment not matched for share '%v' for account '%v'", params.ShrToken, principal.Email)
return metadata.NewGetShareDetailNotFound()
2022-12-22 21:09:37 +01:00
}
var sparkData map[string][]int64
if cfg.Influx != nil {
2023-01-04 20:21:23 +01:00
sparkData, err = sparkDataForShares([]*store.Share{shr})
2022-12-22 21:09:37 +01:00
if err != nil {
2023-01-04 20:21:23 +01:00
logrus.Errorf("error querying spark data for share: %v", err)
2022-12-22 21:09:37 +01:00
}
}
feEndpoint := ""
if shr.FrontendEndpoint != nil {
feEndpoint = *shr.FrontendEndpoint
2022-12-22 21:09:37 +01:00
}
feSelection := ""
if shr.FrontendSelection != nil {
feSelection = *shr.FrontendSelection
2022-12-22 21:09:37 +01:00
}
beProxyEndpoint := ""
if shr.BackendProxyEndpoint != nil {
beProxyEndpoint = *shr.BackendProxyEndpoint
2022-12-22 21:09:37 +01:00
}
return metadata.NewGetShareDetailOK().WithPayload(&rest_model_zrok.Share{
Token: shr.Token,
ZID: shr.ZId,
ShareMode: shr.ShareMode,
BackendMode: shr.BackendMode,
2022-12-22 21:09:37 +01:00
FrontendSelection: feSelection,
FrontendEndpoint: feEndpoint,
BackendProxyEndpoint: beProxyEndpoint,
Reserved: shr.Reserved,
Metrics: sparkData[shr.Token],
CreatedAt: shr.CreatedAt.UnixMilli(),
UpdatedAt: shr.UpdatedAt.UnixMilli(),
2022-12-22 21:09:37 +01:00
})
}