2020-09-22 12:59:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-22 12:59:09 +00:00
|
|
|
"strconv"
|
|
|
|
|
2021-06-01 17:50:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
|
|
|
nodeconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/node"
|
2020-09-22 12:59:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2020-09-22 12:59:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// list of default values for well-known attributes
|
|
|
|
defaultCapacity = 0
|
|
|
|
defaultPrice = 0
|
|
|
|
)
|
|
|
|
|
2021-06-01 17:50:46 +00:00
|
|
|
func parseAttributes(c *config.Config) []*netmap.NodeAttribute {
|
2022-03-01 07:52:24 +00:00
|
|
|
if nodeconfig.Relay(c) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:50:46 +00:00
|
|
|
stringAttributes := nodeconfig.Attributes(c)
|
2020-09-22 12:59:09 +00:00
|
|
|
|
|
|
|
attrs, err := attributes.ParseV2Attributes(stringAttributes, nil)
|
|
|
|
if err != nil {
|
|
|
|
fatalOnErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addWellKnownAttributes(attrs)
|
|
|
|
}
|
|
|
|
|
2021-02-10 10:45:15 +00:00
|
|
|
type wellKnownNodeAttrDesc struct {
|
|
|
|
explicit bool
|
|
|
|
defaultVal string
|
|
|
|
}
|
|
|
|
|
|
|
|
func listWellKnownAttrDesc() map[string]wellKnownNodeAttrDesc {
|
|
|
|
return map[string]wellKnownNodeAttrDesc{
|
2021-02-10 20:41:32 +00:00
|
|
|
netmap.AttrPrice: {defaultVal: strconv.FormatUint(defaultPrice, 10)},
|
|
|
|
netmap.AttrCapacity: {defaultVal: strconv.FormatUint(defaultCapacity, 10)},
|
2021-02-10 20:56:14 +00:00
|
|
|
netmap.AttrUNLOCODE: {explicit: true},
|
2021-02-10 10:45:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
func addWellKnownAttributes(attrs []*netmap.NodeAttribute) []*netmap.NodeAttribute {
|
2021-02-10 10:45:15 +00:00
|
|
|
mWellKnown := listWellKnownAttrDesc()
|
2020-09-22 12:59:09 +00:00
|
|
|
|
2021-02-10 10:45:15 +00:00
|
|
|
// check how user defined well-known attributes
|
2020-09-22 12:59:09 +00:00
|
|
|
for i := range attrs {
|
2021-02-10 10:45:15 +00:00
|
|
|
delete(mWellKnown, attrs[i].Key())
|
2020-09-22 12:59:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 10:45:15 +00:00
|
|
|
for key, desc := range mWellKnown {
|
|
|
|
// check if required attribute is set
|
|
|
|
if desc.explicit {
|
2021-05-18 08:12:51 +00:00
|
|
|
fatalOnErr(fmt.Errorf("missing explicit value of required node attribute %s", key))
|
2021-02-10 10:45:15 +00:00
|
|
|
}
|
2020-09-22 12:59:09 +00:00
|
|
|
|
2021-02-10 10:45:15 +00:00
|
|
|
// set default value of the attribute
|
|
|
|
a := netmap.NewNodeAttribute()
|
|
|
|
a.SetKey(key)
|
|
|
|
a.SetValue(desc.defaultVal)
|
2020-09-22 12:59:09 +00:00
|
|
|
|
2021-02-10 10:45:15 +00:00
|
|
|
attrs = append(attrs, a)
|
2020-09-22 12:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|