-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftypes.go
421 lines (372 loc) · 11.3 KB
/
conftypes.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
Copyright © 2022 ITRS Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package geneos
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"reflect"
"strings"
"gopkg.in/yaml.v3"
)
// Vars is a container for specific variable types. Only one field should
// be set at a time. This list is not complete, much like many of the
// configuration settings.
type Vars struct {
XMLName xml.Name `xml:"var" json:"-" yaml:"-"`
Name string `xml:"name,attr"`
Boolean *bool `xml:"boolean,omitempty" json:",omitempty" yaml:",omitempty"`
Double *float64 `xml:"double,omitempty" json:",omitempty" yaml:",omitempty"`
Integer *int64 `xml:"integer,omitempty" json:",omitempty" yaml:",omitempty"`
String string `xml:"string,omitempty" json:",omitempty" yaml:",omitempty"`
StringList *StringList `xml:"stringList,omitempty" json:",omitempty" yaml:",omitempty"`
NameValueList *NameValueList `xml:"nameValueList,omitempty" json:",omitempty" yaml:",omitempty"`
Macro *Macro `xml:"macro,omitempty" json:",omitempty" yaml:",omitempty"`
Regex *Regex `xml:"regex,omitempty" json:",omitempty" yaml:",omitempty"`
}
// GetKey satisfies the KeyedObject interface
func (v Vars) GetKey() string {
return v.Name
}
// Macro is a container for the various macro variable types. Only
// initialise one field to an empty struct, the rest must be nil
// pointers. e.g.
//
// macro := geneos.Macro{InsecureGatewayPort: &geneos.EmptyStruct{}}
type Macro struct {
InsecureGatewayPort *EmptyStruct `xml:"insecureGatewayPort,omitempty" json:",omitempty" yaml:",omitempty"`
GatewayName *EmptyStruct `xml:"gatewayName,omitempty" json:",omitempty" yaml:",omitempty"`
NetprobeName *EmptyStruct `xml:"netprobeName,omitempty" json:",omitempty" yaml:",omitempty"`
NetprobeHost *EmptyStruct `xml:"netprobeHost,omitempty" json:",omitempty" yaml:",omitempty"`
NetprobePort *EmptyStruct `xml:"netprobePort,omitempty" json:",omitempty" yaml:",omitempty"`
ManagedEntitiesName *EmptyStruct `xml:"managedEntityName,omitempty" json:",omitempty" yaml:",omitempty"`
SamplerName *EmptyStruct `xml:"samplerName,omitempty" json:",omitempty" yaml:",omitempty"`
SecureGatewayPort *EmptyStruct `xml:"secureGatewayPort,omitempty" json:",omitempty" yaml:",omitempty"`
}
// EmptyStruct is an empty struct used to indicate which macro VarMacro refers
// to.
type EmptyStruct struct{}
type StringList struct {
Strings []string `xml:"string" mapstructure:"string"`
}
// UnmarshalText conforms to the mapstructure decode hook to covert an
// empty tag to a map, not a string (from mxj)
func (s *StringList) UnmarshalText(text []byte) error {
if len(text) == 0 {
s = &StringList{}
}
return nil
}
type NameValueList struct {
NameValues []NameValue `xml:"item,omitempty" json:",omitempty" yaml:",omitempty"`
}
// UnmarshalText conforms to the mapstructure decode hook to covert an
// empty tag to a map, not a string (from mxj)
func (s *NameValueList) UnmarshalText(text []byte) error {
if len(text) == 0 {
s = &NameValueList{}
}
return nil
}
type NameValue struct {
Name string `xml:"name"`
Value string `xml:"value"`
}
type Reference struct {
Name string `xml:"ref,attr" json:",omitempty" yaml:",omitempty"`
}
// A SingleLineStringVar is a container for a single line string that
// can be made up of static text and variable references. Use like this:
//
// type MyContainer struct {
// XMLName xml.Name `xml:"mycontainer"`
// VarField *SingleLineStringVar `xml:"fieldname"`
// }
//
// func blah() {
// x := MyContainer{
// VarField: geneos.SingleLineStringVar("hello $(var) world!")
// }
// ...
// }
type SingleLineStringVar struct {
Parts []interface{}
}
// NewSingleLineString take a plain string and locates any Geneos style
// variables of the form $(var) - note these are parenthesis and not brackets -
// and splits the string into Data and Var parts as required so that this can be
// used directly in the XML encodings.
func NewSingleLineString(in string) (s *SingleLineStringVar) {
s = &SingleLineStringVar{}
for i := 0; i < len(in); i++ {
st := strings.Index(in[i:], "$(")
if st == -1 {
s.Parts = append(s.Parts, Data{Data: in[i:]})
return
}
if st > 0 {
s.Parts = append(s.Parts, Data{Data: in[i : i+st]})
}
en := strings.Index(in[i+st+2:], ")")
if en == -1 {
s.Parts = append(s.Parts, Var{Var: in[i+st+2:]})
return
}
s.Parts = append(s.Parts, Var{Var: in[i+st+2 : i+st+2+en]})
i += st + 2 + en
}
return
}
// ensure that Value satisfies xml.Unmarshaler interface
var _ xml.Unmarshaler = (*SingleLineStringVar)(nil)
// UnmarshalXML satisfies the XML Unmarshaler interface
func (v *SingleLineStringVar) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if v == nil {
v = &SingleLineStringVar{}
}
for {
tok, err := d.Token()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
element, ok := tok.(xml.StartElement)
if !ok {
continue
}
switch element.Name.Local {
case "data":
t := &Data{}
err = d.DecodeElement(&t, &element)
if err != nil {
return err
}
v.Parts = append(v.Parts, t)
case "var":
t := &Var{}
err = d.DecodeElement(&t, &element)
if err != nil {
return err
}
v.Parts = append(v.Parts, t)
}
}
}
var _ fmt.Stringer = (*SingleLineStringVar)(nil)
func (s *SingleLineStringVar) String() (out string) {
if s == nil {
return
}
for _, p := range s.Parts {
s, ok := p.(fmt.Stringer)
if ok {
out += s.String()
}
}
return
}
var _ json.Marshaler = (*SingleLineStringVar)(nil)
func (s *SingleLineStringVar) MarshalJSON() (out []byte, err error) {
return json.Marshal(s.String())
}
var _ yaml.Marshaler = (*SingleLineStringVar)(nil)
func (s *SingleLineStringVar) MarshalYAML() (out interface{}, err error) {
out = s.String()
return
}
type Var struct {
XMLName xml.Name `xml:"var" json:"-" yaml:"-"`
Var string `xml:"ref,attr"`
}
func (v Var) String() string {
return "$(" + v.Var + ")"
}
type Data struct {
XMLName xml.Name `xml:"data" json:"-" yaml:"-"`
Data string `xml:",chardata"`
}
func (d Data) String() string {
return d.Data
}
// A Value is either a simple string or a variable
//
// It can also contain an external password reference or an encoded password
type Value struct {
Data *Data `xml:"data,omitempty" json:",omitempty" yaml:",omitempty"`
Var *Var `xml:"var,omitempty" json:",omitempty" yaml:",omitempty"`
ExtPwd string `xml:"extPwd,omitempty" json:",omitempty" yaml:",omitempty"`
StdAES string `xml:"stdAES,omitempty" json:",omitempty" yaml:",omitempty"`
}
// NewValue takes an argument and if a string removes leading and
// trailing spaces. If the string is of the form "$(var)" then returns a
// pointer to a VarData struct containing a Var{} or if a non-empty
// string returns a Data{}. If the string is empty then a nil pointer is
// returned. Any other value is copied as is. This allows
// `xml:",omitempty"“ to leave out VarData fields that contain no data.
func NewValue(in interface{}) (n *Value) {
n = &Value{}
switch s := in.(type) {
case string:
s = strings.TrimSpace(s)
if s == "" {
return nil
}
if strings.HasPrefix(s, "$(") && strings.HasSuffix(s, ")") {
n.Var = &Var{Var: s[2 : len(s)-1]}
} else {
n.Data = &Data{Data: s}
// n.Data = append(n.Data, Data{Data: s})
}
case []string:
for _, str := range s {
n.Data = &Data{Data: str}
// n.Data = append(n.Data, Data{Data: str})
}
default:
if reflect.TypeOf(s).Kind() == reflect.Slice {
sl := reflect.ValueOf(s)
for i := 0; i < sl.Len(); i++ {
n.Data = &Data{Data: fmt.Sprint(sl.Index(i))}
// n.Data = append(n.Data, Data{Data: fmt.Sprint(sl.Index(i))})
}
} else {
n.Data = &Data{Data: fmt.Sprint(s)}
// n.Data = append(n.Data, Data{Data: fmt.Sprint(s)})
}
}
return
}
// ensure that Value satisfies xml.Unmarshaler interface
var _ xml.Unmarshaler = (*Value)(nil)
func (v *Value) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if v == nil {
v = &Value{}
}
for {
tok, err := d.Token()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
element, ok := tok.(xml.StartElement)
if !ok {
continue
}
switch element.Name.Local {
case "data":
t := Data{}
err = d.DecodeElement(&t, &element)
if err != nil {
return err
}
v.Data = &t
case "var":
t := &Var{}
err = d.DecodeElement(&t, &element)
if err != nil {
return err
}
v.Var = t
case "extPwd":
err = d.DecodeElement(&v.ExtPwd, &element)
if err != nil {
return err
}
case "stdAES":
err = d.DecodeElement(&v.StdAES, &element)
if err != nil {
return err
}
}
}
}
var _ fmt.Stringer = (*Value)(nil)
func (s *Value) String() (out string) {
if s == nil {
return ""
}
if s.Var != nil {
return "$(" + s.Var.Var + ")"
}
if s.ExtPwd != "" {
return "extpwd:" + s.ExtPwd
}
if s.StdAES != "" {
return "[encoded-password]"
}
return fmt.Sprint(s.Data)
}
type Regex struct {
Regex string `xml:"regex" json:"regex,omitempty" yaml:"regex,omitempty"`
Flags *RegexFlags `xml:"flags,omitempty" json:"flags,omitempty" yaml:"flags,omitempty"`
}
type RegexFlags struct {
CaseInsensitive *bool `xml:"i,omitempty" json:"i,omitempty" yaml:"i,omitempty"`
DotMatchesAll *bool `xml:"s,omitempty" json:"s,omitempty" yaml:"s,omitempty"`
}
func (r *Regex) String() (s string) {
if r == nil {
return ""
}
s = fmt.Sprintf("/%s/", r.Regex)
if r.Flags == nil {
return
}
if r.Flags.CaseInsensitive != nil && *r.Flags.CaseInsensitive {
s += "i"
}
if r.Flags.DotMatchesAll != nil && *r.Flags.DotMatchesAll {
s += "s"
}
return
}
type Host struct {
Name string `xml:"name,omitempty"`
IPAddress *IPAddress `xml:"ipAddress,omitempty"`
Var *Reference `xml:"var,omitempty"`
}
func (t *Host) String() string {
if t == nil {
return ""
}
if t.IPAddress != nil {
return t.IPAddress.String()
}
if t.Var != nil {
return "$(" + t.Name + ")"
}
return t.Name
}
type IPAddress struct {
Octets []int `xml:"value"` // must be []int else unmarshal treats []uint8 as a []byte
}
func (ip *IPAddress) String() string {
if ip == nil {
return "0.0.0.0"
}
if len(ip.Octets) != 4 {
return ""
}
return fmt.Sprintf("%d.%d.%d.%d", ip.Octets[0], ip.Octets[1], ip.Octets[2], ip.Octets[3])
}