[#189] sdk/netmap: Implement NodeState enum
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
53697081ce
commit
d4f5c27d47
2 changed files with 71 additions and 0 deletions
|
@ -23,6 +23,19 @@ type (
|
|||
Nodes []*Node
|
||||
)
|
||||
|
||||
// NodeState is an enumeration of various states of the NeoFS node.
|
||||
type NodeState uint32
|
||||
|
||||
const (
|
||||
_ NodeState = iota
|
||||
|
||||
// NodeStateOffline is network unavailable state.
|
||||
NodeStateOffline
|
||||
|
||||
// NodeStateOnline is an active state in the network.
|
||||
NodeStateOnline
|
||||
)
|
||||
|
||||
// Enumeration of well-known attributes.
|
||||
const (
|
||||
CapacityAttr = "Capacity"
|
||||
|
@ -98,3 +111,38 @@ func GetBucketWeight(ns Nodes, a aggregator, wf weightFunc) float64 {
|
|||
}
|
||||
return a.Compute()
|
||||
}
|
||||
|
||||
// NodeStateFromV2 converts v2 NodeState to NodeState.
|
||||
func NodeStateFromV2(s netmap.NodeState) NodeState {
|
||||
switch s {
|
||||
default:
|
||||
return 0
|
||||
case netmap.Online:
|
||||
return NodeStateOnline
|
||||
case netmap.Offline:
|
||||
return NodeStateOffline
|
||||
}
|
||||
}
|
||||
|
||||
// ToV2 converts NodeState to v2 NodeState.
|
||||
func (s NodeState) ToV2() netmap.NodeState {
|
||||
switch s {
|
||||
default:
|
||||
return netmap.UnspecifiedState
|
||||
case NodeStateOffline:
|
||||
return netmap.Offline
|
||||
case NodeStateOnline:
|
||||
return netmap.Online
|
||||
}
|
||||
}
|
||||
|
||||
func (s NodeState) String() string {
|
||||
switch s {
|
||||
default:
|
||||
return "UNSPECIFIED"
|
||||
case NodeStateOffline:
|
||||
return "OFFLINE"
|
||||
case NodeStateOnline:
|
||||
return "ONLINE"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,3 +19,26 @@ func TestNode_NetworkAddress(t *testing.T) {
|
|||
|
||||
require.Equal(t, addr, n.NetworkAddress())
|
||||
}
|
||||
|
||||
func TestNodeStateFromV2(t *testing.T) {
|
||||
for _, item := range []struct {
|
||||
s NodeState
|
||||
sV2 netmap.NodeState
|
||||
}{
|
||||
{
|
||||
s: 0,
|
||||
sV2: netmap.UnspecifiedState,
|
||||
},
|
||||
{
|
||||
s: NodeStateOnline,
|
||||
sV2: netmap.Online,
|
||||
},
|
||||
{
|
||||
s: NodeStateOffline,
|
||||
sV2: netmap.Offline,
|
||||
},
|
||||
} {
|
||||
require.Equal(t, item.s, NodeStateFromV2(item.sV2))
|
||||
require.Equal(t, item.sV2, item.s.ToV2())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue