Skip to content

Commit

Permalink
added the possibility to add default backend settings: fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
HeavyHorst committed Oct 29, 2017
1 parent 1cafd84 commit e85e97d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
21 changes: 19 additions & 2 deletions cmd/remco/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
30 changes: 28 additions & 2 deletions cmd/remco/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand All @@ -37,7 +58,6 @@ const (
keys = ["/"]
watch = false
interval = 1
onetime = false
`
)

Expand All @@ -56,6 +76,7 @@ var expectedBackend = BackendConfigs{
Keys: []string{"/"},
Interval: 1,
Onetime: false,
Prefix: "Hallo",
},
},
}
Expand All @@ -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,
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/remco/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/remco/supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var exampleConfiguration = Configuration{
{
Name: "test.toml",
Template: exampleTemplates,
Backend: exampleBackend,
Backends: exampleBackend,
},
},
}
Expand Down

0 comments on commit e85e97d

Please sign in to comment.