Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform: don't prune resource if count has interpolations #5302

Merged
merged 4 commits into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terraform

import (
"bytes"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -3929,3 +3930,87 @@ func TestContext2Apply_singleDestroy(t *testing.T) {
t.Fatalf("bad: %d", invokeCount)
}
}

// GH-5254
func TestContext2Apply_issue5254(t *testing.T) {
// Create a provider. We use "template" here just to match the repro
// we got from the issue itself.
p := testProvider("template")
p.ResourcesReturn = append(p.ResourcesReturn, ResourceType{
Name: "template_file",
})

p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn

// Apply cleanly step 0
t.Log("Applying Step 0")
ctx := testContext2(t, &ContextOpts{
Module: testModule(t, "issue-5254/step-0"),
Providers: map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
},
})

plan, err := ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}
t.Logf("Plan for Step 0: %s", plan)

state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}

// Application success. Now make the modification and store a plan
println("Planning Step 1")
t.Log("Planning Step 1")
ctx = testContext2(t, &ContextOpts{
Module: testModule(t, "issue-5254/step-1"),
State: state,
Providers: map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
},
})

plan, err = ctx.Plan()
if err != nil {
t.Fatalf("err: %s", err)
}

// Write / Read plan to simulate running it through a Plan file
var buf bytes.Buffer
if err := WritePlan(plan, &buf); err != nil {
t.Fatalf("err: %s", err)
}

planFromFile, err := ReadPlan(&buf)
if err != nil {
t.Fatalf("err: %s", err)
}

t.Logf("Plan for Step 1: %s", planFromFile)

// Apply the plan
println("Applying Step 1 (from Plan)")
t.Log("Applying Step 1 (from plan)")
ctx = planFromFile.Context(&ContextOpts{
Providers: map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
},
})

state, err = ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}

/*
actual := strings.TrimSpace(state.String())
expected := strings.TrimSpace(testTerraformApplyProviderAliasStr)
if actual != expected {
t.Fatalf("bad: \n%s", actual)
}
*/
}
8 changes: 8 additions & 0 deletions terraform/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func testDiffFn(
continue
}

// Ignore __-prefixed keys since they're used for magic
if k[0] == '_' && k[1] == '_' {
continue
}

if k == "nil" {
return nil, nil
}
Expand Down Expand Up @@ -100,6 +105,9 @@ func testDiffFn(
if k == "require_new" {
attrDiff.RequiresNew = true
}
if _, ok := c.Raw["__"+k+"_requires_new"]; ok {
attrDiff.RequiresNew = true
}
diff.Attributes[k] = attrDiff
}

Expand Down
7 changes: 7 additions & 0 deletions terraform/graph_config_node_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ func (n *GraphNodeConfigResource) Noop(opts *NoopOpts) bool {
return false
}

// If the count has any interpolations, we can't prune this node since
// we need to be sure to evaluate the count so that splat variables work
// later (which need to know the full count).
if len(n.Resource.RawCount.Interpolations) > 0 {
return false
}

// If we have no module diff, we're certainly a noop. This is because
// it means there is a diff, and that the module we're in just isn't
// in it, meaning we're not doing anything.
Expand Down
10 changes: 10 additions & 0 deletions terraform/test-fixtures/issue-5254/step-0/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
variable "c" { default = 1 }

resource "template_file" "parent" {
count = "${var.c}"
template = "Hi"
}

resource "template_file" "child" {
template = "${join(",", template_file.parent.*.template)} ok"
}
11 changes: 11 additions & 0 deletions terraform/test-fixtures/issue-5254/step-1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "c" { default = 1 }

resource "template_file" "parent" {
count = "${var.c}"
template = "Hi"
}

resource "template_file" "child" {
template = "${join(",", template_file.parent.*.template)}"
__template_requires_new = 1
}