Skip to content

Commit 4903a01

Browse files
committed
[apricot] consequently return Go data structures in Service
This aligns methods offered by Service to return []string instead of JSON string or space-separated values in a string, making it easier to handle internally. Translation to any particular format, be it JSON, is performed by the API code.
1 parent 61259bb commit 4903a01

File tree

7 files changed

+52
-32
lines changed

7 files changed

+52
-32
lines changed

apricot/cacheproxy/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ func (s Service) GetDetectorsForHosts(hosts []string) ([]string, error) {
133133
return detList, nil
134134
}
135135

136-
func (s Service) GetCRUCardsForHost(hostname string) (string, error) {
136+
func (s Service) GetCRUCardsForHost(hostname string) ([]string, error) {
137137
return s.base.GetCRUCardsForHost(hostname)
138138
}
139139

140-
func (s Service) GetEndpointsForCRUCard(hostname, cardSerial string) (string, error) {
140+
func (s Service) GetEndpointsForCRUCard(hostname, cardSerial string) ([]string, error) {
141141
return s.base.GetEndpointsForCRUCard(hostname, cardSerial)
142142
}
143143

apricot/local/service.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,15 @@ func (s *Service) GetDetectorsForHosts(hosts []string) ([]string, error) {
393393
return detectorSlice, nil
394394
}
395395

396-
func (s *Service) GetCRUCardsForHost(hostname string) (string, error) {
396+
func (s *Service) GetCRUCardsForHost(hostname string) ([]string, error) {
397397
s.logMethod()
398398

399399
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
400400
var cards map[string]Card
401401
var serials []string
402402
cfgCards, err := cSrc.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
403403
if err != nil {
404-
return "", err
404+
return nil, err
405405
}
406406
json.Unmarshal([]byte(cfgCards), &cards)
407407
unique := make(map[string]bool)
@@ -411,17 +411,13 @@ func (s *Service) GetCRUCardsForHost(hostname string) (string, error) {
411411
serials = append(serials, card.Serial)
412412
}
413413
}
414-
bytes, err := json.Marshal(serials)
415-
if err != nil {
416-
return "", err
417-
}
418-
return string(bytes), nil
414+
return serials, nil
419415
} else {
420-
return "", errors.New("runtime KV not supported with file backend")
416+
return nil, errors.New("runtime KV not supported with file backend")
421417
}
422418
}
423419

424-
func (s *Service) GetEndpointsForCRUCard(hostname, cardSerial string) (string, error) {
420+
func (s *Service) GetEndpointsForCRUCard(hostname, cardSerial string) ([]string, error) {
425421
s.logMethod()
426422

427423
log.WithPrefix("rpcserver").
@@ -433,23 +429,23 @@ func (s *Service) GetEndpointsForCRUCard(hostname, cardSerial string) (string, e
433429

434430
if cSrc, ok := s.src.(*cfgbackend.ConsulSource); ok {
435431
var cards map[string]Card
436-
var endpoints string
432+
var endpoints []string
437433
cfgCards, err := cSrc.Get(filepath.Join("o2/hardware", "flps", hostname, "cards"))
438434
if err != nil {
439-
return "", err
435+
return nil, err
440436
}
441437
err = json.Unmarshal([]byte(cfgCards), &cards)
442438
if err != nil {
443-
return "", err
439+
return nil, err
444440
}
445441
for _, card := range cards {
446442
if card.Serial == cardSerial {
447-
endpoints = endpoints + card.Endpoint + " "
443+
endpoints = append(endpoints, card.Endpoint)
448444
}
449445
}
450446
return endpoints, nil
451447
} else {
452-
return "", errors.New("runtime KV not supported with file backend")
448+
return nil, errors.New("runtime KV not supported with file backend")
453449
}
454450
}
455451

apricot/remote/server.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ package remote
2727

2828
import (
2929
"context"
30+
"encoding/json"
3031
"runtime"
32+
"strings"
3133

3234
apricotpb "github.com/AliceO2Group/Control/apricot/protos"
3335
"github.com/AliceO2Group/Control/common/logger"
@@ -253,7 +255,12 @@ func (m *RpcServer) GetCRUCardsForHost(_ context.Context, request *apricotpb.Hos
253255
if err != nil {
254256
return nil, err
255257
}
256-
return &apricotpb.CRUCardsResponse{Cards: cards}, E_OK.Err()
258+
cardsJson, err := json.Marshal(cards)
259+
if err != nil {
260+
return nil, err
261+
}
262+
263+
return &apricotpb.CRUCardsResponse{Cards: string(cardsJson)}, E_OK.Err()
257264
}
258265

259266
func (m *RpcServer) GetEndpointsForCRUCard(_ context.Context, request *apricotpb.CardRequest) (*apricotpb.CRUCardEndpointResponse, error) {
@@ -269,7 +276,9 @@ func (m *RpcServer) GetEndpointsForCRUCard(_ context.Context, request *apricotpb
269276
if err != nil {
270277
return nil, err
271278
}
272-
return &apricotpb.CRUCardEndpointResponse{Endpoints: endpoints}, E_OK.Err()
279+
endpointsSpaceSeparated := strings.Join(endpoints, " ")
280+
281+
return &apricotpb.CRUCardEndpointResponse{Endpoints: endpointsSpaceSeparated}, E_OK.Err()
273282
}
274283

275284
func (m *RpcServer) GetRuntimeEntry(_ context.Context, request *apricotpb.GetRuntimeEntryRequest) (*apricotpb.ComponentResponse, error) {

apricot/remote/service.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ package remote
2626

2727
import (
2828
"context"
29+
"encoding/json"
2930
"errors"
3031
"fmt"
3132
"net/url"
33+
"strings"
3234
"time"
3335

3436
apricotpb "github.com/AliceO2Group/Control/apricot/protos"
@@ -199,29 +201,37 @@ func (c *RemoteService) GetDetectorsForHosts(hosts []string) (payload []string,
199201

200202
}
201203

202-
func (c *RemoteService) GetCRUCardsForHost(hostname string) (cards string, err error) {
204+
func (c *RemoteService) GetCRUCardsForHost(hostname string) (cards []string, err error) {
203205
var response *apricotpb.CRUCardsResponse
204206
request := &apricotpb.HostRequest{
205207
Hostname: hostname,
206208
}
207209
response, err = c.cli.GetCRUCardsForHost(context.Background(), request, grpc.EmptyCallOption{})
208210
if err != nil {
209-
return "", err
211+
return nil, err
212+
}
213+
cardsStr := response.GetCards()
214+
err = json.Unmarshal([]byte(cardsStr), &cards)
215+
if err != nil {
216+
return nil, err
210217
}
211-
return response.GetCards(), nil
218+
219+
return cards, nil
212220
}
213221

214-
func (c *RemoteService) GetEndpointsForCRUCard(hostname, cardSerial string) (cards string, err error) {
222+
func (c *RemoteService) GetEndpointsForCRUCard(hostname, cardSerial string) (endpoints []string, err error) {
215223
var response *apricotpb.CRUCardEndpointResponse
216224
request := &apricotpb.CardRequest{
217225
Hostname: hostname,
218226
CardSerial: cardSerial,
219227
}
220228
response, err = c.cli.GetEndpointsForCRUCard(context.Background(), request, grpc.EmptyCallOption{})
221229
if err != nil {
222-
return "", err
230+
return nil, err
223231
}
224-
return response.GetEndpoints(), nil
232+
endpointsStr := response.GetEndpoints()
233+
endpoints = strings.Split(endpointsStr, " ")
234+
return endpoints, nil
225235
}
226236

227237
func (c *RemoteService) GetRuntimeEntry(component string, key string) (payload string, err error) {

configuration/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ type Service interface {
5757

5858
GetDetectorForHost(hostname string) (string, error)
5959
GetDetectorsForHosts(hosts []string) ([]string, error)
60-
GetCRUCardsForHost(hostname string) (string, error)
61-
GetEndpointsForCRUCard(hostname, cardSerial string) (string, error)
60+
GetCRUCardsForHost(hostname string) ([]string, error)
61+
GetEndpointsForCRUCard(hostname, cardSerial string) ([]string, error)
6262

6363
RawGetRecursive(path string) (string, error)
6464
}

configuration/template/fields.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ type ConfigurationService interface {
7171

7272
GetDetectorForHost(hostname string) (string, error)
7373
GetDetectorsForHosts(hosts []string) ([]string, error)
74-
GetCRUCardsForHost(hostname string) (string, error)
75-
GetEndpointsForCRUCard(hostname, cardSerial string) (string, error)
74+
GetCRUCardsForHost(hostname string) ([]string, error)
75+
GetEndpointsForCRUCard(hostname, cardSerial string) ([]string, error)
7676

7777
GetRuntimeEntry(component string, key string) (string, error)
7878
SetRuntimeEntry(component string, key string, value string) error

configuration/template/stackutil.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"encoding/json"
2929
"fmt"
3030
"github.com/AliceO2Group/Control/common/logger/infologger"
31+
"strings"
3132
texttemplate "text/template"
3233
"time"
3334

@@ -136,20 +137,24 @@ func detectorsForHosts(confSvc ConfigurationService, hosts string) string {
136137

137138
func cruCardsForHost(confSvc ConfigurationService, hostname string) string {
138139
defer utils.TimeTrack(time.Now(), "CRUCardsForHost", log.WithPrefix("template"))
139-
payload, err := confSvc.GetCRUCardsForHost(hostname)
140+
cards, err := confSvc.GetCRUCardsForHost(hostname)
140141
if err != nil {
141142
return fmt.Sprintf("[\"error: %s\"]", err.Error())
142143
}
143-
return payload
144+
cardsJson, err := json.Marshal(cards)
145+
if err != nil {
146+
return fmt.Sprintf("[\"error: %s\"]", err.Error())
147+
}
148+
return string(cardsJson)
144149
}
145150

146151
func endpointsForCruCard(confSvc ConfigurationService, hostname string, cardSerial string) string {
147152
defer utils.TimeTrack(time.Now(), "EndpointsForCRUCard", log.WithPrefix("template"))
148-
payload, err := confSvc.GetEndpointsForCRUCard(hostname, cardSerial)
153+
endpoints, err := confSvc.GetEndpointsForCRUCard(hostname, cardSerial)
149154
if err != nil {
150155
return fmt.Sprintf("{\"error\":\"%s\"}", err.Error())
151156
}
152-
return payload
157+
return strings.Join(endpoints, " ")
153158
}
154159

155160
func getRuntimeConfig(confSvc ConfigurationService, component string, key string) string {

0 commit comments

Comments
 (0)