forked from TrueCloudLab/frostfs-node
[#316] cmd/node: Refactor working with well-known node attributes
Split the attributes into those that must be explicitly set in the configuration, and those that, if absent, will be assigned a default value. Support this logic in `addWellKnownAttributes` function. If no explicit attribute is set, the application will panic. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
19681693f7
commit
7a9b02049f
1 changed files with 27 additions and 20 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
||||||
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
|
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,32 +41,38 @@ func readAttributes(v *viper.Viper) (attrs []string) {
|
||||||
return attrs
|
return attrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type wellKnownNodeAttrDesc struct {
|
||||||
|
explicit bool
|
||||||
|
defaultVal string
|
||||||
|
}
|
||||||
|
|
||||||
|
func listWellKnownAttrDesc() map[string]wellKnownNodeAttrDesc {
|
||||||
|
return map[string]wellKnownNodeAttrDesc{
|
||||||
|
netmap.PriceAttr: {defaultVal: strconv.FormatUint(defaultPrice, 10)},
|
||||||
|
netmap.CapacityAttr: {defaultVal: strconv.FormatUint(defaultCapacity, 10)},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func addWellKnownAttributes(attrs []*netmap.NodeAttribute) []*netmap.NodeAttribute {
|
func addWellKnownAttributes(attrs []*netmap.NodeAttribute) []*netmap.NodeAttribute {
|
||||||
var hasCapacity, hasPrice bool
|
mWellKnown := listWellKnownAttrDesc()
|
||||||
|
|
||||||
// check if user defined capacity and price attributes
|
// check how user defined well-known attributes
|
||||||
for i := range attrs {
|
for i := range attrs {
|
||||||
if !hasPrice && attrs[i].Key() == netmap.PriceAttr {
|
delete(mWellKnown, attrs[i].Key())
|
||||||
hasPrice = true
|
|
||||||
} else if !hasCapacity && attrs[i].Key() == netmap.CapacityAttr {
|
|
||||||
hasCapacity = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not override user defined capacity and price attributes
|
for key, desc := range mWellKnown {
|
||||||
|
// check if required attribute is set
|
||||||
if !hasCapacity {
|
if desc.explicit {
|
||||||
capacity := netmap.NewNodeAttribute()
|
fatalOnErr(errors.Errorf("missing explicit value of required node attribute %s", key))
|
||||||
capacity.SetKey(netmap.CapacityAttr)
|
|
||||||
capacity.SetValue(strconv.FormatUint(defaultCapacity, 10))
|
|
||||||
attrs = append(attrs, capacity)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hasPrice {
|
// set default value of the attribute
|
||||||
price := netmap.NewNodeAttribute()
|
a := netmap.NewNodeAttribute()
|
||||||
price.SetKey(netmap.PriceAttr)
|
a.SetKey(key)
|
||||||
price.SetValue(strconv.FormatUint(defaultPrice, 10))
|
a.SetValue(desc.defaultVal)
|
||||||
attrs = append(attrs, price)
|
|
||||||
|
attrs = append(attrs, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
Loading…
Reference in a new issue