Skip to content

Commit 08a37f9

Browse files
committed
feat: templating: add 'state' property
Signed-off-by: Romain Beuque <[email protected]>
1 parent 44a7db9 commit 08a37f9

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ A user can be allowed to resolve a task in three ways:
171171
- `.step.[STEP_NAME].metadata.HTTPStatus`: field `HTTPStatus` from the metadata of a named step
172172
- `.step.[STEP_NAME].children`: the collection of results from a 'foreach' step
173173
- `.step.[STEP_NAME].error`: error message from a failed step
174+
- `.step.[STEP_NAME].state`: current state of the given step
174175
- `.config.[CONFIG_ITEM].bar`: field `bar` from a config item (configstore, see above)
175176
- `.iterator.foo`: field `foo` from the iterator in a loop (see `foreach` steps below)
176177

engine/engine.go

+3
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ func resolve(dbp zesty.DBProvider, res *resolution.Resolution, t *task.Task, deb
349349
res.Values.SetMetadata(s.Name, s.Metadata)
350350
res.Values.SetChildren(s.Name, s.Children)
351351
res.Values.SetError(s.Name, s.Error)
352+
res.Values.SetState(s.Name, s.State)
352353

353354
// call after-run step logic
354355
modifiedSteps := map[string]bool{
@@ -823,7 +824,9 @@ func resolutionStateSetter(res *resolution.Resolution, modifiedSteps map[string]
823824
return func(step, state, message string) {
824825
if _, ok := res.Steps[step]; ok {
825826
res.Steps[step].State = state
827+
res.Values.SetState(step, state)
826828
res.Steps[step].Error = message
829+
res.Values.SetError(step, message)
827830
modifiedSteps[step] = true
828831
}
829832
}

engine/engine_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ func TestStepConditionStates(t *testing.T) {
370370
assert.Equal(t, 2, res.Steps["stepOne"].TryCount)
371371

372372
assert.Equal(t, "REGEXP_MATCH", res.Steps["stepTwo"].State)
373+
assert.Equal(t, "FOO", res.Steps["stepThree"].State)
374+
assert.Equal(t, "changed state matched correctly", res.Steps["stepThree"].Error)
373375
}
374376

375377
func TestResolutionStateCrashed(t *testing.T) {

engine/templates_tests/stepCondition.yaml

+35
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,38 @@ steps:
4141
then:
4242
this: REGEXP_MATCH
4343
message: email address belongs to example.com
44+
stepThree:
45+
dependencies: [stepTwo:ANY]
46+
description: multiple conditions changing state
47+
custom_states: [FOO, BAR]
48+
action:
49+
type: echo
50+
configuration: {output: {foo: bar}}
51+
conditions:
52+
- type: check
53+
if:
54+
- value: '{{.step.this.output.foo}}'
55+
operator: EQ
56+
expected: barrrr
57+
then:
58+
this: CLIENT_ERROR
59+
message: should not have matched
60+
- type: check
61+
if:
62+
- value: '{{.step.this.output.foo}}'
63+
operator: EQ
64+
expected: bar
65+
- value: '{{.step.this.state}}'
66+
operator: EQ
67+
expected: DONE
68+
then:
69+
this: BAR
70+
message: matching is correct
71+
- type: check
72+
if:
73+
- value: '{{.step.this.state}}'
74+
operator: EQ
75+
expected: BAR
76+
then:
77+
this: FOO
78+
message: changed state matched correctly

engine/values/values.go

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
VarKey = "var"
2424
IteratorKey = "iterator" // reserved for transient one-off values, set/unset when applying values to template
2525

26+
StateKey = "state"
2627
OutputKey = "output"
2728
MetadataKey = "metadata"
2829
ChildrenKey = "children"
@@ -140,6 +141,21 @@ func (v *Values) UnsetError(stepName string) {
140141
v.unsetStepData(stepName, ErrorKey)
141142
}
142143

144+
// GetState returns the state of a step
145+
func (v *Values) GetState(stepName string) interface{} {
146+
return v.getStepData(stepName, StateKey)
147+
}
148+
149+
// SetState stores the state of a step
150+
func (v *Values) SetState(stepName string, value interface{}) {
151+
v.setStepData(stepName, StateKey, value)
152+
}
153+
154+
// UnsetState empties the state of a step
155+
func (v *Values) UnsetState(stepName string) {
156+
v.unsetStepData(stepName, StateKey)
157+
}
158+
143159
func (v *Values) getStepData(stepName, field string) interface{} {
144160
stepmap := v.m[StepKey].(map[string]interface{})
145161
if stepmap[stepName] == nil {
@@ -232,6 +248,9 @@ func (v *Values) Apply(templateStr string, item interface{}, stepName string) ([
232248

233249
v.SetError(utask.This, v.GetError(stepName))
234250
defer v.UnsetError(utask.This)
251+
252+
v.SetState(utask.This, v.GetState(stepName))
253+
defer v.UnsetState(utask.This)
235254
}
236255

237256
err = tmpl.Execute(b, v.m)

models/resolution/resolution.go

+1
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ func (r *Resolution) setSteps(st map[string]*step.Step) {
479479
r.Values.SetMetadata(name, s.Metadata)
480480
r.Values.SetChildren(name, s.Children)
481481
r.Values.SetError(s.Name, s.Error)
482+
r.Values.SetState(s.Name, s.State)
482483
}
483484
}
484485

0 commit comments

Comments
 (0)