zrok/controller/overview.go

126 lines
3.8 KiB
Go
Raw Normal View History

2022-08-03 20:58:11 +02:00
package controller
import (
"github.com/go-openapi/runtime/middleware"
2023-05-16 19:45:43 +02:00
"github.com/jmoiron/sqlx"
"github.com/openziti/zrok/controller/store"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/rest_server_zrok/operations/metadata"
2022-08-03 21:05:28 +02:00
"github.com/sirupsen/logrus"
2022-08-03 20:58:11 +02:00
)
2023-05-16 19:45:43 +02:00
type overviewHandler struct{}
func newOverviewHandler() *overviewHandler {
return &overviewHandler{}
}
func (h *overviewHandler) Handle(_ metadata.OverviewParams, principal *rest_model_zrok.Principal) middleware.Responder {
trx, err := str.Begin()
2022-08-03 21:05:28 +02:00
if err != nil {
logrus.Errorf("error starting transaction: %v", err)
2022-09-26 22:35:06 +02:00
return metadata.NewOverviewInternalServerError()
2022-08-03 21:05:28 +02:00
}
2023-05-16 19:45:43 +02:00
defer func() { _ = trx.Rollback() }()
2023-05-16 19:45:43 +02:00
envs, err := str.FindEnvironmentsForAccount(int(principal.ID), trx)
2022-08-03 21:05:28 +02:00
if err != nil {
2022-09-09 16:20:05 +02:00
logrus.Errorf("error finding environments for '%v': %v", principal.Email, err)
2022-09-26 22:35:06 +02:00
return metadata.NewOverviewInternalServerError()
2022-08-03 21:05:28 +02:00
}
accountLimited, err := h.isAccountLimited(principal, trx)
if err != nil {
logrus.Errorf("error checking account limited for '%v': %v", principal.Email, err)
}
ovr := &rest_model_zrok.Overview{AccountLimited: accountLimited}
2022-08-03 21:05:28 +02:00
for _, env := range envs {
ear := &rest_model_zrok.EnvironmentAndResources{
Environment: &rest_model_zrok.Environment{
Address: env.Address,
Description: env.Description,
Host: env.Host,
ZID: env.ZId,
2023-05-16 19:45:43 +02:00
CreatedAt: env.CreatedAt.UnixMilli(),
UpdatedAt: env.UpdatedAt.UnixMilli(),
},
}
shrs, err := str.FindSharesForEnvironment(env.Id, trx)
if err != nil {
logrus.Errorf("error finding shares for environment '%v': %v", env.ZId, err)
return metadata.NewOverviewInternalServerError()
}
for _, shr := range shrs {
feEndpoint := ""
if shr.FrontendEndpoint != nil {
feEndpoint = *shr.FrontendEndpoint
}
feSelection := ""
if shr.FrontendSelection != nil {
feSelection = *shr.FrontendSelection
}
beProxyEndpoint := ""
if shr.BackendProxyEndpoint != nil {
beProxyEndpoint = *shr.BackendProxyEndpoint
}
envShr := &rest_model_zrok.Share{
Token: shr.Token,
ZID: shr.ZId,
ShareMode: shr.ShareMode,
BackendMode: shr.BackendMode,
FrontendSelection: feSelection,
FrontendEndpoint: feEndpoint,
BackendProxyEndpoint: beProxyEndpoint,
Reserved: shr.Reserved,
CreatedAt: shr.CreatedAt.UnixMilli(),
UpdatedAt: shr.UpdatedAt.UnixMilli(),
}
ear.Shares = append(ear.Shares, envShr)
}
fes, err := str.FindFrontendsForEnvironment(env.Id, trx)
if err != nil {
logrus.Errorf("error finding frontends for environment '%v': %v", env.ZId, err)
return metadata.NewOverviewInternalServerError()
}
for _, fe := range fes {
envFe := &rest_model_zrok.Frontend{
ID: int64(fe.Id),
Token: fe.Token,
ZID: fe.ZId,
CreatedAt: fe.CreatedAt.UnixMilli(),
UpdatedAt: fe.UpdatedAt.UnixMilli(),
}
if fe.PrivateShareId != nil {
feShr, err := str.GetShare(*fe.PrivateShareId, trx)
if err != nil {
logrus.Errorf("error getting share for frontend '%v': %v", fe.ZId, err)
return metadata.NewOverviewInternalServerError()
}
envFe.ShrToken = feShr.Token
}
ear.Frontends = append(ear.Frontends, envFe)
}
ovr.Environments = append(ovr.Environments, ear)
2023-05-16 19:45:43 +02:00
}
return metadata.NewOverviewOK().WithPayload(ovr)
2023-05-16 19:45:43 +02:00
}
func (h *overviewHandler) isAccountLimited(principal *rest_model_zrok.Principal, trx *sqlx.Tx) (bool, error) {
var je *store.BandwidthLimitJournalEntry
jEmpty, err := str.IsBandwidthLimitJournalEmpty(int(principal.ID), trx)
if err != nil {
2023-05-16 19:45:43 +02:00
return false, err
}
if !jEmpty {
je, err = str.FindLatestBandwidthLimitJournal(int(principal.ID), trx)
if err != nil {
2023-05-16 19:45:43 +02:00
return false, err
}
}
return je != nil && je.Action == store.LimitLimitAction, nil
}