From 1be620d7872fbce7917761a6a7f2e0be5b249e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Tue, 31 May 2022 12:47:45 +0200 Subject: [PATCH] Tweak dal connection meta for easier FE work * Default capabilities to empty slice * Include primary connection connection parameters in REST responses. --- app/boot_levels.go | 1 + system/rest/dal_connection.go | 15 ++++++++++++++ system/service/dal_connection.go | 34 ++++++++++++++++++++++++++++---- system/service/service.go | 3 ++- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/app/boot_levels.go b/app/boot_levels.go index 300bc602c..f024894e5 100644 --- a/app/boot_levels.go +++ b/app/boot_levels.go @@ -470,6 +470,7 @@ func (app *CortezaApp) InitServices(ctx context.Context) (err error) { Discovery: app.Opt.Discovery, Storage: app.Opt.ObjStore, Template: app.Opt.Template, + DB: app.Opt.DB, Auth: app.Opt.Auth, RBAC: app.Opt.RBAC, Limit: app.Opt.Limit, diff --git a/system/rest/dal_connection.go b/system/rest/dal_connection.go index 2e47be4ed..f5f147126 100644 --- a/system/rest/dal_connection.go +++ b/system/rest/dal_connection.go @@ -10,6 +10,7 @@ import ( federationTypes "github.com/cortezaproject/corteza-server/federation/types" "github.com/cortezaproject/corteza-server/pkg/api" "github.com/cortezaproject/corteza-server/pkg/dal" + "github.com/cortezaproject/corteza-server/pkg/dal/capabilities" "github.com/cortezaproject/corteza-server/pkg/filter" "github.com/cortezaproject/corteza-server/pkg/handle" "github.com/cortezaproject/corteza-server/pkg/payload" @@ -145,6 +146,20 @@ func (ctrl DalConnection) Undelete(ctx context.Context, r *request.DalConnection } func (ctrl DalConnection) makeFilterPayload(ctx context.Context, connections types.DalConnectionSet, f types.DalConnectionFilter) (out *connectionSetPayload, err error) { + for _, c := range connections { + if c.Capabilities.Enforced == nil { + c.Capabilities.Enforced = capabilities.Set{} + } + if c.Capabilities.Supported == nil { + c.Capabilities.Supported = capabilities.Set{} + } + if c.Capabilities.Unsupported == nil { + c.Capabilities.Unsupported = capabilities.Set{} + } + if c.Capabilities.Enabled == nil { + c.Capabilities.Enabled = capabilities.Set{} + } + } out = &connectionSetPayload{ Set: connections, Filter: f, diff --git a/system/service/dal_connection.go b/system/service/dal_connection.go index 9e77fc84b..a35565ff2 100644 --- a/system/service/dal_connection.go +++ b/system/service/dal_connection.go @@ -9,6 +9,7 @@ import ( a "github.com/cortezaproject/corteza-server/pkg/auth" "github.com/cortezaproject/corteza-server/pkg/dal" "github.com/cortezaproject/corteza-server/pkg/dal/capabilities" + "github.com/cortezaproject/corteza-server/pkg/options" "github.com/cortezaproject/corteza-server/store" "github.com/cortezaproject/corteza-server/system/types" @@ -20,6 +21,7 @@ type ( store store.Storer ac connectionAccessController dal dalConnections + dbConf options.DBOpt } connectionAccessController interface { @@ -39,12 +41,13 @@ type ( } ) -func Connection(ctx context.Context, dal dalConnections) *dalConnection { +func Connection(ctx context.Context, dal dalConnections, dbConf options.DBOpt) *dalConnection { return &dalConnection{ ac: DefaultAccessControl, actionlog: DefaultActionlog, store: DefaultStore, dal: dal, + dbConf: dbConf, } } @@ -68,9 +71,9 @@ func (svc *dalConnection) FindByID(ctx context.Context, ID uint64) (q *types.Dal return DalConnectionErrNotAllowedToRead(rProps) } + svc.assurePrimaryConnection(q) return nil }() - return q, svc.recordAction(ctx, rProps, DalConnectionActionLookup, err) } @@ -127,6 +130,8 @@ func (svc *dalConnection) Update(ctx context.Context, upd *types.DalConnection) return DalConnectionErrNotFound(qProps) } + svc.assurePrimaryConnection(old) + if !svc.ac.CanUpdateDalConnection(ctx, old) { return DalConnectionErrNotAllowedToUpdate(qProps) } @@ -147,6 +152,18 @@ func (svc *dalConnection) Update(ctx context.Context, upd *types.DalConnection) return fmt.Errorf("can not update handle for primary connection") } + if old.Config.DefaultModelIdent != upd.Config.DefaultModelIdent { + return fmt.Errorf("can not update defaultModelIdent for primary connection") + } + + if old.Config.DefaultAttributeIdent != upd.Config.DefaultAttributeIdent { + return fmt.Errorf("can not update defaultAttributeIdent for primary connection") + } + + if old.Handle != upd.Handle { + return fmt.Errorf("can not update handle for primary connection") + } + if old.Type != upd.Type { return fmt.Errorf("can not update type for primary connection") } @@ -158,6 +175,7 @@ func (svc *dalConnection) Update(ctx context.Context, upd *types.DalConnection) } q = upd + svc.assurePrimaryConnection(q) var cm dal.ConnectionMeta cm, err = svc.makeConnectionMeta(ctx, upd) @@ -167,7 +185,6 @@ func (svc *dalConnection) Update(ctx context.Context, upd *types.DalConnection) return svc.dal.UpdateConnection(ctx, upd.ID, upd.Config.Connection, cm, upd.ActiveCapabilities()...) }() - return q, svc.recordAction(ctx, qProps, DalConnectionActionUpdate, err) } @@ -272,9 +289,9 @@ func (svc *dalConnection) Search(ctx context.Context, filter types.DalConnection return err } + svc.assurePrimaryConnection(r...) return nil }() - return r, f, svc.recordAction(ctx, aProps, DalConnectionActionSearch, err) } @@ -311,3 +328,12 @@ func (svc *dalConnection) makeConnectionMeta(ctx context.Context, c *types.DalCo return } + +func (svc *dalConnection) assurePrimaryConnection(connections ...*types.DalConnection) { + for _, c := range connections { + if c.Type == types.DalPrimaryConnectionResourceType { + c.Config.Connection = dal.NewDSNConnection(svc.dbConf.DSN) + return + } + } +} diff --git a/system/service/service.go b/system/service/service.go index ae116183d..aadbfe6b4 100644 --- a/system/service/service.go +++ b/system/service/service.go @@ -34,6 +34,7 @@ type ( ActionLog options.ActionLogOpt Discovery options.DiscoveryOpt Storage options.ObjectStoreOpt + DB options.DBOpt Template options.TemplateOpt Auth options.AuthOpt RBAC options.RbacOpt @@ -150,7 +151,7 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, primaryCon DefaultSettings = Settings(ctx, DefaultStore, DefaultLogger, DefaultAccessControl, CurrentSettings) primaryConnectionConfig = primaryConn - DefaultDalConnection = Connection(ctx, dal.Service()) + DefaultDalConnection = Connection(ctx, dal.Service(), c.DB) DefaultDalSensitivityLevel = SensitivityLevel(ctx, dal.Service()) if err != nil {