orchestrator/clickhouse: fix Networks field validation

This commit is contained in:
Vincent Bernat
2022-08-24 10:10:22 +02:00
parent 743c98a78f
commit e93b57034c
3 changed files with 7 additions and 3 deletions

View File

@@ -20,7 +20,10 @@ var Validate *validator.Validate
func RegisterSubnetMapValidation[V any]() {
var zero SubnetMap[V]
validatorFunc := func(field reflect.Value) interface{} {
if subnetMap, ok := field.Interface().(SubnetMap[V]); ok {
switch subnetMap := field.Interface().(type) {
case SubnetMap[V]:
return subnetMap.ToMap()
case *SubnetMap[V]:
return subnetMap.ToMap()
}
return nil

View File

@@ -37,7 +37,7 @@ type Configuration struct {
// Communities is a mapping from exporter IPs to SNMPv2 communities
Communities *helpers.SubnetMap[string]
// SecurityParameters is a mapping from exporter IPs to SNMPv3 security parameters
SecurityParameters *helpers.SubnetMap[SecurityParameters] `validate:"dive"`
SecurityParameters *helpers.SubnetMap[SecurityParameters] `validate:"omitempty,dive"`
}
// SecurityParameters describes SNMPv3 USM security parameters.

View File

@@ -30,7 +30,7 @@ type Configuration struct {
ASNs map[uint32]string
// Networks is a mapping from IP networks to attributes. It is used
// to instantiate the SrcNet* and DstNet* columns.
Networks *helpers.SubnetMap[NetworkAttributes] `validate:"dive"`
Networks *helpers.SubnetMap[NetworkAttributes] `validate:"omitempty,dive"`
// OrchestratorURL allows one to override URL to reach
// orchestrator from Clickhouse
OrchestratorURL string `validate:"isdefault|url"`
@@ -105,4 +105,5 @@ func NetworkAttributesUnmarshallerHook() mapstructure.DecodeHookFunc {
func init() {
helpers.RegisterMapstructureUnmarshallerHook(helpers.SubnetMapUnmarshallerHook[NetworkAttributes]())
helpers.RegisterMapstructureUnmarshallerHook(NetworkAttributesUnmarshallerHook())
helpers.RegisterSubnetMapValidation[NetworkAttributes]()
}