Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templating utilities for JSON data and KV navigation #111

Merged
merged 2 commits into from
Aug 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions integration/confdir/conf.d/service.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[template]
mode = "0644"
src = "service.conf.tmpl"
dest = "/tmp/confd-service-test.conf"
keys = [
"/_consul",
]
25 changes: 25 additions & 0 deletions integration/confdir/templates/service.conf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# All web services
[all]
{{range gets "/_consul/service/rails/*"}}{{$service := (json .Value)}}
server {{$service.Node.Address}}:{{$service.Service.Port}}
{{end}}

[v1]
{{range gets "/_consul/service/rails/v1/*"}}{{$service := (json .Value)}}
server {{$service.Node.Address}}:{{$service.Service.Port}}
{{end}}

[v2]
{{range gets "/_consul/service/rails/v2/*"}}{{$service := (json .Value)}}
server {{$service.Node.Address}}:{{$service.Service.Port}}
{{end}}

[master]
{{range gets "/_consul/service/rails/master/*"}}{{$service := (json .Value)}}
server {{$service.Node.Address}}:{{$service.Service.Port}}
{{end}}

[slave]
{{range gets "/_consul/service/rails/slave/*"}}{{$service := (json .Value)}}
server {{$service.Node.Address}}:{{$service.Service.Port}}
{{end}}
4 changes: 4 additions & 0 deletions integration/consul/test.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ curl -X PUT http://127.0.0.1:8500/v1/kv/database/username -d 'confd'
curl -X PUT http://127.0.0.1:8500/v1/kv/upstream/app1 -d '10.0.1.10:8080'
curl -X PUT http://127.0.0.1:8500/v1/kv/upstream/app2 -d '10.0.1.11:8080'

curl -X PUT http://127.0.0.1:8500/v1/agent/service/register -d '{"ID":"rails1", "Name":"rails","Tags":["master", "v1"],"Port":8080,"Check":{}}'
curl -X PUT http://127.0.0.1:8500/v1/agent/service/register -d '{"ID":"rails2", "Name":"rails","Tags":["master", "v2"],"Port":9090,"Check":{}}'
curl -X PUT http://127.0.0.1:8500/v1/agent/service/register -d '{"ID":"rails3", "Name":"rails","Tags":["slave", "v2"],"Port":9999,"Check":{"Script": "curl localhost:9999", "Interval": "10s", "TTL": "15s"}}'

# Run confd
./confd -onetime -verbose -confdir ./integration/confdir -backend consul -node "127.0.0.1:8500"
23 changes: 22 additions & 1 deletion resource/template/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package template

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -74,7 +75,7 @@ func New(path string, config Config) (*TemplateResource, error) {
tr.store = memkv.New()
tr.prefix = filepath.Join("/", config.Prefix, tr.Prefix)
if tr.Src == "" {
return nil, ErrEmptySrc
return nil, ErrEmptySrc
}
tr.Src = filepath.Join(config.TemplateDir, tr.Src)
return &tr, nil
Expand Down Expand Up @@ -115,10 +116,14 @@ func (t *TemplateResource) createStageFile() error {
// Add template functions
tplFuncMap := make(template.FuncMap)
tplFuncMap["base"] = path.Base
tplFuncMap["parent"] = path.Dir
tplFuncMap["sibling"] = t.GetSibling
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might rename this later, but for now I think it works.

tplFuncMap["get"] = t.store.Get
tplFuncMap["gets"] = t.store.GetAll
tplFuncMap["getv"] = t.store.GetValue
tplFuncMap["getvs"] = t.store.GetAllValues
tplFuncMap["json"] = t.UnmarshalJsonObject
tplFuncMap["jsonArray"] = t.UnmarshalJsonArray

tmpl := template.Must(template.New(path.Base(t.Src)).Funcs(tplFuncMap).ParseFiles(t.Src))
if err = tmpl.Execute(temp, nil); err != nil {
Expand Down Expand Up @@ -255,6 +260,22 @@ func (t *TemplateResource) setFileMode() error {
return nil
}

func (t *TemplateResource) UnmarshalJsonObject(data string) (map[string]interface{}, error) {
var ret map[string]interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
}

func (t *TemplateResource) UnmarshalJsonArray(data string) ([]interface{}, error) {
var ret []interface{}
err := json.Unmarshal([]byte(data), &ret)
return ret, err
}

func (t *TemplateResource) GetSibling(origin string, newKey string) (memkv.KVPair, error) {
return t.store.Get(path.Join("/", path.Dir(origin), newKey))
}

// ProcessTemplateResources is a convenience function that loads all the
// template resources and processes them serially. Called from main.
// It returns a list of errors if any.
Expand Down