added failsafe

This commit is contained in:
ston1th 2020-06-02 00:29:10 +02:00
commit 6d4e4c31f3

16
node.go
View file

@ -8,7 +8,11 @@ import (
"fmt"
)
var ErrServerNotFound = errors.New("server not found")
var (
ErrServerNotFound = errors.New("server not found")
ErrNodesOffline = errors.New("one or more nodes are offline")
ErrNodesNotSearched = errors.New("one or more nodes could not be searched")
)
type NodeStatus string
@ -59,12 +63,16 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
if err != nil {
return
}
nodeOffline := false
nodeNotSearched := false
for _, node := range nl {
if node.Status != NodeStatusOnline {
nodeOffline = true
continue
}
sl, err := c.ListServers(ctx, node)
if err != nil {
nodeNotSearched = true
continue
}
for _, s := range sl {
@ -76,6 +84,12 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
}
}
}
if nodeOffline {
return nil, ErrNodesOffline
}
if nodeNotSearched {
return nil, ErrNodesNotSearched
}
return nil, ErrServerNotFound
}