Skip to content

Commit d2e9943

Browse files
authored
feat(alb): refactor wait handler (#6791)
relates to STACKITTPR-369
1 parent 8c894f7 commit d2e9943

7 files changed

Lines changed: 50 additions & 39 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
- Minimal go version is now Go 1.25
1111
- [v0.14.1](services/alb/CHANGELOG.md#v0141)
1212
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0`
13+
- [v0.14.2](services/alb/CHANGELOG.md#v0142)
14+
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
15+
- `v2api`: **Improvement**: Use new `WaiterHandler` struct in the ALB WaitHandler
1316
- `albwaf`:
1417
- [v0.3.2](services/albwaf/CHANGELOG.md#v032)
1518
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`

services/alb/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.14.2
2+
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
3+
- `v2api`: **Improvement**: Use new `WaiterHandler` struct in the ALB WaitHandler
4+
15
## v0.14.1
26
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0`
37

services/alb/VERSION

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

services/alb/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.25
55
require (
66
github.com/google/go-cmp v0.7.0
77
github.com/google/uuid v1.6.0
8-
github.com/stackitcloud/stackit-sdk-go/core v0.25.0
8+
github.com/stackitcloud/stackit-sdk-go/core v0.26.0
99
)
1010

1111
require github.com/golang-jwt/jwt/v5 v5.3.1 // indirect

services/alb/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
44
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
55
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
66
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7-
github.com/stackitcloud/stackit-sdk-go/core v0.25.0 h1:ra3VEk684MNoq741g+xbZrKjZzhyztq5liUAwwew4DY=
8-
github.com/stackitcloud/stackit-sdk-go/core v0.25.0/go.mod h1:WU1hhxnjXw2EV7CYa1nlEvNpMiRY6CvmIOaHuL3pOaA=
7+
github.com/stackitcloud/stackit-sdk-go/core v0.26.0 h1:jQEb9gkehfp6VCP6TcYk7BI10cz4l0KM2L6hqYBH2QA=
8+
github.com/stackitcloud/stackit-sdk-go/core v0.26.0/go.mod h1:WU1hhxnjXw2EV7CYa1nlEvNpMiRY6CvmIOaHuL3pOaA=

services/alb/v2api/wait/wait.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ package wait
33
import (
44
"context"
55
"errors"
6-
"fmt"
7-
"net/http"
86
"time"
97

10-
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
118
"github.com/stackitcloud/stackit-sdk-go/core/wait"
129
alb "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
1310
)
@@ -21,44 +18,43 @@ const (
2118
)
2219

2320
func CreateOrUpdateLoadbalancerWaitHandler(ctx context.Context, client alb.DefaultAPI, projectId, region, name string) *wait.AsyncActionHandler[alb.LoadBalancer] {
24-
handler := wait.New(func() (bool, *alb.LoadBalancer, error) {
25-
response, err := client.GetLoadBalancer(ctx, projectId, region, name).Execute()
26-
if err != nil {
27-
return false, nil, err
28-
}
29-
if response.HasStatus() {
30-
switch *response.Status {
31-
case LOADBALANCERSTATUS_PENDING:
32-
return false, nil, nil
33-
case LOADBALANCERSTATUS_UNSPECIFIED:
34-
return false, nil, nil
35-
case LOADBALANCERSTATUS_ERROR:
36-
return true, response, fmt.Errorf("loadbalancer in error: %s", *response.Status)
37-
default:
38-
return true, response, nil
21+
waitConfig := wait.WaiterHelper[alb.LoadBalancer, string]{
22+
FetchInstance: client.GetLoadBalancer(ctx, projectId, region, name).Execute,
23+
GetState: func(response *alb.LoadBalancer) (string, error) {
24+
if response == nil {
25+
return "", errors.New("empty response")
3926
}
40-
}
27+
if response.Status == nil {
28+
return "", errors.New("status is missing in response")
29+
}
30+
return *response.Status, nil
31+
},
32+
ActiveState: []string{LOADBALANCERSTATUS_READY},
33+
ErrorState: []string{LOADBALANCERSTATUS_ERROR},
34+
}
4135

42-
return false, nil, nil
43-
})
36+
handler := wait.New(waitConfig.Wait())
4437
handler.SetTimeout(10 * time.Minute)
4538
return handler
4639
}
4740

4841
func DeleteLoadbalancerWaitHandler(ctx context.Context, client alb.DefaultAPI, projectId, region, name string) *wait.AsyncActionHandler[alb.LoadBalancer] {
49-
handler := wait.New(func() (bool, *alb.LoadBalancer, error) {
50-
_, err := client.GetLoadBalancer(ctx, projectId, region, name).Execute()
51-
if err != nil {
52-
var apiErr *oapierror.GenericOpenAPIError
53-
if errors.As(err, &apiErr) {
54-
if statusCode := apiErr.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
55-
return true, nil, nil
56-
}
42+
waitConfig := wait.WaiterHelper[alb.LoadBalancer, string]{
43+
FetchInstance: client.GetLoadBalancer(ctx, projectId, region, name).Execute,
44+
GetState: func(response *alb.LoadBalancer) (string, error) {
45+
if response == nil {
46+
return "", errors.New("empty response")
47+
}
48+
if response.Status == nil {
49+
return "", errors.New("status is missing in response")
5750
}
58-
return true, nil, err
59-
}
60-
return false, nil, nil
61-
})
51+
return *response.Status, nil
52+
},
53+
ActiveState: []string{},
54+
ErrorState: []string{LOADBALANCERSTATUS_ERROR},
55+
}
56+
57+
handler := wait.New(waitConfig.Wait())
6258
handler.SetTimeout(10 * time.Minute)
6359
return handler
6460
}

services/alb/v2api/wait/wait_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,16 @@ func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) {
102102
[]response{
103103
{&alb.LoadBalancer{Status: utils.Ptr("bogus")}, nil},
104104
},
105-
&alb.LoadBalancer{Status: utils.Ptr("bogus")},
106-
false,
105+
nil,
106+
true,
107+
},
108+
{
109+
"no state",
110+
[]response{
111+
{&alb.LoadBalancer{Status: nil}, nil},
112+
},
113+
nil,
114+
true,
107115
},
108116
// no special update tests needed as the states are the same
109117
}

0 commit comments

Comments
 (0)