3
0

Implement corredor script exec through wf fn step

This commit is contained in:
Denis Arh 2021-03-15 17:33:20 +01:00
parent 0910317632
commit b2b6935717
4 changed files with 216 additions and 0 deletions

View File

@ -0,0 +1,116 @@
package automation
// This file is auto-generated.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
// Definitions file that controls how this file is generated:
// automation/automation/corredor_handler.yaml
import (
"context"
atypes "github.com/cortezaproject/corteza-server/automation/types"
"github.com/cortezaproject/corteza-server/pkg/expr"
"github.com/cortezaproject/corteza-server/pkg/wfexec"
)
var _ wfexec.ExecResponse
type (
corredorHandlerRegistry interface {
AddFunctions(ff ...*atypes.Function)
Type(ref string) expr.Type
}
)
func (h corredorHandler) register() {
h.reg.AddFunctions(
h.Exec(),
)
}
type (
corredorExecArgs struct {
hasScript bool
Script string
hasArgs bool
Args interface{}
}
corredorExecResults struct {
Results interface{}
}
)
// Exec function Executes script in Corredor Automation server
//
// expects implementation of exec function:
// func (h corredorHandler) exec(ctx context.Context, args *corredorExecArgs) (results *corredorExecResults, err error) {
// return
// }
func (h corredorHandler) Exec() *atypes.Function {
return &atypes.Function{
Ref: "corredorExec",
Kind: "function",
Labels: map[string]string(nil),
Meta: &atypes.FunctionMeta{
Short: "Executes script in Corredor Automation server",
},
Parameters: []*atypes.Param{
{
Name: "script",
Types: []string{"String"}, Required: true,
},
{
Name: "args",
Types: []string{"Any"},
},
},
Results: []*atypes.Param{
{
Name: "results",
Types: []string{"Any"},
},
},
Handler: func(ctx context.Context, in *expr.Vars) (out *expr.Vars, err error) {
var (
args = &corredorExecArgs{
hasScript: in.Has("script"),
hasArgs: in.Has("args"),
}
)
if err = in.Decode(args); err != nil {
return
}
var results *corredorExecResults
if results, err = h.exec(ctx, args); err != nil {
return
}
out = &expr.Vars{}
{
// converting results.Results (interface{}) to Any
var (
tval expr.TypedValue
)
if tval, err = h.reg.Type("Any").Cast(results.Results); err != nil {
return
} else if err = expr.Assign(out, "results", tval); err != nil {
return
}
}
return
},
}
}

View File

@ -0,0 +1,85 @@
package automation
import (
"context"
"encoding/json"
"github.com/cortezaproject/corteza-server/pkg/corredor"
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/service/event"
"github.com/spf13/cast"
)
type (
corredorHandler struct {
reg corredorHandlerRegistry
svc corredorServiceExecutor
}
corredorServiceExecutor interface {
Exec(ctx context.Context, scriptName string, args corredor.ScriptArgs) (err error)
}
scriptArgs struct {
payload map[string]interface{}
}
)
func CorredorHandler(reg corredorHandlerRegistry, svc corredorServiceExecutor) *corredorHandler {
h := &corredorHandler{
reg: reg,
svc: svc,
}
h.register()
return h
}
func (h corredorHandler) exec(ctx context.Context, args *corredorExecArgs) (r *corredorExecResults, err error) {
sArgs := makeScriptArgs(args.Args)
if err = h.svc.Exec(ctx, args.Script, sArgs); err != nil {
return
}
return &corredorExecResults{Results: sArgs.payload}, nil
}
func makeScriptArgs(in interface{}) *scriptArgs {
return &scriptArgs{
payload: cast.ToStringMap(in),
}
}
// mimic onManual event on system:
func (scriptArgs) ResourceType() string { return event.SystemOnManual().ResourceType() }
func (scriptArgs) EventType() string { return event.SystemOnManual().EventType() }
func (scriptArgs) Match(eventbus.ConstraintMatcher) bool { return false }
func (a *scriptArgs) Encode() (enc map[string][]byte, err error) {
enc = make(map[string][]byte)
for k, v := range a.payload {
enc[k], err = json.Marshal(v)
if err != nil {
return nil, err
}
}
return
}
func (a *scriptArgs) Decode(enc map[string][]byte) (err error) {
a.payload = make(map[string]interface{})
for k, v := range enc {
var aux interface{}
if err = json.Unmarshal(v, &aux); err != nil {
return err
}
a.payload[k] = aux
}
return
}

View File

@ -0,0 +1,13 @@
name: corredor
functions:
exec:
kind: function
meta:
short: Executes script in Corredor Automation server
params:
script: { types: [ { wf: String } ], required: true }
args: { types: [ { wf: Any } ] }
results:
results:
wf: Any

View File

@ -4,6 +4,7 @@ import (
"context"
"github.com/cortezaproject/corteza-server/automation/automation"
"github.com/cortezaproject/corteza-server/pkg/actionlog"
"github.com/cortezaproject/corteza-server/pkg/corredor"
"github.com/cortezaproject/corteza-server/pkg/expr"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/pkg/objstore"
@ -115,6 +116,7 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, c Config)
automation.HttpRequestHandler(Registry())
automation.LogHandler(Registry())
automation.LoopHandler(Registry(), DefaultWorkflow.parser)
automation.CorredorHandler(Registry(), corredor.Service())
return
}