Skip to content

Commit deb7971

Browse files
committed
implement opa_eval to evaluate a query and store the result in a file
1 parent 1b47c6b commit deb7971

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

examples/simple/BUILD.bazel

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@rules_opa//opa:defs.bzl", "opa_binary", "opa_check", "opa_eval_binary", "opa_library", "opa_test")
1+
load("@rules_opa//opa:defs.bzl", "opa_binary", "opa_check", "opa_eval", "opa_eval_binary", "opa_library", "opa_test")
22

33
opa_library(
44
name = "simple",
@@ -44,4 +44,12 @@ opa_eval_binary(
4444
deps = [":simple"],
4545
)
4646

47+
opa_eval(
48+
name = "check_bob_json",
49+
out = "bob.json",
50+
input = '{"name":"bob"}',
51+
query = "data.main.allow",
52+
deps = [":simple"],
53+
)
54+
4755
exports_files(["schemas/input.json"])

opa/defs.bzl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
load("//opa/private:opa_toolchain.bzl", _OpacInfo = "OpacInfo", _opa_toolchain = "opa_toolchain")
2-
load("//opa/private:opa_library.bzl", _OpaInfo = "OpaInfo", _opa_library = "opa_library")
3-
load("//opa/private:opa_test.bzl", _opa_test = "opa_test")
1+
load("//opa/private:opa_binary.bzl", _opa_binary = "opa_binary")
42
load("//opa/private:opa_check.bzl", _opa_check = "opa_check")
3+
load("//opa/private:opa_eval.bzl", _opa_eval = "opa_eval")
54
load("//opa/private:opa_eval_binary.bzl", _opa_eval_binary = "opa_eval_binary")
6-
load("//opa/private:opa_binary.bzl", _opa_binary = "opa_binary")
5+
load("//opa/private:opa_library.bzl", _OpaInfo = "OpaInfo", _opa_library = "opa_library")
6+
load("//opa/private:opa_test.bzl", _opa_test = "opa_test")
7+
load("//opa/private:opa_toolchain.bzl", _OpacInfo = "OpacInfo", _opa_toolchain = "opa_toolchain")
78

89
opa_toolchain = _opa_toolchain
910
opa_library = _opa_library
1011
opa_test = _opa_test
1112
opa_check = _opa_check
1213
opa_eval_binary = _opa_eval_binary
14+
opa_eval = _opa_eval
1315
opa_binary = _opa_binary
1416
OpaInfo = _OpaInfo
1517
OpacInfo = _OpacInfo

opa/private/opa_eval.bzl

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
load(":opa_library.bzl", "OpaInfo")
2+
3+
def _opa_eval_impl(ctx):
4+
toolchain = ctx.toolchains["//tools:toolchain_type"].opacinfo
5+
out_file = ctx.actions.declare_file(ctx.attr.out)
6+
7+
if len(ctx.attr.deps) != 1:
8+
fail("opa_eval only allow a single deps")
9+
10+
bundle = ctx.attr.deps[0][OpaInfo].bundle
11+
additional_inputs = []
12+
13+
args = ctx.actions.args()
14+
15+
args.add(toolchain.opa)
16+
args.add("eval")
17+
args.add("-b").add(bundle)
18+
19+
if ctx.attr.partial:
20+
args.add("--partial")
21+
if ctx.file.input_file:
22+
additional_inputs.append(ctx.file.input_file)
23+
args.add("--input").add(ctx.file.input_file)
24+
if ctx.attr.input:
25+
input_file = ctx.actions.declare_file(ctx.label.name + "_input.txt")
26+
ctx.actions.write(output = input_file, content = ctx.attr.input)
27+
additional_inputs.append(input_file)
28+
args.add("--input").add(input_file)
29+
if ctx.attr.unknowns:
30+
args.add("--unknowns").add(",".join(ctx.attr.unknowns))
31+
32+
args.add("--format=%s" % (ctx.attr.format))
33+
args.add(ctx.attr.query)
34+
35+
ctx.actions.run_shell(
36+
tools = [toolchain.opa],
37+
inputs = [bundle] + additional_inputs,
38+
outputs = [out_file],
39+
arguments = [args],
40+
command = "$@ > " + out_file.path,
41+
)
42+
43+
return [
44+
DefaultInfo(
45+
files = depset([out_file]),
46+
),
47+
]
48+
49+
opa_eval = rule(
50+
implementation = _opa_eval_impl,
51+
attrs = {
52+
"deps": attr.label_list(
53+
providers = [OpaInfo],
54+
mandatory = True,
55+
doc = "The bundle to evaluate",
56+
),
57+
"query": attr.string(
58+
mandatory = True,
59+
doc = "The query to evaluate",
60+
),
61+
"out": attr.string(
62+
mandatory = True,
63+
doc = "Output file name",
64+
),
65+
"format": attr.string(
66+
default = "json",
67+
values = ["json", "values", "bindings", "pretty", "source", "raw"],
68+
doc = "The output format (see https://www.openpolicyagent.org/docs/latest/cli/#output-formats)",
69+
),
70+
"partial": attr.bool(
71+
default = False,
72+
doc = "perform partial evaluation",
73+
),
74+
"unknowns": attr.string_list(
75+
doc = "set paths to treat as unknown during partial evaluation",
76+
),
77+
"input": attr.string(
78+
doc = "input",
79+
),
80+
"input_file": attr.label(
81+
allow_single_file = True,
82+
doc = "input",
83+
),
84+
},
85+
toolchains = ["//tools:toolchain_type"],
86+
)

opa/private/opa_eval_binary.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def _opa_eval_binary_impl(ctx):
55
toolchain = ctx.toolchains["//tools:toolchain_type"].opacinfo
66

77
if len(ctx.attr.deps) != 1:
8-
fail("opa_binary only allow a single deps")
8+
fail("opa_eval_binary only allow a single deps")
99

1010
bundle = ctx.attr.deps[0][OpaInfo].bundle
1111

0 commit comments

Comments
 (0)