2020-09-22 12:59:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-09-22 12:59:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
|
2021-02-10 10:45:15 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-09-22 12:59:09 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// list of default values for well-known attributes
|
|
|
|
defaultCapacity = 0
|
|
|
|
defaultPrice = 0
|
|
|
|
)
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
func parseAttributes(v *viper.Viper) []*netmap.NodeAttribute {
|
2020-09-22 12:59:09 +00:00
|
|
|
stringAttributes := readAttributes(v)
|
|
|
|
|
|
|
|
attrs, err := attributes.ParseV2Attributes(stringAttributes, nil)
|
|
|
|
if err != nil {
|
|
|
|
fatalOnErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addWellKnownAttributes(attrs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readAttributes(v *viper.Viper) (attrs []string) {
|
|
|
|
const maxAttributes = 100
|
|
|
|
|
|
|
|
for i := 0; i < maxAttributes; i++ {
|
|
|
|
attr := v.GetString(cfgNodeAttributePrefix + "_" + strconv.Itoa(i))
|
|
|
|
if attr == "" {
|
|
|
|
return
|
|
|
|
}
|
2021-01-11 15:21:06 +00:00
|
|
|
|
|
|
|
attrs = append(attrs, attr)
|
2020-09-22 12:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 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 {
|
|
|
|
fatalOnErr(errors.Errorf("missing explicit value of required node attribute %s", key))
|
|
|
|
}
|
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
|
|
|
|
}
|