mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
rego: Add persistent storage example
Signed-off-by: Torin Sandall <torinsandall@gmail.com>
This commit is contained in:
@@ -10,9 +10,12 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/open-policy-agent/opa/storage/disk"
|
||||
|
||||
"github.com/open-policy-agent/opa/ast"
|
||||
"github.com/open-policy-agent/opa/rego"
|
||||
"github.com/open-policy-agent/opa/storage"
|
||||
@@ -313,6 +316,94 @@ func ExampleRego_Eval_storage() {
|
||||
// value: [dogs clouds]
|
||||
}
|
||||
|
||||
func ExampleRego_Eval_persistent_storage() {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
data := `{
|
||||
"example": {
|
||||
"users": {
|
||||
"alice": {
|
||||
"likes": ["dogs", "clouds"]
|
||||
},
|
||||
"bob": {
|
||||
"likes": ["pizza", "cats"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
var json map[string]interface{}
|
||||
|
||||
err := util.UnmarshalJSON([]byte(data), &json)
|
||||
if err != nil {
|
||||
// Handle error.
|
||||
}
|
||||
|
||||
// Manually create a persistent storage-layer in a temporary directory.
|
||||
rootDir, err := ioutil.TempDir("", "rego_example")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer os.RemoveAll(rootDir)
|
||||
|
||||
// Configure the store to partition data at `/example/users` so that each
|
||||
// user's data is stored on a different row. Assuming the policy only reads
|
||||
// data for a single user to process the policy query, OPA can avoid loading
|
||||
// _all_ user data into memory this way.
|
||||
store, err := disk.New(ctx, disk.Options{
|
||||
Dir: rootDir,
|
||||
Partitions: []storage.Path{{"example", "user"}},
|
||||
})
|
||||
if err != nil {
|
||||
// Handle error.
|
||||
}
|
||||
|
||||
err = storage.WriteOne(ctx, store, storage.AddOp, storage.Path{}, json)
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
// Run a query that returns the value
|
||||
rs, err := rego.New(
|
||||
rego.Query(`data.example.users["alice"].likes`),
|
||||
rego.Store(store)).Eval(ctx)
|
||||
if err != nil {
|
||||
// Handle error.
|
||||
}
|
||||
|
||||
// Inspect the result.
|
||||
fmt.Println("value:", rs[0].Expressions[0].Value)
|
||||
|
||||
// Re-open the store in the same directory.
|
||||
store.Close(ctx)
|
||||
|
||||
store2, err := disk.New(ctx, disk.Options{
|
||||
Dir: rootDir,
|
||||
Partitions: []storage.Path{{"example", "user"}},
|
||||
})
|
||||
if err != nil {
|
||||
// Handle error.
|
||||
}
|
||||
|
||||
// Run the same query with a new store.
|
||||
rs, err = rego.New(
|
||||
rego.Query(`data.example.users["alice"].likes`),
|
||||
rego.Store(store2)).Eval(ctx)
|
||||
if err != nil {
|
||||
// Handle error.
|
||||
}
|
||||
|
||||
// Inspect the result and observe the same result.
|
||||
fmt.Println("value:", rs[0].Expressions[0].Value)
|
||||
|
||||
// Output:
|
||||
//
|
||||
// value: [dogs clouds]
|
||||
// value: [dogs clouds]
|
||||
}
|
||||
|
||||
func ExampleRego_Eval_transactions() {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
Reference in New Issue
Block a user