config: config files can be in JSON, as well

This commit is contained in:
Mitchell Hashimoto 2014-07-18 16:54:52 -07:00
parent 596e0f7f13
commit 9cd1018f0b
3 changed files with 88 additions and 5 deletions

View File

@ -3,7 +3,7 @@ package config
import (
"fmt"
"io"
"path/filepath"
"strings"
)
// configurable is an interface that must be implemented by any configuration
@ -33,12 +33,13 @@ type fileLoaderFunc func(path string) (configurable, []string, error)
// executes the proper fileLoaderFunc.
func loadTree(root string) (*importTree, error) {
var f fileLoaderFunc
switch filepath.Ext(root) {
case ".tf":
if strings.HasSuffix(root, ".tf") {
f = loadFileLibucl
default:
} else if strings.HasSuffix(root, ".tf.json") {
f = loadFileLibucl
} else {
return nil, fmt.Errorf(
"%s: unknown configuration format. Use '.tf' extension",
"%s: unknown configuration format. Use '.tf' or '.tf.json' extension",
root)
}

View File

@ -72,6 +72,37 @@ func TestLoadBasic_import(t *testing.T) {
}
}
func TestLoadBasic_json(t *testing.T) {
c, err := Load(filepath.Join(fixtureDir, "basic.tf.json"))
if err != nil {
t.Fatalf("err: %s", err)
}
if c == nil {
t.Fatal("config should not be nil")
}
actual := variablesStr(c.Variables)
if actual != strings.TrimSpace(basicVariablesStr) {
t.Fatalf("bad:\n%s", actual)
}
actual = providerConfigsStr(c.ProviderConfigs)
if actual != strings.TrimSpace(basicProvidersStr) {
t.Fatalf("bad:\n%s", actual)
}
actual = resourcesStr(c.Resources)
if actual != strings.TrimSpace(basicResourcesStr) {
t.Fatalf("bad:\n%s", actual)
}
actual = outputsStr(c.Outputs)
if actual != strings.TrimSpace(basicOutputsStr) {
t.Fatalf("bad:\n%s", actual)
}
}
func TestLoad_variables(t *testing.T) {
c, err := Load(filepath.Join(fixtureDir, "variables.tf"))
if err != nil {

View File

@ -0,0 +1,51 @@
{
"variable": {
"foo": {
"default": "bar",
"description": "bar"
}
},
"provider": {
"aws": {
"access_key": "foo",
"secret_key": "bar"
},
"do": {
"api_key": "${var.foo}"
}
},
"resource": {
"aws_instance": {
"db": {
"security_groups": ["${aws_security_group.firewall.*.id}"]
},
"web": {
"ami": "${var.foo}",
"security_groups": [
"foo",
"${aws_security_group.firewall.foo}"
],
"network_interface": {
"device_index": 0,
"description": "Main network interface"
}
}
},
"aws_security_group": {
"firewall": {
"count": 5
}
}
},
"output": {
"web_ip": {
"value": "${aws_instance.web.private_ip}"
}
}
}