From 10426ba619908a38733daa8ee3f6513918415169 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 11 Nov 2016 17:46:34 -0800 Subject: [PATCH] config: parse depends_on for outputs --- config/config.go | 1 + config/config_string.go | 7 ++++++ config/loader_hcl.go | 23 ++++++++++++++++++++ config/loader_test.go | 26 +++++++++++++++++++++++ config/test-fixtures/output-depends-on.tf | 4 ++++ 5 files changed, 61 insertions(+) create mode 100644 config/test-fixtures/output-depends-on.tf diff --git a/config/config.go b/config/config.go index 3c8f8826d..eff7f1ff0 100644 --- a/config/config.go +++ b/config/config.go @@ -154,6 +154,7 @@ type Variable struct { type Output struct { Name string Sensitive bool + DependsOn []string RawConfig *RawConfig } diff --git a/config/config_string.go b/config/config_string.go index 362dc4adf..f11290e87 100644 --- a/config/config_string.go +++ b/config/config_string.go @@ -100,6 +100,13 @@ func outputsStr(os []*Output) string { result += fmt.Sprintf("%s\n", n) + if len(o.DependsOn) > 0 { + result += fmt.Sprintf(" dependsOn\n") + for _, d := range o.DependsOn { + result += fmt.Sprintf(" %s\n", d) + } + } + if len(o.RawConfig.Variables) > 0 { result += fmt.Sprintf(" vars\n") for _, rawV := range o.RawConfig.Variables { diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 2122d622e..3d9042ff9 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -292,11 +292,21 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) { for _, item := range list.Items { n := item.Keys[0].Token.Value().(string) + var listVal *ast.ObjectList + if ot, ok := item.Val.(*ast.ObjectType); ok { + listVal = ot.List + } else { + return nil, fmt.Errorf("output '%s': should be an object", n) + } + var config map[string]interface{} if err := hcl.DecodeObject(&config, item.Val); err != nil { return nil, err } + // Delete special keys + delete(config, "depends_on") + rawConfig, err := NewRawConfig(config) if err != nil { return nil, fmt.Errorf( @@ -305,9 +315,22 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) { err) } + // If we have depends fields, then add those in + var dependsOn []string + if o := listVal.Filter("depends_on"); len(o.Items) > 0 { + err := hcl.DecodeObject(&dependsOn, o.Items[0].Val) + if err != nil { + return nil, fmt.Errorf( + "Error reading depends_on for output %q: %s", + n, + err) + } + } + result = append(result, &Output{ Name: n, RawConfig: rawConfig, + DependsOn: dependsOn, }) } diff --git a/config/loader_test.go b/config/loader_test.go index 73b09a6fe..202d94e97 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -286,6 +286,26 @@ func TestLoadFileBasic_modules(t *testing.T) { } } +func TestLoadFile_outputDependsOn(t *testing.T) { + c, err := LoadFile(filepath.Join(fixtureDir, "output-depends-on.tf")) + if err != nil { + t.Fatalf("err: %s", err) + } + + if c == nil { + t.Fatal("config should not be nil") + } + + if c.Dir != "" { + t.Fatalf("bad: %#v", c.Dir) + } + + actual := outputsStr(c.Outputs) + if actual != strings.TrimSpace(outputDependsOnStr) { + t.Fatalf("bad:\n%s", actual) + } +} + func TestLoadJSONBasic(t *testing.T) { raw, err := ioutil.ReadFile(filepath.Join(fixtureDir, "basic.tf.json")) if err != nil { @@ -1092,6 +1112,12 @@ aws_instance.web (x1) user: var.foo ` +const outputDependsOnStr = ` +value + dependsOn + foo +` + const variablesVariablesStr = ` bar <> diff --git a/config/test-fixtures/output-depends-on.tf b/config/test-fixtures/output-depends-on.tf new file mode 100644 index 000000000..5b4066e2a --- /dev/null +++ b/config/test-fixtures/output-depends-on.tf @@ -0,0 +1,4 @@ +output "value" { + value = "foo" + depends_on = ["foo"] +}