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

chore(baremetal): add support for fast retries with cassettes #1193

Merged
merged 3 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 additions & 0 deletions scaleway/helpers_baremetal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scaleway

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -12,6 +13,7 @@ const (
baremetalServerWaitForTimeout = 60 * time.Minute
baremetalServerRetryFuncTimeout = baremetalServerWaitForTimeout + time.Minute // some RetryFunc are calling a WaitFor
defaultBaremetalServerTimeout = baremetalServerRetryFuncTimeout + time.Minute
baremetalRetryInterval = 5 * time.Second
)

// instanceAPIWithZone returns a new baremetal API and the zone for a Create request
Expand Down Expand Up @@ -99,3 +101,35 @@ func flattenBaremetalIPs(ips []*baremetal.IP) interface{} {
}
return flattendIPs
}

func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
retryInterval := baremetalRetryInterval
if DefaultWaitRetryInterval != nil {
retryInterval = *DefaultWaitRetryInterval
}

server, err := api.WaitForServer(&baremetal.WaitForServerRequest{
Zone: zone,
ServerID: ID,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))

return server, err
}

func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
retryInterval := baremetalRetryInterval
if DefaultWaitRetryInterval != nil {
retryInterval = *DefaultWaitRetryInterval
}

server, err := api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
Zone: zone,
ServerID: ID,
Timeout: scw.TimeDurationPtr(timeout),
RetryInterval: &retryInterval,
}, scw.WithContext(ctx))

return server, err
}
34 changes: 6 additions & 28 deletions scaleway/resource_baremetal_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,7 @@ func resourceScalewayBaremetalServerCreate(ctx context.Context, d *schema.Resour

d.SetId(newZonedID(server.Zone, server.ID).String())

_, err = baremetalAPI.WaitForServer(&baremetal.WaitForServerRequest{
Zone: server.Zone,
ServerID: server.ID,
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutCreate)),
RetryInterval: DefaultWaitRetryInterval,
})
_, err = waitForBaremetalServer(ctx, baremetalAPI, zone, server.ID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -173,12 +168,7 @@ func resourceScalewayBaremetalServerCreate(ctx context.Context, d *schema.Resour
return diag.FromErr(err)
}

_, err = baremetalAPI.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
Zone: server.Zone,
ServerID: server.ID,
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutCreate)),
RetryInterval: DefaultWaitRetryInterval,
})
_, err = waitForBaremetalServerInstall(ctx, baremetalAPI, zone, server.ID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -255,17 +245,12 @@ func resourceScalewayBaremetalServerUpdate(ctx context.Context, d *schema.Resour
SSHKeyIDs: expandStrings(d.Get("ssh_key_ids")),
}

server, err := baremetalAPI.InstallServer(installReq, scw.WithContext(ctx))
_, err := baremetalAPI.InstallServer(installReq, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

_, err = baremetalAPI.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
Zone: server.Zone,
ServerID: server.ID,
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutUpdate)),
RetryInterval: DefaultWaitRetryInterval,
})
_, err = waitForBaremetalServerInstall(ctx, baremetalAPI, zonedID.Zone, zonedID.ID, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -280,25 +265,18 @@ func resourceScalewayBaremetalServerDelete(ctx context.Context, d *schema.Resour
return diag.FromErr(err)
}

server, err := baremetalAPI.DeleteServer(&baremetal.DeleteServerRequest{
_, err = baremetalAPI.DeleteServer(&baremetal.DeleteServerRequest{
Zone: zonedID.Zone,
ServerID: zonedID.ID,
}, scw.WithContext(ctx))

if err != nil {
if is404Error(err) {
return nil
}
return diag.FromErr(err)
}

_, err = baremetalAPI.WaitForServer(&baremetal.WaitForServerRequest{
Zone: server.Zone,
ServerID: server.ID,
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutDelete)),
RetryInterval: DefaultWaitRetryInterval,
})

_, err = waitForBaremetalServer(ctx, baremetalAPI, zonedID.Zone, zonedID.ID, d.Timeout(schema.TimeoutDelete))
if err != nil && !is404Error(err) {
return diag.FromErr(err)
}
Expand Down