Skip to content

Commit

Permalink
Support different trigger parameter operations (#315) (#319)
Browse files Browse the repository at this point in the history
* Support different trigger parameter operations (#315)

Implemented an additional `operation` field for trigger parameters that
dictates what is to be done with the current value at `dest`. Possible
keyword values are:

 - `overwrite` Overwrite the current value (default/prior behavior)
 - `prepend` Prepend the value at `dest` with the value at `source.path`
 - `append` Append the value at `dest` with the value at `source.path`

* Specified that `operation` is optional and generated openapi specs
  • Loading branch information
marxarelli authored and VaibhavPage committed Jul 27, 2019
1 parent f54d651 commit 1d51b87
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 2 deletions.
10 changes: 10 additions & 0 deletions controllers/sensor/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func validateTriggerParameter(parameter *v1alpha1.TriggerParameter) error {
if parameter.Dest == "" {
return fmt.Errorf("parameter destination can't be empty")
}

switch op := parameter.Operation; op {
case v1alpha1.TriggerParameterOpAppend:
case v1alpha1.TriggerParameterOpOverwrite:
case v1alpha1.TriggerParameterOpPrepend:
case v1alpha1.TriggerParameterOpNone:
default:
return fmt.Errorf("parameter operation %+v is invalid", op)
}

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion docs/parameterization.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ A `parameter` contains:
1. `src`:
1. `event`: the name of the event dependency
2. `path`: a key within event payload to look for
3. `value`: default value if sensor can't find `path` in event payload.
3. `value`: default value if sensor can't find `path` in event payload
2. `dest`: destination key within resource definition whose corresponding value needs to be replaced with value from event payload
3. `operation`: what to do with the existing value at `dest`, either `overwrite`, `prepend`, or `append`
9 changes: 8 additions & 1 deletion examples/sensors/complete-trigger-parameterization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# "name": "trigger-wf-1",
# "namespace": "argo-events",
# "bucket": "mybucket",
# "port": "9000",
# "key": "hello.yaml",
# "image": "docker/busybox",
# }
Expand Down Expand Up @@ -48,7 +49,7 @@ spec:
bucket:
name: THIS_WILL_BE_REPLACED_BUCKET
key: THIS_WILL_BE_REPLACED_KEY
endpoint: minio-service.argo-events:9000
endpoint: "minio-service.argo-events:"
insecure: true
accessKey:
key: accesskey
Expand All @@ -74,6 +75,12 @@ spec:
event: "webhook-gateway:example"
path: key
dest: source.s3.bucket.key
- src:
event: "webhook-gateway:example"
path: port
dest: source.s3.endpoint
# Append the port to the existing source.s3.endpoint value
operation: append
# Apply parameter for workflow
resourceParameters:
- src:
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/sensor/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/apis/sensor/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ type TriggerCondition struct {
All []string `json:"all,omitempty" protobuf:"bytes,2,rep,name=all"`
}

// TriggerParameterOperation represents how to set a trigger destination
// resource key
type TriggerParameterOperation string

const (
// TriggerParameterOpNone is the zero value of TriggerParameterOperation
TriggerParameterOpNone TriggerParameterOperation = ""
// TriggerParameterOpAppend means append the new value to the existing
TriggerParameterOpAppend TriggerParameterOperation = "append"
// TriggerParameterOpOverwrite means overwrite the existing value with the new
TriggerParameterOpOverwrite TriggerParameterOperation = "overwrite"
// TriggerParameterOpPrepend means prepend the new value to the existing
TriggerParameterOpPrepend TriggerParameterOperation = "prepend"
)

// TriggerParameter indicates a passed parameter to a service template
type TriggerParameter struct {
// Src contains a source reference to the value of the parameter from a event event
Expand All @@ -249,6 +264,10 @@ type TriggerParameter struct {
// The -1 key can be used to append a value to an existing array.
// See https://github.com/tidwall/sjson#path-syntax for more information about how this is used.
Dest string `json:"dest" protobuf:"bytes,2,name=dest"`

// Operation is what to do with the existing value at Dest, whether to
// 'prepend', 'overwrite', or 'append' it.
Operation TriggerParameterOperation `json:"operation,omitempty" protobuf:"bytes,3,opt,name=operation"`
}

// TriggerParameterSource defines the source for a parameter from a event event
Expand Down
19 changes: 19 additions & 0 deletions sensors/trigger-params.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ func applyParams(jsonObj []byte, params []v1alpha1.TriggerParameter, events map[
if err != nil {
return nil, err
}

switch op := param.Operation; op {
case v1alpha1.TriggerParameterOpAppend, v1alpha1.TriggerParameterOpPrepend:
// prepend or append the current value
current := gjson.GetBytes(jsonObj, param.Dest)

if current.Exists() {
if op == v1alpha1.TriggerParameterOpAppend {
v = current.String() + v
} else {
v = v + current.String()
}
}
case v1alpha1.TriggerParameterOpOverwrite, v1alpha1.TriggerParameterOpNone:
// simply overwrite the current value with the new one
default:
return nil, fmt.Errorf("unsupported trigger parameter operation: %+v", op)
}

// now let's set the value
tmp, err := sjson.SetBytes(jsonObj, param.Dest, v)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions sensors/trigger-params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,44 @@ func Test_applyParams(t *testing.T) {
want: []byte(`{"x":"magaldi"}`),
wantErr: false,
},
{
name: "simpleJSON (prepended field) -> success",
args: args{
jsonObj: []byte(`{"x":"before"}`),
params: []v1alpha1.TriggerParameter{
{
Src: &v1alpha1.TriggerParameterSource{
Event: "simpleJSON",
Path: "name.last",
},
Dest: "x",
Operation: v1alpha1.TriggerParameterOpPrepend,
},
},
events: events,
},
want: []byte(`{"x":"magaldibefore"}`),
wantErr: false,
},
{
name: "simpleJSON (appended field) -> success",
args: args{
jsonObj: []byte(`{"x":"before"}`),
params: []v1alpha1.TriggerParameter{
{
Src: &v1alpha1.TriggerParameterSource{
Event: "simpleJSON",
Path: "name.last",
},
Dest: "x",
Operation: v1alpha1.TriggerParameterOpAppend,
},
},
events: events,
},
want: []byte(`{"x":"beforemagaldi"}`),
wantErr: false,
},
{
name: "non JSON, no default -> pass payload bytes without converting",
args: args{
Expand Down

0 comments on commit 1d51b87

Please sign in to comment.