Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #219 from equinix/re_reservation_timeouts
Browse files Browse the repository at this point in the history
consider metal_device delete timeout settings when awaiting hardware deprovision
  • Loading branch information
displague authored Apr 11, 2022
2 parents c8e9ca6 + a3f731a commit 2e9d3a0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
8 changes: 8 additions & 0 deletions docs/resources/device.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ The `reinstall` block has 3 fields:
* `preserve_data` - (Optional) Whether the non-OS disks should be kept or wiped during reinstall. Defaults to `false`.
* `deprovision_fast` - (Optional) Whether the OS disk should be filled with `00h` bytes before reinstall. Defaults to `false`.

### Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/configuration/resources#operation-timeouts) for certain actions:

* `create` - (Defaults to 20 mins) Used when creating the Device. This includes the time to provision the OS.
* `update` - (Defaults to 20 mins) Used when updating the Device. This includes the time needed to reprovision instances when `reinstall` arguments are used.
* `delete` - (Defaults to 20 mins) Used when deleting the Device. This includes the time to deprovision a hardware reservation when `wait_for_reservation_deprovision` is enabled.

## Attributes Reference

The following attributes are exported:
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/spot_market_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The following arguments are supported:

### Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#operation-timeouts) for certain actions:
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/configuration/resources#operation-timeouts) for certain actions:

* `create` - (Defaults to 60 mins) Used when creating the Spot Market Request and `wait_for_devices == true`)
* `delete` - (Defaults to 60 mins) Used when destroying the Spot Market Request and `wait for devices == true`
Expand Down
9 changes: 6 additions & 3 deletions metal/helpers_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"github.com/packethost/packngo"
)

var wgMap = map[string]*sync.WaitGroup{}
var wgMutex = sync.Mutex{}
var (
wgMap = map[string]*sync.WaitGroup{}
wgMutex = sync.Mutex{}
)

func ifToIPCreateRequest(m interface{}) packngo.IPAddressCreateRequest {
iacr := packngo.IPAddressCreateRequest{}
Expand Down Expand Up @@ -131,6 +133,8 @@ func hwReservationStateRefreshFunc(client *packngo.Client, reservationId, instan
case r != nil && r.Device != nil && (r.Device.ID != "" && r.Device.ID != instanceId):
log.Printf("[WARN] Equinix Metal device instance %s (reservation %s) was reprovisioned to a another instance (%s)", instanceId, reservationId, r.Device.ID)
state = reprovisioned
default:
log.Printf("[DEBUG] Equinix Metal device instance %s (reservation %s) is still deprovisioning", instanceId, reservationId)
}

return r, state, err
Expand Down Expand Up @@ -162,7 +166,6 @@ func getWaitForDeviceLock(deviceID string) *sync.WaitGroup {
}

func waitForDeviceAttribute(d *schema.ResourceData, targets []string, pending []string, attribute string, meta interface{}) (string, error) {

wg := getWaitForDeviceLock(d.Id())
wg.Wait()

Expand Down
21 changes: 14 additions & 7 deletions metal/resource_metal_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ import (
"github.com/packethost/packngo"
)

var matchIPXEScript = regexp.MustCompile(`(?i)^#![i]?pxe`)
var ipAddressTypes = []string{"public_ipv4", "private_ipv4", "public_ipv6"}
var (
matchIPXEScript = regexp.MustCompile(`(?i)^#![i]?pxe`)
ipAddressTypes = []string{"public_ipv4", "private_ipv4", "public_ipv6"}
)

var deviceCommonIncludes = []string{"project", "metro", "facility", "hardware_reservation"}
var deviceReadOptions = &packngo.GetOptions{Includes: deviceCommonIncludes}
var (
deviceCommonIncludes = []string{"project", "metro", "facility", "hardware_reservation"}
deviceReadOptions = &packngo.GetOptions{Includes: deviceCommonIncludes}
)

func resourceMetalDevice() *schema.Resource {
return &schema.Resource{
Expand Down Expand Up @@ -719,12 +723,10 @@ func resourceMetalDeviceUpdate(d *schema.ResourceData, meta interface{}) error {
if _, _, err := client.Devices.Update(d.Id(), &ur); err != nil {
return friendlyError(err)
}

}

if d.HasChange("operating_system") || d.HasChange("user_data") || d.HasChange("custom_data") {
reinstallOptions, err := getReinstallOptions(d)

if err != nil {
return friendlyError(err)
}
Expand Down Expand Up @@ -773,6 +775,8 @@ func resourceMetalDeviceDelete(d *schema.ResourceData, meta interface{}) error {
fdv = true
}

start := time.Now()

resp, err := client.Devices.Delete(d.Id(), fdv)
if ignoreResponseErrors(httpForbidden, httpNotFound)(resp, err) != nil {
return friendlyError(err)
Expand All @@ -782,7 +786,10 @@ func resourceMetalDeviceDelete(d *schema.ResourceData, meta interface{}) error {
if resIdOk {
wfrd, wfrdOK := d.GetOk("wait_for_reservation_deprovision")
if wfrdOK && wfrd.(bool) {
err := waitUntilReservationProvisionable(client, resId.(string), d.Id(), 10*time.Second, 60*time.Minute, 3*time.Second)
// avoid "context: deadline exceeded"
timeout := d.Timeout(schema.TimeoutDelete) - time.Minute - time.Since(start)

err := waitUntilReservationProvisionable(client, resId.(string), d.Id(), 10*time.Second, timeout, 3*time.Second)
if err != nil {
return err
}
Expand Down

0 comments on commit 2e9d3a0

Please sign in to comment.