Files
upstream-corteza/server/system/rest/apigw_route.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

163 lines
4.2 KiB
Go

package rest
import (
"context"
"github.com/cortezaproject/corteza/server/pkg/api"
"github.com/cortezaproject/corteza/server/pkg/filter"
"github.com/cortezaproject/corteza/server/system/rest/request"
"github.com/cortezaproject/corteza/server/system/service"
"github.com/cortezaproject/corteza/server/system/types"
)
type (
ApigwRoute struct {
svc routeService
ac apiGwRouteAccessController
}
routePayload struct {
*types.ApigwRoute
CanGrant bool `json:"canGrant"`
CanUpdateApigwRoute bool `json:"canUpdateApigwRoute"`
CanDeleteApigwRoute bool `json:"canDeleteApigwRoute"`
}
routeSetPayload struct {
Filter types.ApigwRouteFilter `json:"filter"`
Set []*routePayload `json:"set"`
}
routeService interface {
FindByID(ctx context.Context, ID uint64) (*types.ApigwRoute, error)
Create(ctx context.Context, new *types.ApigwRoute) (*types.ApigwRoute, error)
Update(ctx context.Context, upd *types.ApigwRoute) (*types.ApigwRoute, error)
DeleteByID(ctx context.Context, ID uint64) error
UndeleteByID(ctx context.Context, ID uint64) error
Search(ctx context.Context, filter types.ApigwRouteFilter) (types.ApigwRouteSet, types.ApigwRouteFilter, error)
}
apiGwRouteAccessController interface {
CanGrant(context.Context) bool
CanCreateApigwRoute(context.Context) bool
CanUpdateApigwRoute(context.Context, *types.ApigwRoute) bool
CanDeleteApigwRoute(context.Context, *types.ApigwRoute) bool
}
)
func (ApigwRoute) New() *ApigwRoute {
return &ApigwRoute{
svc: service.DefaultApigwRoute,
ac: service.DefaultAccessControl,
}
}
func (ctrl *ApigwRoute) List(ctx context.Context, r *request.ApigwRouteList) (interface{}, error) {
var (
err error
f = types.ApigwRouteFilter{
// todo: this should renamed to r.Endpoint after UI is aligned with this
Endpoint: r.Query,
Deleted: filter.State(r.Deleted),
// todo: this should dynamic as Delete
// but making it default to `1`, until UI is aligned with this
Disabled: filter.StateInclusive,
}
)
if f.Paging, err = filter.NewPaging(r.Limit, r.PageCursor); err != nil {
return nil, err
}
f.IncTotal = r.IncTotal
if f.Sorting, err = filter.NewSorting(r.Sort); err != nil {
return nil, err
}
set, f, err := ctrl.svc.Search(ctx, f)
return ctrl.makeFilterPayload(ctx, set, f, err)
}
func (ctrl *ApigwRoute) Create(ctx context.Context, r *request.ApigwRouteCreate) (interface{}, error) {
var (
err error
q = &types.ApigwRoute{
Endpoint: r.Endpoint,
Method: r.Method,
Enabled: r.Enabled,
}
)
q, err = ctrl.svc.Create(ctx, q)
return ctrl.makePayload(ctx, q, err)
}
func (ctrl *ApigwRoute) Read(ctx context.Context, r *request.ApigwRouteRead) (interface{}, error) {
res, err := ctrl.svc.FindByID(ctx, r.RouteID)
return ctrl.makePayload(ctx, res, err)
}
func (ctrl *ApigwRoute) Update(ctx context.Context, r *request.ApigwRouteUpdate) (interface{}, error) {
var (
err error
q = &types.ApigwRoute{
ID: r.RouteID,
Endpoint: r.Endpoint,
Method: r.Method,
Group: r.Group,
Enabled: r.Enabled,
Meta: r.Meta,
UpdatedAt: r.UpdatedAt,
}
)
q, err = ctrl.svc.Update(ctx, q)
return ctrl.makePayload(ctx, q, err)
}
func (ctrl *ApigwRoute) Delete(ctx context.Context, r *request.ApigwRouteDelete) (interface{}, error) {
return api.OK(), ctrl.svc.DeleteByID(ctx, r.RouteID)
}
func (ctrl *ApigwRoute) Undelete(ctx context.Context, r *request.ApigwRouteUndelete) (interface{}, error) {
return api.OK(), ctrl.svc.UndeleteByID(ctx, r.RouteID)
}
func (ctrl *ApigwRoute) makePayload(ctx context.Context, q *types.ApigwRoute, err error) (*routePayload, error) {
if err != nil || q == nil {
return nil, err
}
qq := &routePayload{
ApigwRoute: q,
CanGrant: ctrl.ac.CanGrant(ctx),
CanUpdateApigwRoute: ctrl.ac.CanUpdateApigwRoute(ctx, q),
CanDeleteApigwRoute: ctrl.ac.CanDeleteApigwRoute(ctx, q),
}
return qq, nil
}
func (ctrl *ApigwRoute) makeFilterPayload(ctx context.Context, nn types.ApigwRouteSet, f types.ApigwRouteFilter, err error) (*routeSetPayload, error) {
if err != nil {
return nil, err
}
msp := &routeSetPayload{Filter: f, Set: make([]*routePayload, len(nn))}
for i := range nn {
msp.Set[i], _ = ctrl.makePayload(ctx, nn[i], nil)
}
return msp, nil
}