-
Notifications
You must be signed in to change notification settings - Fork 3
/
routes.go
479 lines (437 loc) · 14.8 KB
/
routes.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
// Copyright 2020 The Cloud Native Events Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package restapi
import (
"encoding/json"
"fmt"
"io"
"strings"
"time"
"github.com/redhat-cne/sdk-go/pkg/types"
"github.com/redhat-cne/rest-api/pkg/localmetrics"
cloudevents "github.com/cloudevents/sdk-go/v2"
ce "github.com/cloudevents/sdk-go/v2/event"
cne "github.com/redhat-cne/sdk-go/pkg/event"
"github.com/redhat-cne/sdk-go/pkg/pubsub"
"github.com/redhat-cne/sdk-go/v1/event"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/redhat-cne/sdk-go/pkg/channel"
"log"
"net/http"
)
// createSubscription create subscription and send it to a channel that is shared by middleware to process
// Creates a new subscription .
// If subscription exists with same resource then existing subscription is returned .
// responses:
//
// 201: repoResp
// 400: badReq
// 204: noContent
func (s *Server) createSubscription(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var response *http.Response
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
sub := pubsub.PubSub{}
if err = json.Unmarshal(bodyBytes, &sub); err != nil {
respondWithError(w, "marshalling error")
localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1)
return
}
if sub.GetEndpointURI() != "" {
response, err = s.HTTPClient.Post(sub.GetEndpointURI(), cloudevents.ApplicationJSON, nil)
if err != nil {
log.Printf("there was error validating endpointurl %v, subscription wont be created", err)
localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1)
respondWithError(w, err.Error())
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusNoContent {
log.Printf("there was an error validating endpointurl %s returned status code %d", sub.GetEndpointURI(), response.StatusCode)
respondWithError(w, "return url validation check failed for create subscription.check endpointURI")
localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1)
return
}
}
// check sub.EndpointURI by get
sub.SetID(uuid.New().String())
_ = sub.SetURILocation(fmt.Sprintf("http://localhost:%d%s%s/%s", s.port, s.apiPath, "subscriptions", sub.ID)) //nolint:errcheck
newSub, err := s.pubSubAPI.CreateSubscription(sub)
if err != nil {
log.Printf("error creating subscription %v", err)
localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1)
respondWithError(w, err.Error())
return
}
log.Printf("subscription created successfully.")
// go ahead and create QDR to this address
s.sendOut(channel.SUBSCRIBER, &newSub)
localmetrics.UpdateSubscriptionCount(localmetrics.ACTIVE, 1)
respondWithJSON(w, http.StatusCreated, newSub)
}
// createPublisher create publisher and send it to a channel that is shared by middleware to process
// Creates a new publisher .
// If publisher exists with same resource then existing publisher is returned .
// responses:
//
// 201: repoResp
// 400: badReq
// 204: noContent
func (s *Server) createPublisher(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
var response *http.Response
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
pub := pubsub.PubSub{}
if err = json.Unmarshal(bodyBytes, &pub); err != nil {
localmetrics.UpdatePublisherCount(localmetrics.FAILCREATE, 1)
respondWithError(w, "marshalling error")
return
}
if pub.GetEndpointURI() != "" {
response, err = s.HTTPClient.Post(pub.GetEndpointURI(), cloudevents.ApplicationJSON, nil)
if err != nil {
log.Printf("there was an error validating the publisher endpointurl %v, publisher won't be created.", err)
localmetrics.UpdatePublisherCount(localmetrics.FAILCREATE, 1)
respondWithError(w, err.Error())
return
}
defer response.Body.Close()
if response.StatusCode != http.StatusNoContent {
log.Printf("there was an error validating endpointurl %s returned status code %d", pub.GetEndpointURI(), response.StatusCode)
localmetrics.UpdatePublisherCount(localmetrics.FAILCREATE, 1)
respondWithError(w, "return url validation check failed for create publisher,check endpointURI")
return
}
}
// check pub.EndpointURI by get
pub.SetID(uuid.New().String())
_ = pub.SetURILocation(fmt.Sprintf("http://localhost:%d%s%s/%s", s.port, s.apiPath, "publishers", pub.ID)) //nolint:errcheck
newPub, err := s.pubSubAPI.CreatePublisher(pub)
if err != nil {
log.Printf("error creating publisher %v", err)
localmetrics.UpdatePublisherCount(localmetrics.FAILCREATE, 1)
respondWithError(w, err.Error())
return
}
log.Printf("publisher created successfully.")
// go ahead and create QDR to this address
s.sendOut(channel.PUBLISHER, &newPub)
localmetrics.UpdatePublisherCount(localmetrics.ACTIVE, 1)
respondWithJSON(w, http.StatusCreated, newPub)
}
func (s *Server) sendOut(eType channel.Type, sub *pubsub.PubSub) {
// go ahead and create QDR to this address
s.dataOut <- &channel.DataChan{
ID: sub.GetID(),
Address: sub.GetResource(),
Data: &ce.Event{},
Type: eType,
Status: channel.NEW,
}
}
func (s *Server) sendOutToDelete(eType channel.Type, sub *pubsub.PubSub) {
// go ahead and create QDR to this address
s.dataOut <- &channel.DataChan{
ID: sub.GetID(),
Address: sub.GetResource(),
Data: &ce.Event{},
Type: eType,
Status: channel.DELETE,
}
}
func (s *Server) getSubscriptionByID(w http.ResponseWriter, r *http.Request) {
queries := mux.Vars(r)
subscriptionID, ok := queries["subscriptionid"]
if !ok {
respondWithError(w, "subscription not found")
return
}
sub, err := s.pubSubAPI.GetSubscription(subscriptionID)
if err != nil {
respondWithError(w, "subscription not found")
return
}
respondWithJSON(w, http.StatusOK, sub)
}
func (s *Server) getPublisherByID(w http.ResponseWriter, r *http.Request) {
queries := mux.Vars(r)
publisherID, ok := queries["publisherid"]
if !ok {
respondWithError(w, "publisher parameter is required")
return
}
pub, err := s.pubSubAPI.GetPublisher(publisherID)
if err != nil {
respondWithError(w, "publisher not found")
return
}
respondWithJSON(w, http.StatusOK, pub)
}
func (s *Server) getSubscriptions(w http.ResponseWriter, _ *http.Request) {
b, err := s.pubSubAPI.GetSubscriptionsFromFile()
if err != nil {
respondWithError(w, "error loading subscriber data")
return
}
respondWithByte(w, http.StatusOK, b)
}
func (s *Server) getPublishers(w http.ResponseWriter, _ *http.Request) {
b, err := s.pubSubAPI.GetPublishersFromFile()
if err != nil {
respondWithError(w, "error loading publishers data")
return
}
respondWithByte(w, http.StatusOK, b)
}
func (s *Server) deletePublisher(w http.ResponseWriter, r *http.Request) {
queries := mux.Vars(r)
publisherID, ok := queries["publisherid"]
if !ok {
respondWithError(w, "publisherid param is missing")
return
}
if err := s.pubSubAPI.DeletePublisher(publisherID); err != nil {
localmetrics.UpdatePublisherCount(localmetrics.FAILDELETE, 1)
respondWithError(w, err.Error())
return
}
localmetrics.UpdatePublisherCount(localmetrics.ACTIVE, -1)
respondWithMessage(w, http.StatusOK, "OK")
}
func (s *Server) deleteSubscription(w http.ResponseWriter, r *http.Request) {
queries := mux.Vars(r)
subscriptionID, ok := queries["subscriptionid"]
if !ok {
respondWithError(w, "subscriptionid param is missing")
return
}
if err := s.pubSubAPI.DeleteSubscription(subscriptionID); err != nil {
localmetrics.UpdateSubscriptionCount(localmetrics.FAILDELETE, 1)
respondWithError(w, err.Error())
return
}
localmetrics.UpdateSubscriptionCount(localmetrics.ACTIVE, -1)
respondWithMessage(w, http.StatusOK, "OK")
}
func (s *Server) deleteAllSubscriptions(w http.ResponseWriter, _ *http.Request) {
size := len(s.pubSubAPI.GetSubscriptions())
if err := s.pubSubAPI.DeleteAllSubscriptions(); err != nil {
respondWithError(w, err.Error())
return
}
//update metrics
if size > 0 {
localmetrics.UpdateSubscriptionCount(localmetrics.ACTIVE, -(size))
}
// go ahead and create QDR to this address
s.sendOutToDelete(channel.SUBSCRIBER, &pubsub.PubSub{ID: "", Resource: "delete-all-subscriptions"})
respondWithMessage(w, http.StatusOK, "deleted all subscriptions")
}
func (s *Server) deleteAllPublishers(w http.ResponseWriter, _ *http.Request) {
size := len(s.pubSubAPI.GetPublishers())
if err := s.pubSubAPI.DeleteAllPublishers(); err != nil {
respondWithError(w, err.Error())
return
}
//update metrics
if size > 0 {
localmetrics.UpdatePublisherCount(localmetrics.ACTIVE, -(size))
}
respondWithMessage(w, http.StatusOK, "deleted all publishers")
}
// publishEvent gets cloud native events and converts it to cloud event and publishes to a transport to send
// it to the consumer
func (s *Server) publishEvent(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, err.Error())
return
}
cneEvent := event.CloudNativeEvent()
if err = json.Unmarshal(bodyBytes, &cneEvent); err != nil {
respondWithError(w, err.Error())
return
} // check if publisher is found
pub, err := s.pubSubAPI.GetPublisher(cneEvent.ID)
if err != nil {
localmetrics.UpdateEventPublishedCount(cneEvent.ID, localmetrics.FAIL, 1)
respondWithError(w, fmt.Sprintf("no publisher data for id %s found to publish event for", cneEvent.ID))
return
}
ceEvent, err := cneEvent.NewCloudEvent(&pub)
if err != nil {
localmetrics.UpdateEventPublishedCount(pub.Resource, localmetrics.FAIL, 1)
respondWithError(w, err.Error())
} else {
s.dataOut <- &channel.DataChan{
Type: channel.EVENT,
Data: ceEvent,
Address: pub.GetResource(),
}
localmetrics.UpdateEventPublishedCount(pub.Resource, localmetrics.SUCCESS, 1)
respondWithMessage(w, http.StatusAccepted, "Event sent")
}
}
// getCurrentState get current status of the events that are subscribed to
func (s *Server) getCurrentState(w http.ResponseWriter, r *http.Request) {
queries := mux.Vars(r)
resourceAddress, ok := queries["resourceAddress"]
if !ok {
respondWithError(w, "resourceAddress parameter not found")
return
}
//identify publisher or subscriber is asking for status
var sub *pubsub.PubSub
if len(s.pubSubAPI.GetSubscriptions()) > 0 {
for _, subscriptions := range s.pubSubAPI.GetSubscriptions() {
if strings.Contains(subscriptions.GetResource(), resourceAddress) {
sub = subscriptions
break
}
}
} else if len(s.pubSubAPI.GetPublishers()) > 0 {
for _, publishers := range s.pubSubAPI.GetPublishers() {
if strings.Contains(publishers.GetResource(), resourceAddress) {
sub = publishers
break
}
}
} else {
respondWithError(w, "subscription not found")
return
}
if sub == nil {
respondWithError(w, "subscription not found")
return
}
cneEvent := event.CloudNativeEvent()
cneEvent.SetID(sub.ID)
cneEvent.Type = channel.STATUS.String()
cneEvent.SetTime(types.Timestamp{Time: time.Now().UTC()}.Time)
cneEvent.SetDataContentType(cloudevents.ApplicationJSON)
cneEvent.SetData(cne.Data{
Version: "v1",
})
ceEvent, err := cneEvent.NewCloudEvent(sub)
if err != nil {
respondWithError(w, err.Error())
} else {
// for http you send to the protocol address
statusChannel := make(chan *channel.StatusChan, 1)
s.dataOut <- &channel.DataChan{
Type: channel.STATUS,
Data: ceEvent,
Address: sub.GetResource(),
StatusChan: statusChannel,
}
select {
case d := <-statusChannel:
if d.Data == nil || d.StatusCode != http.StatusOK {
if string(d.Message) == "" {
d.Message = []byte("event not found")
}
respondWithError(w, string(d.Message))
} else {
respondWithJSON(w, d.StatusCode, *d.Data)
}
case <-time.After(5 * time.Second):
close(statusChannel)
respondWithError(w, "timeout waiting for status")
}
}
}
// pingForSubscribedEventStatus sends ping to the listening address in the producer to fire all status as events
func (s *Server) pingForSubscribedEventStatus(w http.ResponseWriter, r *http.Request) {
queries := mux.Vars(r)
subscriptionID, ok := queries["subscriptionid"]
if !ok {
respondWithError(w, "subscription parameter not found")
return
}
sub, err := s.pubSubAPI.GetSubscription(subscriptionID)
if err != nil {
respondWithError(w, "subscription not found")
return
}
cneEvent := event.CloudNativeEvent()
cneEvent.SetID(sub.ID)
cneEvent.Type = "status_check"
cneEvent.SetTime(types.Timestamp{Time: time.Now().UTC()}.Time)
cneEvent.SetDataContentType(cloudevents.ApplicationJSON)
cneEvent.SetData(cne.Data{
Version: "v1",
})
ceEvent, err := cneEvent.NewCloudEvent(&sub)
if err != nil {
respondWithError(w, err.Error())
} else {
s.dataOut <- &channel.DataChan{
Type: channel.STATUS,
StatusChan: nil,
Data: ceEvent,
Address: fmt.Sprintf("%s/%s", sub.GetResource(), "status"),
}
respondWithMessage(w, http.StatusAccepted, "ping sent")
}
}
// logEvent gets cloud native events and converts it to cloud native event and writes to log
func (s *Server) logEvent(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
respondWithError(w, err.Error())
return
}
cneEvent := event.CloudNativeEvent()
if err := json.Unmarshal(bodyBytes, &cneEvent); err != nil {
respondWithError(w, err.Error())
return
} // check if publisher is found
log.Printf("event received %v", cneEvent)
respondWithMessage(w, http.StatusAccepted, "Event published to log")
}
func dummy(w http.ResponseWriter, _ *http.Request) {
respondWithMessage(w, http.StatusNoContent, "dummy test")
}
func respondWithError(w http.ResponseWriter, message string) {
respondWithJSON(w, http.StatusBadRequest, map[string]string{"error": message})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
if response, err := json.Marshal(payload); err == nil {
w.Header().Set("Content-Type", cloudevents.ApplicationJSON)
w.WriteHeader(code)
w.Write(response) //nolint:errcheck
} else {
respondWithMessage(w, http.StatusBadRequest, "error parsing event data")
}
}
func respondWithMessage(w http.ResponseWriter, code int, message string) {
w.Header().Set("Content-Type", cloudevents.ApplicationJSON)
respondWithJSON(w, code, map[string]string{"status": message})
}
func respondWithByte(w http.ResponseWriter, code int, message []byte) {
w.Header().Set("Content-Type", cloudevents.ApplicationJSON)
w.WriteHeader(code)
w.Write(message) //nolint:errcheck
}