tls default are set when loading yaml config

This commit is contained in:
klaper 2022-06-04 14:42:10 +02:00
parent 571009f7bd
commit f43ad5cd54
No known key found for this signature in database
GPG Key ID: 734C283E807052C3
3 changed files with 21 additions and 8 deletions

View File

@ -1,10 +1,11 @@
package config
import (
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)
// Config represents the configuration for the exporter
@ -40,9 +41,9 @@ type Device struct {
Srv SrvRecord `yaml:"srv,omitempty"`
User string `yaml:"user"`
Password string `yaml:"password"`
Port string `yaml:"port"`
EnableTLS bool `yaml:"tls"`
InsecureTLS bool `yaml:"insecure"`
Port string `yaml:"port,omitempty"`
EnableTLS bool `yaml:"tls,omitempty"`
InsecureTLS bool `yaml:"insecure,omitempty"`
}
type SrvRecord struct {
@ -54,7 +55,7 @@ type DnsServer struct {
Port int `yaml:"port"`
}
// Load reads YAML from reader and unmashals in Config
// Load reads YAML from reader and unmarshalls in Config
func Load(r io.Reader) (*Config, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
@ -69,3 +70,15 @@ func Load(r io.Reader) (*Config, error) {
return c, nil
}
func (d *Device) UnmarshalYAML(unmarshal func(interface{}) error) error {
type inputDevice Device
defaults := &inputDevice{Port: "8728", EnableTLS: false, InsecureTLS: false}
err := unmarshal(defaults)
if err != nil {
log.WithError(err).Error("Device unmarshal error")
return err
}
*d = (Device)(*defaults)
return nil
}

View File

@ -6,8 +6,8 @@ devices:
- name: test2
address: 192.168.2.1
user: test
password: 123
port: 324
password: '123'
port: '324'
tls: true
insecure: true

View File

@ -19,7 +19,7 @@ func TestShouldParse(t *testing.T) {
assertDevice("test1", "192.168.1.1", "foo", "bar", c.Devices[0], t)
assertDevice("test2", "192.168.2.1", "test", "123", c.Devices[1], t)
assertDeviceConnection("", false, false, c.Devices[0], t)
assertDeviceConnection("8728", false, false, c.Devices[0], t)
assertDeviceConnection("324", true, true, c.Devices[1], t)
assertFeature("BGP", c.Features.BGP, t)
assertFeature("Conntrack", c.Features.Conntrack, t)