-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathobject_manager_dtc_lbdn.go
298 lines (263 loc) · 8.29 KB
/
object_manager_dtc_lbdn.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
package ibclient
import (
"encoding/json"
"fmt"
"regexp"
)
func (d *DtcLbdn) MarshalJSON() ([]byte, error) {
type Alias DtcLbdn
aux := &struct {
AuthZones []string `json:"auth_zones"`
Pools []*DtcPoolLink `json:"pools"`
Patterns []string `json:"patterns"`
*Alias
}{
Alias: (*Alias)(d),
}
// Convert AuthZones to a slice of strings
for _, zone := range d.AuthZones {
if zone != nil && zone.Ref != "" {
aux.AuthZones = append(aux.AuthZones, zone.Ref)
}
}
// Convert Pools to a slice of DtcPoolLink
for _, pool := range d.Pools {
if pool != nil {
aux.Pools = append(aux.Pools, pool)
}
}
// Convert Patterns to a slice of strings
for _, pattern := range d.Patterns {
aux.Patterns = append(aux.Patterns, pattern)
}
// Ensure AuthZones, Pools, and Types are set to empty slices if nil
if aux.AuthZones == nil {
aux.AuthZones = make([]string, 0)
}
if aux.Pools == nil {
aux.Pools = make([]*DtcPoolLink, 0)
}
if aux.Patterns == nil {
aux.Patterns = make([]string, 0)
}
return json.Marshal(aux)
}
func (d *DtcLbdn) UnmarshalJSON(data []byte) error {
type Alias DtcLbdn
aux := &struct {
*Alias
AuthZones []string `json:"auth_zones"`
}{
Alias: (*Alias)(d),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
d.AuthZones = make([]*ZoneAuth, len(aux.AuthZones))
for i, ref := range aux.AuthZones {
d.AuthZones[i] = &ZoneAuth{Ref: ref}
}
return nil
}
func (objMgr *ObjectManager) CreateDtcLbdn(name string, authZones []string, comment string, disable bool, autoConsolidatedMonitors bool, ea EA,
lbMethod string, patterns []string, persistence uint32, pools []*DtcPoolLink, priority uint32, topology string, types []string, ttl uint32, usettl bool) (*DtcLbdn, error) {
if name == "" || lbMethod == "" {
return nil, fmt.Errorf("name and lbMethod fields are required to create a Dtc Lbdn object")
}
// get ref id of authzones and replace
var zones []*ZoneAuth
var err error
if len(authZones) > 0 {
zones, err = getAuthZones(authZones, objMgr)
if err != nil {
return nil, err
}
}
// get ref id of pools and replace
var dtcPoolLink []*DtcPoolLink
if len(pools) > 0 {
dtcPoolLink, err = getPools(pools, objMgr)
if err != nil {
return nil, err
}
}
//get ref id of topology and replace
var topologyRef string
if lbMethod == "TOPOLOGY" {
topologyRef, err = getTopology(topology, objMgr)
if err != nil {
return nil, err
}
}
dtcLbdn := NewDtcLbdn("", name, zones, comment, disable, autoConsolidatedMonitors, ea,
lbMethod, patterns, persistence, dtcPoolLink, priority, topologyRef, types, ttl, usettl)
ref, err := objMgr.connector.CreateObject(dtcLbdn)
if err != nil {
return nil, fmt.Errorf("error creating Dtc Lbdn object %s, err: %s", name, err)
}
dtcLbdn.Ref = ref
return dtcLbdn, nil
}
func getTopology(topology string, objMgr *ObjectManager) (string, error) {
var dtcTopology []DtcTopology
var topologyRef string
if topology == "" {
return "", fmt.Errorf("topology field is required when lbMethod is TOPOLOGY")
}
isRef := regexp.MustCompile("^dtc:topology:*")
if !isRef.MatchString(topology) {
sf := map[string]string{
"name": topology,
}
err := objMgr.connector.GetObject(&DtcTopology{}, "", NewQueryParams(false, sf), &dtcTopology)
if err != nil {
return "", fmt.Errorf("error getting %s DtcTopology object: %s", topology, err)
}
if len(dtcTopology) > 0 {
topologyRef = dtcTopology[0].Ref
}
}
return topologyRef, nil
}
func getPools(pools []*DtcPoolLink, objMgr *ObjectManager) ([]*DtcPoolLink, error) {
var dtcPoolLink []*DtcPoolLink
for _, pool := range pools {
sf := map[string]string{"name": pool.Pool}
var dtcPools []DtcPool
isRef := regexp.MustCompile("^dtc:pool:*")
if !isRef.MatchString(pool.Pool) {
err := objMgr.connector.GetObject(&DtcPool{}, "", NewQueryParams(false, sf), &dtcPools)
if err != nil {
return nil, fmt.Errorf("error getting %s DtcPool object: %s", pool.Pool, err)
}
if len(dtcPools) > 0 {
dtcPoolLink = append(dtcPoolLink, &DtcPoolLink{Pool: dtcPools[0].Ref, Ratio: pool.Ratio})
}
}
}
return dtcPoolLink, nil
}
func getAuthZones(authZones []string, objMgr *ObjectManager) ([]*ZoneAuth, error) {
var zoneAuth []ZoneAuth
var zones []*ZoneAuth
for i := 0; i < len(authZones); i++ {
sf := map[string]string{
"fqdn": authZones[i],
}
err := objMgr.connector.GetObject(&ZoneAuth{}, "", NewQueryParams(false, sf), &zoneAuth)
if err != nil {
return nil, fmt.Errorf("error getting %s ZoneAuth object: %s", authZones[i], err)
}
if len(zoneAuth) > 0 {
zones = append(zones, &zoneAuth[0])
}
}
return zones, nil
}
func NewDtcLbdn(ref string, name string, authZones []*ZoneAuth, comment string, disable bool, autoConsolidatedMonitors bool, ea EA,
lbMethod string, patterns []string, persistence uint32, pools []*DtcPoolLink, priority uint32, topology string, types []string, ttl uint32, usettl bool) *DtcLbdn {
lbdn := NewEmptyDtcLbdn()
lbdn.Name = &name
lbdn.Ref = ref
lbdn.AuthZones = authZones
lbdn.Comment = &comment
lbdn.Disable = &disable
lbdn.AutoConsolidatedMonitors = &autoConsolidatedMonitors
lbdn.Ea = ea
lbdn.LbMethod = lbMethod
lbdn.Patterns = patterns
lbdn.Persistence = &persistence
lbdn.Pools = pools
if topology != "" {
lbdn.Topology = &topology
}
lbdn.Priority = &priority
lbdn.Types = types
lbdn.Ttl = &ttl
lbdn.UseTtl = &usettl
return lbdn
}
func NewEmptyDtcLbdn() *DtcLbdn {
dtcLbdn := &DtcLbdn{}
dtcLbdn.SetReturnFields(append(dtcLbdn.ReturnFields(), "extattrs", "disable", "auth_zones", "auto_consolidated_monitors", "lb_method", "patterns", "persistence", "pools", "priority", "topology", "types", "health", "ttl", "use_ttl"))
return dtcLbdn
}
func (objMgr *ObjectManager) DeleteDtcLbdn(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}
func (objMgr *ObjectManager) GetAllDtcLbdn(queryParams *QueryParams) ([]DtcLbdn, error) {
var res []DtcLbdn
lbdn := NewEmptyDtcLbdn()
err := objMgr.connector.GetObject(lbdn, "", queryParams, &res)
if err != nil {
return nil, fmt.Errorf("error getting Dtc Lbdn object, err: %s", err)
}
return res, nil
}
func (objMgr *ObjectManager) GetDtcLbdn(name string) (*DtcLbdn, error) {
dtcLbdn := NewEmptyDtcLbdn()
var res []DtcLbdn
if name == "" {
return nil, fmt.Errorf("name of the record is required to retrieve a unique Dtc Lbdn record")
}
sf := map[string]string{
"name": name,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(dtcLbdn, "", queryParams, &res)
if err != nil {
return nil, err
} else if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf("Dtc Lbdn record with name '%s' not found", name))
}
return &res[0], nil
}
func (objMgr *ObjectManager) GetDtcLbdnByRef(ref string) (*DtcLbdn, error) {
dtcLbdn := NewEmptyDtcLbdn()
err := objMgr.connector.GetObject(dtcLbdn, ref, NewQueryParams(false, nil), &dtcLbdn)
if err != nil {
return nil, err
}
return dtcLbdn, nil
}
func (objMgr *ObjectManager) UpdateDtcLbdn(ref string, name string, authZones []string, comment string, disable bool, autoConsolidatedMonitors bool, ea EA,
lbMethod string, patterns []string, persistence uint32, pools []*DtcPoolLink, priority uint32, topology string, types []string, ttl uint32, usettl bool) (*DtcLbdn, error) {
// get ref id of authzones and replace
var zones []*ZoneAuth
var err error
if len(authZones) > 0 {
zones, err = getAuthZones(authZones, objMgr)
if err != nil {
return nil, err
}
}
// get ref id of pools and replace
var dtcPoolLink []*DtcPoolLink
if len(pools) > 0 {
dtcPoolLink, err = getPools(pools, objMgr)
if err != nil {
return nil, err
}
}
//get ref id of topology and replace
var topologyRef string
if lbMethod == "TOPOLOGY" {
topologyRef, err = getTopology(topology, objMgr)
if err != nil {
return nil, err
}
}
dtcLbdn := NewDtcLbdn(ref, name, zones, comment, disable, autoConsolidatedMonitors, ea,
lbMethod, patterns, persistence, dtcPoolLink, priority, topologyRef, types, ttl, usettl)
newRef, err := objMgr.connector.UpdateObject(dtcLbdn, ref)
if err != nil {
return nil, fmt.Errorf("error updating Dtc Lbdn object %s, err: %s", name, err)
}
dtcLbdn.Ref = newRef
dtcLbdn, err = objMgr.GetDtcLbdnByRef(newRef)
if err != nil {
return nil, fmt.Errorf("error getting updated Dtc Lbdn object %s, err: %s", name, err)
}
return dtcLbdn, nil
}