[#344] netmap: Support ExternalAddr well-known attribute

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-09-24 13:37:08 +03:00 committed by fyrchik
parent 8e3173eacd
commit c6576c8112
2 changed files with 38 additions and 0 deletions

View file

@ -371,8 +371,32 @@ const (
// attrCapacity is a key to the node attribute that indicates the
// total available disk space in Gigabytes.
attrCapacity = "Capacity"
// attrExternalAddr is a key for the attribute storing node external addresses.
attrExternalAddr = "ExternalAddr"
// sepExternalAddr is a separator for multi-value ExternalAddr attribute.
sepExternalAddr = ","
)
// SetExternalAddresses sets multi-addresses to use
// to connect to this node from outside.
//
// Panics if addr is an empty list.
func (x *NodeInfo) SetExternalAddresses(addr ...string) {
x.SetAttribute(attrExternalAddr, strings.Join(addr, sepExternalAddr))
}
// ExternalAddresses returns list of multi-addresses to use
// to connect to this node from outside.
func (x NodeInfo) ExternalAddresses() []string {
a := x.Attribute(attrExternalAddr)
if len(a) == 0 {
return nil
}
return strings.Split(a, sepExternalAddr)
}
// NumberOfAttributes returns number of attributes announced by the node.
//
// See also SetAttribute.

View file

@ -45,3 +45,17 @@ func TestNodeInfo_Status(t *testing.T) {
require.False(t, n.IsOnline())
require.False(t, n.IsOffline())
}
func TestNodeInfo_ExternalAddr(t *testing.T) {
var n NodeInfo
require.Empty(t, n.ExternalAddresses())
require.Panics(t, func() { n.SetExternalAddresses() })
addr := []string{"1", "2", "3"}
n.SetExternalAddresses(addr[0])
require.Equal(t, addr[:1], n.ExternalAddresses())
n.SetExternalAddresses(addr[1:]...)
require.Equal(t, addr[1:], n.ExternalAddresses())
}