template expansion of `json` in chef-solo

Fixes #362
This commit is contained in:
Matthew Hooker 2013-08-28 17:27:14 -07:00
parent 5887472a6f
commit 4b76b2d9e3
2 changed files with 42 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"testing" "testing"
"time" "time"
"encoding/json"
) )
func TestConfigTemplateProcess_timestamp(t *testing.T) { func TestConfigTemplateProcess_timestamp(t *testing.T) {
@ -47,6 +48,39 @@ func TestConfigTemplateProcess_user(t *testing.T) {
} }
} }
func TestJsonTemplateProcess_user(t *testing.T) {
tpl, err := NewConfigTemplate()
if err != nil {
t.Fatalf("err: %s", err)
}
tpl.UserVars["foo"] = "bar"
jsonData := make(map[string]interface{})
jsonData["key"] = map[string]string{
"key1": "{{user `foo`}}",
}
jsonBytes, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
t.Fatalf("err: %s", err)
}
var jsonString = string(jsonBytes)
result, err := tpl.Process(jsonString, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
var dat map[string]map[string]interface{}
if err := json.Unmarshal([]byte(result), &dat); err != nil {
t.Fatalf("err: %s", err)
}
if dat["key"]["key1"] != "bar" {
t.Fatalf("found %s instead", dat["key"]["key1"])
}
}
func TestConfigTemplateValidate(t *testing.T) { func TestConfigTemplateValidate(t *testing.T) {
tpl, err := NewConfigTemplate() tpl, err := NewConfigTemplate()
if err != nil { if err != nil {

View File

@ -95,7 +95,6 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
sliceTemplates := map[string][]string{ sliceTemplates := map[string][]string{
"cookbook_paths": p.config.CookbookPaths, "cookbook_paths": p.config.CookbookPaths,
"remote_cookbook_paths": p.config.RemoteCookbookPaths, "remote_cookbook_paths": p.config.RemoteCookbookPaths,
"run_list": p.config.RunList,
} }
for n, slice := range sliceTemplates { for n, slice := range sliceTemplates {
@ -236,10 +235,17 @@ func (p *Provisioner) createJson(ui packer.Ui, comm packer.Communicator) (string
if err != nil { if err != nil {
return "", err return "", err
} }
var jsonString = string(jsonBytes)
println(jsonString)
result, err := p.config.tpl.Process(jsonString, nil)
if err != nil {
return "", err
}
// Upload the bytes // Upload the bytes
remotePath := filepath.Join(p.config.StagingDir, "node.json") remotePath := filepath.Join(p.config.StagingDir, "node.json")
if err := comm.Upload(remotePath, bytes.NewReader(jsonBytes)); err != nil { if err := comm.Upload(remotePath, bytes.NewReader([]byte(result))); err != nil {
return "", err return "", err
} }