-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathobject_manager_dtc_pool.go
507 lines (469 loc) · 16.8 KB
/
object_manager_dtc_pool.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package ibclient
import (
"encoding/json"
"fmt"
"reflect"
)
type Monitor struct {
Name string
Type string
}
// Updating Servers in DtcServerLink with reference
func updateServerReferences(servers []*DtcServerLink, objMgr *ObjectManager) error {
for _, link := range servers {
sf := map[string]string{"name": link.Server}
queryParams := NewQueryParams(false, sf)
var serverResult []DtcServer
err := objMgr.connector.GetObject(&DtcServer{}, "dtc:server", queryParams, &serverResult)
if err != nil {
return fmt.Errorf("error getting Dtc Server %s, err: %s", link.Server, err)
}
if len(serverResult) > 0 {
link.Server = serverResult[0].Ref
}
}
return nil
}
// get the monitor reference
func getMonitorReference(monitorName string, monitorType string, objMgr *ObjectManager) (string, error) {
if monitorType == "" {
return "", nil
}
fields := map[string]string{"name": monitorName}
queryParams := NewQueryParams(false, fields)
var monitorResult []DtcMonitorHttp
monitorTypeKey := fmt.Sprintf("dtc:monitor:%s", monitorType)
err := objMgr.connector.GetObject(&DtcMonitorHttp{}, monitorTypeKey, queryParams, &monitorResult)
if err != nil {
return "", fmt.Errorf("error getting Dtc Monitor object %s, err: %s", monitorName, err)
}
if len(monitorResult) > 0 {
return monitorResult[0].Ref, nil
}
return "", fmt.Errorf("Dtc Monitor with name %s not found", monitorName)
}
func (cm ConsolidatedMonitorsWrapper) MarshalJSON() ([]byte, error) {
if !cm.IsNull {
if reflect.DeepEqual(cm.ConsolidatedMonitors, []*DtcPoolConsolidatedMonitorHealth{}) {
return []byte("[]"), nil
}
}
return json.Marshal(cm.ConsolidatedMonitors)
}
func (cm *ConsolidatedMonitorsWrapper) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
cm.IsNull = true
cm.ConsolidatedMonitors = nil
return nil
}
cm.IsNull = false
return json.Unmarshal(data, &cm.ConsolidatedMonitors)
}
type ConsolidatedMonitorsWrapper struct {
ConsolidatedMonitors []*DtcPoolConsolidatedMonitorHealth
IsNull bool
}
func (d *DtcPool) MarshalJSON() ([]byte, error) {
type Alias DtcPool
aux := &struct {
Monitors []string `json:"monitors"`
Servers []*DtcServerLink `json:"servers"`
ConsolidatedMonitors *ConsolidatedMonitorsWrapper `json:"consolidated_monitors,omitempty"`
*Alias
}{
Alias: (*Alias)(d),
}
// Convert Monitors to a slice of strings
if len(d.Monitors) == 0 {
aux.Monitors = []string{}
} else {
for _, monitor := range d.Monitors {
if monitor != nil {
aux.Monitors = append(aux.Monitors, monitor.Ref)
}
}
}
// Unsetting Servers if they are empty
if len(d.Servers) == 0 {
aux.Servers = []*DtcServerLink{}
} else {
aux.Servers = d.Servers
}
// Conditionally handle ConsolidatedMonitors
if *d.AutoConsolidatedMonitors {
// auto_consolidated_monitors = true
if d.ConsolidatedMonitors != nil {
// consolidated_monitors is empty, omit it
aux.ConsolidatedMonitors = nil
}
} else {
// auto_consolidated_monitors = false
if len(d.ConsolidatedMonitors) == 0 {
// consolidated_monitors is empty, marshal as "[]"
aux.ConsolidatedMonitors = &ConsolidatedMonitorsWrapper{IsNull: false, ConsolidatedMonitors: []*DtcPoolConsolidatedMonitorHealth{}}
} else {
// consolidated_monitors is non-empty, marshal as is
aux.ConsolidatedMonitors = &ConsolidatedMonitorsWrapper{IsNull: false, ConsolidatedMonitors: d.ConsolidatedMonitors}
}
}
return json.Marshal(aux)
}
func (d *DtcPool) UnmarshalJSON(data []byte) error {
type Alias DtcPool
aux := &struct {
Monitors []string `json:"monitors,omitempty"`
*Alias
}{
Alias: (*Alias)(d),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
// Convert Monitors from []string to []*DtcMonitorHttp
for _, ref := range aux.Monitors {
d.Monitors = append(d.Monitors, &DtcMonitorHttp{Ref: ref})
}
return nil
}
func NewEmptyDtcPool() *DtcPool {
poolDtc := &DtcPool{}
poolDtc.SetReturnFields(append(poolDtc.ReturnFields(), "lb_preferred_method", "servers", "lb_dynamic_ratio_preferred", "monitors", "auto_consolidated_monitors", "consolidated_monitors", "disable",
"extattrs", "health", "lb_alternate_method", "lb_alternate_topology", "lb_dynamic_ratio_alternate", "lb_preferred_topology", "quorum", "ttl", "use_ttl", "availability"))
return poolDtc
}
func NewDtcPool(comment string,
name string,
lbPreferredMethod string,
lbDynamicRatioPreferred *SettingDynamicratio,
servers []*DtcServerLink,
monitors []*DtcMonitorHttp,
lbPreferredTopology *string,
lbAlternateMethod string,
lbAlternateTopology *string,
lbDynamicRatioAlternate *SettingDynamicratio,
eas EA,
autoConsolidatedMonitors bool,
availability string,
consolidatedMonitors []*DtcPoolConsolidatedMonitorHealth,
ttl uint32,
useTTL bool,
disable bool,
quorum uint32) *DtcPool {
DtcPool := NewEmptyDtcPool()
DtcPool.Comment = &comment
DtcPool.Name = &name
DtcPool.LbPreferredMethod = lbPreferredMethod
DtcPool.Servers = servers
DtcPool.LbDynamicRatioPreferred = lbDynamicRatioPreferred
DtcPool.Monitors = monitors
DtcPool.LbPreferredTopology = lbPreferredTopology
DtcPool.LbAlternateMethod = lbAlternateMethod
DtcPool.LbAlternateTopology = lbAlternateTopology
DtcPool.LbDynamicRatioAlternate = lbDynamicRatioAlternate
DtcPool.Ea = eas
DtcPool.AutoConsolidatedMonitors = &autoConsolidatedMonitors
DtcPool.Availability = availability
DtcPool.ConsolidatedMonitors = consolidatedMonitors
DtcPool.Ttl = &ttl
DtcPool.UseTtl = &useTTL
DtcPool.Disable = &disable
DtcPool.Quorum = &quorum
return DtcPool
}
func (objMgr *ObjectManager) CreateDtcPool(
comment string,
name string,
lbPreferredMethod string,
lbDynamicRatioPreferred map[string]interface{},
servers []*DtcServerLink,
monitors []Monitor,
lbPreferredTopology *string,
lbAlternateMethod string,
lbAlternateTopology *string,
lbDynamicRatioAlternate map[string]interface{},
eas EA,
autoConsolidatedMonitors bool,
availability string,
ttl uint32,
useTTL bool,
disable bool,
quorum uint32) (*DtcPool, error) {
if name == "" || lbPreferredMethod == "" {
return nil, fmt.Errorf("name and preferred load balancing method must be provided to create a pool")
}
if lbPreferredMethod == "DYNAMIC_RATIO" && lbDynamicRatioPreferred == nil {
return nil, fmt.Errorf("LbDynamicRatioPreferred cannot be nil when the preferred load balancing method is set to DYNAMIC_RATIO")
}
if lbPreferredMethod == "TOPOLOGY" && lbPreferredTopology == nil {
return nil, fmt.Errorf("preferred topology cannot be nil when preferred load balancing method is set to TOPOLOGY")
}
//update servers with server references
err := updateServerReferences(servers, objMgr)
if err != nil {
return nil, err
}
// update the monitor in LbDynamicRatioPreferred with reference
var lbDynamicRatioPreferredMethod *SettingDynamicratio
if lbDynamicRatioPreferred != nil {
monitor, _ := lbDynamicRatioPreferred["monitor"].(Monitor)
method, _ := lbDynamicRatioPreferred["method"].(string)
monitorMetric, _ := lbDynamicRatioPreferred["monitor_metric"].(string)
monitorWeighing, _ := lbDynamicRatioPreferred["monitor_weighing"].(string)
invertMonitorMetric, _ := lbDynamicRatioPreferred["invert_monitor_metric"].(bool)
monitorRef, err := getMonitorReference(monitor.Name, monitor.Type, objMgr)
if err != nil {
return nil, err
}
lbDynamicRatioPreferredMethod = &SettingDynamicratio{
Method: method,
Monitor: monitorRef,
MonitorMetric: monitorMetric,
MonitorWeighing: monitorWeighing,
InvertMonitorMetric: invertMonitorMetric,
}
} else {
lbDynamicRatioPreferredMethod = nil
}
// Convert monitor names to monitor references
var monitorResults []*DtcMonitorHttp
for _, monitor := range monitors {
monitorRef, err := getMonitorReference(monitor.Name, monitor.Type, objMgr)
if err != nil {
return nil, err
}
monitorResults = append(monitorResults, &DtcMonitorHttp{Ref: monitorRef})
}
//Update the topology name with the topology reference
if lbPreferredTopology != nil {
topology, err := getTopology(*lbPreferredTopology, objMgr)
if err != nil {
return nil, err
}
lbPreferredTopology = &topology
}
//Update the topology name with the topology reference
if lbAlternateTopology != nil {
topologyAlternate, err := getTopology(*lbAlternateTopology, objMgr)
if err != nil {
return nil, err
}
lbAlternateTopology = &topologyAlternate
}
//update the monitor in LbDynamicRatioPreferred with reference
var lbDynamicRatioAlternateMethod *SettingDynamicratio
if lbDynamicRatioAlternate != nil {
monitorAlternate, _ := lbDynamicRatioAlternate["monitor"].(Monitor)
methodAlternate, _ := lbDynamicRatioAlternate["method"].(string)
monitorMetricAlternate, _ := lbDynamicRatioAlternate["monitor_metric"].(string)
monitorWeighingAlternate, _ := lbDynamicRatioAlternate["monitor_weighing"].(string)
interferometricAlternate, _ := lbDynamicRatioAlternate["invert_monitor_metric"].(bool)
monitorRefAlternate, err := getMonitorReference(monitorAlternate.Name, monitorAlternate.Type, objMgr)
if err != nil {
return nil, err
}
lbDynamicRatioAlternateMethod = &SettingDynamicratio{
Method: methodAlternate,
Monitor: monitorRefAlternate,
MonitorMetric: monitorMetricAlternate,
MonitorWeighing: monitorWeighingAlternate,
InvertMonitorMetric: interferometricAlternate,
}
} else {
lbDynamicRatioAlternateMethod = nil
}
// Create the DtcPool
poolDtc := NewDtcPool(comment, name, lbPreferredMethod, lbDynamicRatioPreferredMethod, servers, monitorResults, lbPreferredTopology, lbAlternateMethod, lbAlternateTopology, lbDynamicRatioAlternateMethod, eas, autoConsolidatedMonitors, availability, nil, ttl, useTTL, disable, quorum)
ref, err := objMgr.connector.CreateObject(poolDtc)
if err != nil {
return nil, err
}
poolDtc.Ref = ref
return poolDtc, nil
}
func (objMgr *ObjectManager) GetAllDtcPool(queryParams *QueryParams) ([]DtcPool, error) {
var res []DtcPool
pool := NewEmptyDtcPool()
err := objMgr.connector.GetObject(pool, "", queryParams, &res)
if err != nil {
return nil, fmt.Errorf("error getting Dtc Pool object, err: %s", err)
}
return res, nil
}
func (objMgr *ObjectManager) UpdateDtcPool(
ref string,
comment string,
name string,
lbPreferredMethod string,
lbDynamicRatioPreferred map[string]interface{},
servers []*DtcServerLink,
monitors []Monitor,
lbPreferredTopology *string,
lbAlternateMethod string,
lbAlternateTopology *string,
lbDynamicRatioAlternate map[string]interface{},
eas EA,
autoConsolidatedMonitors bool,
availability string,
userMonitors []map[string]interface{},
ttl uint32,
useTTL bool,
disable bool,
quorum uint32) (*DtcPool, error) {
if lbPreferredMethod == "DYNAMIC_RATIO" && lbDynamicRatioPreferred == nil {
return nil, fmt.Errorf("LbDynamicRatioPreferred cannot be nil when the preferred load balancing method is set to DYNAMIC_RATIO")
}
if lbPreferredMethod == "TOPOLOGY" && lbPreferredTopology == nil {
return nil, fmt.Errorf("preferred topology cannot be nil when preferred load balancing method is set to TOPOLOGY")
}
if autoConsolidatedMonitors && len(userMonitors) > 0 {
return nil, fmt.Errorf("either AutoConsolidatedMonitors or ConsolidatedMonitors should be set.")
}
//update servers with server references
err := updateServerReferences(servers, objMgr)
if err != nil {
return nil, err
}
// Convert LbDynamicRatioPreferred to use monitor reference
var lbDynamicRatioPreferredMethod *SettingDynamicratio
if lbDynamicRatioPreferred != nil {
monitor, _ := lbDynamicRatioPreferred["monitor"].(Monitor)
method, _ := lbDynamicRatioPreferred["method"].(string)
monitorMetric, _ := lbDynamicRatioPreferred["monitor_metric"].(string)
monitorWeighing, _ := lbDynamicRatioPreferred["monitor_weighing"].(string)
invertMonitorMetric, _ := lbDynamicRatioPreferred["invert_monitor_metric"].(bool)
monitorRef, err := getMonitorReference(monitor.Name, monitor.Type, objMgr)
if err != nil {
return nil, err
}
lbDynamicRatioPreferredMethod = &SettingDynamicratio{
Method: method,
Monitor: monitorRef,
MonitorMetric: monitorMetric,
MonitorWeighing: monitorWeighing,
InvertMonitorMetric: invertMonitorMetric,
}
} else {
lbDynamicRatioPreferredMethod = nil
}
// Convert monitor names to monitor references
var monitorResults []*DtcMonitorHttp
for _, monitor := range monitors {
monitorRef, err := getMonitorReference(monitor.Name, monitor.Type, objMgr)
if err != nil {
return nil, err
}
monitorResults = append(monitorResults, &DtcMonitorHttp{Ref: monitorRef})
}
//Update the topology name with the topology reference
if lbPreferredTopology != nil {
topology, err := getTopology(*lbPreferredTopology, objMgr)
if err != nil {
return nil, err
}
lbPreferredTopology = &topology
}
//Update the topology name with the topology reference
if lbAlternateTopology != nil {
topologyAlternate, err := getTopology(*lbAlternateTopology, objMgr)
if err != nil {
return nil, err
}
lbAlternateTopology = &topologyAlternate
}
//Convert LbDynamicRatioAlternate to use monitor reference
var lbDynamicRatioAlternateMethod *SettingDynamicratio
if lbDynamicRatioAlternate != nil {
monitorAlternate, _ := lbDynamicRatioAlternate["monitor"].(Monitor)
methodAlternate, _ := lbDynamicRatioAlternate["method"].(string)
monitorMetricAlternate, _ := lbDynamicRatioAlternate["monitor_metric"].(string)
monitorWeighingAlternate, _ := lbDynamicRatioAlternate["monitor_weighing"].(string)
invertMonitorMetricAlternate, _ := lbDynamicRatioAlternate["invert_monitor_metric"].(bool)
monitorRefAlternate, err := getMonitorReference(monitorAlternate.Name, monitorAlternate.Type, objMgr)
if err != nil {
return nil, err
}
lbDynamicRatioAlternateMethod = &SettingDynamicratio{
Method: methodAlternate,
Monitor: monitorRefAlternate,
MonitorMetric: monitorMetricAlternate,
MonitorWeighing: monitorWeighingAlternate,
InvertMonitorMetric: invertMonitorMetricAlternate,
}
} else {
lbDynamicRatioAlternateMethod = nil
}
//processing user input to retrieve monitor references and creating a slice of *DtcPoolConsolidatedMonitorHealth structs with updated monitor references.
var consolidatedMonitors []*DtcPoolConsolidatedMonitorHealth
if userMonitors != nil {
if len(userMonitors) == 0 {
consolidatedMonitors = []*DtcPoolConsolidatedMonitorHealth{}
} else {
for _, userMonitor := range userMonitors {
monitor, okMonitor := userMonitor["monitor"].(Monitor)
monitorAvailability, okAvail := userMonitor["availability"].(string)
fullHealthComm, _ := userMonitor["full_health_communication"].(bool)
members, okMember := userMonitor["members"].([]string)
if !okMonitor {
return nil, fmt.Errorf("required field missing: monitor")
}
if !okAvail {
return nil, fmt.Errorf("required field missing: availability")
}
if !okMember {
return nil, fmt.Errorf("required field missing: members")
}
monitorRef, err := getMonitorReference(monitor.Name, monitor.Type, objMgr)
if err != nil {
return nil, err
}
consolidatedMonitor := &DtcPoolConsolidatedMonitorHealth{
Members: members,
Monitor: monitorRef,
Availability: monitorAvailability,
FullHealthCommunication: fullHealthComm,
}
consolidatedMonitors = append(consolidatedMonitors, consolidatedMonitor)
}
}
}
poolDtc := NewDtcPool(comment, name, lbPreferredMethod, lbDynamicRatioPreferredMethod, servers, monitorResults, lbPreferredTopology, lbAlternateMethod, lbAlternateTopology, lbDynamicRatioAlternateMethod, eas, autoConsolidatedMonitors, availability, consolidatedMonitors, ttl, useTTL, disable, quorum)
poolDtc.Ref = ref
reference, err := objMgr.connector.UpdateObject(poolDtc, ref)
if err != nil {
return nil, err
}
poolDtc.Ref = reference
poolDtc, err = objMgr.GetDtcPoolByRef(reference)
if err != nil {
return nil, err
}
return poolDtc, nil
}
func (objMgr *ObjectManager) GetDtcPoolByRef(ref string) (*DtcPool, error) {
poolDtc := NewEmptyDtcPool()
err := objMgr.connector.GetObject(
poolDtc, ref, NewQueryParams(false, nil), &poolDtc)
return poolDtc, err
}
func (objMgr *ObjectManager) GetDtcPool(name string) (*DtcPool, error) {
dtcPool := NewEmptyDtcPool()
var res []DtcPool
if name == "" {
return nil, fmt.Errorf("name of the record is required to retrieve a unique Dtc Pool record")
}
sf := map[string]string{
"name": name,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(dtcPool, "", queryParams, &res)
if err != nil {
return nil, err
} else if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf("Dtc Pool record with name '%s' not found", name))
}
return &res[0], nil
}
func (objMgr *ObjectManager) DeleteDtcPool(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}