https://play.openpolicyagent.org/p/Yx4t1akZmC #334
rakesh0022
started this conversation in
Community
Replies: 1 comment 5 replies
-
Hi @rakesh0022 👋 If you're running the latest version of OPA, you can use the new package test
import future.keywords
deny[msg] {
labels = {"lifecycle": "", "project_name": ""}
new_labels = {"lifecycle": "prod", "project_name": "test"}
object.keys(labels) == object.keys(new_labels)
msg := sprintf("label is matching %v ", [new_labels])
} If you're running an older version of OPA, a custom function to iterate over object keys could look like this: package test
import future.keywords
deny[msg] {
labels = {"lifecycle": "", "project_name": ""}
new_labels = {"lifecycle": "prod", "project_name": "test"}
keys(labels) == keys(new_labels)
msg := sprintf("label is matching %v ", [new_labels])
}
keys(o) := {k | _ = o[k]} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
i am trying to write a rego code for comparing "key" of multiple map not the "value", only the key is present in both the maps policy should give true result else false
below is the code i prepared, howver its not working(not getting true as a result)
added the rego playground link in the title itself
package test
import future.keywords
deny[msg] {
labels = {"lifecycle": "", "project_name":""}
new_labels = {"lifecycle":"prod", "project_name": "test"}
keys_in_map(labels).keys == keys_in_map(new_labels).keys
msg := sprintf("label is matching %v ", [new_labels ])
}
keys(labels) = [k | l := labels, {k, _} := l]
#keys(labels) = [k | k := label, _ := label[k]]
key(new_labels) = [k | k = new_labels _ := new_labels[k]]
error i am getting is
2 errors occurred:
policy.rego:10: rego_parse_error: unexpected assign token: expected "," or "]"
keys(labels) = [k | l := labels, {k, _} := l]
^
policy.rego:10: rego_parse_error: unexpected assign token: expected rule value term (e.g., keys[] = { ... })
keys(labels) = [k | l := labels, {k, _} := l]
^
Beta Was this translation helpful? Give feedback.
All reactions