forked from infobloxopen/infoblox-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_manager.go
648 lines (587 loc) · 23.4 KB
/
object_manager.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package ibclient
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"strings"
)
// Compile-time interface checks
var _ IBObjectManager = new(ObjectManager)
type IBObjectManager interface {
GetDNSView(name string) (*View, error)
AllocateIP(netview string, cidr string, ipAddr string, isIPv6 bool, macOrDuid string, name string, comment string, eas EA) (*FixedAddress, error)
AllocateNetwork(netview string, cidr string, isIPv6 bool, prefixLen uint, comment string, eas EA) (network *Network, err error)
AllocateNetworkContainer(netview string, cidr string, isIPv6 bool, prefixLen uint, comment string, eas EA) (netContainer *NetworkContainer, err error)
CreateARecord(netView string, dnsView string, name string, cidr string, ipAddr string, ttl uint32, useTTL bool, comment string, ea EA) (*RecordA, error)
CreateAAAARecord(netView string, dnsView string, recordName string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordAAAA, error)
CreateZoneAuth(fqdn string, ea EA) (*ZoneAuth, error)
CreateCNAMERecord(dnsview string, canonical string, recordname string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordCNAME, error)
CreateDefaultNetviews(globalNetview string, localNetview string) (globalNetviewRef string, localNetviewRef string, err error)
CreateEADefinition(eadef EADefinition) (*EADefinition, error)
CreateHostRecord(enabledns bool, enabledhcp bool, recordName string, netview string, dnsview string, ipv4cidr string, ipv6cidr string, ipv4Addr string, ipv6Addr string, macAddr string, duid string, useTtl bool, ttl uint32, comment string, eas EA, aliases []string) (*HostRecord, error)
CreateMXRecord(dnsView string, fqdn string, mx string, preference uint32, ttl uint32, useTtl bool, comment string, eas EA) (*RecordMX, error)
CreateNetwork(netview string, cidr string, isIPv6 bool, comment string, eas EA) (*Network, error)
CreateNetworkContainer(netview string, cidr string, isIPv6 bool, comment string, eas EA) (*NetworkContainer, error)
CreateNetworkView(name string, comment string, setEas EA) (*NetworkView, error)
CreatePTRRecord(networkView string, dnsView string, ptrdname string, recordName string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordPTR, error)
CreateSRVRecord(dnsView string, name string, priority uint32, weight uint32, port uint32, target string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordSRV, error)
CreateTXTRecord(dnsView string, recordName string, text string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordTXT, error)
CreateZoneDelegated(fqdn string, delegate_to []NameServer) (*ZoneDelegated, error)
DeleteARecord(ref string) (string, error)
DeleteAAAARecord(ref string) (string, error)
DeleteZoneAuth(ref string) (string, error)
DeleteCNAMERecord(ref string) (string, error)
DeleteFixedAddress(ref string) (string, error)
DeleteHostRecord(ref string) (string, error)
DeleteMXRecord(ref string) (string, error)
DeleteNetwork(ref string) (string, error)
DeleteNetworkContainer(ref string) (string, error)
DeleteNetworkView(ref string) (string, error)
DeletePTRRecord(ref string) (string, error)
DeleteSRVRecord(ref string) (string, error)
DeleteTXTRecord(ref string) (string, error)
DeleteZoneDelegated(ref string) (string, error)
GetARecordByRef(ref string) (*RecordA, error)
GetARecord(dnsview string, recordName string, ipAddr string) (*RecordA, error)
GetAAAARecord(dnsview string, recordName string, ipAddr string) (*RecordAAAA, error)
GetAAAARecordByRef(ref string) (*RecordAAAA, error)
GetCNAMERecord(dnsview string, canonical string, recordName string) (*RecordCNAME, error)
GetCNAMERecordByRef(ref string) (*RecordCNAME, error)
GetEADefinition(name string) (*EADefinition, error)
GetFixedAddress(netview string, cidr string, ipAddr string, isIPv6 bool, macOrDuid string) (*FixedAddress, error)
GetFixedAddressByRef(ref string) (*FixedAddress, error)
GetHostRecord(netview string, dnsview string, recordName string, ipv4addr string, ipv6addr string) (*HostRecord, error)
SearchHostRecordByAltId(internalId string, ref string, eaNameForInternalId string) (*HostRecord, error)
GetHostRecordByRef(ref string) (*HostRecord, error)
GetIpAddressFromHostRecord(host HostRecord) (string, error)
GetMXRecord(dnsView string, fqdn string, mx string, preference uint32) (*RecordMX, error)
GetMXRecordByRef(ref string) (*RecordMX, error)
GetNetwork(netview string, cidr string, isIPv6 bool, ea EA) (*Network, error)
GetNetworkByRef(ref string) (*Network, error)
GetNetworkContainer(netview string, cidr string, isIPv6 bool, eaSearch EA) (*NetworkContainer, error)
GetNetworkContainerByRef(ref string) (*NetworkContainer, error)
GetNetworkView(name string) (*NetworkView, error)
GetNetworkViewByRef(ref string) (*NetworkView, error)
GetPTRRecord(dnsview string, ptrdname string, recordName string, ipAddr string) (*RecordPTR, error)
GetPTRRecordByRef(ref string) (*RecordPTR, error)
GetSRVRecord(dnsView string, name string, target string, port uint32) (*RecordSRV, error)
GetSRVRecordByRef(ref string) (*RecordSRV, error)
GetTXTRecord(dnsview string, name string) (*RecordTXT, error)
GetTXTRecordByRef(ref string) (*RecordTXT, error)
GetZoneAuthByRef(ref string) (*ZoneAuth, error)
GetZoneDelegated(fqdn string) (*ZoneDelegated, error)
GetCapacityReport(name string) ([]CapacityReport, error)
GetUpgradeStatus(statusType string) ([]UpgradeStatus, error)
GetAllMembers() ([]Member, error)
GetGridInfo() ([]Grid, error)
GetGridLicense() ([]License, error)
SearchObjectByAltId(objType string, internalId string, ref string, eaNameForInternalId string) (interface{}, error)
ReleaseIP(netview string, cidr string, ipAddr string, isIPv6 bool, macAddr string) (string, error)
UpdateAAAARecord(ref string, netView string, recordName string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, setEas EA) (*RecordAAAA, error)
UpdateCNAMERecord(ref string, canonical string, recordName string, useTtl bool, ttl uint32, comment string, setEas EA) (*RecordCNAME, error)
UpdateFixedAddress(fixedAddrRef string, netview string, name string, cidr string, ipAddr string, matchclient string, macOrDuid string, comment string, eas EA) (*FixedAddress, error)
UpdateHostRecord(hostRref string, enabledns bool, enabledhcp bool, name string, netview string, dnsView string, ipv4cidr string, ipv6cidr string, ipv4Addr string, ipv6Addr string, macAddress string, duid string, useTtl bool, ttl uint32, comment string, eas EA, aliases []string) (*HostRecord, error)
UpdateMXRecord(ref string, dnsView string, fqdn string, mx string, preference uint32, ttl uint32, useTtl bool, comment string, eas EA) (*RecordMX, error)
UpdateNetwork(ref string, setEas EA, comment string) (*Network, error)
UpdateNetworkContainer(ref string, setEas EA, comment string) (*NetworkContainer, error)
UpdateNetworkView(ref string, name string, comment string, setEas EA) (*NetworkView, error)
UpdatePTRRecord(ref string, netview string, ptrdname string, name string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, setEas EA) (*RecordPTR, error)
UpdateSRVRecord(ref string, name string, priority uint32, weight uint32, port uint32, target string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordSRV, error)
UpdateTXTRecord(ref string, recordName string, text string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordTXT, error)
UpdateARecord(ref string, name string, ipAddr string, cidr string, netview string, ttl uint32, useTTL bool, comment string, eas EA) (*RecordA, error)
UpdateZoneDelegated(ref string, delegate_to []NameServer) (*ZoneDelegated, error)
GetDnsMember(ref string) ([]Dns, error)
UpdateDnsStatus(ref string, status bool) (Dns, error)
GetDhcpMember(ref string) ([]Dhcp, error)
UpdateDhcpStatus(ref string, status bool) (Dhcp, error)
}
const (
ARecord = "A"
AaaaRecord = "AAAA"
CnameRecord = "CNAME"
MxRecord = "MX"
SrvRecord = "SRV"
TxtRecord = "TXT"
PtrRecord = "PTR"
HostRecordConst = "Host"
DnsViewConst = "DNSView"
ZoneAuthConst = "ZoneAuth"
NetworkViewConst = "NetworkView"
NetworkConst = "Network"
NetworkContainerConst = "NetworkContainer"
)
// Map of record type to its corresponding object
var getRecordTypeMap = map[string]func(ref string) IBObject{
ARecord: func(ref string) IBObject {
return NewEmptyRecordA()
},
AaaaRecord: func(ref string) IBObject {
return NewEmptyRecordAAAA()
},
CnameRecord: func(ref string) IBObject {
return NewEmptyRecordCNAME()
},
MxRecord: func(ref string) IBObject {
return NewEmptyRecordMX()
},
SrvRecord: func(ref string) IBObject {
return NewEmptyRecordSRV()
},
TxtRecord: func(ref string) IBObject {
return NewEmptyRecordTXT()
},
PtrRecord: func(ref string) IBObject {
return NewEmptyRecordPTR()
},
HostRecordConst: func(ref string) IBObject {
return NewEmptyHostRecord()
},
DnsViewConst: func(ref string) IBObject {
return NewEmptyDNSView()
},
ZoneAuthConst: func(ref string) IBObject {
zone := &ZoneAuth{}
zone.SetReturnFields(append(
zone.ReturnFields(),
"comment",
"ns_group",
"soa_default_ttl",
"soa_expire",
"soa_negative_ttl",
"soa_refresh",
"soa_retry",
"view",
"zone_format",
"extattrs",
))
return zone
},
NetworkViewConst: func(ref string) IBObject {
return NewEmptyNetworkView()
},
NetworkContainerConst: func(ref string) IBObject {
return NewNetworkContainer("", "", false, "", nil)
},
NetworkConst: func(ref string) IBObject {
r := regexp.MustCompile("^ipv6network\\/.+")
isIPv6 := r.MatchString(ref)
return NewNetwork("", "", isIPv6, "", nil)
},
}
// Map returns the object with search fields with the given record type
var getObjectWithSearchFieldsMap = map[string]func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error){
ARecord: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*RecordA).Ref != "" {
return res, nil
}
var recordAList []*RecordA
err := objMgr.connector.GetObject(NewEmptyRecordA(), "", NewQueryParams(false, sf), &recordAList)
if err == nil && len(recordAList) > 0 {
res = recordAList[0]
}
return res, err
},
AaaaRecord: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*RecordAAAA).Ref != "" {
return res, nil
}
var recordAaaList []*RecordAAAA
err := objMgr.connector.GetObject(NewEmptyRecordAAAA(), "", NewQueryParams(false, sf), &recordAaaList)
if err == nil && len(recordAaaList) > 0 {
res = recordAaaList[0]
}
return res, err
},
CnameRecord: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*RecordCNAME).Ref != "" {
return res, nil
}
var cNameList []*RecordCNAME
err := objMgr.connector.GetObject(NewEmptyRecordCNAME(), "", NewQueryParams(false, sf), &cNameList)
if err == nil && len(cNameList) > 0 {
res = cNameList[0]
}
return res, err
},
MxRecord: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*RecordMX).Ref != "" {
return res, nil
}
var mxList []*RecordMX
err := objMgr.connector.GetObject(NewEmptyRecordMX(), "", NewQueryParams(false, sf), &mxList)
if err == nil && len(mxList) > 0 {
res = mxList[0]
}
return res, err
},
SrvRecord: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*RecordSRV).Ref != "" {
return res, nil
}
var srvList []*RecordSRV
err := objMgr.connector.GetObject(NewEmptyRecordSRV(), "", NewQueryParams(false, sf), &srvList)
if err == nil && len(srvList) > 0 {
res = srvList[0]
}
return res, err
},
TxtRecord: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*RecordTXT).Ref != "" {
return res, nil
}
var txtList []*RecordTXT
err := objMgr.connector.GetObject(NewEmptyRecordTXT(), "", NewQueryParams(false, sf), &txtList)
if err == nil && len(txtList) > 0 {
res = txtList[0]
}
return res, err
},
PtrRecord: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*RecordPTR).Ref != "" {
return res, nil
}
var ptrList []*RecordPTR
err := objMgr.connector.GetObject(NewEmptyRecordPTR(), "", NewQueryParams(false, sf), &ptrList)
if err == nil && len(ptrList) > 0 {
res = ptrList[0]
}
return res, err
},
HostRecordConst: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*HostRecord).Ref != "" {
return res, nil
}
var hostRecordList []*HostRecord
err := objMgr.connector.GetObject(NewEmptyHostRecord(), "", NewQueryParams(false, sf), &hostRecordList)
if err == nil && len(hostRecordList) > 0 {
res = hostRecordList[0]
}
return res, err
},
DnsViewConst: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*View).Ref != "" {
return res, nil
}
var dnsViewList []*View
err := objMgr.connector.GetObject(NewEmptyDNSView(), "", NewQueryParams(false, sf), &dnsViewList)
if err == nil && len(dnsViewList) > 0 {
res = dnsViewList[0]
}
return res, err
},
ZoneAuthConst: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*ZoneAuth).Ref != "" {
return res, nil
}
zoneAuth := recordType.(*ZoneAuth)
var zoneAuthList []*ZoneAuth
err := objMgr.connector.GetObject(zoneAuth, "", NewQueryParams(false, sf), &zoneAuthList)
if err == nil && len(zoneAuthList) > 0 {
res = zoneAuthList[0]
}
return res, err
},
NetworkViewConst: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*NetworkView).Ref != "" {
return res, nil
}
var networkViewList []*NetworkView
err := objMgr.connector.GetObject(NewEmptyNetworkView(), "", NewQueryParams(false, sf), &networkViewList)
if err == nil && len(networkViewList) > 0 {
res = networkViewList[0]
}
return res, err
},
// TODO: Do we need to add netview string, cidr string, isIPv6 bool, ea EA to create network container
NetworkContainerConst: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*NetworkContainer).Ref != "" {
return res, nil
}
var networkContainerList []*NetworkContainer
err := objMgr.connector.GetObject(NewNetworkContainer("", "", false, "", nil), "", NewQueryParams(false, sf), &networkContainerList)
if err == nil && len(networkContainerList) > 0 {
res = networkContainerList[0]
}
return res, err
},
//TODO: Do we need to add netview string, cidr string, isIPv6 bool, ea EA to create network
NetworkConst: func(recordType IBObject, objMgr *ObjectManager, sf map[string]string) (interface{}, error) {
var res interface{}
if recordType.(*Network).Ref != "" {
return res, nil
}
var networkList []*Network
err := objMgr.connector.GetObject(NewNetwork("", "", false, "", nil), "", NewQueryParams(false, sf), &networkList)
if err == nil && len(networkList) > 0 {
res = networkList[0]
}
return res, err
},
}
type ObjectManager struct {
connector IBConnector
cmpType string
tenantID string
}
func NewObjectManager(connector IBConnector, cmpType string, tenantID string) IBObjectManager {
objMgr := &ObjectManager{}
objMgr.connector = connector
objMgr.cmpType = cmpType
objMgr.tenantID = tenantID
return objMgr
}
// CreateMultiObject unmarshals the result into slice of maps
func (objMgr *ObjectManager) CreateMultiObject(req *MultiRequest) ([]map[string]interface{}, error) {
conn := objMgr.connector.(*Connector)
queryParams := NewQueryParams(false, nil)
res, err := conn.makeRequest(CREATE, req, "", queryParams)
if err != nil {
return nil, err
}
var result []map[string]interface{}
err = json.Unmarshal(res, &result)
if err != nil {
return nil, err
}
return result, nil
}
// GetUpgradeStatus returns the grid upgrade information
func (objMgr *ObjectManager) GetUpgradeStatus(statusType string) ([]UpgradeStatus, error) {
var res []UpgradeStatus
if statusType == "" {
// TODO option may vary according to the WAPI version, need to
// throw relevant error.
msg := fmt.Sprintf("Status type can not be nil")
return res, errors.New(msg)
}
upgradestatus := NewUpgradeStatus(UpgradeStatus{})
sf := map[string]string{
"type": statusType,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(upgradestatus, "", queryParams, &res)
return res, err
}
// GetAllMembers returns all members information
func (objMgr *ObjectManager) GetAllMembers() ([]Member, error) {
var res []Member
memberObj := NewMember(Member{})
err := objMgr.connector.GetObject(
memberObj, "", NewQueryParams(false, nil), &res,
)
return res, err
}
// GetCapacityReport returns all capacity for members
func (objMgr *ObjectManager) GetCapacityReport(name string) ([]CapacityReport, error) {
var res []CapacityReport
capacityReport := NewCapcityReport(CapacityReport{})
sf := map[string]string{
"name": name,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(capacityReport, "", queryParams, &res)
return res, err
}
// GetLicense returns the license details for member
func (objMgr *ObjectManager) GetLicense() ([]License, error) {
var res []License
licenseObj := NewLicense(License{})
err := objMgr.connector.GetObject(
licenseObj, "", NewQueryParams(false, nil), &res,
)
return res, err
}
// GetLicense returns the license details for grid
func (objMgr *ObjectManager) GetGridLicense() ([]License, error) {
var res []License
licenseObj := NewGridLicense(License{})
err := objMgr.connector.GetObject(
licenseObj, "", NewQueryParams(false, nil), &res,
)
return res, err
}
// GetGridInfo returns the details for grid
func (objMgr *ObjectManager) GetGridInfo() ([]Grid, error) {
var res []Grid
gridObj := NewGrid(Grid{})
err := objMgr.connector.GetObject(
gridObj, "", NewQueryParams(false, nil), &res,
)
return res, err
}
// CreateZoneAuth creates zones and subs by passing fqdn
func (objMgr *ObjectManager) CreateZoneAuth(
fqdn string,
eas EA) (*ZoneAuth, error) {
zoneAuth := NewZoneAuth(
ZoneAuth{
Fqdn: fqdn,
Ea: eas},
)
ref, err := objMgr.connector.CreateObject(zoneAuth)
zoneAuth.Ref = ref
return zoneAuth, err
}
// Retreive a authortative zone by ref
func (objMgr *ObjectManager) GetZoneAuthByRef(ref string) (*ZoneAuth, error) {
res := NewZoneAuth(ZoneAuth{})
if ref == "" {
return nil, fmt.Errorf("empty reference to an object is not allowed")
}
err := objMgr.connector.GetObject(
res, ref, NewQueryParams(false, nil), res,
)
return res, err
}
// DeleteZoneAuth deletes an auth zone
func (objMgr *ObjectManager) DeleteZoneAuth(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}
// GetZoneAuth returns the authoritatives zones
func (objMgr *ObjectManager) GetZoneAuth() ([]ZoneAuth, error) {
var res []ZoneAuth
zoneAuth := NewZoneAuth(ZoneAuth{})
err := objMgr.connector.GetObject(
zoneAuth, "", NewQueryParams(false, nil), &res,
)
return res, err
}
// GetZoneDelegated returns the delegated zone
func (objMgr *ObjectManager) GetZoneDelegated(fqdn string) (*ZoneDelegated, error) {
if len(fqdn) == 0 {
return nil, nil
}
var res []ZoneDelegated
zoneDelegated := NewZoneDelegated(ZoneDelegated{})
sf := map[string]string{
"fqdn": fqdn,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(zoneDelegated, "", queryParams, &res)
if err != nil || res == nil || len(res) == 0 {
return nil, err
}
return &res[0], nil
}
// CreateZoneDelegated creates delegated zone
func (objMgr *ObjectManager) CreateZoneDelegated(fqdn string, delegate_to []NameServer) (*ZoneDelegated, error) {
zoneDelegated := NewZoneDelegated(
ZoneDelegated{
Fqdn: fqdn,
DelegateTo: delegate_to},
)
ref, err := objMgr.connector.CreateObject(zoneDelegated)
zoneDelegated.Ref = ref
return zoneDelegated, err
}
// UpdateZoneDelegated updates delegated zone
func (objMgr *ObjectManager) UpdateZoneDelegated(ref string, delegate_to []NameServer) (*ZoneDelegated, error) {
zoneDelegated := NewZoneDelegated(
ZoneDelegated{
Ref: ref,
DelegateTo: delegate_to},
)
refResp, err := objMgr.connector.UpdateObject(zoneDelegated, ref)
zoneDelegated.Ref = refResp
return zoneDelegated, err
}
// DeleteZoneDelegated deletes delegated zone
func (objMgr *ObjectManager) DeleteZoneDelegated(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}
// SearchObjectByAltId is a generic function to search object by alternate id
func (objMgr *ObjectManager) SearchObjectByAltId(
objType string, ref string, internalId string, eaNameForInternalId string) (interface{}, error) {
var (
err error
recordType IBObject
res interface{}
)
val, ok := getRecordTypeMap[objType]
if !ok {
return nil, fmt.Errorf("unknown record type")
}
recordType = val(ref)
if ref != "" {
// Fetching object by reference
if err := objMgr.connector.GetObject(recordType, ref, NewQueryParams(false, nil), &res); err != nil {
if _, ok := err.(*NotFoundError); !ok {
return nil, err
}
}
success, err := validateObjByInternalId(res, internalId, eaNameForInternalId)
if err != nil {
return nil, err
} else if success {
return res, nil
}
}
sf := map[string]string{
fmt.Sprintf("*%s", eaNameForInternalId): internalId,
}
// Fetch the object by search fields
getObjectWithSearchFields, ok := getObjectWithSearchFieldsMap[objType]
if !ok {
return nil, fmt.Errorf("unknown record type")
}
res, err = getObjectWithSearchFields(recordType, objMgr, sf)
if err != nil {
return nil, err
}
if res == nil {
return nil, NewNotFoundError("record not found")
}
return &res, nil
}
// validateObjByInternalId validates the object by comparing the given internal with the object's internal id
func validateObjByInternalId(res interface{}, internalId, eaNameForInternalId string) (bool, error) {
var success bool
if res == nil {
return success, nil
} else if strings.TrimSpace(internalId) == "" {
// return object if internal id is empty
success = true
return success, nil
}
byteObj, err := json.Marshal(res)
if err != nil {
return success, fmt.Errorf("error marshaling JSON: %v", err)
}
obj := make(map[string]interface{})
err = json.Unmarshal(byteObj, &obj)
if err != nil {
return success, fmt.Errorf("error unmarshaling JSON: %v", err)
}
extAttrs, err := getInterfaceValueFromMap(obj, "extattrs")
if err == nil {
resInternalId, err := getInterfaceValueFromMap(extAttrs, eaNameForInternalId)
if err == nil && resInternalId["value"] != nil && resInternalId["value"].(string) == internalId {
// return object if object's internal id matches with the given internal id
success = true
return success, nil
}
return success, err
}
return success, err
}
// getInterfaceValueFromMap returns the value, after converting it into a map[string]interface{}, of the given key from the map
func getInterfaceValueFromMap(m map[string]interface{}, key string) (map[string]interface{}, error) {
if val, ok := m[key]; ok && val != nil {
res := val.(map[string]interface{})
return res, nil
}
return nil, fmt.Errorf("key %s not found in map", key)
}