generate volumemout jsonpatch array depending on if container already have volumemount #87
-
Hello Team, I want to have a jsonpatch like below, basically, generate new attribute of volmount if container does not have this attribute yet.
basically, this stuck as the if else rego block just CANNOT be embedded into any rule or function block.
make_vol_patch(path, num_str, value) = patchCode { add_vol_patch(path, num_str, value) = patchCode {
} vm_value := {"name": "volmount1"} patches[patch]{
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The function should not be defined inside of the rule, but can definitely be called from there. Something like this should work: package play
vm_value := {"name": "volmount1"}
patches[patch] {
some i
container_i := input.containers[i]
patch := {
"op": "add",
"path": concat("/", get_path(i, object.get(container_i, "volmount", false))),
"value": [vm_value],
}
}
get_path(i, exists) = ["/containers", format_int(i, 10), "volmount", "-"] {
exists
}
get_path(i, exists) = ["/containers", format_int(i, 10), "volmount"] {
not exists
}
Though if you are trying to build the path dynamically for mutation, that's actually trickier than it seems. Have a look at this repo from @tsandall for some example code doing what I think you are trying to do :) |
Beta Was this translation helpful? Give feedback.
The function should not be defined inside of the rule, but can definitely be called from there. Something like this should work:
Though if you are trying to build the path dynamically for mutation, that's actually trickier than it seems. Have a look at this repo from @tsandall …