terraform: fix module output handling. Fixes #474

This commit is contained in:
Armon Dadgar 2014-11-24 19:18:52 -08:00
parent f26b8df92f
commit a5d444b8e3
6 changed files with 67 additions and 5 deletions

View File

@ -523,7 +523,7 @@ func (c *walkContext) Walk() error {
// On Apply, we prune so that we don't do outputs if we destroyed
mod.prune()
}
if len(mod.Resources) == 0 {
if len(mod.Resources) == 0 && len(conf.Resources) != 0 {
mod.Outputs = nil
return nil
}

View File

@ -913,6 +913,35 @@ func TestContextApply(t *testing.T) {
}
}
func TestContextApply_emptyModule(t *testing.T) {
m := testModule(t, "apply-empty-module")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
if _, err := ctx.Plan(nil); err != nil {
t.Fatalf("err: %s", err)
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(state.String())
actual = strings.Replace(actual, " ", "", -1)
expected := strings.TrimSpace(testTerraformApplyEmptyModuleStr)
if actual != expected {
t.Fatalf("bad: \n%s\nexpect:\n%s", actual, expected)
}
}
func TestContextApply_createBeforeDestroy(t *testing.T) {
m := testModule(t, "apply-good-create-before")
p := testProvider("aws")

View File

@ -294,12 +294,12 @@ func (m *ModuleState) GoString() string {
}
func (m *ModuleState) String() string {
if len(m.Resources) == 0 {
return "<no state>"
}
var buf bytes.Buffer
if len(m.Resources) == 0 {
buf.WriteString("<no state>")
}
names := make([]string, 0, len(m.Resources))
for name, _ := range m.Resources {
names = append(names, name)

View File

@ -164,6 +164,21 @@ aws_instance.foo:
type = aws_instance
`
const testTerraformApplyEmptyModuleStr = `
<no state>
Outputs:
end = XXXX
module.child:
<no state>
Outputs:
aws_access_key = YYYYY
aws_route53_zone_id = XXXX
aws_secret_key = ZZZZ
`
const testTerraformApplyDependsCreateBeforeStr = `
aws_instance.lb:
ID = foo

View File

@ -0,0 +1,11 @@
output "aws_route53_zone_id" {
value = "XXXX"
}
output "aws_access_key" {
value = "YYYYY"
}
output "aws_secret_key" {
value = "ZZZZ"
}

View File

@ -0,0 +1,7 @@
module "child" {
source = "./child"
}
output "end" {
value = "${module.child.aws_route53_zone_id}"
}