terraform: new apply graph understands destroying deposed only

This commit is contained in:
Mitchell Hashimoto 2016-11-28 13:41:22 -08:00
parent 6ee3a08799
commit 41c56ad002
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
8 changed files with 134 additions and 1 deletions

View File

@ -1014,6 +1014,61 @@ aws_instance.bar.1:
`)
}
// Test that when we have a deposed instance but a good primary, we still
// destroy the deposed instance.
func TestContext2Apply_createBeforeDestroy_deposedOnly(t *testing.T) {
m := testModule(t, "apply-cbd-deposed-only")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.bar": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
},
Deposed: []*InstanceState{
&InstanceState{
ID: "foo",
},
},
},
},
},
},
}
ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: state,
})
if p, err := ctx.Plan(); err != nil {
t.Fatalf("err: %s", err)
} else {
t.Logf(p.String())
}
state, err := ctx.Apply()
if err != nil {
t.Fatalf("err: %s", err)
}
checkStateString(t, state, `
aws_instance.bar:
ID = bar
`)
}
func TestContext2Apply_destroyComputed(t *testing.T) {
m := testModule(t, "apply-destroy-computed")
p := testProvider("aws")

View File

@ -0,0 +1,3 @@
resource "aws_instance" "bar" {
lifecycle { create_before_destroy = true }
}

View File

@ -0,0 +1,7 @@
resource "aws_instance" "bar" {
count = 2
require_new = "xyz"
lifecycle {
create_before_destroy = true
}
}

View File

@ -58,7 +58,7 @@ func (t *DiffTransformer) Transform(g *Graph) error {
addr.Path = m.Path[1:]
// If we're destroying, add the destroy node
if inst.Destroy {
if inst.Destroy || inst.GetDestroyDeposed() {
abstract := &NodeAbstractResource{Addr: addr}
g.Add(&NodeDestroyResource{NodeAbstractResource: abstract})
}

9
test/foo/main.tf Normal file
View File

@ -0,0 +1,9 @@
resource "null_resource" "foo" {
count = 2
provisioner "local-exec" { command = "sleep ${count.index*3}" }
//provisioner "local-exec" { command = "exit 1" }
lifecycle { create_before_destroy = true }
}

1
test/index.html Normal file
View File

@ -0,0 +1 @@
Hello, World

13
test/main.tf Normal file
View File

@ -0,0 +1,13 @@
variable "username" {
default = "bob"
}
data "template_file" "user" {
template = "$${USERNAME}"
vars {
USERNAME = "${var.username}"
}
provisioner "local-exec" {
command = "echo ${self.rendered} > user.txt"
}
}

45
test/real.tfstate Normal file
View File

@ -0,0 +1,45 @@
{
"version": 3,
"terraform_version": "0.7.2",
"serial": 393,
"lineage": "e8e8cc31-ebe6-4260-bb6a-eed53258cc08",
"modules": [
{
"path": [
"root"
],
"outputs": {},
"resources": {
"aws_route53_record.rslc-xxx-test-route53-record": {
"type": "aws_route53_record",
"depends_on": [
],
"primary": {
"id": "ZKBENUPLDUDMJ_xxx-test.rslcare.com.au_A",
"attributes": {
"alias.#": "1",
"alias.2624757427.evaluate_target_health": "false",
"alias.2624757427.name": "awseb-e-3-AWSEBLoa-1OO1X7V7IRMMF-999999999.ap-southeast-2.elb.amazonaws.com.",
"alias.2624757427.zone_id": "Z2999QAZ9SRTIC",
"fqdn": "hws-test.rslcare.com.au",
"health_check_id": "",
"id": "ZKBENUPLDUDMJ_hws-test.rslcare.com.au_A",
"name": "xxx-test.example.com",
"records.#": "0",
"set_identifier": "",
"ttl": "0",
"type": "A",
"zone_id": "ZKBENUPLDUDMJ"
},
"meta": {
"schema_version": "2"
},
"tainted": false
},
"deposed": [],
"provider": ""
}
}
}
]
}