/
client.go
249 lines (212 loc) · 6 KB
/
client.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// generated code; DO NOT EDIT
package choria_registryclient
import (
"fmt"
"sync"
"time"
"context"
coreclient "github.com/choria-io/go-choria/client/client"
"github.com/choria-io/go-choria/config"
"github.com/choria-io/go-choria/inter"
"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"
)
// Stats are the statistics for a request
type Stats interface {
Agent() string
Action() string
All() bool
NoResponseFrom() []string
UnexpectedResponseFrom() []string
DiscoveredCount() int
DiscoveredNodes() *[]string
FailCount() int
OKCount() int
ResponsesCount() int
PublishDuration() (time.Duration, error)
RequestDuration() (time.Duration, error)
DiscoveryDuration() (time.Duration, error)
OverrideDiscoveryTime(start time.Time, end time.Time)
UniqueRequestID() string
}
// NodeSource discovers nodes
type NodeSource interface {
Reset()
Discover(ctx context.Context, fw inter.Framework, filters []FilterFunc) ([]string, error)
}
// FilterFunc can generate a Choria filter
type FilterFunc func(f *protocol.Filter) error
// RenderFormat is the format used by the RenderResults helper
type RenderFormat int
const (
// JSONFormat renders the results as a JSON document
JSONFormat RenderFormat = iota
// TextFormat renders the results as a Choria typical result set in line with choria req output
TextFormat
// TableFormat renders all successful responses in a table
TableFormat
// TXTFooter renders only the request summary statistics
TXTFooter
)
// DisplayMode overrides the DDL display hints
type DisplayMode uint8
const (
// DisplayDDL shows results based on the configuration in the DDL file
DisplayDDL = DisplayMode(iota)
// DisplayOK shows only passing results
DisplayOK
// DisplayFailed shows only failed results
DisplayFailed
// DisplayAll shows all results
DisplayAll
// DisplayNone shows no results
DisplayNone
)
type Log interface {
Debugf(format string, args ...any)
Infof(format string, args ...any)
Warnf(format string, args ...any)
Errorf(format string, args ...any)
Fatalf(format string, args ...any)
Panicf(format string, args ...any)
}
// ChoriaRegistryClient to the choria_registry agent
type ChoriaRegistryClient struct {
fw inter.Framework
cfg *config.Config
ddl *agent.DDL
ns NodeSource
clientOpts *initOptions
clientRPCOpts []rpcclient.RequestOption
filters []FilterFunc
targets []string
workers int
exprFilter string
noReplies bool
sync.Mutex
}
// Metadata is the agent metadata
type Metadata struct {
License string `json:"license"`
Author string `json:"author"`
Timeout int `json:"timeout"`
Name string `json:"name"`
Version string `json:"version"`
URL string `json:"url"`
Description string `json:"description"`
}
// Must create a new client and panics on error
func Must(fw inter.Framework, opts ...InitializationOption) (client *ChoriaRegistryClient) {
c, err := New(fw, opts...)
if err != nil {
panic(err)
}
return c
}
// New creates a new client to the choria_registry agent
func New(fw inter.Framework, opts ...InitializationOption) (client *ChoriaRegistryClient, err error) {
c := &ChoriaRegistryClient{
fw: fw,
ddl: &agent.DDL{},
clientRPCOpts: []rpcclient.RequestOption{},
filters: []FilterFunc{
FilterFunc(coreclient.AgentFilter("choria_registry")),
},
clientOpts: &initOptions{
cfgFile: coreclient.UserConfig(),
},
targets: []string{},
}
for _, opt := range opts {
opt(c.clientOpts)
}
c.cfg = c.fw.Configuration()
if c.clientOpts.dt > 0 {
c.cfg.DiscoveryTimeout = int(c.clientOpts.dt.Seconds())
}
if c.clientOpts.ns == nil {
switch c.cfg.DefaultDiscoveryMethod {
case "choria":
c.clientOpts.ns = &PuppetDBNS{}
default:
c.clientOpts.ns = &BroadcastNS{}
}
}
c.ns = c.clientOpts.ns
if c.clientOpts.logger == nil {
c.clientOpts.logger = c.fw.Logger("choria_registry")
} else {
c.fw.SetLogger(c.clientOpts.logger.Logger)
}
c.ddl, err = DDL()
if err != nil {
return nil, fmt.Errorf("could not parse embedded DDL: %s", err)
}
return c, nil
}
// AgentMetadata is the agent metadata this client supports
func (p *ChoriaRegistryClient) AgentMetadata() *Metadata {
return &Metadata{
License: p.ddl.Metadata.License,
Author: p.ddl.Metadata.Author,
Timeout: p.ddl.Metadata.Timeout,
Name: p.ddl.Metadata.Name,
Version: p.ddl.Metadata.Version,
URL: p.ddl.Metadata.URL,
Description: p.ddl.Metadata.Description,
}
}
// DiscoverNodes performs a discovery using the configured filter and node source
func (p *ChoriaRegistryClient) DiscoverNodes(ctx context.Context) (nodes []string, err error) {
p.Lock()
defer p.Unlock()
return p.ns.Discover(ctx, p.fw, p.filters)
}
// Names performs the names action
//
// Description: List of all known DDLs for a specific plugin type
//
// Required Inputs:
// - plugin_type (string) - The type of plugin
func (p *ChoriaRegistryClient) Names(inputPluginType string) *NamesRequester {
d := &NamesRequester{
outc: nil,
r: &requester{
args: map[string]any{
"plugin_type": inputPluginType,
},
action: "names",
client: p,
},
}
action, _ := p.ddl.ActionInterface(d.r.action)
action.SetDefaults(d.r.args)
return d
}
// Ddl performs the ddl action
//
// Description: Retrieve the DDL for a specific plugin
//
// Required Inputs:
// - name (string) - The name of the plugin
// - plugin_type (string) - The type of plugin
//
// Optional Inputs:
// - format (string) - The result format the plugin should be retrieved in
func (p *ChoriaRegistryClient) Ddl(inputName string, inputPluginType string) *DdlRequester {
d := &DdlRequester{
outc: nil,
r: &requester{
args: map[string]any{
"name": inputName,
"plugin_type": inputPluginType,
},
action: "ddl",
client: p,
},
}
action, _ := p.ddl.ActionInterface(d.r.action)
action.SetDefaults(d.r.args)
return d
}