Skip to content

Commit f923583

Browse files
kostorrteo
authored andcommitted
[core] Update repo rpcs to return errors
1 parent 6f531f0 commit f923583

4 files changed

Lines changed: 47 additions & 67 deletions

File tree

coconut/control/control.go

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -657,17 +657,13 @@ func AddRepo(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Command, ar
657657
return err
658658
}
659659

660-
var response *pb.AddRepoReply
661-
response, err = rpc.AddRepo(cxt, &pb.AddRepoRequest{Name: args[0]}, grpc.EmptyCallOption{})
660+
_, err = rpc.AddRepo(cxt, &pb.AddRepoRequest{Name: args[0]}, grpc.EmptyCallOption{})
662661
if err != nil {
662+
fmt.Fprintln(o, "Cannot add repository.")
663663
return err
664664
}
665665

666-
if response.GetErrorString() == "" {
667-
fmt.Fprintln(o, "Repository succesfully added.")
668-
} else {
669-
fmt.Fprintln(o, "Cannot add repository:", response.GetErrorString())
670-
}
666+
fmt.Fprintln(o, "Repository succesfully added.")
671667

672668
return nil
673669
}
@@ -691,13 +687,9 @@ func RemoveRepo(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Command,
691687
}
692688

693689
newDefaultRepo := response.GetNewDefaultRepo()
694-
if response.GetOk() {
695-
fmt.Fprintln(o, "Repository removed succsefully")
696-
if newDefaultRepo != "" {
697-
fmt.Fprintln(o, "New default repo is: " + newDefaultRepo)
698-
}
699-
} else {
700-
fmt.Fprintln(o, "Repository not found")
690+
fmt.Fprintln(o, "Repository removed succsefully.")
691+
if newDefaultRepo != "" {
692+
fmt.Fprintln(o, "New default repo is: " + newDefaultRepo)
701693
}
702694

703695
return nil
@@ -710,28 +702,23 @@ func RefreshRepos(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Comman
710702
return err
711703
}
712704

713-
var response *pb.RefreshReposReply
714705
if len(args) == 0 {
715-
response, err = rpc.RefreshRepos(cxt, &pb.RefreshReposRequest{Index: -1}, grpc.EmptyCallOption{})
706+
_, err = rpc.RefreshRepos(cxt, &pb.RefreshReposRequest{Index: -1}, grpc.EmptyCallOption{})
716707
} else if len(args) == 1 {
717708
index, _ := strconv.ParseInt(args[0], 10, 32)
718709

719-
response, err = rpc.RefreshRepos(cxt, &pb.RefreshReposRequest{Index: int32(index)}, grpc.EmptyCallOption{})
710+
_, err = rpc.RefreshRepos(cxt, &pb.RefreshReposRequest{Index: int32(index)}, grpc.EmptyCallOption{})
720711
}
721712

722713
if err != nil {
714+
fmt.Fprintln(o, "Repository refresh operation failed.")
723715
return err
724716
}
725717

726-
errorString := response.GetErrorString()
727-
if errorString == "" {
728-
if len(args) == 0 {
729-
fmt.Fprintln(o, "Repositories refreshed succesfully")
730-
} else {
731-
fmt.Fprintln(o, "Repository refreshed succesfully")
732-
}
718+
if len(args) == 0 {
719+
fmt.Fprintln(o, "Repositories refreshed succesfully")
733720
} else {
734-
fmt.Fprintln(o, "Repository refresh operation failed:", errorString)
721+
fmt.Fprintln(o, "Repository refreshed succesfully")
735722
}
736723

737724
return nil
@@ -748,18 +735,13 @@ func SetDefaultRepo(cxt context.Context, rpc *coconut.RpcClient, cmd *cobra.Comm
748735

749736
index, _ := strconv.ParseInt(args[0], 10, 32)
750737

751-
var response *pb.SetDefaultRepoReply
752-
response, err = rpc.SetDefaultRepo(cxt, &pb.SetDefaultRepoRequest{Index: int32(index)}, grpc.EmptyCallOption{})
738+
_, err = rpc.SetDefaultRepo(cxt, &pb.SetDefaultRepoRequest{Index: int32(index)}, grpc.EmptyCallOption{})
753739
if err != nil {
740+
fmt.Fprintln(o, "Operation failed.")
754741
return err
755742
}
756743

757-
errorString := response.GetErrorString()
758-
if errorString == "" {
759-
fmt.Fprintln(o, "Default repository updated succesfully")
760-
} else {
761-
fmt.Fprintln(o, "Operation failed:", errorString)
762-
}
744+
fmt.Fprintln(o, "Default repository update succesfully")
763745

764746
return nil
765747
}

core/protos/o2control.proto

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ service Control {
5757
rpc GetWorkflowTemplates (GetWorkflowTemplatesRequest) returns (GetWorkflowTemplatesReply) {}
5858

5959
rpc ListRepos(ListReposRequest) returns (ListReposReply) {}
60-
rpc AddRepo(AddRepoRequest) returns (AddRepoReply) {}
60+
rpc AddRepo(AddRepoRequest) returns (Empty) {}
6161
rpc RemoveRepo(RemoveRepoRequest) returns (RemoveRepoReply) {}
62-
rpc RefreshRepos(RefreshReposRequest) returns (RefreshReposReply) {}
63-
rpc SetDefaultRepo(SetDefaultRepoRequest) returns (SetDefaultRepoReply) {}
62+
rpc RefreshRepos(RefreshReposRequest) returns (Empty) {}
63+
rpc SetDefaultRepo(SetDefaultRepoRequest) returns (Empty) {}
6464
}
6565

6666
////////////////////////////////////////
@@ -312,31 +312,22 @@ message AddRepoRequest {
312312
string name = 1;
313313
}
314314

315-
message AddRepoReply {
316-
string errorString = 1;
317-
}
318-
319315
message RemoveRepoRequest {
320316
int32 index = 1;
321317
}
322318

323319
message RemoveRepoReply {
324-
bool ok = 1;
325-
string newDefaultRepo = 2;
320+
string newDefaultRepo = 1;
326321
}
327322

328323
message RefreshReposRequest {
329324
int32 index = 1;
330325
}
331326

332-
message RefreshReposReply {
333-
string errorString = 1;
334-
}
335-
336327
message SetDefaultRepoRequest {
337328
int32 index = 1;
338329
}
339330

340-
message SetDefaultRepoReply {
341-
string errorString = 1;
331+
message Empty {
332+
342333
}

core/repos/repomanager.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,15 @@ func (manager *RepoManager) getRepoByIndex(index int) (*Repo, error) {
236236
}
237237
}
238238

239-
func (manager *RepoManager) RemoveRepoByIndex(index int) (bool, string) {
239+
func (manager *RepoManager) RemoveRepoByIndex(index int) (string, error) {
240240
manager.mutex.Lock()
241241
defer manager.mutex.Unlock()
242242

243243
newDefaultRepoString := ""
244244

245245
repo, err := manager.getRepoByIndex(index)
246246
if err != nil {
247-
return false, ""
247+
return "", err
248248
}
249249

250250
wasDefault := repo.Default
@@ -256,7 +256,7 @@ func (manager *RepoManager) RemoveRepoByIndex(index int) (bool, string) {
256256
if wasDefault && len(manager.repoList) > 0 {
257257
newDefaultRepo, err := manager.getRepoByIndex(0)
258258
if err != nil {
259-
return false, newDefaultRepoString
259+
return newDefaultRepoString, err
260260
}
261261
manager.setDefaultRepo(newDefaultRepo)
262262
newDefaultRepoString = newDefaultRepo.GetIdentifier()
@@ -266,7 +266,7 @@ func (manager *RepoManager) RemoveRepoByIndex(index int) (bool, string) {
266266
log.Warning("Failed to update default_repo backend")
267267
}
268268
}
269-
return true, newDefaultRepoString
269+
return newDefaultRepoString, nil
270270
}
271271

272272
func (manager *RepoManager) RefreshRepos() error {
@@ -372,7 +372,11 @@ func (manager *RepoManager) setDefaultRepo(repo *Repo) {
372372
}
373373

374374
func (manager *RepoManager) UpdateDefaultRepoByIndex(index int) error {
375-
newDefaultRepo := manager.repoList[manager.GetOrderedRepolistKeys()[index]]
375+
orderedRepoList := manager.GetOrderedRepolistKeys()
376+
if index >= len(orderedRepoList) {
377+
return errors.New("Default repo index out of range")
378+
}
379+
newDefaultRepo := manager.repoList[orderedRepoList[index]]
376380
if newDefaultRepo == nil {
377381
return errors.New("Repo not found")
378382
} else if newDefaultRepo == manager.defaultRepo {

core/server.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -530,20 +530,19 @@ func (m *RpcServer) ListRepos(cxt context.Context, req *pb.ListReposRequest) (*p
530530
return &pb.ListReposReply{Repos: repoInfos}, nil
531531
}
532532

533-
func (m *RpcServer) AddRepo(cxt context.Context, req *pb.AddRepoRequest) (*pb.AddRepoReply, error) {
533+
func (m *RpcServer) AddRepo(cxt context.Context, req *pb.AddRepoRequest) (*pb.Empty, error) {
534534
m.logMethod()
535535

536536
if req == nil {
537537
return nil, status.New(codes.InvalidArgument, "received nil request").Err()
538538
}
539539

540540
err := the.RepoManager().AddRepo(req.Name)
541-
if err == nil { //new Repo -> refresh
542-
return &pb.AddRepoReply{ErrorString: "" }, nil
543-
} else {
544-
return &pb.AddRepoReply{ErrorString: err.Error() }, nil
541+
if err != nil {
542+
return nil, err
545543
}
546544

545+
return &pb.Empty{}, nil
547546
}
548547

549548
func (m *RpcServer) RemoveRepo(cxt context.Context, req *pb.RemoveRepoRequest) (*pb.RemoveRepoReply, error) {
@@ -553,12 +552,16 @@ func (m *RpcServer) RemoveRepo(cxt context.Context, req *pb.RemoveRepoRequest) (
553552
return nil, status.New(codes.InvalidArgument, "received nil request").Err()
554553
}
555554

556-
ok, newDefaultRepo := the.RepoManager().RemoveRepoByIndex(int(req.Index))
555+
newDefaultRepo, err := the.RepoManager().RemoveRepoByIndex(int(req.Index))
557556

558-
return &pb.RemoveRepoReply{Ok: ok, NewDefaultRepo: newDefaultRepo}, nil
557+
if err != nil {
558+
return nil, err
559+
}
560+
561+
return &pb.RemoveRepoReply{NewDefaultRepo: newDefaultRepo}, nil
559562
}
560563

561-
func (m *RpcServer) RefreshRepos(cxt context.Context, req *pb.RefreshReposRequest) (*pb.RefreshReposReply, error) {
564+
func (m *RpcServer) RefreshRepos(cxt context.Context, req *pb.RefreshReposRequest) (*pb.Empty, error) {
562565
m.logMethod()
563566

564567
if req == nil {
@@ -572,13 +575,13 @@ func (m *RpcServer) RefreshRepos(cxt context.Context, req *pb.RefreshReposReques
572575
err = the.RepoManager().RefreshRepoByIndex(int(req.Index))
573576
}
574577
if err != nil {
575-
return &pb.RefreshReposReply{ErrorString: err.Error()}, nil
578+
return nil, err
576579
}
577580

578-
return &pb.RefreshReposReply{ErrorString: ""}, nil
581+
return &pb.Empty{}, nil
579582
}
580583

581-
func (m *RpcServer) SetDefaultRepo(cxt context.Context, req *pb.SetDefaultRepoRequest) (*pb.SetDefaultRepoReply, error) {
584+
func (m *RpcServer) SetDefaultRepo(cxt context.Context, req *pb.SetDefaultRepoRequest) (*pb.Empty, error) {
582585
m.logMethod()
583586

584587
if req == nil {
@@ -587,8 +590,8 @@ func (m *RpcServer) SetDefaultRepo(cxt context.Context, req *pb.SetDefaultRepoRe
587590

588591
err := the.RepoManager().UpdateDefaultRepoByIndex(int(req.Index))
589592
if err != nil {
590-
return &pb.SetDefaultRepoReply{ErrorString: err.Error()}, nil
591-
} else {
592-
return &pb.SetDefaultRepoReply{ErrorString: ""}, nil
593+
return nil, err
593594
}
595+
596+
return &pb.Empty{}, nil
594597
}

0 commit comments

Comments
 (0)