Skip to content

Commit b2ffcc0

Browse files
authored
Merge branch 'main' into feat/STACKITSDK-388-redis-refactor-wait-handler
2 parents 98e8cac + 8ac1cea commit b2ffcc0

5 files changed

Lines changed: 68 additions & 135 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@
276276
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0`
277277
- [v0.27.2](services/opensearch/CHANGELOG.md#v0272)
278278
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
279+
- [v0.28.0](services/opensearch/CHANGELOG.md#v0280)
280+
- **Improvement:** Use new WaiterHelper for opensearch waiters
279281
- `postgresflex`:
280282
- [v1.6.3](services/postgresflex/CHANGELOG.md#v163)
281283
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`

services/opensearch/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.28.0
2+
- **Improvement:** Use new WaiterHelper for opensearch waiters
3+
14
## v0.27.2
25
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
36

services/opensearch/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.27.2
1+
v0.28.0

services/opensearch/v1api/wait/wait.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wait
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"strings"
@@ -27,46 +28,12 @@ const (
2728

2829
// CreateInstanceWaitHandler will wait for instance creation
2930
func CreateInstanceWaitHandler(ctx context.Context, a opensearch.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[opensearch.Instance] {
30-
handler := wait.New(func() (waitFinished bool, response *opensearch.Instance, err error) {
31-
s, err := a.GetInstance(ctx, projectId, instanceId).Execute()
32-
if err != nil {
33-
return false, nil, err
34-
}
35-
if s.Status == nil {
36-
return false, nil, fmt.Errorf("create failed for instance with id %s. The response is not valid: the status are missing", instanceId)
37-
}
38-
switch *s.Status {
39-
case INSTANCESTATUS_ACTIVE:
40-
return true, s, nil
41-
case INSTANCESTATUS_FAILED:
42-
return true, s, fmt.Errorf("create failed for instance with id %s: %s", instanceId, s.LastOperation.Description)
43-
}
44-
return false, nil, nil
45-
})
46-
handler.SetTimeout(45 * time.Minute)
47-
return handler
31+
return createOrUpdateInstanceWaitHandler(ctx, a, projectId, instanceId)
4832
}
4933

5034
// PartialUpdateInstanceWaitHandler will wait for instance update
5135
func PartialUpdateInstanceWaitHandler(ctx context.Context, a opensearch.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[opensearch.Instance] {
52-
handler := wait.New(func() (waitFinished bool, response *opensearch.Instance, err error) {
53-
s, err := a.GetInstance(ctx, projectId, instanceId).Execute()
54-
if err != nil {
55-
return false, nil, err
56-
}
57-
if s.Status == nil {
58-
return false, nil, fmt.Errorf("update failed for instance with id %s. The response is not valid: the instance id or the status are missing", instanceId)
59-
}
60-
switch *s.Status {
61-
case INSTANCESTATUS_ACTIVE:
62-
return true, s, nil
63-
case INSTANCESTATUS_FAILED:
64-
return true, s, fmt.Errorf("update failed for instance with id %s: %s", instanceId, s.LastOperation.Description)
65-
}
66-
return false, nil, nil
67-
})
68-
handler.SetTimeout(45 * time.Minute)
69-
return handler
36+
return createOrUpdateInstanceWaitHandler(ctx, a, projectId, instanceId)
7037
}
7138

7239
// DeleteInstanceWaitHandler will wait for instance deletion
@@ -144,3 +111,24 @@ func DeleteCredentialsWaitHandler(ctx context.Context, a opensearch.DefaultAPI,
144111
handler.SetTimeout(1 * time.Minute)
145112
return handler
146113
}
114+
115+
func createOrUpdateInstanceWaitHandler(ctx context.Context, a opensearch.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[opensearch.Instance] {
116+
waitConfig := wait.WaiterHelper[opensearch.Instance, string]{
117+
FetchInstance: a.GetInstance(ctx, projectId, instanceId).Execute,
118+
GetState: func(s *opensearch.Instance) (string, error) {
119+
if s == nil {
120+
return "", errors.New("empty response")
121+
}
122+
if s.Status == nil {
123+
return "", errors.New("status is missing")
124+
}
125+
return *s.Status, nil
126+
},
127+
ActiveState: []string{INSTANCESTATUS_ACTIVE},
128+
ErrorState: []string{INSTANCESTATUS_FAILED},
129+
}
130+
131+
handler := wait.New(waitConfig.Wait())
132+
handler.SetTimeout(45 * time.Minute)
133+
return handler
134+
}

services/opensearch/v1api/wait/wait_test.go

Lines changed: 38 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wait
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67
"testing/synctest"
78
"time"
@@ -10,6 +11,7 @@ import (
1011

1112
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1213
"github.com/stackitcloud/stackit-sdk-go/core/utils"
14+
"github.com/stackitcloud/stackit-sdk-go/core/wait"
1315
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"
1416
)
1517

@@ -77,7 +79,7 @@ func newAPIMock(settings *mockSettings) opensearch.DefaultAPI {
7779
}
7880
}
7981

80-
func TestCreateInstanceWaitHandler(t *testing.T) {
82+
func TestCreateOrUpdateInstanceWaitHandler(t *testing.T) {
8183
tests := []struct {
8284
desc string
8385
getFails bool
@@ -86,14 +88,14 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
8688
wantResp bool
8789
}{
8890
{
89-
desc: "create_succeeded",
91+
desc: "succeeded",
9092
getFails: false,
9193
resourceState: utils.Ptr(INSTANCESTATUS_ACTIVE),
9294
wantErr: false,
9395
wantResp: true,
9496
},
9597
{
96-
desc: "create_failed",
98+
desc: "failed",
9799
getFails: false,
98100
resourceState: utils.Ptr(INSTANCESTATUS_FAILED),
99101
wantErr: true,
@@ -113,108 +115,46 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
113115
wantResp: false,
114116
},
115117
}
116-
for _, tt := range tests {
117-
t.Run(tt.desc, func(t *testing.T) {
118-
synctest.Test(t, func(t *testing.T) {
119-
instanceId := "foo-bar"
120-
121-
apiClient := newAPIMock(&mockSettings{
122-
instanceGetFails: tt.getFails,
123-
instanceResourceId: instanceId,
124-
instanceResourceState: tt.resourceState,
125-
})
126-
127-
var wantRes *opensearch.Instance
128-
if tt.wantResp {
129-
wantRes = &opensearch.Instance{
130-
InstanceId: &instanceId,
131-
Status: tt.resourceState,
132-
}
133-
}
134-
135-
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "pid", instanceId)
136-
137-
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
138-
139-
if (err != nil) != tt.wantErr {
140-
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
141-
}
142-
diff := cmp.Diff(gotRes, wantRes)
143-
if diff != "" {
144-
t.Fatalf("handler gotRes = %+v\n want %+v\n diff = %s", gotRes, wantRes, diff)
145-
}
146-
})
147-
})
148-
}
149-
}
150118

151-
func TestUpdateInstanceWaitHandler(t *testing.T) {
152-
tests := []struct {
153-
desc string
154-
getFails bool
155-
resourceState *string
156-
wantErr bool
157-
wantResp bool
158-
}{
159-
{
160-
desc: "update_succeeded",
161-
getFails: false,
162-
resourceState: utils.Ptr(INSTANCESTATUS_ACTIVE),
163-
wantErr: false,
164-
wantResp: true,
165-
},
166-
{
167-
desc: "update_failed",
168-
getFails: false,
169-
resourceState: utils.Ptr(INSTANCESTATUS_FAILED),
170-
wantErr: true,
171-
wantResp: true,
172-
},
173-
{
174-
desc: "get_fails",
175-
getFails: true,
176-
wantErr: true,
177-
wantResp: false,
178-
},
179-
{
180-
desc: "timeout",
181-
getFails: false,
182-
resourceState: utils.Ptr("ANOTHER STATE"),
183-
wantErr: true,
184-
wantResp: false,
185-
},
119+
handlers := map[string]func(context.Context, opensearch.DefaultAPI, string, string) *wait.AsyncActionHandler[opensearch.Instance]{
120+
"common logic": createOrUpdateInstanceWaitHandler,
121+
"create": CreateInstanceWaitHandler,
122+
"update": PartialUpdateInstanceWaitHandler,
186123
}
187-
for _, tt := range tests {
188-
t.Run(tt.desc, func(t *testing.T) {
189-
synctest.Test(t, func(t *testing.T) {
190-
instanceId := "foo-bar"
191-
192-
apiClient := newAPIMock(&mockSettings{
193-
instanceGetFails: tt.getFails,
194-
instanceResourceId: instanceId,
195-
instanceResourceState: tt.resourceState,
196-
})
197124

198-
var wantRes *opensearch.Instance
199-
if tt.wantResp {
200-
wantRes = &opensearch.Instance{
201-
InstanceId: &instanceId,
202-
Status: tt.resourceState,
125+
for handlerDesc, handlerFn := range handlers {
126+
for _, tt := range tests {
127+
t.Run(fmt.Sprintf("%s - %s", handlerDesc, tt.desc), func(t *testing.T) {
128+
synctest.Test(t, func(t *testing.T) {
129+
instanceId := "foo-bar"
130+
131+
apiClient := newAPIMock(&mockSettings{
132+
instanceGetFails: tt.getFails,
133+
instanceResourceId: instanceId,
134+
instanceResourceState: tt.resourceState,
135+
})
136+
137+
var wantRes *opensearch.Instance
138+
if tt.wantResp {
139+
wantRes = &opensearch.Instance{
140+
InstanceId: &instanceId,
141+
Status: tt.resourceState,
142+
}
203143
}
204-
}
205144

206-
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceId)
145+
handler := handlerFn(context.Background(), apiClient, "pid", instanceId)
146+
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
207147

208-
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
209-
210-
if (err != nil) != tt.wantErr {
211-
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
212-
}
213-
if !cmp.Equal(gotRes, wantRes) {
214-
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
215-
}
148+
if (err != nil) != tt.wantErr {
149+
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
150+
}
151+
diff := cmp.Diff(gotRes, wantRes)
152+
if diff != "" {
153+
t.Fatalf("handler gotRes = %+v\n want %+v\n diff = %s", gotRes, wantRes, diff)
154+
}
155+
})
216156
})
217-
})
157+
}
218158
}
219159
}
220160

0 commit comments

Comments
 (0)