config: parse depends_on for outputs

This commit is contained in:
Mitchell Hashimoto 2016-11-11 17:46:34 -08:00
parent 008b8b4c23
commit 10426ba619
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 61 additions and 0 deletions

View File

@ -154,6 +154,7 @@ type Variable struct {
type Output struct {
Name string
Sensitive bool
DependsOn []string
RawConfig *RawConfig
}

View File

@ -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 {

View File

@ -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,
})
}

View File

@ -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
<>

View File

@ -0,0 +1,4 @@
output "value" {
value = "foo"
depends_on = ["foo"]
}