Skip to content

Commit

Permalink
config: config files can be in JSON, as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Jul 18, 2014
1 parent 596e0f7 commit 9cd1018
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
11 changes: 6 additions & 5 deletions config/import_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"fmt"
"io"
"path/filepath"
"strings"
)

// configurable is an interface that must be implemented by any configuration
Expand Down Expand Up @@ -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)
}

Expand Down
31 changes: 31 additions & 0 deletions config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 51 additions & 0 deletions config/test-fixtures/basic.tf.json
Original file line number Diff line number Diff line change
@@ -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}"
}
}
}

0 comments on commit 9cd1018

Please sign in to comment.