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

adds wait_for_shutoff to resource_libvirt_domain #1066

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions libvirt/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,16 @@ func destroyDomainByUserRequest(virConn *libvirt.Libvirt, d *schema.ResourceData

return nil
}

func waitForDomainShutoff(virConn *libvirt.Libvirt, domain libvirt.Domain) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
state, _, err := virConn.DomainGetState(domain, 0)
if err != nil {
return nil, "", err
}
if libvirt.DomainState(state) == libvirt.DomainShutoff {
return domain, "SHUTOFF", nil
}
return domain, "NOT-SHUTOFF", err
}
}
29 changes: 29 additions & 0 deletions libvirt/resource_libvirt_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
libvirt "github.com/digitalocean/go-libvirt"
"github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/suppress"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"libvirt.org/go/libvirtxml"
)
Expand All @@ -41,6 +42,7 @@ func resourceLibvirtDomain() *schema.Resource {
Timeouts: &schema.ResourceTimeout{
//nolint:gomnd
Create: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -101,6 +103,13 @@ func resourceLibvirtDomain() *schema.Resource {
},
},
},
"wait_for_shutoff": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: false,
Required: false,
},
"running": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -1118,6 +1127,26 @@ func resourceLibvirtDomainDelete(ctx context.Context, d *schema.ResourceData, me
return diag.Errorf("couldn't get info about domain: %s", err)
}

if d.Get("wait_for_shutoff").(bool) && libvirt.DomainState(state) == libvirt.DomainRunning {
log.Printf("[DEBUG] Shutting down libvirt_domain")
if err := virConn.DomainShutdown(domain); err != nil {
return diag.Errorf("couldn't shutdown libvirt domain: %s", err)
}

stateConf := &resource.StateChangeConf{
Pending: []string{"NOT-SHUTOFF"},
Target: []string{"SHUTOFF"},
Refresh: waitForDomainShutoff(virConn, domain),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: resourceStateDelay,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for domain to reach SHUTOFF state: %s", err)
}
state = int32(libvirt.DomainShutoff)
}

if state == int32(libvirt.DomainRunning) || state == int32(libvirt.DomainPaused) {
if err := virConn.DomainDestroy(domain); err != nil {
return diag.Errorf("couldn't destroy libvirt domain: %s", err)
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The following arguments are supported:
will be created.
* `memory` - (Optional) The amount of memory in MiB. If not specified the domain
will be created with 512 MiB of memory be used.
* `wait_for_shutoff` - (Optional) Use `true` to shutoff instance before destroying. Allows guest VM to shutdown gracefully. If not specified `false` is assumed.
* `running` - (Optional) Use `false` to turn off the instance. If not specified,
true is assumed and the instance, if stopped, will be started at next apply.
* `disk` - (Optional) An array of one or more disks to attach to the domain. The
Expand Down