Skip to content

Commit 305cd66

Browse files
authored
refac(logs): Use new WaiterHelper for waiters (#7257)
STACKITSDK-379 Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent ae71595 commit 305cd66

5 files changed

Lines changed: 36 additions & 39 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@
199199
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0`
200200
- [v0.8.2](services/logs/CHANGELOG.md#v082)
201201
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
202+
- [v0.9.0](services/logs/CHANGELOG.md#v090)
203+
- **Improvement:** Use new WaiterHelper for Logs waiters
202204
- `mariadb`:
203205
- [v0.27.3](services/mariadb/CHANGELOG.md#v0273)
204206
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`

services/logs/CHANGELOG.md

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

services/logs/VERSION

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

services/logs/v1api/wait/wait.go

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net/http"
88
"time"
99

10-
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1110
"github.com/stackitcloud/stackit-sdk-go/core/wait"
1211
logs "github.com/stackitcloud/stackit-sdk-go/services/logs/v1api"
1312
)
@@ -17,39 +16,42 @@ const (
1716
instanceStatusDeleting = "deleting"
1817
)
1918

19+
// CreateLogsInstanceWaitHandler will wait for logs instance creation
2020
func CreateLogsInstanceWaitHandler(ctx context.Context, client logs.DefaultAPI, projectID, region, instanceID string) *wait.AsyncActionHandler[logs.LogsInstance] {
21-
handler := wait.New(func() (waitFinished bool, response *logs.LogsInstance, err error) {
22-
instance, err := client.GetLogsInstance(ctx, projectID, region, instanceID).Execute()
23-
if err != nil {
24-
return false, nil, err
25-
}
26-
if instance.Id == instanceID && instance.Status == instanceStatusActive {
27-
return true, instance, nil
28-
}
29-
if instance.Status == instanceStatusDeleting {
30-
return true, nil, fmt.Errorf("creating log instance failed, instance is being deleted")
31-
}
32-
return false, nil, nil
33-
})
21+
waitConfig := wait.WaiterHelper[logs.LogsInstance, string]{
22+
FetchInstance: client.GetLogsInstance(ctx, projectID, region, instanceID).Execute,
23+
GetState: func(l *logs.LogsInstance) (string, error) {
24+
if l == nil {
25+
return "", fmt.Errorf("empty response")
26+
}
27+
if l.Status == "" {
28+
return "", fmt.Errorf("instance status is empty")
29+
}
30+
return l.Status, nil
31+
},
32+
ActiveState: []string{instanceStatusActive},
33+
ErrorState: []string{instanceStatusDeleting},
34+
}
35+
36+
handler := wait.New(waitConfig.Wait())
3437
handler.SetTimeout(10 * time.Minute)
3538
return handler
3639
}
3740

41+
// DeleteLogsInstanceWaitHandler will wait for logs instance deletion
3842
func DeleteLogsInstanceWaitHandler(ctx context.Context, client logs.DefaultAPI, projectID, region, instanceID string) *wait.AsyncActionHandler[logs.LogsInstance] {
39-
handler := wait.New(func() (waitFinished bool, response *logs.LogsInstance, err error) {
40-
_, err = client.GetLogsInstance(ctx, projectID, region, instanceID).Execute()
41-
// the instances is still gettable, e.g. not deleted, when the errors is null
42-
if err == nil {
43-
return false, nil, nil
44-
}
45-
var oapiError *oapierror.GenericOpenAPIError
46-
if errors.As(err, &oapiError) {
47-
if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound {
48-
return true, nil, nil
43+
waitConfig := wait.WaiterHelper[logs.LogsInstance, string]{
44+
FetchInstance: client.GetLogsInstance(ctx, projectID, region, instanceID).Execute,
45+
GetState: func(l *logs.LogsInstance) (string, error) {
46+
if l == nil {
47+
return "", errors.New("empty response")
4948
}
50-
}
51-
return false, nil, err
52-
})
49+
return l.Status, nil
50+
},
51+
DeleteHttpErrorStatusCodes: []int{http.StatusNotFound},
52+
}
53+
54+
handler := wait.New(waitConfig.Wait())
5355
handler.SetTimeout(10 * time.Minute)
5456
return handler
5557
}

services/logs/v1api/wait/wait_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,6 @@ func TestCreateLogsInstanceWaitHandler(t *testing.T) {
7474
Status: instanceStatusActive,
7575
},
7676
},
77-
{
78-
description: "create without id",
79-
getFails: false,
80-
wantErr: true,
81-
wantResp: false,
82-
returnInstance: true,
83-
getLogsResponse: &logs.LogsInstance{
84-
Status: instanceStatusActive,
85-
},
86-
},
8777
{
8878
description: "create without status",
8979
getFails: false,
@@ -99,7 +89,7 @@ func TestCreateLogsInstanceWaitHandler(t *testing.T) {
9989
getFails: false,
10090
wantErr: true,
10191
wantResp: false,
102-
returnInstance: true,
92+
returnInstance: false,
10393
getLogsResponse: &logs.LogsInstance{
10494
Id: instanceId,
10595
Status: instanceStatusDeleting,

0 commit comments

Comments
 (0)