From ded83ff5db129513da7befca6042a850b384e4bf Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 8 Jan 2020 17:05:41 +0100 Subject: [PATCH] Improve onManual trigger for all resources --- api/compose/spec.json | 6 ++++++ api/compose/spec/record.json | 6 ++++++ compose/rest/module.go | 4 +++- compose/rest/namespace.go | 3 ++- compose/rest/page.go | 4 +++- compose/rest/record.go | 13 ++++++------- compose/rest/request/record.go | 2 ++ docs/compose/README.md | 1 + system/rest/application.go | 4 +++- system/rest/role.go | 4 +++- system/rest/user.go | 5 ++++- 11 files changed, 39 insertions(+), 13 deletions(-) diff --git a/api/compose/spec.json b/api/compose/spec.json index 07311318d..e0a914107 100644 --- a/api/compose/spec.json +++ b/api/compose/spec.json @@ -1035,6 +1035,12 @@ "type": "string", "title": "Script to execute", "required": true + }, + { + "type": "types.RecordValueSet", + "name": "values", + "required": true, + "title": "Record values" } ] } diff --git a/api/compose/spec/record.json b/api/compose/spec/record.json index 1e263e32c..32256fd28 100644 --- a/api/compose/spec/record.json +++ b/api/compose/spec/record.json @@ -333,6 +333,12 @@ "required": true, "title": "Script to execute", "type": "string" + }, + { + "name": "values", + "required": true, + "title": "Record values", + "type": "types.RecordValueSet" } ] } diff --git a/compose/rest/module.go b/compose/rest/module.go index 4eadf6727..71f8d400f 100644 --- a/compose/rest/module.go +++ b/compose/rest/module.go @@ -150,7 +150,9 @@ func (ctrl *Module) TriggerScript(ctx context.Context, r *request.ModuleTriggerS return } - return resputil.OK(), corredor.Service().ExecOnManual(ctx, r.Script, event.ModuleOnManual(module, nil, namespace)) + // @todo implement same behaviour as we have on record - module+oldModule + err = corredor.Service().ExecOnManual(ctx, r.Script, event.ModuleOnManual(module, module, namespace)) + return ctrl.makePayload(ctx, module, err) } func (ctrl Module) makePayload(ctx context.Context, m *types.Module, err error) (*modulePayload, error) { diff --git a/compose/rest/namespace.go b/compose/rest/namespace.go index 331e87229..27631019d 100644 --- a/compose/rest/namespace.go +++ b/compose/rest/namespace.go @@ -133,7 +133,8 @@ func (ctrl *Namespace) TriggerScript(ctx context.Context, r *request.NamespaceTr return } - return resputil.OK(), corredor.Service().ExecOnManual(ctx, r.Script, event.NamespaceOnManual(namespace, nil)) + err = corredor.Service().ExecOnManual(ctx, r.Script, event.NamespaceOnManual(namespace, nil)) + return ctrl.makePayload(ctx, namespace, err) } func (ctrl Namespace) makePayload(ctx context.Context, ns *types.Namespace, err error) (*namespacePayload, error) { diff --git a/compose/rest/page.go b/compose/rest/page.go index 0fa6c18f1..9e976275b 100644 --- a/compose/rest/page.go +++ b/compose/rest/page.go @@ -170,7 +170,9 @@ func (ctrl *Page) TriggerScript(ctx context.Context, r *request.PageTriggerScrip return } - return resputil.OK(), corredor.Service().ExecOnManual(ctx, r.Script, event.PageOnManual(page, nil, namespace)) + // @todo implement same behaviour as we have on record - page+oldPage + err = corredor.Service().ExecOnManual(ctx, r.Script, event.PageOnManual(page, page, namespace)) + return ctrl.makePayload(ctx, page, err) } func (ctrl Page) makePayload(ctx context.Context, c *types.Page, err error) (*pagePayload, error) { diff --git a/compose/rest/record.go b/compose/rest/record.go index e1479ab7f..7d429149c 100644 --- a/compose/rest/record.go +++ b/compose/rest/record.go @@ -379,11 +379,12 @@ func (ctrl Record) Exec(ctx context.Context, r *request.RecordExec) (interface{} func (ctrl *Record) TriggerScript(ctx context.Context, r *request.RecordTriggerScript) (rsp interface{}, err error) { var ( record *types.Record + oldRecord *types.Record module *types.Module namespace *types.Namespace ) - if record, err = ctrl.record.With(ctx).FindByID(r.NamespaceID, r.RecordID); err != nil { + if oldRecord, err = ctrl.record.With(ctx).FindByID(r.NamespaceID, r.RecordID); err != nil { return } @@ -395,15 +396,13 @@ func (ctrl *Record) TriggerScript(ctx context.Context, r *request.RecordTriggerS return } + record = oldRecord + record.Values = r.Values + err = corredor.Service().ExecOnManual( ctx, r.Script, - event.RecordOnManual( - record, - nil, - module, - namespace, - ), + event.RecordOnManual(record, oldRecord, module, namespace), ) // Script can return modified record and we'll pass it on to the caller diff --git a/compose/rest/request/record.go b/compose/rest/request/record.go index d50d4fb81..35d63bda0 100644 --- a/compose/rest/request/record.go +++ b/compose/rest/request/record.go @@ -801,6 +801,7 @@ type RecordTriggerScript struct { NamespaceID uint64 `json:",string"` ModuleID uint64 `json:",string"` Script string + Values types.RecordValueSet } func NewRecordTriggerScript() *RecordTriggerScript { @@ -814,6 +815,7 @@ func (r RecordTriggerScript) Auditable() map[string]interface{} { out["namespaceID"] = r.NamespaceID out["moduleID"] = r.ModuleID out["script"] = r.Script + out["values"] = r.Values return out } diff --git a/docs/compose/README.md b/docs/compose/README.md index 37abc51bc..210234ea2 100644 --- a/docs/compose/README.md +++ b/docs/compose/README.md @@ -1028,6 +1028,7 @@ Compose records | namespaceID | uint64 | PATH | Namespace ID | N/A | YES | | moduleID | uint64 | PATH | Module ID | N/A | YES | | script | string | POST | Script to execute | N/A | YES | +| values | types.RecordValueSet | POST | Record values | N/A | YES | --- diff --git a/system/rest/application.go b/system/rest/application.go index 1f1124b72..bdb01907f 100644 --- a/system/rest/application.go +++ b/system/rest/application.go @@ -129,7 +129,9 @@ func (ctrl *Application) TriggerScript(ctx context.Context, r *request.Applicati return } - return resputil.OK(), corredor.Service().ExecOnManual(ctx, r.Script, event.ApplicationOnManual(application, nil)) + // @todo implement same behaviour as we have on record - Application+oldApplication + err = corredor.Service().ExecOnManual(ctx, r.Script, event.ApplicationOnManual(application, application)) + return application, err } func (ctrl Application) makePayload(ctx context.Context, m *types.Application, err error) (*applicationPayload, error) { diff --git a/system/rest/role.go b/system/rest/role.go index beef06bcf..30f65ef24 100644 --- a/system/rest/role.go +++ b/system/rest/role.go @@ -185,7 +185,9 @@ func (ctrl *Role) TriggerScript(ctx context.Context, r *request.RoleTriggerScrip return } - return resputil.OK(), corredor.Service().ExecOnManual(ctx, r.Script, event.RoleOnManual(role, nil)) + // @todo implement same behaviour as we have on record - role+oldRole + err = corredor.Service().ExecOnManual(ctx, r.Script, event.RoleOnManual(role, role)) + return role, err } func (ctrl Role) makePayload(ctx context.Context, m *types.Role, err error) (*rolePayload, error) { diff --git a/system/rest/user.go b/system/rest/user.go index 80658508e..1d2b9f390 100644 --- a/system/rest/user.go +++ b/system/rest/user.go @@ -141,7 +141,10 @@ func (ctrl *User) TriggerScript(ctx context.Context, r *request.UserTriggerScrip return } - return resputil.OK(), corredor.Service().ExecOnManual(ctx, r.Script, event.UserOnManual(user, nil)) + // @todo implement same behaviour as we have on record - user+oldUser + err = corredor.Service().ExecOnManual(ctx, r.Script, event.UserOnManual(user, user)) + return user, err + } func (ctrl User) makeFilterPayload(ctx context.Context, uu types.UserSet, f types.UserFilter, err error) (*userSetPayload, error) {