Skip to content

Commit 632cc92

Browse files
committed
Merge pull request hashicorp#2476 from hashicorp/b-orphan-provider-inherit
terraform: module orphans providers should inherit config
2 parents fb66be7 + afe5ec6 commit 632cc92

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎terraform/context_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,6 +4392,54 @@ func TestContext2Apply_moduleDestroyOrder(t *testing.T) {
43924392
}
43934393
}
43944394

4395+
func TestContext2Apply_moduleOrphanProvider(t *testing.T) {
4396+
m := testModule(t, "apply-module-orphan-provider-inherit")
4397+
p := testProvider("aws")
4398+
p.ApplyFn = testApplyFn
4399+
p.DiffFn = testDiffFn
4400+
4401+
p.ConfigureFn = func(c *ResourceConfig) error {
4402+
if _, ok := c.Get("value"); !ok {
4403+
return fmt.Errorf("value is not found")
4404+
}
4405+
4406+
return nil
4407+
}
4408+
4409+
// Create a state with an orphan module
4410+
state := &State{
4411+
Modules: []*ModuleState{
4412+
&ModuleState{
4413+
Path: []string{"root", "child"},
4414+
Resources: map[string]*ResourceState{
4415+
"aws_instance.bar": &ResourceState{
4416+
Type: "aws_instance",
4417+
Primary: &InstanceState{
4418+
ID: "bar",
4419+
},
4420+
},
4421+
},
4422+
},
4423+
},
4424+
}
4425+
4426+
ctx := testContext2(t, &ContextOpts{
4427+
Module: m,
4428+
State: state,
4429+
Providers: map[string]ResourceProviderFactory{
4430+
"aws": testProviderFuncFixed(p),
4431+
},
4432+
})
4433+
4434+
if _, err := ctx.Plan(); err != nil {
4435+
t.Fatalf("err: %s", err)
4436+
}
4437+
4438+
if _, err := ctx.Apply(); err != nil {
4439+
t.Fatalf("err: %s", err)
4440+
}
4441+
}
4442+
43954443
func TestContext2Apply_moduleVarResourceCount(t *testing.T) {
43964444
m := testModule(t, "apply-module-var-resource-count")
43974445
p := testProvider("aws")

‎terraform/graph_builder.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,12 @@ func (b *BuiltinGraphBuilder) Steps(path []string) []GraphTransformer {
117117
&MissingProviderTransformer{Providers: b.Providers},
118118
&ProviderTransformer{},
119119
&CloseProviderTransformer{},
120-
&PruneProviderTransformer{},
121120
&DisableProviderTransformer{},
122121

123122
// Provisioner-related transformations
124123
&MissingProvisionerTransformer{Provisioners: b.Provisioners},
125124
&ProvisionerTransformer{},
126125
&CloseProvisionerTransformer{},
127-
&PruneProvisionerTransformer{},
128126

129127
// Run our vertex-level transforms
130128
&VertexTransformer{
@@ -154,6 +152,12 @@ func (b *BuiltinGraphBuilder) Steps(path []string) []GraphTransformer {
154152
// We don't do the following for modules.
155153
if len(path) <= 1 {
156154
steps = append(steps,
155+
// Prune the providers and provisioners. This must happen
156+
// only once because flattened modules might depend on empty
157+
// providers.
158+
&PruneProviderTransformer{},
159+
&PruneProvisionerTransformer{},
160+
157161
// Create the destruction nodes
158162
&DestroyTransformer{FullDestroy: b.Destroy},
159163
&CreateBeforeDestroyTransformer{},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
value = "foo"
3+
}

0 commit comments

Comments
 (0)