From 6347f846ebff0b1cc49b099d47939973681b0c8b Mon Sep 17 00:00:00 2001
From: Leonard Lyubich <leonard@nspcc.ru>
Date: Thu, 5 Nov 2020 14:39:50 +0300
Subject: [PATCH] [#189] sdk/netmap: Implement NodeAttribute type

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
---
 pkg/netmap/node_info.go      | 54 ++++++++++++++++++++++++++++++++++++
 pkg/netmap/node_info_test.go | 38 +++++++++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git a/pkg/netmap/node_info.go b/pkg/netmap/node_info.go
index 8db1cae..2c14de2 100644
--- a/pkg/netmap/node_info.go
+++ b/pkg/netmap/node_info.go
@@ -26,6 +26,9 @@ type (
 // NodeState is an enumeration of various states of the NeoFS node.
 type NodeState uint32
 
+// NodeAttribute represents v2 compatible attribute of the NeoFS Storage Node.
+type NodeAttribute netmap.Attribute
+
 const (
 	_ NodeState = iota
 
@@ -146,3 +149,54 @@ func (s NodeState) String() string {
 		return "ONLINE"
 	}
 }
+
+// NewNodeAttribute creates and returns new NodeAttribute instance.
+func NewNodeAttribute() *NodeAttribute {
+	return NewNodeAttributeFromV2(new(netmap.Attribute))
+}
+
+// NodeAttributeFromV2 converts v2 node Attribute to NodeAttribute.
+func NewNodeAttributeFromV2(a *netmap.Attribute) *NodeAttribute {
+	return (*NodeAttribute)(a)
+}
+
+// ToV2 converts NodeAttribute to v2 node Attribute.
+func (a *NodeAttribute) ToV2() *netmap.Attribute {
+	return (*netmap.Attribute)(a)
+}
+
+// Key returns key to the node attribute.
+func (a *NodeAttribute) Key() string {
+	return (*netmap.Attribute)(a).
+		GetKey()
+}
+
+// SetKey sets key to the node attribute.
+func (a *NodeAttribute) SetKey(key string) {
+	(*netmap.Attribute)(a).
+		SetKey(key)
+}
+
+// Value returns value of the node attribute.
+func (a *NodeAttribute) Value() string {
+	return (*netmap.Attribute)(a).
+		GetValue()
+}
+
+// SetValue sets value of the node attribute.
+func (a *NodeAttribute) SetValue(val string) {
+	(*netmap.Attribute)(a).
+		SetValue(val)
+}
+
+// ParentKeys returns list of parent keys.
+func (a *NodeAttribute) ParentKeys() []string {
+	return (*netmap.Attribute)(a).
+		GetParents()
+}
+
+// SetParentKeys sets list of parent keys.
+func (a *NodeAttribute) SetParentKeys(keys ...string) {
+	(*netmap.Attribute)(a).
+		SetParents(keys)
+}
diff --git a/pkg/netmap/node_info_test.go b/pkg/netmap/node_info_test.go
index 93d1568..abbc96f 100644
--- a/pkg/netmap/node_info_test.go
+++ b/pkg/netmap/node_info_test.go
@@ -42,3 +42,41 @@ func TestNodeStateFromV2(t *testing.T) {
 		require.Equal(t, item.sV2, item.s.ToV2())
 	}
 }
+
+func TestNodeAttributeFromV2(t *testing.T) {
+	aV2 := new(netmap.Attribute)
+	aV2.SetKey("key")
+	aV2.SetValue("value")
+	aV2.SetParents([]string{"par1", "par2"})
+
+	a := NewNodeAttributeFromV2(aV2)
+
+	require.Equal(t, aV2, a.ToV2())
+}
+
+func TestNodeAttribute_Key(t *testing.T) {
+	a := NewNodeAttribute()
+	key := "some key"
+
+	a.SetKey(key)
+
+	require.Equal(t, key, a.Key())
+}
+
+func TestNodeAttribute_Value(t *testing.T) {
+	a := NewNodeAttribute()
+	val := "some value"
+
+	a.SetValue(val)
+
+	require.Equal(t, val, a.Value())
+}
+
+func TestNodeAttribute_ParentKeys(t *testing.T) {
+	a := NewNodeAttribute()
+	keys := []string{"par1", "par2"}
+
+	a.SetParentKeys(keys...)
+
+	require.Equal(t, keys, a.ParentKeys())
+}