provider/openstack: Force Deletion of Instances (#12689)

This commit adds the `force_delete` argument, enabling instances
to be forcefully deleted.
This commit is contained in:
Joe Topjian 2017-03-14 10:26:59 -06:00 committed by Paul Stack
parent ad12d8804b
commit 9683f705f7
3 changed files with 46 additions and 3 deletions

View File

@ -324,6 +324,11 @@ func resourceComputeInstanceV2() *schema.Resource {
Optional: true, Optional: true,
Default: false, Default: false,
}, },
"force_delete": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
}, },
} }
} }
@ -882,9 +887,18 @@ func resourceComputeInstanceV2Delete(d *schema.ResourceData, meta interface{}) e
} }
} }
err = servers.Delete(computeClient, d.Id()).ExtractErr() if d.Get("force_delete").(bool) {
if err != nil { log.Printf("[DEBUG] Force deleting OpenStack Instance %s", d.Id())
return fmt.Errorf("Error deleting OpenStack server: %s", err) 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. // Wait for the instance to delete before moving on.

View File

@ -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 { func testAccCheckComputeV2InstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config) config := testAccProvider.Meta().(*Config)
computeClient, err := config.computeV2Client(OS_REGION_NAME) computeClient, err := config.computeV2Client(OS_REGION_NAME)
@ -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
}
`

View File

@ -337,6 +337,10 @@ The following arguments are supported:
before destroying it, thus giving chance for guest OS daemons to stop correctly. 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. 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: The `network` block supports: