github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/client/choria_utilclient/action_info.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 // InfoRequester performs a RPC request to choria_util#info 21 type InfoRequester struct { 22 r *requester 23 outc chan *InfoOutput 24 } 25 26 // InfoOutput is the output from the info action 27 type InfoOutput struct { 28 details *ResultDetails 29 reply map[string]any 30 } 31 32 // InfoResult is the result from a info action 33 type InfoResult struct { 34 ddl *agent.DDL 35 stats *rpcclient.Stats 36 outputs []*InfoOutput 37 rpcreplies []*replyfmt.RPCReply 38 mu sync.Mutex 39 } 40 41 func (d *InfoResult) 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 *InfoResult) Stats() Stats { 76 return d.stats 77 } 78 79 // ResultDetails is the details about the request 80 func (d *InfoOutput) ResultDetails() *ResultDetails { 81 return d.details 82 } 83 84 // HashMap is the raw output data 85 func (d *InfoOutput) HashMap() map[string]any { 86 return d.reply 87 } 88 89 // JSON is the JSON representation of the output data 90 func (d *InfoOutput) JSON() ([]byte, error) { 91 return json.Marshal(d.reply) 92 } 93 94 // ParseInfoOutput parses the result value from the Info action into target 95 func (d *InfoOutput) ParseInfoOutput(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 *InfoRequester) Do(ctx context.Context) (*InfoResult, error) { 111 dres := &InfoResult{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 := &InfoOutput{ 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 *InfoResult) AllOutputs() []*InfoOutput { 170 return d.outputs 171 } 172 173 // EachOutput iterates over all results received 174 func (d *InfoResult) EachOutput(h func(r *InfoOutput)) { 175 for _, resp := range d.outputs { 176 h(resp) 177 } 178 } 179 180 // ChoriaVersion is the value of the choria_version output 181 // 182 // Description: Choria version 183 func (d *InfoOutput) ChoriaVersion() string { 184 val := d.reply["choria_version"] 185 186 return val.(string) 187 188 } 189 190 // ClientFlavour is the value of the client_flavour output 191 // 192 // Description: Middleware client library flavour 193 func (d *InfoOutput) ClientFlavour() string { 194 val := d.reply["client_flavour"] 195 196 return val.(string) 197 198 } 199 200 // ClientOptions is the value of the client_options output 201 // 202 // Description: Active Middleware client options 203 func (d *InfoOutput) ClientOptions() map[string]any { 204 val := d.reply["client_options"] 205 206 return val.(map[string]any) 207 208 } 209 210 // ClientStats is the value of the client_stats output 211 // 212 // Description: Middleware client statistics 213 func (d *InfoOutput) ClientStats() map[string]any { 214 val := d.reply["client_stats"] 215 216 return val.(map[string]any) 217 218 } 219 220 // ClientVersion is the value of the client_version output 221 // 222 // Description: Middleware client library version 223 func (d *InfoOutput) ClientVersion() string { 224 val := d.reply["client_version"] 225 226 return val.(string) 227 228 } 229 230 // ConnectedServer is the value of the connected_server output 231 // 232 // Description: Connected middleware server 233 func (d *InfoOutput) ConnectedServer() string { 234 val := d.reply["connected_server"] 235 236 return val.(string) 237 238 } 239 240 // Connector is the value of the connector output 241 // 242 // Description: Connector plugin 243 func (d *InfoOutput) Connector() string { 244 val := d.reply["connector"] 245 246 return val.(string) 247 248 } 249 250 // ConnectorTls is the value of the connector_tls output 251 // 252 // Description: If the connector is running with TLS security enabled 253 func (d *InfoOutput) ConnectorTls() bool { 254 val := d.reply["connector_tls"] 255 256 return val.(bool) 257 258 } 259 260 // FacterCommand is the value of the facter_command output 261 // 262 // Description: Command used for Facter 263 func (d *InfoOutput) FacterCommand() string { 264 val := d.reply["facter_command"] 265 266 return val.(string) 267 268 } 269 270 // FacterDomain is the value of the facter_domain output 271 // 272 // Description: Facter domain 273 func (d *InfoOutput) FacterDomain() string { 274 val := d.reply["facter_domain"] 275 276 return val.(string) 277 278 } 279 280 // MiddlewareServers is the value of the middleware_servers output 281 // 282 // Description: Middleware Servers configured or discovered 283 func (d *InfoOutput) MiddlewareServers() []any { 284 val := d.reply["middleware_servers"] 285 286 return val.([]any) 287 288 } 289 290 // Path is the value of the path output 291 // 292 // Description: Active OS PATH 293 func (d *InfoOutput) Path() string { 294 val := d.reply["path"] 295 296 return val.(string) 297 298 } 299 300 // SecureProtocol is the value of the secure_protocol output 301 // 302 // Description: If the protocol is running with PKI security enabled 303 func (d *InfoOutput) SecureProtocol() bool { 304 val := d.reply["secure_protocol"] 305 306 return val.(bool) 307 308 } 309 310 // Security is the value of the security output 311 // 312 // Description: Security Provider plugin 313 func (d *InfoOutput) Security() string { 314 val := d.reply["security"] 315 316 return val.(string) 317 318 } 319 320 // SrvDomain is the value of the srv_domain output 321 // 322 // Description: Configured SRV domain 323 func (d *InfoOutput) SrvDomain() string { 324 val := d.reply["srv_domain"] 325 326 return val.(string) 327 328 } 329 330 // UsingSrv is the value of the using_srv output 331 // 332 // Description: Indicates if SRV records are considered 333 func (d *InfoOutput) UsingSrv() bool { 334 val := d.reply["using_srv"] 335 336 return val.(bool) 337 338 }