From 9cd1018f0b050b5801cfe351b02418f400fc1a31 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 18 Jul 2014 16:54:52 -0700 Subject: [PATCH] config: config files can be in JSON, as well --- config/import_tree.go | 11 ++++--- config/loader_test.go | 31 ++++++++++++++++++ config/test-fixtures/basic.tf.json | 51 ++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 config/test-fixtures/basic.tf.json diff --git a/config/import_tree.go b/config/import_tree.go index 0e1c39c08..f9b28c62f 100644 --- a/config/import_tree.go +++ b/config/import_tree.go @@ -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) } diff --git a/config/loader_test.go b/config/loader_test.go index 378eb9518..d51da23ee 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -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 { diff --git a/config/test-fixtures/basic.tf.json b/config/test-fixtures/basic.tf.json new file mode 100644 index 000000000..a7d3b308c --- /dev/null +++ b/config/test-fixtures/basic.tf.json @@ -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}" + } + } +}