forked from infobloxopen/infoblox-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_manager_dtc_server.go
165 lines (151 loc) · 4.85 KB
/
object_manager_dtc_server.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
package ibclient
import "fmt"
func NewEmptyDtcServer() *DtcServer {
dtcServer := &DtcServer{}
dtcServer.SetReturnFields(append(dtcServer.ReturnFields(), "extattrs", "auto_create_host_record", "disable", "health", "monitors", "sni_hostname", "use_sni_hostname"))
return dtcServer
}
func NewDtcServer(comment string,
name string,
host string,
autoCreateHostRecord bool,
disable bool,
ea EA,
monitors []*DtcServerMonitor,
sniHostname string,
useSniHostname bool,
) *DtcServer {
DtcServer := NewEmptyDtcServer()
DtcServer.Comment = &comment
DtcServer.Name = &name
DtcServer.Host = &host
DtcServer.AutoCreateHostRecord = &autoCreateHostRecord
DtcServer.Disable = &disable
DtcServer.Ea = ea
DtcServer.Monitors = monitors
DtcServer.SniHostname = &sniHostname
DtcServer.UseSniHostname = &useSniHostname
return DtcServer
}
func (objMgr *ObjectManager) CreateDtcServer(
comment string,
name string,
host string,
autoCreateHostRecord bool,
disable bool,
ea EA,
monitors []map[string]interface{},
sniHostname string,
useSniHostname bool,
) (*DtcServer, error) {
if name == "" || host == "" {
return nil, fmt.Errorf("name and host fields are required to create a dtc server")
}
if (useSniHostname && sniHostname == "") || (!useSniHostname && sniHostname != "") {
return nil, fmt.Errorf("'sni_hostname' must be provided when 'use_sni_hostname' is enabled, " +
"and 'use_sni_hostname' must be enabled if 'sni_hostname' is provided")
}
var serverMonitors []*DtcServerMonitor
for _, userMonitor := range monitors {
monitor, okMonitor := userMonitor["monitor"].(Monitor)
monitorHost, _ := userMonitor["host"].(string)
if !okMonitor {
return nil, fmt.Errorf("\"Required field missing: monitor")
}
monitorRef, err := getMonitorReference(monitor.Name, monitor.Type, objMgr)
if err != nil {
return nil, err
}
serverMonitor := &DtcServerMonitor{
Monitor: monitorRef,
Host: monitorHost,
}
serverMonitors = append(serverMonitors, serverMonitor)
}
dtcServer := NewDtcServer(comment, name, host, autoCreateHostRecord, disable, ea, serverMonitors, sniHostname, useSniHostname)
ref, err := objMgr.connector.CreateObject(dtcServer)
if err != nil {
return nil, err
}
dtcServer.Ref = ref
return dtcServer, nil
}
func (objMgr *ObjectManager) GetAllDtcServer(queryParams *QueryParams) ([]DtcServer, error) {
var res []DtcServer
server := NewEmptyDtcServer()
err := objMgr.connector.GetObject(server, "", queryParams, &res)
if err != nil {
return nil, fmt.Errorf("error getting DtcServer object, err: %s", err)
}
return res, nil
}
func (objMgr *ObjectManager) GetDtcServer(name string, host string) (*DtcServer, error) {
var res []DtcServer
server := NewEmptyDtcServer()
if name == "" || host == "" {
return nil, fmt.Errorf("name and host of the server are required to retreive a unique dtc server")
}
sf := map[string]string{
"name": name,
"host": host,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(server, "", queryParams, &res)
if err != nil {
return nil, err
} else if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf("Dtc server with name '%s' and host '%s' not found", name, host))
}
return &res[0], nil
}
func (objMgr *ObjectManager) UpdateDtcServer(
ref string,
comment string,
name string,
host string,
autoCreateHostRecord bool,
disable bool,
ea EA,
monitors []map[string]interface{},
sniHostname string,
useSniHostname bool) (*DtcServer, error) {
if (useSniHostname && sniHostname == "") || (!useSniHostname && sniHostname != "") {
return nil, fmt.Errorf("'sni_hostname' must be provided when 'use_sni_hostname' is enabled, " +
"and 'use_sni_hostname' must be enabled if 'sni_hostname' is provided")
}
var serverMonitors []*DtcServerMonitor
for _, userMonitor := range monitors {
monitor, okMonitor := userMonitor["monitor"].(Monitor)
monitorHost, _ := userMonitor["host"].(string)
if !okMonitor {
return nil, fmt.Errorf("\"Required field missing: monitor")
}
monitorRef, err := getMonitorReference(monitor.Name, monitor.Type, objMgr)
if err != nil {
return nil, err
}
serverMonitor := &DtcServerMonitor{
Monitor: monitorRef,
Host: monitorHost,
}
serverMonitors = append(serverMonitors, serverMonitor)
}
dtcServer := NewDtcServer(comment, name, host, autoCreateHostRecord, disable, ea, serverMonitors, sniHostname, useSniHostname)
dtcServer.Ref = ref
ref, err := objMgr.connector.UpdateObject(dtcServer, ref)
if err != nil {
return nil, err
}
dtcServer.Ref = ref
return dtcServer, nil
}
func (objMgr *ObjectManager) GetDtcServerByRef(ref string) (*DtcServer, error) {
serverDtc := NewEmptyDtcServer()
err := objMgr.connector.GetObject(
serverDtc, ref, NewQueryParams(false, nil), &serverDtc)
return serverDtc, err
}
func (objMgr *ObjectManager) DeleteDtcServer(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}