github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/client/choria_utilclient/action_machine_state.go (about) 1 // generated code; DO NOT EDIT 2 // 3 // Client for Choria RPC Agent 'choria_util' Version 0.28.0 generated using Choria version 0.28.0 4 5 package choria_utilclient 6 7 import ( 8 "context" 9 "encoding/json" 10 "fmt" 11 "io" 12 "sync" 13 14 "github.com/choria-io/go-choria/protocol" 15 rpcclient "github.com/choria-io/go-choria/providers/agent/mcorpc/client" 16 "github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent" 17 "github.com/choria-io/go-choria/providers/agent/mcorpc/replyfmt" 18 ) 19 20 // MachineStateRequester performs a RPC request to choria_util#machine_state 21 type MachineStateRequester struct { 22 r *requester 23 outc chan *MachineStateOutput 24 } 25 26 // MachineStateOutput is the output from the machine_state action 27 type MachineStateOutput struct { 28 details *ResultDetails 29 reply map[string]any 30 } 31 32 // MachineStateResult is the result from a machine_state action 33 type MachineStateResult struct { 34 ddl *agent.DDL 35 stats *rpcclient.Stats 36 outputs []*MachineStateOutput 37 rpcreplies []*replyfmt.RPCReply 38 mu sync.Mutex 39 } 40 41 func (d *MachineStateResult) RenderResults(w io.Writer, format RenderFormat, displayMode DisplayMode, verbose bool, silent bool, colorize bool, log Log) error { 42 d.mu.Lock() 43 defer d.mu.Unlock() 44 45 if d.stats == nil { 46 return fmt.Errorf("result stats is not set, result was not completed") 47 } 48 49 results := &replyfmt.RPCResults{ 50 Agent: d.stats.Agent(), 51 Action: d.stats.Action(), 52 Replies: d.rpcreplies, 53 Stats: d.stats, 54 } 55 56 addl, err := d.ddl.ActionInterface(d.stats.Action()) 57 if err != nil { 58 return err 59 } 60 61 switch format { 62 case JSONFormat: 63 return results.RenderJSON(w, addl) 64 case TableFormat: 65 return results.RenderTable(w, addl) 66 case TXTFooter: 67 results.RenderTXTFooter(w, verbose) 68 return nil 69 default: 70 return results.RenderTXT(w, addl, verbose, silent, replyfmt.DisplayMode(displayMode), colorize, log) 71 } 72 } 73 74 // Stats is the rpc request stats 75 func (d *MachineStateResult) Stats() Stats { 76 return d.stats 77 } 78 79 // ResultDetails is the details about the request 80 func (d *MachineStateOutput) ResultDetails() *ResultDetails { 81 return d.details 82 } 83 84 // HashMap is the raw output data 85 func (d *MachineStateOutput) HashMap() map[string]any { 86 return d.reply 87 } 88 89 // JSON is the JSON representation of the output data 90 func (d *MachineStateOutput) JSON() ([]byte, error) { 91 return json.Marshal(d.reply) 92 } 93 94 // ParseMachineStateOutput parses the result value from the MachineState action into target 95 func (d *MachineStateOutput) ParseMachineStateOutput(target any) error { 96 j, err := d.JSON() 97 if err != nil { 98 return fmt.Errorf("could not access payload: %s", err) 99 } 100 101 err = json.Unmarshal(j, target) 102 if err != nil { 103 return fmt.Errorf("could not unmarshal JSON payload: %s", err) 104 } 105 106 return nil 107 } 108 109 // Do performs the request 110 func (d *MachineStateRequester) Do(ctx context.Context) (*MachineStateResult, error) { 111 dres := &MachineStateResult{ddl: d.r.client.ddl} 112 113 addl, err := dres.ddl.ActionInterface(d.r.action) 114 if err != nil { 115 return nil, err 116 } 117 118 handler := func(pr protocol.Reply, r *rpcclient.RPCReply) { 119 // filtered by expr filter 120 if r == nil { 121 return 122 } 123 124 output := &MachineStateOutput{ 125 reply: make(map[string]any), 126 details: &ResultDetails{ 127 sender: pr.SenderID(), 128 code: int(r.Statuscode), 129 message: r.Statusmsg, 130 ts: pr.Time(), 131 }, 132 } 133 134 addl.SetOutputDefaults(output.reply) 135 136 err := json.Unmarshal(r.Data, &output.reply) 137 if err != nil { 138 d.r.client.errorf("Could not decode reply from %s: %s", pr.SenderID(), err) 139 } 140 141 // caller wants a channel so we dont return a resultset too (lots of memory etc) 142 // this is unused now, no support for setting a channel 143 if d.outc != nil { 144 d.outc <- output 145 return 146 } 147 148 // else prepare our result set 149 dres.mu.Lock() 150 dres.outputs = append(dres.outputs, output) 151 dres.rpcreplies = append(dres.rpcreplies, &replyfmt.RPCReply{ 152 Sender: pr.SenderID(), 153 RPCReply: r, 154 }) 155 dres.mu.Unlock() 156 } 157 158 res, err := d.r.do(ctx, handler) 159 if err != nil { 160 return nil, err 161 } 162 163 dres.stats = res 164 165 return dres, nil 166 } 167 168 // AllOutputs provide access to all outputs 169 func (d *MachineStateResult) AllOutputs() []*MachineStateOutput { 170 return d.outputs 171 } 172 173 // EachOutput iterates over all results received 174 func (d *MachineStateResult) EachOutput(h func(r *MachineStateOutput)) { 175 for _, resp := range d.outputs { 176 h(resp) 177 } 178 } 179 180 // Instance is an optional input to the machine_state action 181 // 182 // Description: Machine Instance ID 183 func (d *MachineStateRequester) Instance(v string) *MachineStateRequester { 184 d.r.args["instance"] = v 185 186 return d 187 } 188 189 // Name is an optional input to the machine_state action 190 // 191 // Description: Machine Name 192 func (d *MachineStateRequester) Name(v string) *MachineStateRequester { 193 d.r.args["name"] = v 194 195 return d 196 } 197 198 // Path is an optional input to the machine_state action 199 // 200 // Description: Machine Path 201 func (d *MachineStateRequester) Path(v string) *MachineStateRequester { 202 d.r.args["path"] = v 203 204 return d 205 } 206 207 // AvailableTransitions is the value of the available_transitions output 208 // 209 // Description: The list of available transitions this autonomous agent can make 210 func (d *MachineStateOutput) AvailableTransitions() []any { 211 val := d.reply["available_transitions"] 212 213 return val.([]any) 214 215 } 216 217 // CurrentState is the value of the current_state output 218 // 219 // Description: The Choria Scout specific state for Scout checks 220 func (d *MachineStateOutput) CurrentState() any { 221 val, ok := d.reply["current_state"] 222 if !ok || val == nil { 223 // we have to avoid returning nil.(any) 224 return nil 225 } 226 227 return val 228 229 } 230 231 // Id is the value of the id output 232 // 233 // Description: The unique running ID of the autonomous agent 234 func (d *MachineStateOutput) Id() string { 235 val := d.reply["id"] 236 237 return val.(string) 238 239 } 240 241 // Name is the value of the name output 242 // 243 // Description: The name of the autonomous agent 244 func (d *MachineStateOutput) Name() string { 245 val := d.reply["name"] 246 247 return val.(string) 248 249 } 250 251 // Path is the value of the path output 252 // 253 // Description: The location on disk where the autonomous agent is stored 254 func (d *MachineStateOutput) Path() string { 255 val := d.reply["path"] 256 257 return val.(string) 258 259 } 260 261 // Scout is the value of the scout output 262 // 263 // Description: True when this autonomous agent represents a Choria Scout Check 264 func (d *MachineStateOutput) Scout() bool { 265 val := d.reply["scout"] 266 267 return val.(bool) 268 269 } 270 271 // StartTime is the value of the start_time output 272 // 273 // Description: The time the autonomous agent was started in unix seconds 274 func (d *MachineStateOutput) StartTime() string { 275 val := d.reply["start_time"] 276 277 return val.(string) 278 279 } 280 281 // State is the value of the state output 282 // 283 // Description: The current state the agent is in 284 func (d *MachineStateOutput) State() string { 285 val := d.reply["state"] 286 287 return val.(string) 288 289 } 290 291 // Version is the value of the version output 292 // 293 // Description: The version of the autonomous agent 294 func (d *MachineStateOutput) Version() string { 295 val := d.reply["version"] 296 297 return val.(string) 298 299 }