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

detect scaled in resources when evaluating *s #17765

Merged
merged 1 commit into from
Apr 3, 2018
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
59 changes: 59 additions & 0 deletions terraform/context_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9541,3 +9541,62 @@ func TestContext2Apply_plannedInterpolatedCount(t *testing.T) {
t.Fatalf("apply failed: %s", err)
}
}

func TestContext2Apply_scaleInMultivarRef(t *testing.T) {
m := testModule(t, "apply-resource-scale-in")

p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn

providerResolver := ResourceProviderResolverFixed(
map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
)

s := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.one": {
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
},
Provider: "provider.aws",
},
"aws_instance.two": {
Type: "aws_instance",
Primary: &InstanceState{
ID: "foo",
Attributes: map[string]string{
"val": "foo",
},
},
Provider: "provider.aws",
},
},
},
},
}

ctx := testContext2(t, &ContextOpts{
Module: m,
ProviderResolver: providerResolver,
State: s,
Variables: map[string]interface{}{"count": "0"},
})

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

// Applying the plan should now succeed
_, err = ctx.Apply()
if err != nil {
t.Fatalf("apply failed: %s", err)
}
}
8 changes: 7 additions & 1 deletion terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,13 @@ func (i *Interpolater) resourceCountMax(
// use "cr.Count()" but that doesn't work if the count is interpolated
// and we can't guarantee that so we instead depend on the state.
max := -1
for k, _ := range ms.Resources {
for k, s := range ms.Resources {
// This resource may have been just removed, in which case the Primary
// may be nil, or just empty.
if s == nil || s.Primary == nil || len(s.Primary.Attributes) == 0 {
continue
}

// Get the index number for this resource
index := ""
if k == id {
Expand Down
13 changes: 13 additions & 0 deletions terraform/test-fixtures/apply-resource-scale-in/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "count" {}

resource "aws_instance" "one" {
count = "${var.count}"
}

locals {
"one_id" = "${element(concat(aws_instance.one.*.id, list("")), 0)}"
}

resource "aws_instance" "two" {
val = "${local.one_id}"
}