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"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
func addWellKnownAttributes(attrs []*netmap.NodeAttribute) []*netmap.NodeAttribute {
|
2020-09-22 12:59:09 +00:00
|
|
|
var hasCapacity, hasPrice bool
|
|
|
|
|
|
|
|
// check if user defined capacity and price attributes
|
|
|
|
for i := range attrs {
|
2020-11-16 10:26:35 +00:00
|
|
|
if !hasPrice && attrs[i].Key() == netmap.PriceAttr {
|
2020-09-22 12:59:09 +00:00
|
|
|
hasPrice = true
|
2020-11-16 10:26:35 +00:00
|
|
|
} else if !hasCapacity && attrs[i].Key() == netmap.CapacityAttr {
|
2020-09-22 12:59:09 +00:00
|
|
|
hasCapacity = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not override user defined capacity and price attributes
|
|
|
|
|
|
|
|
if !hasCapacity {
|
2020-11-16 10:26:35 +00:00
|
|
|
capacity := netmap.NewNodeAttribute()
|
|
|
|
capacity.SetKey(netmap.CapacityAttr)
|
2020-09-22 12:59:09 +00:00
|
|
|
capacity.SetValue(strconv.FormatUint(defaultCapacity, 10))
|
|
|
|
attrs = append(attrs, capacity)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasPrice {
|
2020-11-16 10:26:35 +00:00
|
|
|
price := netmap.NewNodeAttribute()
|
|
|
|
price.SetKey(netmap.PriceAttr)
|
2020-09-22 12:59:09 +00:00
|
|
|
price.SetValue(strconv.FormatUint(defaultPrice, 10))
|
|
|
|
attrs = append(attrs, price)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
}
|