terraform: destroy orphans

This commit is contained in:
Mitchell Hashimoto 2014-06-25 21:58:33 -07:00
parent e8035a5c96
commit 1d9d33c8a0
4 changed files with 61 additions and 7 deletions

View File

@ -99,7 +99,11 @@ func (d *Diff) String() string {
rdiff := d.Resources[name] rdiff := d.Resources[name]
crud := "UPDATE" crud := "UPDATE"
if rdiff.RequiresNew() { if rdiff.RequiresNew() && rdiff.Destroy {
crud = "DESTROY/CREATE"
} else if rdiff.Destroy {
crud = "DESTROY"
} else if rdiff.RequiresNew() {
crud = "CREATE" crud = "CREATE"
} }
@ -179,7 +183,7 @@ func (d *ResourceDiff) Empty() bool {
return true return true
} }
return len(d.Attributes) == 0 return !d.Destroy && len(d.Attributes) == 0
} }
// RequiresNew returns true if the diff requires the creation of a new // RequiresNew returns true if the diff requires the creation of a new

View File

@ -225,11 +225,19 @@ func (t *Terraform) planWalkFn(
result.init() result.init()
cb := func(r *Resource) (map[string]string, error) { cb := func(r *Resource) (map[string]string, error) {
var diff *ResourceDiff
if r.Config == nil {
// This is an orphan (no config), so we mark it to be destroyed
diff = &ResourceDiff{Destroy: true}
} else {
// Get a diff from the newest state // Get a diff from the newest state
diff, err := r.Provider.Diff(r.State, r.Config) var err error
diff, err = r.Provider.Diff(r.State, r.Config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
l.Lock() l.Lock()
if !diff.Empty() { if !diff.Empty() {
@ -304,7 +312,7 @@ func (t *Terraform) genericWalkFn(
} }
// Make sure that at least some resource configuration is set // Make sure that at least some resource configuration is set
if rn.Resource.Config == nil { if rn.Resource.Config == nil && !rn.Orphan {
if rn.Config == nil { if rn.Config == nil {
rn.Resource.Config = new(ResourceConfig) rn.Resource.Config = new(ResourceConfig)
} else { } else {

View File

@ -161,6 +161,31 @@ func TestTerraformPlan_computed(t *testing.T) {
} }
} }
func TestTerraformPlan_orphan(t *testing.T) {
c := testConfig(t, "plan-orphan")
tf := testTerraform2(t, nil)
s := &State{
Resources: map[string]*ResourceState{
"aws_instance.baz": &ResourceState{
ID: "bar",
Type: "aws_instance",
},
},
}
plan, err := tf.Plan(c, s, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
actual := strings.TrimSpace(plan.String())
expected := strings.TrimSpace(testTerraformPlanOrphanStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}
func TestTerraformPlan_state(t *testing.T) { func TestTerraformPlan_state(t *testing.T) {
c := testConfig(t, "plan-good") c := testConfig(t, "plan-good")
tf := testTerraform2(t, nil) tf := testTerraform2(t, nil)
@ -495,6 +520,20 @@ STATE:
<no state> <no state>
` `
const testTerraformPlanOrphanStr = `
DIFF:
DESTROY: aws_instance.baz
UPDATE: aws_instance.foo
num: "" => "2"
type: "" => "aws_instance"
STATE:
aws_instance.baz:
ID = bar
`
const testTerraformPlanStateStr = ` const testTerraformPlanStateStr = `
DIFF: DIFF:

View File

@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
num = "2"
}