Merge pull request #24623 from hashicorp/jbardin/module-output-references
Correctly connect module output references during plan
This commit is contained in:
commit
5ddb1a5808
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue