Skip to content

Commit

Permalink
node: Warn user on LOCODE mismatches
Browse files Browse the repository at this point in the history
If a configuration sets LOCODE attributes (non-emtpy values) and they differ
from "the database" ones, use the user's ones still but with a warning message.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
  • Loading branch information
carpawell committed Nov 24, 2023
1 parent 532f423 commit f8f0817
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions cmd/neofs-node/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/locode-db/pkg/locodedb"
nodeconfig "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config/node"
"github.com/nspcc-dev/neofs-node/pkg/util/attributes"
"go.uber.org/zap"
)

func parseAttributes(c *cfg) {
Expand All @@ -31,27 +32,68 @@ func parseAttributes(c *cfg) {
countryCode := locAttr[:locodedb.CountryCodeLen]
n := &c.cfgNodeInfo.localInfo

if countryCode != "" && n.CountryCode() == "" {
n.SetCountryCode(countryCode)
fromUser := n.CountryCode()
if fromUser != "" && fromUser != countryCode {
c.log.Warn("locode attribute mismatch, configuration value kept: country code",
zap.String("configuration_value", fromUser),
zap.String("database_value", countryCode))
} else {
setIfNotEmpty(n.SetCountryCode, countryCode)
}
if record.Country != "" && n.CountryName() == "" {
n.SetCountryName(record.Country)

fromUser = n.CountryName()
if fromUser != "" && fromUser != record.Country {
c.log.Warn("locode attribute mismatch, configuration value kept: country name",
zap.String("configuration_value", fromUser),
zap.String("database_value", record.Country))
} else {
setIfNotEmpty(n.SetCountryName, record.Country)
}
if record.Location != "" && n.LocationName() == "" {
n.SetLocationName(record.Location)

fromUser = n.LocationName()
if fromUser != "" && fromUser != record.Location {
c.log.Warn("locode attribute mismatch, configuration value kept: location name",
zap.String("configuration_value", fromUser),
zap.String("database_value", record.Location))
} else {
setIfNotEmpty(n.SetLocationName, record.Location)
}
if record.Cont.String() != "" && n.ContinentName() == "" {
n.SetContinentName(record.Cont.String())

fromUser = n.ContinentName()
if fromUser != "" && fromUser != record.Cont.String() {
c.log.Warn("locode attribute mismatch, configuration value kept: continent",
zap.String("configuration_value", fromUser),
zap.String("database_value", record.Cont.String()))
} else {
setIfNotEmpty(n.SetContinentName, record.Cont.String())
}
if record.SubDivCode != "" && n.SubdivisionCode() == "" {
n.SetSubdivisionCode(record.SubDivCode)

fromUser = n.SubdivisionCode()
if fromUser != "" && fromUser != record.SubDivCode {
c.log.Warn("locode attribute mismatch, configuration value kept: subdivision code",
zap.String("configuration_value", fromUser),
zap.String("database_value", record.SubDivCode))
} else {
setIfNotEmpty(n.SetSubdivisionCode, record.SubDivCode)
}
if record.SubDivName != "" && n.SubdivisionName() == "" {
n.SetSubdivisionName(record.SubDivName)

fromUser = n.SubdivisionName()
if fromUser != "" && fromUser != record.SubDivName {
c.log.Warn("locode attribute mismatch, configuration value kept: subdivision name",
zap.String("configuration_value", fromUser),
zap.String("database_value", record.SubDivName))
} else {
setIfNotEmpty(n.SetSubdivisionName, record.SubDivName)
}
}

func getRecord(lc string) (locodedb.Record, error) {
rec, err := locodedb.Get(lc)
return rec, err
}

func setIfNotEmpty(setter func(string), value string) {
if value != "" {
setter(value)
}
}

0 comments on commit f8f0817

Please sign in to comment.