config: parse depends_on for outputs
This commit is contained in:
parent
008b8b4c23
commit
10426ba619
|
@ -154,6 +154,7 @@ type Variable struct {
|
||||||
type Output struct {
|
type Output struct {
|
||||||
Name string
|
Name string
|
||||||
Sensitive bool
|
Sensitive bool
|
||||||
|
DependsOn []string
|
||||||
RawConfig *RawConfig
|
RawConfig *RawConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,6 +100,13 @@ func outputsStr(os []*Output) string {
|
||||||
|
|
||||||
result += fmt.Sprintf("%s\n", n)
|
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 {
|
if len(o.RawConfig.Variables) > 0 {
|
||||||
result += fmt.Sprintf(" vars\n")
|
result += fmt.Sprintf(" vars\n")
|
||||||
for _, rawV := range o.RawConfig.Variables {
|
for _, rawV := range o.RawConfig.Variables {
|
||||||
|
|
|
@ -292,11 +292,21 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
|
||||||
for _, item := range list.Items {
|
for _, item := range list.Items {
|
||||||
n := item.Keys[0].Token.Value().(string)
|
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{}
|
var config map[string]interface{}
|
||||||
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
if err := hcl.DecodeObject(&config, item.Val); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete special keys
|
||||||
|
delete(config, "depends_on")
|
||||||
|
|
||||||
rawConfig, err := NewRawConfig(config)
|
rawConfig, err := NewRawConfig(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
|
@ -305,9 +315,22 @@ func loadOutputsHcl(list *ast.ObjectList) ([]*Output, error) {
|
||||||
err)
|
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{
|
result = append(result, &Output{
|
||||||
Name: n,
|
Name: n,
|
||||||
RawConfig: rawConfig,
|
RawConfig: rawConfig,
|
||||||
|
DependsOn: dependsOn,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
func TestLoadJSONBasic(t *testing.T) {
|
||||||
raw, err := ioutil.ReadFile(filepath.Join(fixtureDir, "basic.tf.json"))
|
raw, err := ioutil.ReadFile(filepath.Join(fixtureDir, "basic.tf.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1092,6 +1112,12 @@ aws_instance.web (x1)
|
||||||
user: var.foo
|
user: var.foo
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const outputDependsOnStr = `
|
||||||
|
value
|
||||||
|
dependsOn
|
||||||
|
foo
|
||||||
|
`
|
||||||
|
|
||||||
const variablesVariablesStr = `
|
const variablesVariablesStr = `
|
||||||
bar
|
bar
|
||||||
<>
|
<>
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
output "value" {
|
||||||
|
value = "foo"
|
||||||
|
depends_on = ["foo"]
|
||||||
|
}
|
Loading…
Reference in New Issue