/
action_checks.go
188 lines (154 loc) · 4.25 KB
/
action_checks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// generated code; DO NOT EDIT
//
// Client for Choria RPC Agent 'scout' Version 0.28.0 generated using Choria version 0.28.0
package scoutclient
import (
"context"
"encoding/json"
"fmt"
"io"
"sync"
"github.com/choria-io/go-choria/protocol"
rpcclient "github.com/choria-io/go-choria/providers/agent/mcorpc/client"
"github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent"
"github.com/choria-io/go-choria/providers/agent/mcorpc/replyfmt"
)
// ChecksRequester performs a RPC request to scout#checks
type ChecksRequester struct {
r *requester
outc chan *ChecksOutput
}
// ChecksOutput is the output from the checks action
type ChecksOutput struct {
details *ResultDetails
reply map[string]any
}
// ChecksResult is the result from a checks action
type ChecksResult struct {
ddl *agent.DDL
stats *rpcclient.Stats
outputs []*ChecksOutput
rpcreplies []*replyfmt.RPCReply
mu sync.Mutex
}
func (d *ChecksResult) RenderResults(w io.Writer, format RenderFormat, displayMode DisplayMode, verbose bool, silent bool, colorize bool, log Log) error {
d.mu.Lock()
defer d.mu.Unlock()
if d.stats == nil {
return fmt.Errorf("result stats is not set, result was not completed")
}
results := &replyfmt.RPCResults{
Agent: d.stats.Agent(),
Action: d.stats.Action(),
Replies: d.rpcreplies,
Stats: d.stats,
}
addl, err := d.ddl.ActionInterface(d.stats.Action())
if err != nil {
return err
}
switch format {
case JSONFormat:
return results.RenderJSON(w, addl)
case TableFormat:
return results.RenderTable(w, addl)
case TXTFooter:
results.RenderTXTFooter(w, verbose)
return nil
default:
return results.RenderTXT(w, addl, verbose, silent, replyfmt.DisplayMode(displayMode), colorize, log)
}
}
// Stats is the rpc request stats
func (d *ChecksResult) Stats() Stats {
return d.stats
}
// ResultDetails is the details about the request
func (d *ChecksOutput) ResultDetails() *ResultDetails {
return d.details
}
// HashMap is the raw output data
func (d *ChecksOutput) HashMap() map[string]any {
return d.reply
}
// JSON is the JSON representation of the output data
func (d *ChecksOutput) JSON() ([]byte, error) {
return json.Marshal(d.reply)
}
// ParseChecksOutput parses the result value from the Checks action into target
func (d *ChecksOutput) ParseChecksOutput(target any) error {
j, err := d.JSON()
if err != nil {
return fmt.Errorf("could not access payload: %s", err)
}
err = json.Unmarshal(j, target)
if err != nil {
return fmt.Errorf("could not unmarshal JSON payload: %s", err)
}
return nil
}
// Do performs the request
func (d *ChecksRequester) Do(ctx context.Context) (*ChecksResult, error) {
dres := &ChecksResult{ddl: d.r.client.ddl}
addl, err := dres.ddl.ActionInterface(d.r.action)
if err != nil {
return nil, err
}
handler := func(pr protocol.Reply, r *rpcclient.RPCReply) {
// filtered by expr filter
if r == nil {
return
}
output := &ChecksOutput{
reply: make(map[string]any),
details: &ResultDetails{
sender: pr.SenderID(),
code: int(r.Statuscode),
message: r.Statusmsg,
ts: pr.Time(),
},
}
addl.SetOutputDefaults(output.reply)
err := json.Unmarshal(r.Data, &output.reply)
if err != nil {
d.r.client.errorf("Could not decode reply from %s: %s", pr.SenderID(), err)
}
// caller wants a channel so we dont return a resultset too (lots of memory etc)
// this is unused now, no support for setting a channel
if d.outc != nil {
d.outc <- output
return
}
// else prepare our result set
dres.mu.Lock()
dres.outputs = append(dres.outputs, output)
dres.rpcreplies = append(dres.rpcreplies, &replyfmt.RPCReply{
Sender: pr.SenderID(),
RPCReply: r,
})
dres.mu.Unlock()
}
res, err := d.r.do(ctx, handler)
if err != nil {
return nil, err
}
dres.stats = res
return dres, nil
}
// AllOutputs provide access to all outputs
func (d *ChecksResult) AllOutputs() []*ChecksOutput {
return d.outputs
}
// EachOutput iterates over all results received
func (d *ChecksResult) EachOutput(h func(r *ChecksOutput)) {
for _, resp := range d.outputs {
h(resp)
}
}
// Checks is the value of the checks output
//
// Description: Details about each check
func (d *ChecksOutput) Checks() []any {
val := d.reply["checks"]
return val.([]any)
}