github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/client/rpcutilclient/client.go (about) 1 // generated code; DO NOT EDIT 2 3 package rpcutilclient 4 5 import ( 6 "fmt" 7 "sync" 8 "time" 9 10 "context" 11 12 coreclient "github.com/choria-io/go-choria/client/client" 13 "github.com/choria-io/go-choria/config" 14 "github.com/choria-io/go-choria/inter" 15 "github.com/choria-io/go-choria/protocol" 16 rpcclient "github.com/choria-io/go-choria/providers/agent/mcorpc/client" 17 "github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent" 18 ) 19 20 // Stats are the statistics for a request 21 type Stats interface { 22 Agent() string 23 Action() string 24 All() bool 25 NoResponseFrom() []string 26 UnexpectedResponseFrom() []string 27 DiscoveredCount() int 28 DiscoveredNodes() *[]string 29 FailCount() int 30 OKCount() int 31 ResponsesCount() int 32 PublishDuration() (time.Duration, error) 33 RequestDuration() (time.Duration, error) 34 DiscoveryDuration() (time.Duration, error) 35 OverrideDiscoveryTime(start time.Time, end time.Time) 36 UniqueRequestID() string 37 } 38 39 // NodeSource discovers nodes 40 type NodeSource interface { 41 Reset() 42 Discover(ctx context.Context, fw inter.Framework, filters []FilterFunc) ([]string, error) 43 } 44 45 // FilterFunc can generate a Choria filter 46 type FilterFunc func(f *protocol.Filter) error 47 48 // RenderFormat is the format used by the RenderResults helper 49 type RenderFormat int 50 51 const ( 52 // JSONFormat renders the results as a JSON document 53 JSONFormat RenderFormat = iota 54 55 // TextFormat renders the results as a Choria typical result set in line with choria req output 56 TextFormat 57 58 // TableFormat renders all successful responses in a table 59 TableFormat 60 61 // TXTFooter renders only the request summary statistics 62 TXTFooter 63 ) 64 65 // DisplayMode overrides the DDL display hints 66 type DisplayMode uint8 67 68 const ( 69 // DisplayDDL shows results based on the configuration in the DDL file 70 DisplayDDL = DisplayMode(iota) 71 // DisplayOK shows only passing results 72 DisplayOK 73 // DisplayFailed shows only failed results 74 DisplayFailed 75 // DisplayAll shows all results 76 DisplayAll 77 // DisplayNone shows no results 78 DisplayNone 79 ) 80 81 type Log interface { 82 Debugf(format string, args ...any) 83 Infof(format string, args ...any) 84 Warnf(format string, args ...any) 85 Errorf(format string, args ...any) 86 Fatalf(format string, args ...any) 87 Panicf(format string, args ...any) 88 } 89 90 // RpcutilClient to the rpcutil agent 91 type RpcutilClient struct { 92 fw inter.Framework 93 cfg *config.Config 94 ddl *agent.DDL 95 ns NodeSource 96 clientOpts *initOptions 97 clientRPCOpts []rpcclient.RequestOption 98 filters []FilterFunc 99 targets []string 100 workers int 101 exprFilter string 102 noReplies bool 103 104 sync.Mutex 105 } 106 107 // Metadata is the agent metadata 108 type Metadata struct { 109 License string `json:"license"` 110 Author string `json:"author"` 111 Timeout int `json:"timeout"` 112 Name string `json:"name"` 113 Version string `json:"version"` 114 URL string `json:"url"` 115 Description string `json:"description"` 116 } 117 118 // Must create a new client and panics on error 119 func Must(fw inter.Framework, opts ...InitializationOption) (client *RpcutilClient) { 120 c, err := New(fw, opts...) 121 if err != nil { 122 panic(err) 123 } 124 125 return c 126 } 127 128 // New creates a new client to the rpcutil agent 129 func New(fw inter.Framework, opts ...InitializationOption) (client *RpcutilClient, err error) { 130 c := &RpcutilClient{ 131 fw: fw, 132 ddl: &agent.DDL{}, 133 clientRPCOpts: []rpcclient.RequestOption{}, 134 filters: []FilterFunc{ 135 FilterFunc(coreclient.AgentFilter("rpcutil")), 136 }, 137 clientOpts: &initOptions{ 138 cfgFile: coreclient.UserConfig(), 139 }, 140 targets: []string{}, 141 } 142 143 for _, opt := range opts { 144 opt(c.clientOpts) 145 } 146 147 c.cfg = c.fw.Configuration() 148 149 if c.clientOpts.dt > 0 { 150 c.cfg.DiscoveryTimeout = int(c.clientOpts.dt.Seconds()) 151 } 152 153 if c.clientOpts.ns == nil { 154 switch c.cfg.DefaultDiscoveryMethod { 155 case "choria": 156 c.clientOpts.ns = &PuppetDBNS{} 157 default: 158 c.clientOpts.ns = &BroadcastNS{} 159 } 160 } 161 c.ns = c.clientOpts.ns 162 163 if c.clientOpts.logger == nil { 164 c.clientOpts.logger = c.fw.Logger("rpcutil") 165 } else { 166 c.fw.SetLogger(c.clientOpts.logger.Logger) 167 } 168 169 c.ddl, err = DDL() 170 if err != nil { 171 return nil, fmt.Errorf("could not parse embedded DDL: %s", err) 172 } 173 174 return c, nil 175 } 176 177 // AgentMetadata is the agent metadata this client supports 178 func (p *RpcutilClient) AgentMetadata() *Metadata { 179 return &Metadata{ 180 License: p.ddl.Metadata.License, 181 Author: p.ddl.Metadata.Author, 182 Timeout: p.ddl.Metadata.Timeout, 183 Name: p.ddl.Metadata.Name, 184 Version: p.ddl.Metadata.Version, 185 URL: p.ddl.Metadata.URL, 186 Description: p.ddl.Metadata.Description, 187 } 188 } 189 190 // DiscoverNodes performs a discovery using the configured filter and node source 191 func (p *RpcutilClient) DiscoverNodes(ctx context.Context) (nodes []string, err error) { 192 p.Lock() 193 defer p.Unlock() 194 195 return p.ns.Discover(ctx, p.fw, p.filters) 196 } 197 198 // AgentInventory performs the agent_inventory action 199 // 200 // Description: Inventory of all agents on the server including versions, licenses and more 201 func (p *RpcutilClient) AgentInventory() *AgentInventoryRequester { 202 d := &AgentInventoryRequester{ 203 outc: nil, 204 r: &requester{ 205 args: map[string]any{}, 206 action: "agent_inventory", 207 client: p, 208 }, 209 } 210 211 action, _ := p.ddl.ActionInterface(d.r.action) 212 action.SetDefaults(d.r.args) 213 214 return d 215 } 216 217 // CollectiveInfo performs the collective_info action 218 // 219 // Description: Info about the main and sub collectives that the server belongs to 220 func (p *RpcutilClient) CollectiveInfo() *CollectiveInfoRequester { 221 d := &CollectiveInfoRequester{ 222 outc: nil, 223 r: &requester{ 224 args: map[string]any{}, 225 action: "collective_info", 226 client: p, 227 }, 228 } 229 230 action, _ := p.ddl.ActionInterface(d.r.action) 231 action.SetDefaults(d.r.args) 232 233 return d 234 } 235 236 // DaemonStats performs the daemon_stats action 237 // 238 // Description: Get statistics from the running daemon 239 func (p *RpcutilClient) DaemonStats() *DaemonStatsRequester { 240 d := &DaemonStatsRequester{ 241 outc: nil, 242 r: &requester{ 243 args: map[string]any{}, 244 action: "daemon_stats", 245 client: p, 246 }, 247 } 248 249 action, _ := p.ddl.ActionInterface(d.r.action) 250 action.SetDefaults(d.r.args) 251 252 return d 253 } 254 255 // GetConfigItem performs the get_config_item action 256 // 257 // Description: Get the active value of a specific config property 258 // 259 // Required Inputs: 260 // - item (string) - The item to retrieve from the server 261 func (p *RpcutilClient) GetConfigItem(inputItem string) *GetConfigItemRequester { 262 d := &GetConfigItemRequester{ 263 outc: nil, 264 r: &requester{ 265 args: map[string]any{ 266 "item": inputItem, 267 }, 268 action: "get_config_item", 269 client: p, 270 }, 271 } 272 273 action, _ := p.ddl.ActionInterface(d.r.action) 274 action.SetDefaults(d.r.args) 275 276 return d 277 } 278 279 // GetData performs the get_data action 280 // 281 // Description: Get data from a data plugin 282 // 283 // Required Inputs: 284 // - source (string) - The data plugin to retrieve information from 285 // 286 // Optional Inputs: 287 // - query (string) - The query argument to supply to the data plugin 288 func (p *RpcutilClient) GetData(inputSource string) *GetDataRequester { 289 d := &GetDataRequester{ 290 outc: nil, 291 r: &requester{ 292 args: map[string]any{ 293 "source": inputSource, 294 }, 295 action: "get_data", 296 client: p, 297 }, 298 } 299 300 action, _ := p.ddl.ActionInterface(d.r.action) 301 action.SetDefaults(d.r.args) 302 303 return d 304 } 305 306 // GetFact performs the get_fact action 307 // 308 // Description: Retrieve a single fact from the fact store 309 // 310 // Required Inputs: 311 // - fact (string) - The fact to retrieve 312 func (p *RpcutilClient) GetFact(inputFact string) *GetFactRequester { 313 d := &GetFactRequester{ 314 outc: nil, 315 r: &requester{ 316 args: map[string]any{ 317 "fact": inputFact, 318 }, 319 action: "get_fact", 320 client: p, 321 }, 322 } 323 324 action, _ := p.ddl.ActionInterface(d.r.action) 325 action.SetDefaults(d.r.args) 326 327 return d 328 } 329 330 // GetFacts performs the get_facts action 331 // 332 // Description: Retrieve multiple facts from the fact store 333 // 334 // Required Inputs: 335 // - facts (string) - Facts to retrieve 336 func (p *RpcutilClient) GetFacts(inputFacts string) *GetFactsRequester { 337 d := &GetFactsRequester{ 338 outc: nil, 339 r: &requester{ 340 args: map[string]any{ 341 "facts": inputFacts, 342 }, 343 action: "get_facts", 344 client: p, 345 }, 346 } 347 348 action, _ := p.ddl.ActionInterface(d.r.action) 349 action.SetDefaults(d.r.args) 350 351 return d 352 } 353 354 // Inventory performs the inventory action 355 // 356 // Description: System Inventory 357 func (p *RpcutilClient) Inventory() *InventoryRequester { 358 d := &InventoryRequester{ 359 outc: nil, 360 r: &requester{ 361 args: map[string]any{}, 362 action: "inventory", 363 client: p, 364 }, 365 } 366 367 action, _ := p.ddl.ActionInterface(d.r.action) 368 action.SetDefaults(d.r.args) 369 370 return d 371 } 372 373 // Ping performs the ping action 374 // 375 // Description: Responds to requests for PING with PONG 376 func (p *RpcutilClient) Ping() *PingRequester { 377 d := &PingRequester{ 378 outc: nil, 379 r: &requester{ 380 args: map[string]any{}, 381 action: "ping", 382 client: p, 383 }, 384 } 385 386 action, _ := p.ddl.ActionInterface(d.r.action) 387 action.SetDefaults(d.r.args) 388 389 return d 390 }