Skip to content

Commit d7656a2

Browse files
authored
Added assign_if transformation (#276)
Fixes #275 Signed-off-by: Kuldeep Singh Pal <[email protected]>
1 parent 0184936 commit d7656a2

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ parameters:
391391
output: value_smaller_than10
392392
type: add_if
393393
parameters: <10
394+
- input: value
395+
output: dir
396+
type: add_if
397+
parameters: ==1
398+
assignee: in
394399
- input: dstPort
395400
output: service
396401
type: add_service
@@ -418,7 +423,8 @@ The second `add_if` generates a new field named `value_smaller_than10` that cont
418423
the contents of the `value` field for entries that satisfy the condition specified
419424
in the `parameters` variable (smaller than 10 in the example above). In addition, the
420425
field `value_smaller_than10_Evaluate` with value `true` is added to all satisfied
421-
entries
426+
entries. if `assignee` field is set, then on satified parmater i.e. if parameter evalutes true then
427+
`output` value will get value of `assignee` key.
422428

423429
The third rule `add_service` generates a new field named `service` with the known network
424430
service name of `dstPort` port and `protocol` protocol. Unrecognized ports are ignored

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Following is the supported API format for network transformations:
117117
add_service: add output network service field from input port and parameters protocol field
118118
add_kubernetes: add output kubernetes fields from input
119119
parameters: parameters specific to type
120+
assignee: value needs to assign to output field
120121
kubeConfigPath: path to kubeconfig file (optional)
121122
servicesFile: path to services file (optional, default: /etc/services)
122123
protocolsFile: path to protocols file (optional, default: /etc/protocols)

pkg/api/transform_network.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type NetworkTransformRule struct {
4343
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
4444
Type string `yaml:"type,omitempty" json:"type,omitempty" enum:"TransformNetworkOperationEnum" doc:"one of the following:"`
4545
Parameters string `yaml:"parameters,omitempty" json:"parameters,omitempty" doc:"parameters specific to type"`
46+
Assignee string `yaml:"assignee,omitempty" json:"assignee,omitempty" doc:"value needs to assign to output field"`
4647
}
4748

4849
type NetworkTransformRules []NetworkTransformRule

pkg/pipeline/transform/transform_network.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func (n *Network) TransformEntry(inputEntry config.GenericMap) config.GenericMap
9292
}
9393
result, evaluateErr := expression.Evaluate(map[string]interface{}{"val": outputEntry[rule.Input]})
9494
if evaluateErr == nil && result.(bool) {
95-
outputEntry[rule.Output] = outputEntry[rule.Input]
95+
if rule.Assignee != "" {
96+
outputEntry[rule.Output] = rule.Assignee
97+
} else {
98+
outputEntry[rule.Output] = outputEntry[rule.Input]
99+
}
96100
outputEntry[rule.Output+"_Evaluate"] = true
97101
}
98102
case api.TransformNetworkOperationName("AddSubnet"):

pkg/pipeline/transform/transform_network_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,20 @@ func Test_Transform_AddIfScientificNotation(t *testing.T) {
302302
Type: "add_if",
303303
Parameters: "<10",
304304
},
305+
api.NetworkTransformRule{
306+
Input: "value",
307+
Output: "dir",
308+
Assignee: "in",
309+
Type: "add_if",
310+
Parameters: "==1",
311+
},
312+
api.NetworkTransformRule{
313+
Input: "value",
314+
Output: "dir",
315+
Assignee: "out",
316+
Type: "add_if",
317+
Parameters: "==0",
318+
},
305319
},
306320
},
307321
}
@@ -320,6 +334,20 @@ func Test_Transform_AddIfScientificNotation(t *testing.T) {
320334
output = newNetworkTransform.Transform([]config.GenericMap{entry})
321335
require.Equal(t, true, output[0]["smaller_than_10_Evaluate"])
322336
require.Equal(t, 1.2345e-67, output[0]["smaller_than_10"])
337+
338+
entry = config.GenericMap{
339+
"value": 1,
340+
}
341+
output = newNetworkTransform.Transform([]config.GenericMap{entry})
342+
require.Equal(t, true, output[0]["dir_Evaluate"])
343+
require.Equal(t, "in", output[0]["dir"])
344+
345+
entry = config.GenericMap{
346+
"value": 0,
347+
}
348+
output = newNetworkTransform.Transform([]config.GenericMap{entry})
349+
require.Equal(t, true, output[0]["dir_Evaluate"])
350+
require.Equal(t, "out", output[0]["dir"])
323351
}
324352

325353
func Test_TransformNetworkOperationNameCustomServices(t *testing.T) {

0 commit comments

Comments
 (0)