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
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package automation
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/cortezaproject/corteza/server/compose/types"
|
|
)
|
|
|
|
type (
|
|
moduleService interface {
|
|
FindByID(ctx context.Context, namespaceID, moduleID uint64) (*types.Module, error)
|
|
FindByHandle(ctx context.Context, namespaceID uint64, handle string) (*types.Module, error)
|
|
Find(ctx context.Context, filter types.ModuleFilter) (set types.ModuleSet, f types.ModuleFilter, err error)
|
|
|
|
Create(ctx context.Context, module *types.Module) (*types.Module, error)
|
|
Update(ctx context.Context, module *types.Module) (*types.Module, error)
|
|
|
|
DeleteByID(ctx context.Context, namespaceID uint64, moduleID uint64) error
|
|
}
|
|
|
|
modulesHandler struct {
|
|
reg modulesHandlerRegistry
|
|
ns namespaceService
|
|
mod moduleService
|
|
}
|
|
|
|
moduleLookup interface {
|
|
GetModule() (bool, uint64, string, *types.Module)
|
|
}
|
|
)
|
|
|
|
func ModulesHandler(reg modulesHandlerRegistry, ns namespaceService, mod moduleService) *modulesHandler {
|
|
h := &modulesHandler{
|
|
reg: reg,
|
|
ns: ns,
|
|
mod: mod,
|
|
}
|
|
|
|
h.register()
|
|
return h
|
|
}
|
|
|
|
func (h modulesHandler) lookup(ctx context.Context, args *modulesLookupArgs) (results *modulesLookupResults, err error) {
|
|
results = &modulesLookupResults{}
|
|
results.Module, err = lookupModule(ctx, h.ns, h.mod, args)
|
|
return
|
|
}
|
|
|
|
func getModuleID(ctx context.Context, nsSvc namespaceService, modSvc moduleService, args moduleLookup) (namespaceID uint64, moduleID uint64, err error) {
|
|
namespaceID, err = getNamespaceID(ctx, nsSvc, args.(namespaceLookup))
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var mod *types.Module
|
|
if _, moduleID, _, _ = args.GetModule(); moduleID > 0 {
|
|
return
|
|
} else if mod, err = lookupModule(ctx, nsSvc, modSvc, args); err != nil {
|
|
return
|
|
} else {
|
|
return namespaceID, mod.ID, nil
|
|
}
|
|
}
|
|
|
|
func lookupModule(ctx context.Context, nsSvc namespaceService, modSvc moduleService, args moduleLookup) (*types.Module, error) {
|
|
namespaceID, err := getNamespaceID(ctx, nsSvc, args.(namespaceLookup))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not load namespace: %w", err)
|
|
}
|
|
|
|
_, ID, handle, module := args.GetModule()
|
|
|
|
switch {
|
|
case module != nil:
|
|
return module, nil
|
|
case ID > 0:
|
|
return modSvc.FindByID(ctx, namespaceID, ID)
|
|
case len(handle) > 0:
|
|
return modSvc.FindByHandle(ctx, namespaceID, handle)
|
|
}
|
|
|
|
return nil, fmt.Errorf("empty module lookup params")
|
|
}
|