-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathservicehttp.go
More file actions
658 lines (588 loc) · 24.9 KB
/
servicehttp.go
File metadata and controls
658 lines (588 loc) · 24.9 KB
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
655
656
657
658
/*
* === This file is part of ALICE O² ===
*
* Copyright 2019-2023 CERN and copyright holders of ALICE O².
* Author: Claire Guyot <claire.eloise.guyot@cern.ch>
* Author: Teo Mrnjavac <teo.m@cern.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In applying this license CERN does not waive the privileges and
* immunities granted to it by virtue of its status as an
* Intergovernmental Organization or submit itself to any jurisdiction.
*/
package local
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
_ "github.com/AliceO2Group/Control/apricot/docs"
apricotpb "github.com/AliceO2Group/Control/apricot/protos"
"github.com/AliceO2Group/Control/common/logger/infologger"
"github.com/AliceO2Group/Control/common/system"
"github.com/AliceO2Group/Control/configuration"
"github.com/AliceO2Group/Control/configuration/componentcfg"
"github.com/gorilla/mux"
"github.com/spf13/viper"
httpSwagger "github.com/swaggo/http-swagger/v2"
)
type HttpService struct {
svc configuration.Service
}
// @title O² Apricot REST API
// @version 1.0
// @description REST API for ALICE O² Apricot configuration service
// @contact.name O² FLP support
// @contact.url https://alice-flp.docs.cern.ch/
// @contact.email alice-o2-flp-support@cern.ch
// @externalDocs.description AliECS handbook
// @externalDocs.url https://alice-flp.docs.cern.ch/aliecs/handbook/
func newHandlerForHttpService(httpsvc *HttpService) *mux.Router {
router := mux.NewRouter()
// documentation endpoint
_ = router.PathPrefix("/docs/").Handler(httpSwagger.WrapHandler)
// component configuration API
// GET /components
apiComponents := router.PathPrefix("/components").Subrouter()
apiComponents.HandleFunc("", httpsvc.ApiListComponents).Methods(http.MethodGet)
apiComponents.HandleFunc("/", httpsvc.ApiListComponents).Methods(http.MethodGet)
// POST /components/_invalidate_cache
apiComponents.HandleFunc("/_invalidate_cache", httpsvc.ApiInvalidateCache).Methods(http.MethodPost)
// GET /components/{component}
apiComponentsEntries := router.PathPrefix("/components/{component}").Subrouter()
// GET /components/{component} returns all, raw is ignored
apiComponentsEntries.HandleFunc("", httpsvc.ApiListComponentEntries).Methods(http.MethodGet)
apiComponentsEntries.HandleFunc("/", httpsvc.ApiListComponentEntries).Methods(http.MethodGet)
// runtype = {runtype} rolename = any, raw excludes ANY runtype, if false returns all
apiComponentsEntries.HandleFunc("/{runtype}", httpsvc.ApiListComponentEntries).Methods(http.MethodGet)
apiComponentsEntries.HandleFunc("/{runtype}/", httpsvc.ApiListComponentEntries).Methods(http.MethodGet)
// runtype = {runtype} rolename = {rolename}, raw excludes ANY runtype and any rolename, if false returns all
apiComponentsEntries.HandleFunc("/{runtype}/{rolename}", httpsvc.ApiListComponentEntries).Methods(http.MethodGet)
apiComponentsEntries.HandleFunc("/{runtype}/{rolename}/", httpsvc.ApiListComponentEntries).Methods(http.MethodGet)
apiComponentQuery := router.PathPrefix("/components/{component}/{runtype}/{rolename}/{remainder:.*}").Subrouter()
// GET /components/{component}/{runtype}/{rolename}/{remainder:.*} with '/resolve' within the remainder,
// assumes this is not a raw path, returns a raw path like {component}/{runtype}/{rolename}/{entry}
apiComponentQuery.HandleFunc("/resolve", httpsvc.ApiResolveComponentQuery).Methods(http.MethodGet)
// GET /components/{component}/{runtype}/{rolename}/{remainder:.*}, accepts raw or non-raw path, returns payload
// that may be processed or not depending on process=true or false
apiComponentQuery.HandleFunc("", httpsvc.ApiGetComponentConfiguration).Methods(http.MethodGet)
apiComponentQuery.HandleFunc("/", httpsvc.ApiGetComponentConfiguration).Methods(http.MethodGet)
// inventory API
apiInventoryFlps := router.PathPrefix("/inventory/flps").Subrouter()
apiInventoryFlps.HandleFunc("", httpsvc.ApiGetFlps).Methods(http.MethodGet)
apiInventoryFlps.HandleFunc("/", httpsvc.ApiGetFlps).Methods(http.MethodGet)
apiInventoryFlps.HandleFunc("/{format}", httpsvc.ApiGetFlps).Methods(http.MethodGet)
apiInventoryDetectors := router.PathPrefix("/inventory/detectors").Subrouter()
apiInventoryDetectors.HandleFunc("", httpsvc.ApiGetDetectorsInventory).Methods(http.MethodGet)
apiInventoryDetectors.HandleFunc("/", httpsvc.ApiGetDetectorsInventory).Methods(http.MethodGet)
apiInventoryDetectors.HandleFunc("/{format}", httpsvc.ApiGetDetectorsInventory).Methods(http.MethodGet)
apiInventoryDetectorFlps := router.PathPrefix("/inventory/detectors/{detector}/flps").Subrouter()
apiInventoryDetectorFlps.HandleFunc("", httpsvc.ApiGetDetectorFlps).Methods(http.MethodGet)
apiInventoryDetectorFlps.HandleFunc("/", httpsvc.ApiGetDetectorFlps).Methods(http.MethodGet)
apiInventoryDetectorFlps.HandleFunc("/{format}", httpsvc.ApiGetDetectorFlps).Methods(http.MethodGet)
return router
}
func NewHttpService(service configuration.Service) (svr *http.Server) {
httpsvc := &HttpService{
svc: service,
}
handler := newHandlerForHttpService(httpsvc)
httpsvr := &http.Server{
Handler: handler,
Addr: ":" + strconv.Itoa(viper.GetInt("httpListenPort")),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
// async-start of http Service and capture error
go func() {
log.WithField("port", viper.GetInt("httpListenPort")).
WithField("level", infologger.IL_Support).
Info("HTTP service started")
err := httpsvr.ListenAndServe()
if err != nil {
log.WithError(err).Error("HTTP service returned error")
}
}()
return httpsvr
}
// ApiListComponents lists configuration components
//
// @Summary List Apricot-managed configuration components
// @Description Returns a list of all configuration components managed by Apricot
// @Tags component configuration
// @Produce json
// @Produce plain
// @Param format query string false "Output format, json or text" Enums(json, text) Default(text)
// @Success 200 {array} string "List of components, either as JSON array or comma-separated plain text"
// @Failure 500 {string} string "Internal server error"
// @Router /components [get]
func (httpsvc *HttpService) ApiListComponents(w http.ResponseWriter, r *http.Request) {
queryArgs := r.URL.Query()
format := queryArgs.Get("format")
switch format {
case "json":
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
components, err := httpsvc.svc.ListComponents()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
out, _ := json.MarshalIndent(err, "", "\t")
_, _ = fmt.Fprintln(w, string(out))
return
}
sort.Strings(components)
response, err := json.MarshalIndent(components, "", "\t")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
out, _ := json.MarshalIndent(err, "", "\t")
_, _ = fmt.Fprintln(w, string(out))
return
}
_, _ = fmt.Fprintln(w, string(response))
return
case "text":
fallthrough
default:
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
components, err := httpsvc.svc.ListComponents()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintln(w, err)
return
}
sort.Strings(components)
response := strings.Join(components, "\n")
_, _ = fmt.Fprintln(w, string(response))
}
}
// ApiInvalidateCache clears the component template cache
//
// @Summary Clear cache of Apricot-managed configuration component templates
// @Description Invalidates all cached templates for all configuration components managed by Apricot
// @Tags component configuration
// @Produce plain
// @Success 200
// @Router /components/_invalidate_cache [post]
func (httpsvc *HttpService) ApiInvalidateCache(w http.ResponseWriter, r *http.Request) {
log.WithField("level", infologger.IL_Support).
Debug("invalidating component template cache")
httpsvc.svc.InvalidateComponentTemplateCache()
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(w, "OK")
}
// ApiListComponentEntries lists all entries for a given configuration component
//
// @Summary Lists all entries for an Apricot-managed configuration component
// @Description Returns a list of all entries belonging to a given configuration component corresponding to the given query. The component must be provided. If the run type is not provided, entries for all run types are returned. If the role name is not provided, entries for all role names are returned. If the raw flag is true, AApricot will not attempt to include "ANY"/"any" run types or role names, and will return only the entries stored under the provided Consul path. If the raw flag is false, Apricot will include all entries for the given component, run type and role name, including entries stored under "ANY" and "any" paths corresponding to fallback values valid for all run types or role names.
// @Tags component configuration
// @Produce json
// @Produce plain
// @Param format query string false "Output format, json or text" Enums(json, text) Default(text)
// @Param raw query boolean false "If true, the query returns strictly Consul paths without catching all run types or role names with ANY/any" Default(false)
// @Param component path string true "Configuration component"
// @Param runtype path string false "O² Run type, must be capitalized" Default(ANY)
// @Param rolename path string false "Role name" Default(any)
// @Success 200 {array} string "List of entries, either as JSON array or comma-separated plain text"
// @Failure 400 {string} string "Bad request, if the run type is invalid"
// @Failure 500 {string} string "Internal server error"
// @Router /components/{component} [get]
// @Router /components/{component}/{runtype} [get]
// @Router /components/{component}/{runtype}/{rolename} [get]
func (httpsvc *HttpService) ApiListComponentEntries(w http.ResponseWriter, r *http.Request) {
// GET /components/{component} returns all, raw is ignored
// runtype = {runtype} rolename = any, raw excludes ANY runtype, if false returns all
// runtype = {runtype} rolename = {rolename}, raw excludes ANY runtype and any rolename, if false returns all
queryParams := mux.Vars(r)
component, hasComponent := queryParams["component"]
if !hasComponent {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "component name not provided")
return
}
runtypeS, hasRuntype := queryParams["runtype"]
runType := apricotpb.RunType_NULL
if hasRuntype {
runtypeS = strings.ToUpper(runtypeS)
runTypeInt, isRunTypeValid := apricotpb.RunType_value[runtypeS]
if !isRunTypeValid {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "runtype not valid")
return
} else {
runType = apricotpb.RunType(runTypeInt)
}
}
rolename, hasRolename := queryParams["rolename"]
queryArgs := r.URL.Query()
rawS := queryArgs.Get("raw")
raw, err := strconv.ParseBool(rawS)
if err != nil {
raw = false
}
entries, err := httpsvc.svc.ListComponentEntries(&componentcfg.EntriesQuery{
Component: component,
RunType: runType,
RoleName: rolename,
})
filterPrefixes := make(map[string]struct{})
if hasRuntype {
if hasRolename { // we filter for runtype and rolename, and if !raw, also combos with ANY and any
filterPrefixes[runtypeS+"/"+rolename] = struct{}{}
if !raw {
filterPrefixes["ANY"+"/"+rolename] = struct{}{}
filterPrefixes[runtypeS+"/any"] = struct{}{}
filterPrefixes["ANY/any"] = struct{}{}
}
} else { // no rolename provided, we only filter for runtype and ANY if !raw
filterPrefixes[runtypeS] = struct{}{}
if !raw {
filterPrefixes["ANY"] = struct{}{}
}
}
}
filteredEntries := make([]string, 0)
if hasRuntype { // if there's any filtering to do
for _, entry := range entries {
for filterPrefix := range filterPrefixes {
if strings.HasPrefix(entry, filterPrefix) {
filteredEntries = append(filteredEntries, entry)
}
}
}
} else { // no filtering, return all entries
filteredEntries = entries
}
format := queryArgs.Get("format")
switch format {
case "json":
w.Header().Set("Content-Type", "application/json")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
out, _ := json.MarshalIndent(err, "", "\t")
_, _ = fmt.Fprintln(w, string(out))
return
}
w.WriteHeader(http.StatusOK)
sort.Strings(filteredEntries)
response, err := json.MarshalIndent(filteredEntries, "", "\t")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
out, _ := json.MarshalIndent(err, "", "\t")
_, _ = fmt.Fprintln(w, string(out))
return
}
_, _ = fmt.Fprintln(w, string(response))
return
case "text":
fallthrough
default:
w.Header().Set("Content-Type", "text/plain")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintln(w, err)
return
}
w.WriteHeader(http.StatusOK)
sort.Strings(filteredEntries)
response := strings.Join(filteredEntries, "\n")
_, _ = fmt.Fprintln(w, string(response))
}
}
// ApiResolveComponentQuery resolves a query for a given component, run type, role name and entry key
//
// @Summary Resolves a query for a given component, run type, role name and entry key
// @Description Returns a resolved path for a given component, run type, role name and entry key. The path points to an actual existing entry in Consul, resolving ANY run type and any rolename wildcards.
// @Tags component configuration
// @Produce plain
// @Param component path string true "Configuration component"
// @Param runtype path string true "O² Run type, must be capitalized"
// @Param rolename path string true "Role name"
// @Param entry path string true "Entry key"
// @Success 200 {string} string "Resolved path for the queried entry"
// @Failure 400 {string} string "Bad request, if a parameter is invalid"
// @Failure 500 {string} string "Internal server error"
// @Router /components/{component}/{runtype}/{rolename}/{entry}/resolve [get]
func (httpsvc *HttpService) ApiResolveComponentQuery(w http.ResponseWriter, r *http.Request) {
// GET /components/{component}/{runtype}/{rolename}/{entry}/resolve, assumes this is not a raw path, returns a raw path
// like {component}/{runtype}/{rolename}/{entry}
queryParams := mux.Vars(r)
component, hasComponent := queryParams["component"]
if !hasComponent {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "component name not provided")
return
}
runtypeS, hasRuntype := queryParams["runtype"]
runType := apricotpb.RunType_NULL
if hasRuntype {
runtypeS = strings.ToUpper(runtypeS)
runTypeInt, isRunTypeValid := apricotpb.RunType_value[runtypeS]
if !isRunTypeValid {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "runtype not valid")
return
} else {
runType = apricotpb.RunType(runTypeInt)
}
} else {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "runtype not provided")
return
}
rolename, hasRolename := queryParams["rolename"]
if !hasRolename {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "rolename not provided")
return
}
entry, hasEntry := queryParams["remainder"]
if !hasEntry {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "entry not provided")
return
}
entry = strings.TrimSuffix(entry, "/resolve")
resolved, err := httpsvc.svc.ResolveComponentQuery(&componentcfg.Query{
Component: component,
RunType: runType,
RoleName: rolename,
EntryKey: entry,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintln(w, err)
return
}
resolvedStr := resolved.Path()
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(w, resolvedStr)
}
// ApiGetComponentConfiguration returns the processed configuration payload for a given component, run type, role name and entry key
//
// @Summary Returns a configuration payload for a given component, run type, role name and entry key
// @Description The provided component, run type, role name and entry key are used to query the configuration service for a configuration entry, which is then processed in the O² Apricot template system to produce the final payload, and returned as string..
// @Tags component configuration
// @Produce plain
// @Param process query boolean false "If true, template processing is performed to produce the final payload; if false, the entry is returned verbatim. In the true case, any number of additional string key-value pairs may be passed as query parameters (e.g. ?process=true&mykey1=myvalue1&mykey2=myvalue2), which are then fed into the template system as variables that affect configuration payload generation" Default(false)
// @Param component path string true "Configuration component"
// @Param runtype path string true "O² Run type, must be capitalized"
// @Param rolename path string true "Role name"
// @Param entry path string true "Entry key"
// @Success 200 {string} string "Configuration payload for the queried entry, either processed or verbatim depending on the process flag"
// @Failure 400 {string} string "Bad request, if a parameter is invalid"
// @Failure 500 {string} string "Internal server error"
// @Router /components/{component}/{runtype}/{rolename}/{entry} [get]
func (httpsvc *HttpService) ApiGetComponentConfiguration(w http.ResponseWriter, r *http.Request) {
// GET /components/{component}/{runtype}/{rolename}/{entry}, accepts raw or non-raw path, returns payload
// that may be processed or not depending on process=true or false
queryParams := mux.Vars(r)
component, hasComponent := queryParams["component"]
if !hasComponent {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "component name not provided")
return
}
runtypeS, hasRuntype := queryParams["runtype"]
runType := apricotpb.RunType_NULL
if hasRuntype {
runtypeS = strings.ToUpper(runtypeS)
runTypeInt, isRunTypeValid := apricotpb.RunType_value[runtypeS]
if !isRunTypeValid {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "runtype not valid")
return
} else {
runType = apricotpb.RunType(runTypeInt)
}
} else {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "runtype not provided")
return
}
rolename, hasRolename := queryParams["rolename"]
if !hasRolename {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "rolename not provided")
return
}
entry, hasEntry := queryParams["remainder"]
if !hasEntry {
w.WriteHeader(http.StatusBadRequest)
_, _ = fmt.Fprintln(w, "entry not provided")
return
}
entry = strings.TrimSuffix(entry, "/")
queryArgs := r.URL.Query()
processS := queryArgs.Get("process")
process, err := strconv.ParseBool(processS)
if err != nil {
process = false
}
if queryArgs.Has("process") {
queryArgs.Del("process")
}
varStack := make(map[string]string)
for k, v := range queryArgs {
if len(v) > 0 {
varStack[k] = v[0]
}
}
var payload string
if process {
payload, err = httpsvc.svc.GetAndProcessComponentConfiguration(&componentcfg.Query{
Component: component,
RunType: runType,
RoleName: rolename,
EntryKey: entry,
}, varStack)
} else {
payload, err = httpsvc.svc.GetComponentConfiguration(&componentcfg.Query{
Component: component,
RunType: runType,
RoleName: rolename,
EntryKey: entry,
})
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintln(w, err)
return
}
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintln(w, payload)
}
// ApiGetFlps returns the list of FLPs in the cluster known to Apricot
//
// @Summary Returns the list of FLPs in the cluster known to Apricot
// @Description Returns the list of all Apricot-managed hosts in the cluster that are known to be FLPs, newline-separated or JSON depending on the format parameter
// @Tags cluster inventory
// @Param format path string false "Output format, json or text" Enums(json, text) Default(text)
// @Produce plain
// @Success 200 {string} string "List of FLPs in the cluster known to Apricot, newline-separated or JSON depending on the format parameter"
// @Router /inventory/flps/{format} [get]
func (httpsvc *HttpService) ApiGetFlps(w http.ResponseWriter, r *http.Request) {
httpsvc.ApiGetHostInventory(w, r, "")
}
// ApiGetDetectorFlps returns the list of FLPs in the cluster that serve a given detector
//
// @Summary Returns the list of FLPs in the cluster that serve a given detector
// @Description Returns the list of all Apricot-managed hosts in the cluster that are known to be FLPs and serving the given detector, newline-separated or JSON depending on the format parameter
// @Tags cluster inventory
// @Param format path string false "Output format, json or text" Enums(json, text) Default(text)
// @Produce plain
// @Success 200 {string} string "List of FLPs in the cluster known to Apricot, newline-separated or JSON depending on the format parameter"
// @Router /inventory/detectors/{detector}/flps/{format} [get]
func (httpsvc *HttpService) ApiGetDetectorFlps(w http.ResponseWriter, r *http.Request) {
queryParam := mux.Vars(r)
detector := queryParam["detector"]
_, err := system.IDString(detector)
if err != nil {
log.WithError(err).Warn("Error, the detector name provided is not valid.")
} else {
httpsvc.ApiGetHostInventory(w, r, detector)
}
}
func (httpsvc *HttpService) ApiGetHostInventory(w http.ResponseWriter, r *http.Request, detector string) {
hosts, err := httpsvc.svc.GetHostInventory(detector)
if err != nil {
log.WithError(err).Warn("Error, could not retrieve host list.")
}
httpsvc.ApiPrintClusterInformation(w, r, hosts, nil)
}
// ApiGetDetectorsInventory returns the list of detectors belonging to the installed instance known to Apricot
//
// @Summary Returns the list of detectors belonging to the installed instance known to Apricot
// @Description Returns the list of all detectors known to Apricot that belong to the installed instance, newline-separated or JSON depending on the format parameter
// @Tags cluster inventory
// @Param format path string false "Output format, json or text" Enums(json, text) Default(text)
// @Produce plain
// @Success 200 {string} string "List of detectors in the instance known to Apricot, newline-separated or JSON depending on the format parameter"
// @Router /inventory/detectors/{format} [get]
func (httpsvc *HttpService) ApiGetDetectorsInventory(w http.ResponseWriter, r *http.Request) {
inventory, err := httpsvc.svc.GetDetectorsInventory()
if err != nil {
log.WithError(err).Warn("Error, could not retrieve detectors inventory list.")
}
httpsvc.ApiPrintClusterInformation(w, r, nil, inventory)
}
func (httpsvc *HttpService) ApiPrintClusterInformation(w http.ResponseWriter, r *http.Request, hosts []string, inventory map[string][]string) {
queryParam := mux.Vars(r)
format := ""
format = queryParam["format"]
if format == "" {
format = "text"
}
switch format {
case "json":
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
var result []byte
var err error
if hosts != nil {
result, err = json.MarshalIndent(hosts, "", "\t")
if err != nil {
log.WithError(err).Warn("Error, could not marshal hosts.")
}
} else if inventory != nil {
result, err = json.MarshalIndent(inventory, "", "\t")
if err != nil {
log.WithError(err).Warn("Error, could not marshal inventory.")
}
}
_, err = fmt.Fprintln(w, string(result))
if err != nil {
log.WithField("result", string(result)).
WithField(infologger.Level, infologger.IL_Support).
WithError(err).
Warn("Error, could not write a part of HTTP response to response writer.")
}
case "text":
fallthrough
default:
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
if hosts != nil {
for _, hostname := range hosts {
_, err := fmt.Fprintf(w, "%s\n", hostname)
if err != nil {
log.WithField(infologger.Level, infologger.IL_Support).
WithError(err).
Warn("Error, could not write a part of HTTP response to response writer.")
}
}
} else if inventory != nil {
for detector, flps := range inventory {
_, err := fmt.Fprintf(w, "%s\n", detector)
if err != nil {
log.WithField(infologger.Level, infologger.IL_Support).
WithError(err).
Warn("Error, could not write a part of HTTP response to response writer.")
}
for _, hostname := range flps {
_, err = fmt.Fprintf(w, "\t%s\n", hostname)
if err != nil {
log.WithField(infologger.Level, infologger.IL_Support).
WithError(err).
Warn("Error, could not write a part of HTTP response to response writer.")
}
}
}
}
}
}