Skip to content

Commit 76cc42b

Browse files
authored
chore(baremetal): add support for fast retries with cassettes (#1193)
1 parent 209531f commit 76cc42b

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

scaleway/helpers_baremetal.go

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scaleway
22

33
import (
4+
"context"
45
"time"
56

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

1719
// instanceAPIWithZone returns a new baremetal API and the zone for a Create request
@@ -99,3 +101,35 @@ func flattenBaremetalIPs(ips []*baremetal.IP) interface{} {
99101
}
100102
return flattendIPs
101103
}
104+
105+
func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
106+
retryInterval := baremetalRetryInterval
107+
if DefaultWaitRetryInterval != nil {
108+
retryInterval = *DefaultWaitRetryInterval
109+
}
110+
111+
server, err := api.WaitForServer(&baremetal.WaitForServerRequest{
112+
Zone: zone,
113+
ServerID: ID,
114+
Timeout: scw.TimeDurationPtr(timeout),
115+
RetryInterval: &retryInterval,
116+
}, scw.WithContext(ctx))
117+
118+
return server, err
119+
}
120+
121+
func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone scw.Zone, ID string, timeout time.Duration) (*baremetal.Server, error) {
122+
retryInterval := baremetalRetryInterval
123+
if DefaultWaitRetryInterval != nil {
124+
retryInterval = *DefaultWaitRetryInterval
125+
}
126+
127+
server, err := api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
128+
Zone: zone,
129+
ServerID: ID,
130+
Timeout: scw.TimeDurationPtr(timeout),
131+
RetryInterval: &retryInterval,
132+
}, scw.WithContext(ctx))
133+
134+
return server, err
135+
}

scaleway/resource_baremetal_server.go

+6-28
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ func resourceScalewayBaremetalServerCreate(ctx context.Context, d *schema.Resour
152152

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

155-
_, err = baremetalAPI.WaitForServer(&baremetal.WaitForServerRequest{
156-
Zone: server.Zone,
157-
ServerID: server.ID,
158-
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutCreate)),
159-
RetryInterval: DefaultWaitRetryInterval,
160-
})
155+
_, err = waitForBaremetalServer(ctx, baremetalAPI, zone, server.ID, d.Timeout(schema.TimeoutCreate))
161156
if err != nil {
162157
return diag.FromErr(err)
163158
}
@@ -173,12 +168,7 @@ func resourceScalewayBaremetalServerCreate(ctx context.Context, d *schema.Resour
173168
return diag.FromErr(err)
174169
}
175170

176-
_, err = baremetalAPI.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
177-
Zone: server.Zone,
178-
ServerID: server.ID,
179-
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutCreate)),
180-
RetryInterval: DefaultWaitRetryInterval,
181-
})
171+
_, err = waitForBaremetalServerInstall(ctx, baremetalAPI, zone, server.ID, d.Timeout(schema.TimeoutCreate))
182172
if err != nil {
183173
return diag.FromErr(err)
184174
}
@@ -255,17 +245,12 @@ func resourceScalewayBaremetalServerUpdate(ctx context.Context, d *schema.Resour
255245
SSHKeyIDs: expandStrings(d.Get("ssh_key_ids")),
256246
}
257247

258-
server, err := baremetalAPI.InstallServer(installReq, scw.WithContext(ctx))
248+
_, err := baremetalAPI.InstallServer(installReq, scw.WithContext(ctx))
259249
if err != nil {
260250
return diag.FromErr(err)
261251
}
262252

263-
_, err = baremetalAPI.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
264-
Zone: server.Zone,
265-
ServerID: server.ID,
266-
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutUpdate)),
267-
RetryInterval: DefaultWaitRetryInterval,
268-
})
253+
_, err = waitForBaremetalServerInstall(ctx, baremetalAPI, zonedID.Zone, zonedID.ID, d.Timeout(schema.TimeoutUpdate))
269254
if err != nil {
270255
return diag.FromErr(err)
271256
}
@@ -280,25 +265,18 @@ func resourceScalewayBaremetalServerDelete(ctx context.Context, d *schema.Resour
280265
return diag.FromErr(err)
281266
}
282267

283-
server, err := baremetalAPI.DeleteServer(&baremetal.DeleteServerRequest{
268+
_, err = baremetalAPI.DeleteServer(&baremetal.DeleteServerRequest{
284269
Zone: zonedID.Zone,
285270
ServerID: zonedID.ID,
286271
}, scw.WithContext(ctx))
287-
288272
if err != nil {
289273
if is404Error(err) {
290274
return nil
291275
}
292276
return diag.FromErr(err)
293277
}
294278

295-
_, err = baremetalAPI.WaitForServer(&baremetal.WaitForServerRequest{
296-
Zone: server.Zone,
297-
ServerID: server.ID,
298-
Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutDelete)),
299-
RetryInterval: DefaultWaitRetryInterval,
300-
})
301-
279+
_, err = waitForBaremetalServer(ctx, baremetalAPI, zonedID.Zone, zonedID.ID, d.Timeout(schema.TimeoutDelete))
302280
if err != nil && !is404Error(err) {
303281
return diag.FromErr(err)
304282
}

0 commit comments

Comments
 (0)