terraform: don't include providers if not targeted

Fixes #12009

This is a simple change similar to #10911 where we need to exclude
providers that aren't targeted.
This commit is contained in:
Mitchell Hashimoto 2017-02-17 09:21:29 -08:00
parent d1ecaff6f6
commit 9062a1893e
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
9 changed files with 85 additions and 0 deletions

View File

@ -2519,6 +2519,39 @@ STATE:
}
}
func TestContext2Plan_targetedModuleWithProvider(t *testing.T) {
m := testModule(t, "plan-targeted-module-with-provider")
p := testProvider("null")
p.DiffFn = testDiffFn
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"null": testProviderFuncFixed(p),
},
Targets: []string{"module.child2"},
})
plan, err := ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(`
DIFF:
module.child2:
CREATE: null_resource.foo
STATE:
<no state>
`)
if actual != expected {
t.Fatalf("expected:\n%s\n\ngot:\n%s", expected, actual)
}
}
func TestContext2Plan_targetedOrphan(t *testing.T) {
m := testModule(t, "plan-targeted-orphan")
p := testProvider("aws")

View File

@ -33,6 +33,24 @@ func TestPlanGraphBuilder(t *testing.T) {
}
}
func TestPlanGraphBuilder_targetModule(t *testing.T) {
b := &PlanGraphBuilder{
Module: testModule(t, "graph-builder-plan-target-module-provider"),
Providers: []string{"null"},
Targets: []string{"module.child2"},
}
g, err := b.Build(RootModulePath)
if err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("Graph: %s", g.String())
testGraphNotContains(t, g, "module.child1.provider.null")
testGraphNotContains(t, g, "module.child1.null_resource.foo")
}
const testPlanGraphBuilderStr = `
aws_instance.web
aws_security_group.firewall

View File

@ -38,6 +38,13 @@ func (n *NodeAbstractProvider) Path() []string {
return n.PathValue
}
// RemovableIfNotTargeted
func (n *NodeAbstractProvider) RemoveIfNotTargeted() bool {
// We need to add this so that this node will be removed if
// it isn't targeted or a dependency of a target.
return true
}
// GraphNodeReferencer
func (n *NodeAbstractProvider) References() []string {
if n.Config == nil {

View File

@ -0,0 +1,4 @@
variable "key" {}
provider "null" { key = "${var.key}" }
resource "null_resource" "foo" {}

View File

@ -0,0 +1,4 @@
variable "key" {}
provider "null" { key = "${var.key}" }
resource "null_resource" "foo" {}

View File

@ -0,0 +1,2 @@
module "child1" { source = "./child1" }
module "child2" { source = "./child2" }

View File

@ -0,0 +1,4 @@
variable "key" {}
provider "null" { key = "${var.key}" }
resource "null_resource" "foo" {}

View File

@ -0,0 +1,4 @@
variable "key" {}
provider "null" { key = "${var.key}" }
resource "null_resource" "foo" {}

View File

@ -0,0 +1,9 @@
module "child1" {
source = "./child1"
key = "value"
}
module "child2" {
source = "./child2"
key = "value"
}