From e85e97dad163ad5d37a4834185a96a2dd39f03f8 Mon Sep 17 00:00:00 2001 From: Rene Kaufmann Date: Sun, 29 Oct 2017 11:36:44 +0100 Subject: [PATCH] added the possibility to add default backend settings: fixes #15 --- cmd/remco/config.go | 21 +++++++++++++++++++-- cmd/remco/config_test.go | 30 ++++++++++++++++++++++++++++-- cmd/remco/supervisor.go | 4 ++-- cmd/remco/supervisor_test.go | 2 +- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/cmd/remco/config.go b/cmd/remco/config.go index c347e688..a5e99f5f 100644 --- a/cmd/remco/config.go +++ b/cmd/remco/config.go @@ -62,11 +62,16 @@ type Configuration struct { Resource []Resource } +type DefaultBackends struct { + Backends BackendConfigs `toml:"default_backends"` + Resource []Resource +} + // Resource is the representation of an resource configuration type Resource struct { Exec template.ExecConfig Template []*template.Renderer - Backend BackendConfigs + Backends BackendConfigs `toml:"backend"` // defaults to the filename of the resource Name string @@ -87,12 +92,22 @@ func readFileAndExpandEnv(path string) ([]byte, error) { // It returns an error if any. func NewConfiguration(path string) (Configuration, error) { var c Configuration + var dbc DefaultBackends buf, err := readFileAndExpandEnv(path) if err != nil { return c, err } + if err := toml.Unmarshal(buf, &dbc); err != nil { + return c, errors.Wrapf(err, "toml unmarshal failed: %s", path) + } + + c.Resource = dbc.Resource + for i := 0; i < len(c.Resource); i++ { + c.Resource[i].Backends = dbc.Backends + } + if err := toml.Unmarshal(buf, &c); err != nil { return c, errors.Wrapf(err, "toml unmarshal failed: %s", path) } @@ -120,7 +135,9 @@ func NewConfiguration(path string) (Configuration, error) { if err != nil { return c, err } - var r Resource + r := Resource{ + Backends: dbc.Backends, + } if err := toml.Unmarshal(buf, &r); err != nil { return c, errors.Wrapf(err, "toml unmarshal failed: %s", fp) } diff --git a/cmd/remco/config_test.go b/cmd/remco/config_test.go index 777a3038..985802aa 100644 --- a/cmd/remco/config_test.go +++ b/cmd/remco/config_test.go @@ -24,6 +24,27 @@ const ( log_level = "debug" log_format = "text" include_dir = "/tmp/resource.d/" + + [default_backends] + [default_backends.mock] + onetime = false + prefix = "Hallo" + + + [[resource]] + name = "haproxy" + [[resource.template]] + src = "/tmp/test12345.tmpl" + dst = "/tmp/test12345.cfg" + checkCmd = "" + reloadCmd = "" + mode = "0644" + [resource.backend] + [resource.backend.mock] + keys = ["/"] + watch = false + interval = 1 + ` resourceFile string = ` [[template]] @@ -37,7 +58,6 @@ const ( keys = ["/"] watch = false interval = 1 - onetime = false ` ) @@ -56,6 +76,7 @@ var expectedBackend = BackendConfigs{ Keys: []string{"/"}, Interval: 1, Onetime: false, + Prefix: "Hallo", }, }, } @@ -65,10 +86,15 @@ var expected = Configuration{ LogFormat: "text", IncludeDir: "/tmp/resource.d/", Resource: []Resource{ + { + Name: "haproxy", + Template: expectedTemplates, + Backends: expectedBackend, + }, { Name: "test.toml", Template: expectedTemplates, - Backend: expectedBackend, + Backends: expectedBackend, }, }, } diff --git a/cmd/remco/supervisor.go b/cmd/remco/supervisor.go index fc22363b..bb919acc 100644 --- a/cmd/remco/supervisor.go +++ b/cmd/remco/supervisor.go @@ -178,8 +178,8 @@ func (ru *Supervisor) runResource(r []Resource, stop, stopped chan struct{}) { go func(r Resource) { defer wait.Done() - backendConfigs := r.Backend.GetBackends() - for _, v := range r.Backend.Plugin { + backendConfigs := r.Backends.GetBackends() + for _, v := range r.Backends.Plugin { backendConfigs = append(backendConfigs, &v) } diff --git a/cmd/remco/supervisor_test.go b/cmd/remco/supervisor_test.go index e32b5635..772ee8d4 100644 --- a/cmd/remco/supervisor_test.go +++ b/cmd/remco/supervisor_test.go @@ -45,7 +45,7 @@ var exampleConfiguration = Configuration{ { Name: "test.toml", Template: exampleTemplates, - Backend: exampleBackend, + Backends: exampleBackend, }, }, }