Skip to content

Commit

Permalink
feat: templating: add 'state' property
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Beuque <[email protected]>
  • Loading branch information
rbeuque74 committed Apr 1, 2020
1 parent 44a7db9 commit 08a37f9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ A user can be allowed to resolve a task in three ways:
- `.step.[STEP_NAME].metadata.HTTPStatus`: field `HTTPStatus` from the metadata of a named step
- `.step.[STEP_NAME].children`: the collection of results from a 'foreach' step
- `.step.[STEP_NAME].error`: error message from a failed step
- `.step.[STEP_NAME].state`: current state of the given step
- `.config.[CONFIG_ITEM].bar`: field `bar` from a config item (configstore, see above)
- `.iterator.foo`: field `foo` from the iterator in a loop (see `foreach` steps below)

Expand Down
3 changes: 3 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func resolve(dbp zesty.DBProvider, res *resolution.Resolution, t *task.Task, deb
res.Values.SetMetadata(s.Name, s.Metadata)
res.Values.SetChildren(s.Name, s.Children)
res.Values.SetError(s.Name, s.Error)
res.Values.SetState(s.Name, s.State)

// call after-run step logic
modifiedSteps := map[string]bool{
Expand Down Expand Up @@ -823,7 +824,9 @@ func resolutionStateSetter(res *resolution.Resolution, modifiedSteps map[string]
return func(step, state, message string) {
if _, ok := res.Steps[step]; ok {
res.Steps[step].State = state
res.Values.SetState(step, state)
res.Steps[step].Error = message
res.Values.SetError(step, message)
modifiedSteps[step] = true
}
}
Expand Down
2 changes: 2 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ func TestStepConditionStates(t *testing.T) {
assert.Equal(t, 2, res.Steps["stepOne"].TryCount)

assert.Equal(t, "REGEXP_MATCH", res.Steps["stepTwo"].State)
assert.Equal(t, "FOO", res.Steps["stepThree"].State)
assert.Equal(t, "changed state matched correctly", res.Steps["stepThree"].Error)
}

func TestResolutionStateCrashed(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions engine/templates_tests/stepCondition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,38 @@ steps:
then:
this: REGEXP_MATCH
message: email address belongs to example.com
stepThree:
dependencies: [stepTwo:ANY]
description: multiple conditions changing state
custom_states: [FOO, BAR]
action:
type: echo
configuration: {output: {foo: bar}}
conditions:
- type: check
if:
- value: '{{.step.this.output.foo}}'
operator: EQ
expected: barrrr
then:
this: CLIENT_ERROR
message: should not have matched
- type: check
if:
- value: '{{.step.this.output.foo}}'
operator: EQ
expected: bar
- value: '{{.step.this.state}}'
operator: EQ
expected: DONE
then:
this: BAR
message: matching is correct
- type: check
if:
- value: '{{.step.this.state}}'
operator: EQ
expected: BAR
then:
this: FOO
message: changed state matched correctly
19 changes: 19 additions & 0 deletions engine/values/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
VarKey = "var"
IteratorKey = "iterator" // reserved for transient one-off values, set/unset when applying values to template

StateKey = "state"
OutputKey = "output"
MetadataKey = "metadata"
ChildrenKey = "children"
Expand Down Expand Up @@ -140,6 +141,21 @@ func (v *Values) UnsetError(stepName string) {
v.unsetStepData(stepName, ErrorKey)
}

// GetState returns the state of a step
func (v *Values) GetState(stepName string) interface{} {
return v.getStepData(stepName, StateKey)
}

// SetState stores the state of a step
func (v *Values) SetState(stepName string, value interface{}) {
v.setStepData(stepName, StateKey, value)
}

// UnsetState empties the state of a step
func (v *Values) UnsetState(stepName string) {
v.unsetStepData(stepName, StateKey)
}

func (v *Values) getStepData(stepName, field string) interface{} {
stepmap := v.m[StepKey].(map[string]interface{})
if stepmap[stepName] == nil {
Expand Down Expand Up @@ -232,6 +248,9 @@ func (v *Values) Apply(templateStr string, item interface{}, stepName string) ([

v.SetError(utask.This, v.GetError(stepName))
defer v.UnsetError(utask.This)

v.SetState(utask.This, v.GetState(stepName))
defer v.UnsetState(utask.This)
}

err = tmpl.Execute(b, v.m)
Expand Down
1 change: 1 addition & 0 deletions models/resolution/resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ func (r *Resolution) setSteps(st map[string]*step.Step) {
r.Values.SetMetadata(name, s.Metadata)
r.Values.SetChildren(name, s.Children)
r.Values.SetError(s.Name, s.Error)
r.Values.SetState(s.Name, s.State)
}
}

Expand Down

0 comments on commit 08a37f9

Please sign in to comment.