Example data:
{
"serviceA_primary": {
"foo": 1
"bar": 2
},
"serviceA_secondary": {
"foo": 1,
"bar": 3
},
"serviceB_primary": {
"bar": 2
},
"serviceB_secondary": {
"bar": 3
}
...
}
I want to write a test that for every key with serviceA in the name, it's bar
value matches with the bar
value of the corresponding serviceB.
For example, serviceA_primary.bar == serviceB_primary.bar
and serviceA_secondary.bar == serviceB_secondary.bar
Honestly don't really know how to approach. I was planning on filtering out serviceB
and then for every serivceA
do something. But I don't think it's the right approach.
deny_due_to_mismatch[res] if {
some key, val in input
res := {k: v |
regex.match(`serviceA`, key)
k := mpkhs_coord_name
v := mpkhs_coord_info
}
}
TLDR; This logic is simple to express using the
every
keyword (which you can enable withimport rego.v1
). For example:To make this happen, I defined two helper rules
service_a
andservice_b
that generate maps containing the respective services, keyed by the suffixes (e.g.,"primary",
"secondary"`, etc.)There is duplicated logic here, however, if there will only ever be
serviceA
andserviceB
then I think this is fine. On the other hand, if you want to generalize the logic you could write a function that returns a map of services for a given name/prefix. For example:Then you could use the function like so:
Here's a full example in the playground: https://play.openpolicyagent.org/p/53Hh2wU2Uh