-
Notifications
You must be signed in to change notification settings - Fork 88
/
objects.go
654 lines (555 loc) · 17.5 KB
/
objects.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
649
650
651
652
653
654
package ibclient
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"time"
)
const MACADDR_ZERO = "00:00:00:00:00:00"
type Bool bool
func (b Bool) MarshalJSON() ([]byte, error) {
if b {
return json.Marshal("True")
}
return json.Marshal("False")
}
type EA map[string]interface{}
func (ea EA) Count() int {
return len(ea)
}
func (ea EA) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
for k, v := range ea {
value := make(map[string]interface{})
value["value"] = v
m[k] = value
}
return json.Marshal(m)
}
func (ea *EA) UnmarshalJSON(b []byte) (err error) {
var m map[string]map[string]interface{}
decoder := json.NewDecoder(bytes.NewBuffer(b))
decoder.UseNumber()
err = decoder.Decode(&m)
if err != nil {
return
}
*ea = make(EA)
for k, v := range m {
val := v["value"]
switch valType := reflect.TypeOf(val).String(); valType {
case "json.Number":
var i64 int64
i64, err = val.(json.Number).Int64()
val = int(i64)
case "string":
if val.(string) == "True" {
val = Bool(true)
} else if val.(string) == "False" {
val = Bool(false)
}
case "[]interface {}":
nval := val.([]interface{})
nVals := make([]string, len(nval))
for i, v := range nval {
nVals[i] = fmt.Sprintf("%v", v)
}
val = nVals
default:
val = fmt.Sprintf("%v", val)
}
(*ea)[k] = val
}
return
}
type EASearch map[string]interface{}
func (eas EASearch) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
for k, v := range eas {
m["*"+k] = v
}
return json.Marshal(m)
}
type IBBase struct {
returnFields []string
eaSearch EASearch
}
type IBObject interface {
ObjectType() string
ReturnFields() []string
EaSearch() EASearch
SetReturnFields([]string)
}
func (obj *IBBase) ReturnFields() []string {
return obj.returnFields
}
func (obj *IBBase) SetReturnFields(rf []string) {
obj.returnFields = rf
}
func (obj *IBBase) EaSearch() EASearch {
return obj.eaSearch
}
// QueryParams is a general struct to add query params used in makeRequest
type QueryParams struct {
forceProxy bool
searchFields map[string]string
}
func NewQueryParams(forceProxy bool, searchFields map[string]string) *QueryParams {
qp := QueryParams{forceProxy: forceProxy}
if searchFields != nil {
qp.searchFields = searchFields
} else {
qp.searchFields = make(map[string]string)
}
return &qp
}
type RequestBody struct {
Data map[string]interface{} `json:"data,omitempty"`
Args map[string]string `json:"args,omitempty"`
Method string `json:"method"`
Object string `json:"object,omitempty"`
EnableSubstitution bool `json:"enable_substitution,omitempty"`
AssignState map[string]string `json:"assign_state,omitempty"`
Discard bool `json:"discard,omitempty"`
}
type SingleRequest struct {
IBBase `json:"-"`
Body *RequestBody
}
type MultiRequest struct {
IBBase `json:"-"`
Body []*RequestBody
}
func (r *MultiRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(r.Body)
}
func NewMultiRequest(body []*RequestBody) *MultiRequest {
req := &MultiRequest{Body: body}
return req
}
func (MultiRequest) ObjectType() string {
return "request"
}
func NewRequest(body *RequestBody) *SingleRequest {
req := &SingleRequest{Body: body}
return req
}
func (SingleRequest) ObjectType() string {
return "request"
}
type NextavailableIPv4Addrs struct {
NextavailableIPv4Addr IpNextAvailableInfo `json:"ipv4addr,omitempty"`
}
type NextavailableIPv6Addrs struct {
NextavailableIPv6Addr IpNextAvailableInfo `json:"ipv6addr,omitempty"`
}
type IpNextAvailable struct {
IBBase `json:"-"`
objectType string
Name string `json:"name"`
NextAvailableIPv4Addr *IpNextAvailableInfo `json:"ipv4addr,omitempty"`
NextAvailableIPv6Addr *IpNextAvailableInfo `json:"ipv6addr,omitempty"`
NextAvailableIPv4Addrs []NextavailableIPv4Addrs `json:"ipv4addrs,omitempty"`
NextAvailableIPv6Addrs []NextavailableIPv6Addrs `json:"ipv6addrs,omitempty"`
Comment string `json:"comment"`
Ea EA `json:"extattrs"`
Disable bool `json:"disable,omitempty"`
EnableDns *bool `json:"configure_for_dns,omitempty"`
EnableDhcp bool `json:"configure_for_dhcp,omitempty"`
MacAddr string `json:"mac,omitempty"`
Duid string `json:"duid,omitempty"`
NetworkView string `json:"network_view,omitempty"`
DnsView string `json:"view,omitempty"`
UseTtl bool `json:"use_ttl,omitempty"`
Ttl uint32 `json:"ttl,omitempty"`
Aliases []string `json:"aliases,omitempty"`
}
func (ni *IpNextAvailable) ObjectType() string {
return ni.objectType
}
func (ni *IpNextAvailable) SetObjectType(objectType string) {
ni.objectType = objectType
}
type IpNextAvailableInfo struct {
Function string `json:"_object_function"`
ResultField string `json:"_result_field"`
Object string `json:"_object"`
ObjectParams map[string]string `json:"_object_parameters"`
Params map[string][]string `json:"_parameters"`
NetviewName string `json:"network_view,omitempty"`
UseEaInheritance bool `json:"use_for_ea_inheritance"`
EnableDhcp bool `json:"configure_for_dhcp,omitempty"`
MacAddr string `json:"mac,omitempty"`
Duid string `json:"duid,omitempty"`
}
func NewIpNextAvailableInfo(objectParams map[string]string, params map[string][]string, useEaInheritance bool, ipAddrType string) *IpNextAvailableInfo {
nextAvailableIpInfo := IpNextAvailableInfo{
Function: "next_available_ip",
ResultField: "ips",
ObjectParams: objectParams,
Params: params,
UseEaInheritance: useEaInheritance,
}
if ipAddrType == "IPV6" {
nextAvailableIpInfo.Object = "ipv6network"
} else {
nextAvailableIpInfo.Object = "network"
}
return &nextAvailableIpInfo
}
func NewIpNextAvailable(name string, objectType string, objectParams map[string]string, params map[string][]string,
useEaInheritance bool, ea EA, comment string, disable bool, n *int, ipAddrType string,
enableDns bool, enableDhcp bool, macAddr string, duid string, networkView string, dnsView string, useTtl bool, ttl uint32, aliases []string) *IpNextAvailable {
nextAvailableIP := IpNextAvailable{
Name: name,
objectType: objectType,
Ea: ea,
Comment: comment,
Disable: disable,
UseTtl: useTtl,
Ttl: ttl,
DnsView: dnsView,
}
enableDhcpv6 := enableDhcp && duid != ""
enableDhcpv4 := enableDhcp && macAddr != "" && macAddr != MACADDR_ZERO
if n != nil && *n > 1 {
ipInfo := make([]IpNextAvailableInfo, *n)
for i := 0; i < *n; i++ {
ipInfo[i] = *NewIpNextAvailableInfo(objectParams, params, useEaInheritance, ipAddrType)
if ipAddrType == "IPV6" {
ipInfo[i].EnableDhcp = enableDhcpv6
ipInfo[i].Duid = duid
nextAvailableIP.NextAvailableIPv6Addrs = append(nextAvailableIP.NextAvailableIPv6Addrs, NextavailableIPv6Addrs{NextavailableIPv6Addr: ipInfo[i]})
} else {
ipInfo[i].EnableDhcp = enableDhcpv4
ipInfo[i].MacAddr = macAddr
nextAvailableIP.NextAvailableIPv4Addrs = append(nextAvailableIP.NextAvailableIPv4Addrs, NextavailableIPv4Addrs{NextavailableIPv4Addr: ipInfo[i]})
}
}
} else {
ipInfo := NewIpNextAvailableInfo(objectParams, params, useEaInheritance, ipAddrType)
switch objectType {
case "record:a":
nextAvailableIP.NextAvailableIPv4Addr = ipInfo
case "record:aaaa":
nextAvailableIP.NextAvailableIPv6Addr = ipInfo
case "record:host":
{
nextAvailableIP.EnableDns = &enableDns
nextAvailableIP.NetworkView = networkView
nextAvailableIP.Aliases = aliases
switch ipAddrType {
case "IPV4":
ipInfo.EnableDhcp = enableDhcpv4
ipInfo.MacAddr = macAddr
nextAvailableIP.NextAvailableIPv4Addrs = []NextavailableIPv4Addrs{{*ipInfo}}
case "IPV6":
ipInfo.EnableDhcp = enableDhcpv6
ipInfo.Duid = duid
nextAvailableIP.NextAvailableIPv6Addrs = []NextavailableIPv6Addrs{{*ipInfo}}
case "Both":
ipv4Info := NewIpNextAvailableInfo(objectParams, params, useEaInheritance, "IPV4")
ipv4Info.EnableDhcp = enableDhcpv4
ipv4Info.MacAddr = macAddr
nextAvailableIP.NextAvailableIPv4Addrs = []NextavailableIPv4Addrs{{*ipv4Info}}
ipv6Info := NewIpNextAvailableInfo(objectParams, params, useEaInheritance, "IPV6")
ipv6Info.EnableDhcp = enableDhcpv6
ipv6Info.Duid = duid
nextAvailableIP.NextAvailableIPv6Addrs = []NextavailableIPv6Addrs{{*ipv6Info}}
}
}
}
}
return &nextAvailableIP
}
type NetworkContainerNextAvailable struct {
IBBase `json:"-"`
objectType string
Network *NetworkContainerNextAvailableInfo `json:"network"`
NetviewName string `json:"network_view,omitempty"`
Comment string `json:"comment"`
Ea EA `json:"extattrs"`
}
func (nc *NetworkContainerNextAvailable) ObjectType() string {
return nc.objectType
}
func (nc *NetworkContainerNextAvailable) SetObjectType(objectType string) {
nc.objectType = objectType
}
type NetworkContainerNextAvailableInfo struct {
Function string `json:"_object_function"`
ResultField string `json:"_result_field"`
Object string `json:"_object"`
ObjectParams map[string]string `json:"_object_parameters"`
Params map[string]uint `json:"_parameters"`
NetviewName string `json:"network_view,omitempty"`
}
func NewNetworkContainerNextAvailableInfo(netview, cidr string, prefixLen uint, isIPv6 bool) *NetworkContainerNextAvailableInfo {
containerInfo := NetworkContainerNextAvailableInfo{
Function: "next_available_network",
ResultField: "networks",
ObjectParams: map[string]string{"network": cidr},
Params: map[string]uint{"cidr": prefixLen},
NetviewName: netview,
}
if isIPv6 {
containerInfo.Object = "ipv6networkcontainer"
} else {
containerInfo.Object = "networkcontainer"
}
return &containerInfo
}
func NewNetworkContainerNextAvailable(ncav *NetworkContainerNextAvailableInfo, isIPv6 bool, comment string, ea EA) *NetworkContainerNextAvailable {
nc := &NetworkContainerNextAvailable{
Network: ncav,
NetviewName: ncav.NetviewName,
Ea: ea,
Comment: comment,
}
if isIPv6 {
nc.objectType = "ipv6networkcontainer"
} else {
nc.objectType = "networkcontainer"
}
nc.returnFields = []string{"extattrs", "network", "network_view", "comment"}
return nc
}
type FixedAddress struct {
IBBase `json:"-"`
objectType string
Ref string `json:"_ref,omitempty"`
NetviewName string `json:"network_view,omitempty"`
Cidr string `json:"network,omitempty"`
Comment string `json:"comment"`
IPv4Address string `json:"ipv4addr,omitempty"`
IPv6Address string `json:"ipv6addr,omitempty"`
Duid string `json:"duid,omitempty"`
Mac string `json:"mac,omitempty"`
Name string `json:"name,omitempty"`
MatchClient string `json:"match_client,omitempty"`
Ea EA `json:"extattrs"`
}
func (fa FixedAddress) ObjectType() string {
return fa.objectType
}
func NewEmptyFixedAddress(isIPv6 bool) *FixedAddress {
res := &FixedAddress{}
res.Ea = make(EA)
if isIPv6 {
res.objectType = "ipv6fixedaddress"
res.returnFields = []string{"extattrs", "ipv6addr", "duid", "name", "network", "network_view", "comment"}
} else {
res.objectType = "fixedaddress"
res.returnFields = []string{"extattrs", "ipv4addr", "mac", "name", "network", "network_view", "comment"}
}
return res
}
func NewFixedAddress(
netView string,
name string,
ipAddr string,
cidr string,
macOrDuid string,
clients string,
eas EA,
ref string,
isIPv6 bool,
comment string) *FixedAddress {
res := NewEmptyFixedAddress(isIPv6)
res.NetviewName = netView
res.Name = name
res.Cidr = cidr
res.MatchClient = clients
res.Ea = eas
res.Ref = ref
res.Comment = comment
if isIPv6 {
res.IPv6Address = ipAddr
res.Duid = macOrDuid
} else {
res.IPv4Address = ipAddr
res.Mac = macOrDuid
}
return res
}
func NewEmptyDNSView() *View {
res := &View{}
res.returnFields = []string{"extattrs", "name", "network_view", "comment"}
return res
}
type Network struct {
IBBase `json:"-"`
objectType string
Ref string `json:"_ref,omitempty"`
NetviewName string `json:"network_view,omitempty"`
Cidr string `json:"network,omitempty"`
Ea EA `json:"extattrs"`
Comment string `json:"comment"`
}
func (n Network) ObjectType() string {
return n.objectType
}
func NewNetwork(netview string, cidr string, isIPv6 bool, comment string, ea EA) *Network {
var res Network
res.NetviewName = netview
res.Cidr = cidr
res.Ea = ea
res.Comment = comment
if isIPv6 {
res.objectType = "ipv6network"
} else {
res.objectType = "network"
}
res.returnFields = []string{"extattrs", "network", "network_view", "comment"}
return &res
}
type NetworkContainer struct {
IBBase `json:"-"`
objectType string
Ref string `json:"_ref,omitempty"`
NetviewName string `json:"network_view,omitempty"`
Cidr string `json:"network,omitempty"`
Comment string `json:"comment"`
Ea EA `json:"extattrs"`
}
func (nc NetworkContainer) ObjectType() string {
return nc.objectType
}
func NewNetworkContainer(netview, cidr string, isIPv6 bool, comment string, ea EA) *NetworkContainer {
nc := NetworkContainer{
NetviewName: netview,
Cidr: cidr,
Ea: ea,
Comment: comment,
}
if isIPv6 {
nc.objectType = "ipv6networkcontainer"
} else {
nc.objectType = "networkcontainer"
}
nc.returnFields = []string{"extattrs", "network", "network_view", "comment"}
return &nc
}
// License represents license wapi object
type License struct {
IBBase `json:"-"`
objectType string
Ref string `json:"_ref,omitempty"`
ExpirationStatus string `json:"expiration_status,omitempty"`
ExpiryDate int `json:"expiry_date,omitempty"`
HwID string `json:"hwid,omitempty"`
Key string `json:"key,omitempty"`
Kind string `json:"kind,omitempty"`
Limit string `json:"limit,omitempty"`
LimitContext string `json:"limit_context,omitempty"`
Licensetype string `json:"type,omitempty"`
}
func (l License) ObjectType() string {
return l.objectType
}
func NewGridLicense(license License) *License {
result := license
result.objectType = "license:gridwide"
returnFields := []string{"expiration_status",
"expiry_date",
"key",
"limit",
"limit_context",
"type"}
result.returnFields = returnFields
return &result
}
func NewLicense(license License) *License {
result := license
returnFields := []string{"expiration_status",
"expiry_date",
"hwid",
"key",
"kind",
"limit",
"limit_context",
"type"}
result.objectType = "member:license"
result.returnFields = returnFields
return &result
}
var mxRecordReturnFieldsList = []string{"mail_exchanger", "view", "name", "preference", "ttl", "use_ttl", "comment", "extattrs", "zone"}
func NewEmptyRecordMX() *RecordMX {
res := &RecordMX{}
res.returnFields = mxRecordReturnFieldsList
return res
}
func NewRecordMX(rm RecordMX) *RecordMX {
res := rm
res.returnFields = mxRecordReturnFieldsList
return &res
}
var srvRecordReturnFieldsList = []string{"name", "view", "priority", "weight", "port", "target", "ttl", "use_ttl", "comment", "extattrs", "zone"}
func NewEmptyRecordSRV() *RecordSRV {
res := RecordSRV{}
res.returnFields = srvRecordReturnFieldsList
return &res
}
func NewRecordSRV(rv RecordSRV) *RecordSRV {
res := rv
res.returnFields = srvRecordReturnFieldsList
return &res
}
// UnixTime is used to marshall/unmarshall epoch seconds
// presented in different parts of WAPI objects
type UnixTime struct {
time.Time
}
func (u *UnixTime) UnmarshalJSON(b []byte) error {
var timestamp int64
err := json.Unmarshal(b, ×tamp)
if err != nil {
return err
}
u.Time = time.Unix(timestamp, 0)
return nil
}
func (u UnixTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%d", u.Time.Unix())), nil
}
type Dns struct {
IBBase `json:"-"`
objectType string
Ref string `json:"_ref,omitempty"`
Ea EA `json:"extattrs"`
Comment string `json:"comment,omitempty"`
HostName string `json:"host_name,omitempty"`
IPv4Address string `json:"ipv4addr,omitempty"`
EnableDns bool `json:"enable_dns"`
}
func (d Dns) ObjectType() string {
return d.objectType
}
func NewDns(dns Dns) *Dns {
result := dns
result.objectType = "member:dns"
returnFields := []string{"enable_dns", "host_name"}
result.returnFields = returnFields
return &result
}
type Dhcp struct {
IBBase `json:"-"`
objectType string
Ref string `json:"_ref,omitempty"`
Ea EA `json:"extattrs"`
Comment string `json:"comment,omitempty"`
HostName string `json:"host_name,omitempty"`
IPv4Address string `json:"ipv4addr,omitempty"`
EnableDhcp bool `json:"enable_dhcp"`
}
func (d Dhcp) ObjectType() string {
return d.objectType
}
func NewDhcp(dhcp Dhcp) *Dhcp {
result := dhcp
result.objectType = "member:dhcpproperties"
returnFields := []string{"enable_dhcp", "host_name"}
result.returnFields = returnFields
return &result
}