Skip to content

Commit 3237d2f

Browse files
authored
Merge branch 'main' into si/vpn-waiter
2 parents c408573 + 305cd66 commit 3237d2f

8 files changed

Lines changed: 75 additions & 88 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@
178178
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0`
179179
- [v1.12.2](services/loadbalancer/CHANGELOG.md#v1122)
180180
- **Dependencies:** Bump STACKIT SDK core module to `v0.26.0`
181+
- [v1.13.0](services/loadbalancer/CHANGELOG.md#v1130)
182+
- **Improvement:** Use new WaiterHelper for LoadBalancer waiters
183+
- **Breaking Change:** `v2api/wait/DeleteLoadBalancerWaitHandler` now returns a `LoadBalancer` instead of `struct{}`
181184
- `logme`:
182185
- [v0.27.3](services/logme/CHANGELOG.md#v0273)
183186
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`
@@ -196,6 +199,8 @@
196199
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0`
197200
- [v0.8.2](services/logs/CHANGELOG.md#v082)
198201
- **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
199204
- `mariadb`:
200205
- [v0.27.3](services/mariadb/CHANGELOG.md#v0273)
201206
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`

services/loadbalancer/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v1.13.0
2+
- **Improvement:** Use new WaiterHelper for LoadBalancer waiters
3+
- **Breaking Change:** `v2api/wait/DeleteLoadBalancerWaitHandler` now returns a `LoadBalancer` instead of `struct{}`
4+
15
## v1.12.2
26
- **Dependencies:** Bump STACKIT SDK core module to `v0.26.0`
37

services/loadbalancer/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.12.2
1+
v1.13.0

services/loadbalancer/v2api/wait/wait.go

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package wait
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
6-
"net/http"
77
"strings"
88
"time"
99

10-
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1110
"github.com/stackitcloud/stackit-sdk-go/core/wait"
1211
loadbalancer "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api"
1312
)
@@ -23,58 +22,42 @@ const (
2322

2423
// CreateLoadBalancerWaitHandler will wait for load balancer creation
2524
func CreateLoadBalancerWaitHandler(ctx context.Context, a loadbalancer.DefaultAPI, projectId, region, instanceName string) *wait.AsyncActionHandler[loadbalancer.LoadBalancer] {
26-
handler := wait.New(func() (waitFinished bool, response *loadbalancer.LoadBalancer, err error) {
27-
s, err := a.GetLoadBalancer(ctx, projectId, region, instanceName).Execute()
28-
if err != nil {
29-
return false, nil, err
30-
}
31-
if s == nil || s.Name == nil || *s.Name != instanceName || s.Status == nil {
32-
return false, nil, nil
33-
}
34-
35-
var errors []string
36-
if len(s.Errors) > 0 {
37-
for _, err := range s.Errors {
38-
errors = append(errors, fmt.Sprintf("%s: %s", *err.Type, *err.Description))
25+
waitConfig := wait.WaiterHelper[loadbalancer.LoadBalancer, string]{
26+
FetchInstance: a.GetLoadBalancer(ctx, projectId, region, instanceName).Execute,
27+
GetState: func(r *loadbalancer.LoadBalancer) (string, error) {
28+
if r == nil || r.Status == nil {
29+
return "", errors.New("response or status is nil")
3930
}
40-
return true, s, fmt.Errorf("create failed for instance with name %s, got status %s and errors: %s", instanceName, *s.Status, strings.Join(errors, ";"))
41-
}
42-
43-
switch *s.Status {
44-
case LOADBALANCERSTATUS_READY:
45-
return true, s, nil
46-
case LOADBALANCERSTATUS_UNSPECIFIED:
47-
return false, nil, nil
48-
case LOADBALANCERSTATUS_PENDING:
49-
return false, nil, nil
50-
case LOADBALANCERSTATUS_TERMINATING:
51-
return true, s, fmt.Errorf("create failed for instance with name %s, got status %s", instanceName, LOADBALANCERSTATUS_TERMINATING)
52-
case LOADBALANCERSTATUS_ERROR:
53-
return true, s, fmt.Errorf("create failed for instance with name %s, got status %s", instanceName, LOADBALANCERSTATUS_ERROR)
54-
default:
55-
return true, s, fmt.Errorf("instance with name %s has unexpected status %s", instanceName, *s.Status)
56-
}
57-
})
31+
var sb strings.Builder
32+
if r.Errors != nil {
33+
for _, err := range r.Errors {
34+
sb.WriteString(fmt.Sprintf("%s: %s; ", *err.Type, *err.Description))
35+
}
36+
return "", fmt.Errorf("create failed for instance with name %s, got status %s and errors: %s", instanceName, *r.Status, sb.String())
37+
}
38+
return *r.Status, nil
39+
},
40+
ActiveState: []string{LOADBALANCERSTATUS_READY},
41+
ErrorState: []string{LOADBALANCERSTATUS_TERMINATING, LOADBALANCERSTATUS_ERROR},
42+
}
43+
handler := wait.New(waitConfig.Wait())
5844
handler.SetTimeout(45 * time.Minute)
5945
return handler
6046
}
6147

6248
// DeleteLoadBalancerWaitHandler will wait for load balancer deletion
63-
func DeleteLoadBalancerWaitHandler(ctx context.Context, a loadbalancer.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[struct{}] {
64-
handler := wait.New(func() (waitFinished bool, response *struct{}, err error) {
65-
_, err = a.GetLoadBalancer(ctx, projectId, region, instanceId).Execute()
66-
if err == nil {
67-
return false, nil, nil
68-
}
69-
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
70-
if !ok {
71-
return false, nil, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError")
72-
}
73-
if oapiErr.StatusCode != http.StatusNotFound {
74-
return false, nil, err
75-
}
76-
return true, nil, nil
77-
})
49+
func DeleteLoadBalancerWaitHandler(ctx context.Context, a loadbalancer.DefaultAPI, projectId, region, instanceId string) *wait.AsyncActionHandler[loadbalancer.LoadBalancer] {
50+
waitConfig := wait.WaiterHelper[loadbalancer.LoadBalancer, string]{
51+
FetchInstance: a.GetLoadBalancer(ctx, projectId, region, instanceId).Execute,
52+
GetState: func(l *loadbalancer.LoadBalancer) (string, error) {
53+
if l == nil || l.Status == nil {
54+
return "", errors.New("response or status is nil")
55+
}
56+
return *l.Status, nil
57+
},
58+
ErrorState: []string{LOADBALANCERSTATUS_ERROR},
59+
}
60+
handler := wait.New(waitConfig.Wait())
7861
handler.SetTimeout(15 * time.Minute)
7962
return handler
8063
}

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)