-
Notifications
You must be signed in to change notification settings - Fork 5
/
response.go
806 lines (747 loc) · 22.5 KB
/
response.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
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// MIT License
//
// (C) Copyright [2018-2023] Hewlett Packard Enterprise Development LP
//
// 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 main
import (
"encoding/json"
"fmt"
"net/http"
base "github.com/Cray-HPE/hms-base"
"github.com/Cray-HPE/hms-smd/v2/pkg/sm"
)
type Response struct {
C int `json:"code"`
M string `json:"message"`
}
// Send a JSON error or message. If the ecode indicates an error, send
// a properly formatted RFC7807 problem.
// If it does not, fall back to the original CAPMC format, which will
// now just be used for success cases or odd HTTP status codes that
// don't suggest a RFC7807 problem response.
// We use the 7807 problem format for 4xx and 5xx errors, though
// in practice the latter (server errors) will probably not be used here
// as they do not describe invalid requests but server-specific issues.
func sendJsonError(w http.ResponseWriter, ecode int, message string) {
if ecode < 400 {
sendJsonResponse(w, ecode, message)
} else {
// Use library function in HMS base. Problem will be
// a generic one with title matching the HTTP Status code text
// with message as the details field. For this type of problem
// title can just be set to "about:blank" so we need no
// custom URL. The optional instance field is omitted as well
// so no URL/URI is needed there either.
base.SendProblemDetailsGeneric(w, ecode, message)
}
}
// Send a simple message for cases where need a non-error response. If
// a more feature filled message needs to be returned then do it with a
// different function. Code is the http status response, converted to
// zero for success-related responses.
func sendJsonResponse(w http.ResponseWriter, ecode int, message string) {
// if the HTTP call was a success then put zero in the returned json
// error field. This is what capmc does.
http_code := ecode
if ecode >= 200 && ecode <= 299 {
ecode = 0
}
data := Response{ecode, message}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if http_code != http.StatusNoContent {
err := json.NewEncoder(w).Encode(data)
if err != nil {
fmt.Printf("Yikes, I couldn't encode a JSON status response: %s\n", err)
}
}
}
// If an HMS-created error, return it as a bad status request. If another
// type, just print a generic failed to query DB error message.
func sendJsonDBError(w http.ResponseWriter, prefix, internalErr string, err error) {
if base.IsHMSError(err) {
sendJsonError(w, http.StatusBadRequest, prefix+err.Error())
} else {
if internalErr != "" {
sendJsonError(w, http.StatusInternalServerError, internalErr)
} else {
sendJsonError(w, http.StatusInternalServerError,
"failed to query DB.")
}
}
}
// When creating one or more resources, we want return 201 Created along with
// an array of links to the newly-created resources. The location header
// should also be set to the new resource (if just one) or to the affected
// collection.
func sendJsonNewResourceIDArray(w http.ResponseWriter, collectionURI string,
uris []*sm.ResourceURI) {
http_code := 201
w.Header().Set("Content-Type", "application/json")
numUris := len(uris)
if numUris == 0 {
http_code = 204
} else if numUris == 1 {
// Set Location to most specific component affected. If only
// a single new resource is created, use its URI
w.Header().Set("Location", uris[0].URI)
} else {
// Otherwise, set it to the collection.
w.Header().Set("Location", collectionURI)
}
w.WriteHeader(http_code)
if numUris != 0 {
err := json.NewEncoder(w).Encode(uris)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// When creating a single resource, we want return 201 Created along with
// a link to the newly-created resource. The location header
// should also be set to the new resource.
func sendJsonNewResourceID(w http.ResponseWriter, uri *sm.ResourceURI) {
http_code := 201
w.Header().Set("Content-Type", "application/json")
if uri == nil {
http_code = 204
} else {
// Set Location to the newly-created object's URI
w.Header().Set("Location", uri.URI)
}
w.WriteHeader(http_code)
if uri != nil {
err := json.NewEncoder(w).Encode(uri)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Return a link to a single resource that is not new.
func sendJsonResourceID(w http.ResponseWriter, uri *sm.ResourceURI) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
if uri == nil {
http_code = 204
}
w.WriteHeader(http_code)
if uri != nil {
err := json.NewEncoder(w).Encode(uri)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Return a link to an array of resources that are not new.
func sendJsonResourceIDArray(w http.ResponseWriter, uris []*sm.ResourceURI) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
if len(uris) == 0 {
http_code = 204
}
w.WriteHeader(http_code)
if len(uris) != 0 {
err := json.NewEncoder(w).Encode(uris)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// HMS Component response
func sendJsonCompRsp(w http.ResponseWriter, comp *base.Component) {
http_code := 200
if comp == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if comp != nil {
err := json.NewEncoder(w).Encode(comp)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Named HMS Component array response (i.e. GET on HMS Components collection)
func sendJsonCompArrayRsp(w http.ResponseWriter, comps *base.ComponentArray) {
http_code := 200
if comps == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if comps != nil {
err := json.NewEncoder(w).Encode(comps)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Node NID Mapping response, single entry
func sendJsonNodeMapRsp(w http.ResponseWriter, m *sm.NodeMap) {
http_code := 200
if m == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if m != nil {
err := json.NewEncoder(w).Encode(m)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Named NodeMaps array response (i.e. GET on NodeMaps collection)
func sendJsonNodeMapArrayRsp(
w http.ResponseWriter,
nnms *sm.NodeMapArray) {
http_code := 200
if nnms == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if nnms != nil {
err := json.NewEncoder(w).Encode(nnms)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Hardware inventory by Location, single entry, with FRU data if populated
func sendJsonHWInvByLocRsp(w http.ResponseWriter, hl *sm.HWInvByLoc) {
http_code := 200
if hl == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if hl != nil {
err := json.NewEncoder(w).Encode(hl)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Hardware inventory by Location, multiple entry, with FRU data if populated
func sendJsonHWInvByLocsRsp(w http.ResponseWriter, hl []*sm.HWInvByLoc) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if hl != nil {
err := json.NewEncoder(w).Encode(hl)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Hardware inventory by FRU, single entry, by FRU ID
func sendJsonHWInvByFRURsp(w http.ResponseWriter, hf *sm.HWInvByFRU) {
http_code := 200
if hf == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if hf != nil {
err := json.NewEncoder(w).Encode(hf)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Hardware inventory by FRU ID, multiple entries
func sendJsonHWInvByFRUsRsp(w http.ResponseWriter, hf []*sm.HWInvByFRU) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if hf != nil {
err := json.NewEncoder(w).Encode(hf)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// System Hardware Inventory - Ordered collection of system hardware.
func sendJsonSystemHWInvRsp(w http.ResponseWriter, hw *sm.SystemHWInventory) {
http_code := 200
if hw == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if hw != nil {
err := json.NewEncoder(w).Encode(hw)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Hardware inventory history of a single component(locational xname or FRU ID).
func sendJsonHWInvHistRsp(w http.ResponseWriter, hh *sm.HWInvHistArray) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if hh != nil {
err := json.NewEncoder(w).Encode(hh)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Hardware inventory history ordered by locational xname or FRU ID.
func sendJsonHWInvHistArrayRsp(w http.ResponseWriter, hhs *sm.HWInvHistResp) {
http_code := 200
if hhs == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if hhs != nil {
err := json.NewEncoder(w).Encode(hhs)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Individual RedfishEndpoint response, matching a single xname ID.
func sendJsonRFEndpointRsp(w http.ResponseWriter, ep *sm.RedfishEndpoint) {
http_code := 200
if ep == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if ep != nil {
err := json.NewEncoder(w).Encode(ep)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// RedfishEndpoint named array response, e.g. GET on RedfishEndpoints
// collection rather than an individual named entry.
func sendJsonRFEndpointArrayRsp(w http.ResponseWriter, eps *sm.RedfishEndpointArray) {
http_code := 200
if eps == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if eps != nil {
err := json.NewEncoder(w).Encode(eps)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Individual ComponentEndpoint response, matching a single xname ID. These
// are subcomponents of a RedfishEndpoint created during discovery.
func sendJsonCompEndpointRsp(w http.ResponseWriter, cep *sm.ComponentEndpoint) {
http_code := 200
if cep == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if cep != nil {
err := json.NewEncoder(w).Encode(cep)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// ComponentEndpoint named array response, e.g. GET on RedfishEndpoints
// collection rather than an individual named entry.
func sendJsonCompEndpointArrayRsp(w http.ResponseWriter, ceps *sm.ComponentEndpointArray) {
http_code := 200
if ceps == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if ceps != nil {
err := json.NewEncoder(w).Encode(ceps)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Individual ServiceEndpoint response, matching a single service type and
// xname ID. These are subcomponents of a RedfishEndpoint created during
// discovery.
func sendJsonServiceEndpointRsp(w http.ResponseWriter, sep *sm.ServiceEndpoint) {
http_code := 200
if sep == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if sep != nil {
err := json.NewEncoder(w).Encode(sep)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// ServiceEndpoint named array response, e.g. GET on ServiceEndpoints
// collection rather than an individual named entry.
func sendJsonServiceEndpointArrayRsp(w http.ResponseWriter, seps *sm.ServiceEndpointArray) {
http_code := 200
if seps == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if seps != nil {
err := json.NewEncoder(w).Encode(seps)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Individual CompEthInterface response, matching a single ID.
func sendJsonCompEthInterfaceRsp(w http.ResponseWriter, cei *sm.CompEthInterface) {
http_code := 200
if cei == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if cei != nil {
err := json.NewEncoder(w).Encode(cei)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of component ethernet interfaces
func sendJsonCompEthInterfaceArrayRsp(w http.ResponseWriter, ceis []*sm.CompEthInterface) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
err := json.NewEncoder(w).Encode(ceis)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
// Individual CompEthInterface V2 response, matching a single ID.
func sendJsonCompEthInterfaceV2Rsp(w http.ResponseWriter, cei *sm.CompEthInterfaceV2) {
http_code := 200
if cei == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if cei != nil {
err := json.NewEncoder(w).Encode(cei)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of component ethernet interfaces V2
func sendJsonCompEthInterfaceV2ArrayRsp(w http.ResponseWriter, ceis []*sm.CompEthInterfaceV2) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
err := json.NewEncoder(w).Encode(ceis)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
// Individual CompEthInterface response, matching a single ID.
func sendJsonCompEthInterfaceIPAddressMappingsRsp(w http.ResponseWriter, ipm *sm.IPAddressMapping) {
http_code := 200
if ipm == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if ipm != nil {
err := json.NewEncoder(w).Encode(ipm)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of IPAddress mappings for a component ethernet interface
func sendJsonCompEthInterfaceIPAddressMappingsArrayRsp(w http.ResponseWriter, ceis []sm.IPAddressMapping) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
err := json.NewEncoder(w).Encode(ceis)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
// Single DiscoveryStatus
func sendJsonDiscoveryStatusRsp(w http.ResponseWriter, stat *sm.DiscoveryStatus) {
http_code := 200
if stat == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if stat != nil {
err := json.NewEncoder(w).Encode(stat)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of DiscoveryStatus
func sendJsonDiscoveryStatusArrayRsp(w http.ResponseWriter, stats []*sm.DiscoveryStatus) {
http_code := 200
if len(stats) == 0 {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if len(stats) != 0 {
err := json.NewEncoder(w).Encode(stats)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of SCN subscriptions
func sendJsonSCNSubscriptionArrayRsp(w http.ResponseWriter, subs *sm.SCNSubscriptionArray) {
http_code := 200
if subs == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if subs != nil {
err := json.NewEncoder(w).Encode(subs)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// A SCN subscription
func sendJsonSCNSubscriptionRsp(w http.ResponseWriter, sub *sm.SCNSubscription) {
http_code := 200
if sub == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if sub != nil {
err := json.NewEncoder(w).Encode(sub)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of groups
func sendJsonGroupArrayRsp(w http.ResponseWriter, groups *[]sm.Group) {
http_code := 200
if groups == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if groups != nil {
err := json.NewEncoder(w).Encode(groups)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// A group
func sendJsonGroupRsp(w http.ResponseWriter, group *sm.Group) {
http_code := 200
if group == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if group != nil {
err := json.NewEncoder(w).Encode(group)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of strings
func sendJsonStringArrayRsp(w http.ResponseWriter, strs *[]string) {
http_code := 200
if strs == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if strs != nil {
err := json.NewEncoder(w).Encode(strs)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of group members
func sendJsonMembersRsp(w http.ResponseWriter, members *sm.Members) {
http_code := 200
if members == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if members != nil {
err := json.NewEncoder(w).Encode(members)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of partitions
func sendJsonPartitionArrayRsp(w http.ResponseWriter, parts *[]sm.Partition) {
http_code := 200
if parts == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if parts != nil {
err := json.NewEncoder(w).Encode(parts)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// A partition
func sendJsonPartitionRsp(w http.ResponseWriter, part *sm.Partition) {
http_code := 200
if part == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if part != nil {
err := json.NewEncoder(w).Encode(part)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of memberships
func sendJsonMembershipArrayRsp(w http.ResponseWriter, memberships []*sm.Membership) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
err := json.NewEncoder(w).Encode(memberships)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
// A membership
func sendJsonMembershipRsp(w http.ResponseWriter, membership *sm.Membership) {
http_code := 200
if membership == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if membership != nil {
err := json.NewEncoder(w).Encode(membership)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Array of v2 component locks
func sendJsonCompLockV2Rsp(w http.ResponseWriter, cls sm.CompLockV2Status) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
err := json.NewEncoder(w).Encode(cls)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
// Array of v2 component lock update results
func sendJsonCompLockV2UpdateRsp(w http.ResponseWriter, clu sm.CompLockV2UpdateResult) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
err := json.NewEncoder(w).Encode(clu)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
// Array of v2 component reservations
func sendJsonCompReservationRsp(w http.ResponseWriter, crs sm.CompLockV2ReservationResult) {
http_code := 200
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
err := json.NewEncoder(w).Encode(crs)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
// Power Mapping response, single entry
func sendJsonPowerMapRsp(w http.ResponseWriter, m *sm.PowerMap) {
http_code := 200
if m == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if m != nil {
err := json.NewEncoder(w).Encode(m)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// Named PowerMaps array response (i.e. GET on PowerMaps collection)
func sendJsonPowerMapArrayRsp(
w http.ResponseWriter,
ms []*sm.PowerMap) {
http_code := 200
if ms == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if ms != nil {
err := json.NewEncoder(w).Encode(ms)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}
// A list of HMS enum values
func sendJsonValueRsp(w http.ResponseWriter, vals *HMSValues) {
http_code := 200
if vals == nil {
http_code = 204
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http_code)
if vals != nil {
err := json.NewEncoder(w).Encode(vals)
if err != nil {
fmt.Printf("Couldn't encode a JSON command response: %s\n", err)
}
}
}