Merge pull request #24623 from hashicorp/jbardin/module-output-references

Correctly connect module output references during plan
This commit is contained in:
James Bardin 2020-04-10 14:46:25 -04:00 committed by GitHub
commit 5ddb1a5808
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 2 deletions

View File

@ -80,6 +80,15 @@ type AbsModuleCallOutput struct {
Name string
}
// ModuleCallOutput returns the referenceable ModuleCallOutput for this
// particular instance.
func (co AbsModuleCallOutput) ModuleCallOutput() ModuleCallOutput {
return ModuleCallOutput{
Call: co.Call.Call,
Name: co.Name,
}
}
func (co AbsModuleCallOutput) String() string {
return fmt.Sprintf("%s.%s", co.Call.String(), co.Name)
}

View File

@ -55,9 +55,7 @@ func (s *State) String() string {
buf.WriteByte('.')
buf.WriteString(step.Name)
if step.InstanceKey != addrs.NoKey {
buf.WriteByte('[')
buf.WriteString(step.InstanceKey.String())
buf.WriteByte(']')
}
}
buf.WriteString(":\n")

View File

@ -10940,3 +10940,78 @@ func TestContext2Apply_ProviderMeta_refreshdata_setInvalid(t *testing.T) {
t.Errorf("Expected unsupported argument error, none received")
}
}
func TestContext2Apply_expandModuleVariables(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
module "mod1" {
for_each = toset(["a"])
source = "./mod"
}
module "mod2" {
source = "./mod"
in = module.mod1["a"].out
}
`,
"mod/main.tf": `
resource "aws_instance" "foo" {
foo = var.in
}
variable "in" {
type = string
default = "default"
}
output "out" {
value = aws_instance.foo.id
}
`,
})
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
},
})
_, diags := ctx.Plan()
if diags.HasErrors() {
t.Fatal(diags.ErrWithWarnings())
}
state, diags := ctx.Apply()
if diags.HasErrors() {
t.Fatal(diags.ErrWithWarnings())
}
expected := `<no state>
module.mod1["a"]:
aws_instance.foo:
ID = foo
provider = provider["registry.terraform.io/hashicorp/aws"]
foo = default
type = aws_instance
Outputs:
out = foo
module.mod2:
aws_instance.foo:
ID = foo
provider = provider["registry.terraform.io/hashicorp/aws"]
foo = foo
type = aws_instance
Dependencies:
module.mod1.aws_instance.foo`
if state.String() != expected {
t.Fatalf("expected:\n%s\ngot:\n%s\n", expected, state)
}
}

View File

@ -262,6 +262,11 @@ func (m *ReferenceMap) References(v dag.Vertex) []dag.Vertex {
subject = ri.ContainingResource()
case addrs.ResourceInstancePhase:
subject = ri.ContainingResource()
case addrs.AbsModuleCallOutput:
subject = ri.ModuleCallOutput()
default:
log.Printf("[WARN] ReferenceTransformer: reference not found: %q", subject)
continue
}
key = m.referenceMapKey(v, subject)
}