Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions aagent/watchers/kvwatcher/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/nats-io/nats.go"
"github.com/tidwall/gjson"

"github.com/choria-io/ccm/hiera"
"github.com/choria-io/go-choria/aagent/model"
Expand Down Expand Up @@ -59,6 +60,7 @@ type properties struct {
RepublishTrigger string `mapstructure:"republish_trigger"`
HieraConfig bool `mapstructure:"hiera_config"`
StoreKey string `mapstructure:"store_key"`
Query string `mapstructure:"query"`
}

type Watcher struct {
Expand Down Expand Up @@ -337,6 +339,15 @@ func (w *Watcher) parseValue(val []byte) (any, error) {
}
}

if w.properties.Query != "" {
j, err := json.Marshal(parsedValue)
if err != nil {
return nil, fmt.Errorf("could not marshal data for query: %w", err)
}

res := gjson.GetBytes(j, w.properties.Query)
parsedValue = res.Value()
}
} else if bytes.HasPrefix(v, []byte("[")) && bytes.HasSuffix(v, []byte("]")) {
parsedValue = []any{}
err := json.Unmarshal(v, &parsedValue)
Expand Down
8 changes: 8 additions & 0 deletions aagent/watchers/kvwatcher/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ var _ = Describe("AAgent/Watchers/KvWatcher", func() {
Expect(err).ToNot(HaveOccurred())
})

It("Should support data queries", func() {
w.properties.Query = "spec"
kve.EXPECT().Value().Return([]byte("{\"spec\": \"foo\"}")).MinTimes(1)
machine.EXPECT().DataPut("machines", "foo").Return(nil).Times(1)
_, err := w.poll()
Expect(err).ToNot(HaveOccurred())
})

It("Should handle a leading and trailing unicode whitespace", func() {
kve.EXPECT().Value().Return([]byte("\n \t{\"spec\": \"foo\"}\t \n")).MinTimes(1)
machine.EXPECT().DataPut("machines", map[string]any{"spec": "foo"}).Return(nil).Times(1)
Expand Down