News features and improvements (#8)
* added config file implementation, refactoring * add gitignore * improved test * preperations for more metrics * added resource metrics * added first bgp metrics * added asn as label for bgp metrics * added prefix and message counts to bgp metrics * simplified * Update README.md * added yaml dependency * fixed go routine call * added timeout * clean up * added TLS support * set default api port for TLS * added routes metric * added missing log information
This commit is contained in:
committed by
Steve Brunton
parent
c37abb638f
commit
f2866a3a2f
34
config/config.go
Normal file
34
config/config.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Devices []Device `yaml:"devices"`
|
||||
}
|
||||
|
||||
type Device struct {
|
||||
Name string `yaml:"name"`
|
||||
Address string `yaml:"address"`
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
}
|
||||
|
||||
func Load(r io.Reader) (*Config, error) {
|
||||
b, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &Config{}
|
||||
err = yaml.Unmarshal(b, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
49
config/config_test.go
Normal file
49
config/config_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShouldParse(t *testing.T) {
|
||||
b := loadTestFile(t)
|
||||
c, err := Load(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
t.Fatalf("could not parse: %v", err)
|
||||
}
|
||||
|
||||
if len(c.Devices) != 2 {
|
||||
t.Fatalf("expected 2 devices, got %v", len(c.Devices))
|
||||
}
|
||||
|
||||
assertConfig("test1", "192.168.1.1", "foo", "bar", c.Devices[0], t)
|
||||
assertConfig("test2", "192.168.2.1", "test", "123", c.Devices[1], t)
|
||||
}
|
||||
|
||||
func loadTestFile(t *testing.T) []byte {
|
||||
b, err := ioutil.ReadFile("test/config.test.yml")
|
||||
if err != nil {
|
||||
t.Fatalf("could not load config: %v", err)
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
func assertConfig(name, address, user, password string, c Device, t *testing.T) {
|
||||
if c.Name != name {
|
||||
t.Fatalf("expected name %s, got %s", name, c.Name)
|
||||
}
|
||||
|
||||
if c.Address != address {
|
||||
t.Fatalf("expected address %s, got %s", address, c.Address)
|
||||
}
|
||||
|
||||
if c.User != user {
|
||||
t.Fatalf("expected user %s, got %s", user, c.User)
|
||||
}
|
||||
|
||||
if c.Password != password {
|
||||
t.Fatalf("expected password %s, got %s", password, c.Password)
|
||||
}
|
||||
}
|
||||
11
config/test/config.test.yml
Normal file
11
config/test/config.test.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
---
|
||||
|
||||
devices:
|
||||
- name: test1
|
||||
address: 192.168.1.1
|
||||
user: foo
|
||||
password: bar
|
||||
- name: test2
|
||||
address: 192.168.2.1
|
||||
user: test
|
||||
password: 123
|
||||
Reference in New Issue
Block a user