-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
227 lines (202 loc) · 6.91 KB
/
config.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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
)
//SERVICE define which IP version we are use (currently only dhcp4 supported)
var SERVICE []string = []string{"dhcp4"}
type configReq struct {
Command string `json:"command"`
Service []string `json:"service"`
}
type configSet struct {
Command string `json:"command"`
Service []string `json:"service"`
Arguments Arguments `json:"arguments"`
}
//NestedElem elements in root
type NestedElem struct {
Arguments Arguments `json:"arguments"`
Result int `json:"result"`
}
//Arguments structure
type Arguments struct {
Dhcp4 Dhcp4 `json:"Dhcp4"`
Logging interface{} `json:"Logging"` // If you dont need to modify this area just pass as interface{}
}
//Dhcp4 structure
type Dhcp4 struct {
Subnet4 []Subnet4 `json:"subnet4"`
InterfacesConfig interface{} `json:"interfaces-config"`
ControlSocket interface{} `json:"control-socket"`
LeaseDatabase interface{} `json:"lease-database"`
ExpiredLeasesProcessing interface{} `json:"expired-leases-processing"`
OptionData interface{} `json:"option-data"`
}
//Subnet4 structure
type Subnet4 struct {
Subnet interface{} `json:"subnet"`
Pools interface{} `json:"pools"`
OptionData interface{} `json:"option-data"`
RenewTimer int `json:"renew-timer"`
RebindTimer int `json:"rebind-timer"`
ValidLifetime int `json:"valid-lifetime"`
Reservations []Reservations `json:"reservations"`
}
//Reservations structure
type Reservations struct {
Hostname string `json:"hostname"`
Hwaddress string `json:"hw-address"`
Ipaddress string `json:"ip-address"`
}
// Config struct for provider
type Config struct {
Server string
Username string
Password string
Configfile string
}
// Client for connections
type Client struct {
Config *Config
currentConfig []NestedElem
httpClient *http.Client
}
func check(e error) {
if e != nil {
log.Fatal(e)
panic(e)
}
}
//Client In go (c *Config) means this is method assosieted to struct Config
func (c *Config) Client() (*Client, error) {
log.Println("[INFO] Configuring kea-api client")
htclient := &http.Client{}
var data []byte
services := []string{"dhcp4"}
jsonStr := configReq{Command: "config-get", Service: services}
b, err := json.Marshal(jsonStr)
check(err)
req, err := http.NewRequest("POST", c.Server, bytes.NewBuffer(b))
req.SetBasicAuth(c.Username, c.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := htclient.Do(req)
if err != nil {
log.Fatalf("The HTTP request failed with error %s\n", err)
} else {
data, _ = ioutil.ReadAll(resp.Body)
log.Printf("[INFO] %s\n", string(data))
}
log.Println("[INFO] Terminating GET_CONF application...")
var m []NestedElem
err = json.Unmarshal(data, &m)
client := &Client{
Config: c,
currentConfig: m,
httpClient: htclient,
}
return client, nil
}
//ReadLease method to check if lease exists in kea-dhcpd4
func (c *Client) ReadLease(r Reservations) bool {
for _, reservation := range c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations {
if reservation.Hostname == r.Hostname {
log.Printf("[DEBUG] read function found host\n")
return true
}
}
log.Printf("[DEBUG] read function cannot found the host\n")
return false
}
//NewLease method to create new lease fot kea-dhcpd4 works!
func (c *Client) NewLease(r Reservations) error {
var data []byte
c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations = append(c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations, r)
jsonSet := configSet{Command: "config-set", Service: SERVICE, Arguments: c.currentConfig[0].Arguments}
enc, err := json.Marshal(jsonSet)
check(err)
req, err := http.NewRequest("POST", c.Config.Server, bytes.NewBuffer(enc))
req.SetBasicAuth(c.Config.Username, c.Config.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
log.Printf("[Error] The HTTP request failed with error %s\n", err)
} else {
data, _ = ioutil.ReadAll(resp.Body)
c.SaveConfig()
log.Printf("[INFO] New lease shoud be added.. %s\n", string(data))
}
return nil
}
// UpdateLease resource works!
func (c *Client) UpdateLease(r Reservations) error {
var data []byte
for index, reservation := range c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations {
if reservation.Hostname == r.Hostname {
c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations[index] = r
}
}
jsonSet := configSet{Command: "config-set", Service: SERVICE, Arguments: c.currentConfig[0].Arguments}
enc, err := json.Marshal(jsonSet)
check(err)
req, err := http.NewRequest("POST", c.Config.Server, bytes.NewBuffer(enc))
req.SetBasicAuth(c.Config.Username, c.Config.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
log.Printf("[Error] The HTTP request failed with error %s\n", err)
} else {
data, _ = ioutil.ReadAll(resp.Body)
c.SaveConfig()
log.Printf("[INFO] The lease shoud be updated.. %s\n", string(data))
}
return nil
}
// DeleteLease resource
func (c *Client) DeleteLease(r Reservations) error {
var data []byte
for index, reservation := range c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations {
if reservation.Hostname == r.Hostname {
c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations = append(c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations[:index],
c.currentConfig[0].Arguments.Dhcp4.Subnet4[0].Reservations[index+1:]...)
}
}
jsonSet := configSet{Command: "config-set", Service: SERVICE, Arguments: c.currentConfig[0].Arguments}
enc, err := json.Marshal(jsonSet)
check(err)
req, err := http.NewRequest("POST", c.Config.Server, bytes.NewBuffer(enc))
req.SetBasicAuth(c.Config.Username, c.Config.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
log.Printf("[Error] The HTTP request failed with error %s\n", err)
} else {
data, _ = ioutil.ReadAll(resp.Body)
c.SaveConfig()
log.Printf("[INFO] The lease should be deleted if existed.. %s\n", string(data))
}
return nil
}
//SaveConfig method to save a config file for kea-dhcpd4
func (c *Client) SaveConfig() error {
var data []byte
jsonWrite := "{ \"command\": \"config-write\", \"service\": [ \"dhcp4\" ], \"arguments\":{\"filename\":\"" + c.Config.Configfile + "\"} }"
buff := new(bytes.Buffer)
json.NewEncoder(buff).Encode(jsonWrite)
req, err := http.NewRequest("POST", c.Config.Server, strings.NewReader(jsonWrite))
check(err)
req.SetBasicAuth(c.Config.Username, c.Config.Password)
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
log.Printf("[ERROR] The HTTP request failed with error %s\n", err)
} else {
data, _ = ioutil.ReadAll(resp.Body)
log.Printf("[INFO] Configuration file should be saved.. %s\n", string(data))
}
return nil
}