Skip to content

Commit 28b1796

Browse files
Refactor Server ResizeWaitHandler (#873)
* Refactor server resizeWaitHandler * Change after review
1 parent 7f3d65d commit 28b1796

File tree

3 files changed

+21
-106
lines changed

3 files changed

+21
-106
lines changed

core/wait/wait.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ type AsyncActionCheck[T any] func() (waitFinished bool, response *T, err error)
2020

2121
// AsyncActionHandler handles waiting for a specific async action to be finished.
2222
type AsyncActionHandler[T any] struct {
23-
checkFn AsyncActionCheck[T]
24-
sleepBeforeWait time.Duration
25-
throttle time.Duration
26-
timeout time.Duration
27-
tempErrRetryLimit int
23+
checkFn AsyncActionCheck[T]
24+
sleepBeforeWait time.Duration
25+
throttle time.Duration
26+
timeout time.Duration
27+
tempErrRetryLimit int
28+
IntermediateStateReached bool
2829
}
2930

3031
// New initializes an AsyncActionHandler

services/iaasalpha/wait/wait.go

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -100,56 +100,38 @@ func CreateServerWaitHandler(ctx context.Context, a APIClientInterface, projectI
100100
return handler
101101
}
102102

103-
// ResizingServerWaitHandler will wait for a server to be resizing
104-
// Ii is an intermediate step inside the ResizeServerWaitHandler
105-
func resizingServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) *wait.AsyncActionHandler[iaasalpha.Server] {
103+
// ResizeServerWaitHandler will wait for server resize
104+
// It checks for an intermediate resizing status and only then waits for the server to become active
105+
func ResizeServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) (h *wait.AsyncActionHandler[iaasalpha.Server]) {
106106
handler := wait.New(func() (waitFinished bool, response *iaasalpha.Server, err error) {
107107
server, err := a.GetServerExecute(ctx, projectId, serverId)
108108
if err != nil {
109109
return false, server, err
110110
}
111+
111112
if server.Id == nil || server.Status == nil {
112113
return false, server, fmt.Errorf("resizing failed for server with id %s, the response is not valid: the id or the status are missing", serverId)
113114
}
114-
if *server.Id == serverId && *server.Status == ServerResizingStatus {
115-
return true, server, nil
116-
}
115+
117116
if *server.Id == serverId && *server.Status == ErrorStatus {
118117
if server.ErrorMessage != nil {
119118
return true, server, fmt.Errorf("resizing failed for server with id %s: %s", serverId, *server.ErrorMessage)
120119
}
121120
return true, server, fmt.Errorf("resizing failed for server with id %s", serverId)
122121
}
123-
return false, server, nil
124-
})
125-
handler.SetTimeout(10 * time.Minute)
126-
return handler
127-
}
128122

129-
// ResizeServerWaitHandler will wait for server resize
130-
// It checks for an intermediate resizing status and only then waits for the server to become active
131-
func ResizeServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) *wait.AsyncActionHandler[iaasalpha.Server] {
132-
handler := wait.New(func() (waitFinished bool, response *iaasalpha.Server, err error) {
133-
server, err := resizingServerWaitHandler(ctx, a, projectId, serverId).WaitWithContext(ctx)
134-
if err != nil {
135-
return false, server, err
136-
}
137-
server, err = a.GetServerExecute(ctx, projectId, serverId)
138-
if err != nil {
139-
return false, server, err
140-
}
141-
if server.Id == nil || server.Status == nil {
142-
return false, server, fmt.Errorf("resizing failed for server with id %s, the response is not valid: the id or the status are missing", serverId)
123+
if !h.IntermediateStateReached {
124+
if *server.Id == serverId && *server.Status == ServerResizingStatus {
125+
h.IntermediateStateReached = true
126+
return false, server, nil
127+
}
128+
return false, server, nil
143129
}
130+
144131
if *server.Id == serverId && *server.Status == ServerActiveStatus {
145132
return true, server, nil
146133
}
147-
if *server.Id == serverId && *server.Status == ErrorStatus {
148-
if server.ErrorMessage != nil {
149-
return true, server, fmt.Errorf("resizing failed for server with id %s: %s", serverId, *server.ErrorMessage)
150-
}
151-
return true, server, fmt.Errorf("resizing failed for server with id %s", serverId)
152-
}
134+
153135
return false, server, nil
154136
})
155137
handler.SetTimeout(20 * time.Minute)

services/iaasalpha/wait/wait_test.go

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -319,72 +319,6 @@ func TestDeleteServerWaitHandler(t *testing.T) {
319319
}
320320
}
321321

322-
func TestResizingServerWaitHandler(t *testing.T) {
323-
tests := []struct {
324-
desc string
325-
getFails bool
326-
resourceState string
327-
wantErr bool
328-
wantResp bool
329-
}{
330-
{
331-
desc: "resizing",
332-
getFails: false,
333-
resourceState: ServerResizingStatus,
334-
wantErr: false,
335-
wantResp: true,
336-
},
337-
{
338-
desc: "error_status",
339-
getFails: false,
340-
resourceState: ErrorStatus,
341-
wantErr: true,
342-
wantResp: true,
343-
},
344-
{
345-
desc: "get_fails",
346-
getFails: true,
347-
resourceState: "",
348-
wantErr: true,
349-
wantResp: false,
350-
},
351-
{
352-
desc: "timeout",
353-
getFails: false,
354-
resourceState: "ANOTHER Status",
355-
wantErr: true,
356-
wantResp: true,
357-
},
358-
}
359-
for _, tt := range tests {
360-
t.Run(tt.desc, func(t *testing.T) {
361-
apiClient := &apiClientMocked{
362-
getServerFails: tt.getFails,
363-
resourceState: tt.resourceState,
364-
}
365-
366-
var wantRes *iaasalpha.Server
367-
if tt.wantResp {
368-
wantRes = &iaasalpha.Server{
369-
Id: utils.Ptr("sid"),
370-
Status: &tt.resourceState,
371-
}
372-
}
373-
374-
handler := resizingServerWaitHandler(context.Background(), apiClient, "pid", "sid")
375-
376-
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
377-
378-
if (err != nil) != tt.wantErr {
379-
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
380-
}
381-
if !cmp.Equal(gotRes, wantRes) {
382-
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
383-
}
384-
})
385-
}
386-
}
387-
388322
func TestResizeServerWaitHandler(t *testing.T) {
389323
tests := []struct {
390324
desc string
@@ -449,11 +383,9 @@ func TestResizeServerWaitHandler(t *testing.T) {
449383
}
450384
}
451385

452-
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
453-
defer cancel()
454-
handler := ResizeServerWaitHandler(timeoutCtx, apiClient, "pid", "sid")
386+
handler := ResizeServerWaitHandler(context.Background(), apiClient, "pid", "sid")
455387

456-
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(timeoutCtx)
388+
gotRes, err := handler.SetThrottle(1 * time.Millisecond).SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
457389

458390
if (err != nil) != tt.wantErr {
459391
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)

0 commit comments

Comments
 (0)