Skip to content

Commit

Permalink
provider/openstack: Force Deletion of Instances (#12689)
Browse files Browse the repository at this point in the history
This commit adds the `force_delete` argument, enabling instances
to be forcefully deleted.
  • Loading branch information
jtopjian authored and stack72 committed Mar 14, 2017
1 parent db0adac commit e495426
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ func resourceComputeInstanceV2() *schema.Resource {
Optional: true,
Default: false,
},
"force_delete": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand Down Expand Up @@ -882,9 +887,18 @@ func resourceComputeInstanceV2Delete(d *schema.ResourceData, meta interface{}) e
}
}

err = servers.Delete(computeClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenStack server: %s", err)
if d.Get("force_delete").(bool) {
log.Printf("[DEBUG] Force deleting OpenStack Instance %s", d.Id())
err = servers.ForceDelete(computeClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenStack server: %s", err)
}
} else {
log.Printf("[DEBUG] Deleting OpenStack Instance %s", d.Id())
err = servers.Delete(computeClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenStack server: %s", err)
}
}

// Wait for the instance to delete before moving on.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,23 @@ func TestAccComputeV2Instance_metadataRemove(t *testing.T) {
})
}

func TestAccComputeV2Instance_forceDelete(t *testing.T) {
var instance servers.Server
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeV2InstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeV2Instance_forceDelete,
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeV2InstanceExists("openstack_compute_instance_v2.instance_1", &instance),
),
},
},
})
}

func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
computeClient, err := config.computeV2Client(OS_REGION_NAME)
Expand Down Expand Up @@ -1515,3 +1532,11 @@ resource "openstack_compute_instance_v2" "instance_1" {
}
}
`

const testAccComputeV2Instance_forceDelete = `
resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
security_groups = ["default"]
force_delete = true
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ The following arguments are supported:
before destroying it, thus giving chance for guest OS daemons to stop correctly.
If instance doesn't stop within timeout, it will be destroyed anyway.

* `force_delete` - (Optional) Whether to force the OpenStack instance to be
forcefully deleted. This is useful for environments that have reclaim / soft
deletion enabled.


The `network` block supports:

Expand Down

0 comments on commit e495426

Please sign in to comment.