forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_crud.go
296 lines (250 loc) · 7.83 KB
/
dynamic_crud.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
// Copyright (c) 2018 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package api
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
"github.com/gorilla/mux"
)
//------------------------------------------------------------------------------
// dynamicConfMgr maintains a map of config hashes to ids for dynamic
// inputs/outputs and thereby tracks whether a new configuration has changed for
// a particular id.
type dynamicConfMgr struct {
configHashes map[string]string
}
func newDynamicConfMgr() *dynamicConfMgr {
return &dynamicConfMgr{
configHashes: map[string]string{},
}
}
// Set will cache the config hash as the latest for the id and returns whether
// this hash is different to the previous config.
func (d *dynamicConfMgr) Set(id string, conf []byte) bool {
hasher := sha256.New()
hasher.Write(conf)
newHash := hex.EncodeToString(hasher.Sum(nil))
if hash, exists := d.configHashes[id]; exists {
if hash == newHash {
// Same config as before, ignore.
return false
}
}
d.configHashes[id] = newHash
return true
}
// Matches checks whether a provided config matches an existing config for the
// same id.
func (d *dynamicConfMgr) Matches(id string, conf []byte) bool {
if hash, exists := d.configHashes[id]; exists {
hasher := sha256.New()
hasher.Write(conf)
newHash := hex.EncodeToString(hasher.Sum(nil))
if hash == newHash {
// Same config as before.
return true
}
}
return false
}
// Remove will delete a cached hash for id if there is one.
func (d *dynamicConfMgr) Remove(id string) {
delete(d.configHashes, id)
}
//------------------------------------------------------------------------------
// Dynamic is a type for exposing CRUD operations on dynamic broker
// configurations as an HTTP interface. Events can be registered for listening
// to configuration changes, and these events should be forwarded to the
// dynamic broker.
type Dynamic struct {
onUpdate func(id string, conf []byte) error
onDelete func(id string) error
// configs is a map of the latest sanitised configs from our CRUD clients.
configs map[string][]byte
configHashes *dynamicConfMgr
configsMut sync.Mutex
// ids is a map of dynamic components that are currently active and their
// start times.
ids map[string]time.Time
idsMut sync.Mutex
}
// NewDynamic creates a new Dynamic API type.
func NewDynamic() *Dynamic {
return &Dynamic{
onUpdate: func(id string, conf []byte) error { return nil },
onDelete: func(id string) error { return nil },
configs: map[string][]byte{},
configHashes: newDynamicConfMgr(),
ids: map[string]time.Time{},
}
}
//------------------------------------------------------------------------------
// OnUpdate registers a func to handle CRUD events where a request wants to set
// a new value for a dynamic configuration. An error should be returned if the
// configuration is invalid or the component failed.
func (d *Dynamic) OnUpdate(onUpdate func(id string, conf []byte) error) {
d.onUpdate = onUpdate
}
// OnDelete registers a func to handle CRUD events where a request wants to
// remove a dynamic configuration. An error should be returned if the component
// failed to close.
func (d *Dynamic) OnDelete(onDelete func(id string) error) {
d.onDelete = onDelete
}
// Stopped should be called whenever an active dynamic component has closed,
// whether by naturally winding down or from a request.
func (d *Dynamic) Stopped(id string) {
d.idsMut.Lock()
defer d.idsMut.Unlock()
delete(d.ids, id)
}
// Started should be called whenever an active dynamic component has started
// with a new configuration. A normalised form of the configuration should be
// provided and will be delivered to clients that query the component contents.
func (d *Dynamic) Started(id string, config []byte) {
d.idsMut.Lock()
d.ids[id] = time.Now()
d.idsMut.Unlock()
d.configsMut.Lock()
d.configs[id] = config
d.configsMut.Unlock()
}
//------------------------------------------------------------------------------
// HandleList is an http.HandleFunc for returning maps of active dynamic
// components by their id to uptime.
func (d *Dynamic) HandleList(w http.ResponseWriter, r *http.Request) {
var httpErr error
defer func() {
if r.Body != nil {
r.Body.Close()
}
if httpErr != nil {
http.Error(w, "Internal server error", http.StatusBadGateway)
}
}()
type confInfo struct {
Uptime string `json:"uptime"`
Config json.RawMessage `json:"config"`
}
uptimes := map[string]confInfo{}
d.idsMut.Lock()
for k, v := range d.ids {
uptimes[k] = confInfo{
Uptime: time.Since(v).String(),
Config: []byte(`null`),
}
}
d.idsMut.Unlock()
d.configsMut.Lock()
for k, v := range d.configs {
if info, exists := uptimes[k]; exists {
info.Config = v
uptimes[k] = info
} else {
uptimes[k] = confInfo{
Uptime: "stopped",
Config: v,
}
}
}
d.configsMut.Unlock()
var resBytes []byte
if resBytes, httpErr = json.Marshal(uptimes); httpErr == nil {
w.Write(resBytes)
}
}
func (d *Dynamic) handleGETInput(w http.ResponseWriter, r *http.Request) error {
id := mux.Vars(r)["id"]
d.configsMut.Lock()
conf, exists := d.configs[id]
d.configsMut.Unlock()
if !exists {
http.Error(w, fmt.Sprintf("Dynamic component '%v' is not active", id), http.StatusNotFound)
return nil
}
w.Write(conf)
return nil
}
func (d *Dynamic) handlePOSTInput(w http.ResponseWriter, r *http.Request) error {
id := mux.Vars(r)["id"]
reqBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
d.configsMut.Lock()
matched := d.configHashes.Matches(id, reqBytes)
d.configsMut.Unlock()
if matched {
return nil
}
if err = d.onUpdate(id, reqBytes); err != nil {
return err
}
d.configsMut.Lock()
d.configHashes.Set(id, reqBytes)
d.configsMut.Unlock()
return nil
}
func (d *Dynamic) handleDELInput(w http.ResponseWriter, r *http.Request) error {
id := mux.Vars(r)["id"]
if err := d.onDelete(id); err != nil {
return err
}
d.configsMut.Lock()
d.configHashes.Remove(id)
delete(d.configs, id)
d.configsMut.Unlock()
return nil
}
// HandleCRUD is an http.HandleFunc for performing CRUD operations on dynamic
// components by their ids.
func (d *Dynamic) HandleCRUD(w http.ResponseWriter, r *http.Request) {
var httpErr error
defer func() {
if r.Body != nil {
r.Body.Close()
}
if httpErr != nil {
http.Error(w, "Internal server error", http.StatusBadGateway)
}
}()
id := mux.Vars(r)["id"]
if len(id) == 0 {
http.Error(w, "Var `id` must be set", http.StatusBadRequest)
return
}
switch r.Method {
case "POST":
httpErr = d.handlePOSTInput(w, r)
case "GET":
httpErr = d.handleGETInput(w, r)
case "DELETE":
httpErr = d.handleDELInput(w, r)
default:
httpErr = fmt.Errorf("verb not supported: %v", r.Method)
}
}
//------------------------------------------------------------------------------