Skip to content

Commit 42eacb3

Browse files
committed
feat (intake): refactor wait handler to use helper struct
relates to STACKITSDK-375
1 parent 7e224ba commit 42eacb3

5 files changed

Lines changed: 124 additions & 104 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@
137137
- **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0`
138138
- [v0.9.0](services/intake/CHANGELOG.md#v090)
139139
- **Feature:** Added `_UNKNOWN_DEFAULT_OPEN_API` fallback value to all enums to handle unknown API values gracefully.
140+
- [v0.9.1](services/intake/CHANGELOG.md#v091)
141+
- `v1betaapi`: **Improvement**: Use new `WaiterHandler` struct in the Git WaitHandler
140142
- `kms`:
141143
- [v1.6.2](services/kms/CHANGELOG.md#v162)
142144
- **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1`

services/intake/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.9.1
2+
- `v1betaapi`: **Improvement**: Use new `WaiterHandler` struct in the Intake WaitHandler
3+
14
## v0.9.0
25
- **Feature:** Added `_UNKNOWN_DEFAULT_OPEN_API` fallback value to all enums to handle unknown API values gracefully.
36

services/intake/VERSION

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

services/intake/v1betaapi/wait/wait.go

Lines changed: 85 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package wait
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"net/http"
87
"time"
98

10-
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
119
"github.com/stackitcloud/stackit-sdk-go/core/wait"
1210
intake "github.com/stackitcloud/stackit-sdk-go/services/intake/v1betaapi"
1311
)
@@ -27,133 +25,117 @@ const (
2725
INTAKEUSERRESPONSESTATE_DELETING = "deleting"
2826
)
2927

30-
func CreateOrUpdateIntakeRunnerWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] {
31-
handler := wait.New(func() (waitFinished bool, response *intake.IntakeRunnerResponse, err error) {
32-
runner, err := a.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute()
33-
if err != nil {
34-
return false, nil, err
35-
}
36-
37-
if runner == nil {
38-
return false, nil, fmt.Errorf("API returned a nil response for Intake Runner %s", intakeRunnerId)
39-
}
40-
41-
if runner.Id == intakeRunnerId && runner.State == INTAKERUNNERRESPONSESTATE_ACTIVE {
42-
return true, runner, nil
43-
}
44-
28+
func CreateOrUpdateIntakeRunnerWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] {
29+
waitConfig := wait.WaiterHelper[intake.IntakeRunnerResponse, string]{
30+
FetchInstance: client.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute,
31+
GetState: func(response *intake.IntakeRunnerResponse) (string, error) {
32+
if response == nil {
33+
return "", errors.New("empty response")
34+
}
35+
return response.State, nil
36+
},
37+
ActiveState: []string{INTAKERUNNERRESPONSESTATE_ACTIVE},
38+
ErrorState: []string{},
4539
// The API does not have a dedicated failure state for this resource,
4640
// so we rely on the timeout for cases where it never becomes active.
47-
return false, nil, nil
48-
})
41+
}
42+
43+
handler := wait.New(waitConfig.Wait())
4944
handler.SetTimeout(15 * time.Minute)
5045
return handler
5146
}
5247

53-
func DeleteIntakeRunnerWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] {
54-
handler := wait.New(func() (waitFinished bool, response *intake.IntakeRunnerResponse, err error) {
55-
_, err = a.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute()
56-
if err == nil {
57-
// Resource still exists
58-
return false, nil, nil
59-
}
60-
61-
var oapiError *oapierror.GenericOpenAPIError
62-
if errors.As(err, &oapiError) {
63-
if oapiError.StatusCode == http.StatusNotFound {
64-
// Success: Resource is gone
65-
return true, nil, nil
48+
func DeleteIntakeRunnerWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] {
49+
waitConfig := wait.WaiterHelper[intake.IntakeRunnerResponse, string]{
50+
FetchInstance: client.GetIntakeRunner(ctx, projectId, region, intakeRunnerId).Execute,
51+
GetState: func(response *intake.IntakeRunnerResponse) (string, error) {
52+
if response == nil {
53+
return "", errors.New("empty response")
6654
}
67-
}
68-
// An unexpected error occurred
69-
return false, nil, err
70-
})
55+
return response.State, nil
56+
},
57+
ActiveState: []string{},
58+
ErrorState: []string{},
59+
DeleteHttpErrorStatusCodes: []int{http.StatusNotFound},
60+
}
61+
62+
handler := wait.New(waitConfig.Wait())
7163
handler.SetTimeout(15 * time.Minute)
7264
return handler
7365
}
7466

75-
func CreateOrUpdateIntakeWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] {
76-
handler := wait.New(func() (waitFinished bool, response *intake.IntakeResponse, err error) {
77-
ik, err := a.GetIntake(ctx, projectId, region, intakeId).Execute()
78-
if err != nil {
79-
return false, nil, err
80-
}
81-
82-
if ik == nil {
83-
return false, nil, fmt.Errorf("API returned a nil response for Intake %s", intakeId)
84-
}
85-
86-
if ik.Id == intakeId && ik.State == INTAKERESPONSESTATE_ACTIVE {
87-
return true, ik, nil
88-
}
89-
90-
if ik.Id == intakeId && ik.State == INTAKERESPONSESTATE_FAILED {
91-
return true, ik, fmt.Errorf("create/update failed for Intake %s", intakeId)
92-
}
67+
func CreateOrUpdateIntakeWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] {
68+
waitConfig := wait.WaiterHelper[intake.IntakeResponse, string]{
69+
FetchInstance: client.GetIntake(ctx, projectId, region, intakeId).Execute,
70+
GetState: func(response *intake.IntakeResponse) (string, error) {
71+
if response == nil {
72+
return "", errors.New("empty response")
73+
}
74+
return response.State, nil
75+
},
76+
ActiveState: []string{INTAKERUNNERRESPONSESTATE_ACTIVE},
77+
ErrorState: []string{INTAKERESPONSESTATE_FAILED},
78+
}
9379

94-
return false, nil, nil
95-
})
80+
handler := wait.New(waitConfig.Wait())
9681
handler.SetTimeout(10 * time.Minute)
9782
return handler
9883
}
9984

100-
func DeleteIntakeWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] {
101-
handler := wait.New(func() (waitFinished bool, response *intake.IntakeResponse, err error) {
102-
_, err = a.GetIntake(ctx, projectId, region, intakeId).Execute()
103-
if err == nil {
104-
return false, nil, nil
105-
}
106-
107-
var oapiError *oapierror.GenericOpenAPIError
108-
if errors.As(err, &oapiError) {
109-
if oapiError.StatusCode == http.StatusNotFound {
110-
return true, nil, nil
85+
func DeleteIntakeWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] {
86+
waitConfig := wait.WaiterHelper[intake.IntakeResponse, string]{
87+
FetchInstance: client.GetIntake(ctx, projectId, region, intakeId).Execute,
88+
GetState: func(response *intake.IntakeResponse) (string, error) {
89+
if response == nil {
90+
return "", errors.New("empty response")
11191
}
112-
}
113-
return false, nil, err
114-
})
92+
return response.State, nil
93+
},
94+
ActiveState: []string{},
95+
ErrorState: []string{},
96+
DeleteHttpErrorStatusCodes: []int{http.StatusNotFound},
97+
}
98+
99+
handler := wait.New(waitConfig.Wait())
115100
handler.SetTimeout(10 * time.Minute)
116101
return handler
117102
}
118103

119-
func CreateOrUpdateIntakeUserWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] {
120-
handler := wait.New(func() (waitFinished bool, response *intake.IntakeUserResponse, err error) {
121-
user, err := a.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute()
122-
if err != nil {
123-
return false, nil, err
124-
}
125-
126-
if user == nil {
127-
return false, nil, fmt.Errorf("API returned a nil response for Intake User %s", intakeUserId)
128-
}
129-
130-
if user.Id == intakeUserId && user.State == INTAKEUSERRESPONSESTATE_ACTIVE {
131-
return true, user, nil
132-
}
133-
134-
// The API does not have a dedicated failure state for this resource, we rely on the timeout for cases where
135-
// it never becomes active.
136-
return false, nil, nil
137-
})
104+
func CreateOrUpdateIntakeUserWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] {
105+
waitConfig := wait.WaiterHelper[intake.IntakeUserResponse, string]{
106+
FetchInstance: client.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute,
107+
GetState: func(response *intake.IntakeUserResponse) (string, error) {
108+
if response == nil {
109+
return "", errors.New("empty response")
110+
}
111+
return response.State, nil
112+
},
113+
ActiveState: []string{INTAKEUSERRESPONSESTATE_ACTIVE},
114+
ErrorState: []string{},
115+
// The API does not have a dedicated failure state for this resource,
116+
// so we rely on the timeout for cases where it never becomes active.
117+
}
118+
119+
handler := wait.New(waitConfig.Wait())
138120
handler.SetTimeout(5 * time.Minute)
139121
return handler
140122
}
141123

142-
func DeleteIntakeUserWaitHandler(ctx context.Context, a intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] {
143-
handler := wait.New(func() (waitFinished bool, response *intake.IntakeUserResponse, err error) {
144-
_, err = a.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute()
145-
if err == nil {
146-
return false, nil, nil
147-
}
148-
149-
var oapiError *oapierror.GenericOpenAPIError
150-
if errors.As(err, &oapiError) {
151-
if oapiError.StatusCode == http.StatusNotFound {
152-
return true, nil, nil
124+
func DeleteIntakeUserWaitHandler(ctx context.Context, client intake.DefaultAPI, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] {
125+
waitConfig := wait.WaiterHelper[intake.IntakeUserResponse, string]{
126+
FetchInstance: client.GetIntakeUser(ctx, projectId, region, intakeId, intakeUserId).Execute,
127+
GetState: func(response *intake.IntakeUserResponse) (string, error) {
128+
if response == nil {
129+
return "", errors.New("empty response")
153130
}
154-
}
155-
return false, nil, err
156-
})
131+
return response.State, nil
132+
},
133+
ActiveState: []string{},
134+
ErrorState: []string{},
135+
DeleteHttpErrorStatusCodes: []int{http.StatusNotFound},
136+
}
137+
138+
handler := wait.New(waitConfig.Wait())
157139
handler.SetTimeout(5 * time.Minute)
158140
return handler
159141
}

services/intake/v1betaapi/wait/wait_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ func TestCreateOrUpdateIntakeRunnerWaitHandler(t *testing.T) {
132132
State: INTAKERUNNERRESPONSESTATE_RECONCILING,
133133
},
134134
},
135+
{
136+
desc: "wrong state in response",
137+
getFails: false,
138+
wantErr: true,
139+
wantResp: false,
140+
returnRunner: true,
141+
intakeRunnerResponse: &intake.IntakeRunnerResponse{
142+
Id: intakeRunnerId,
143+
State: "wrong state",
144+
},
145+
},
135146
{
136147
desc: "nil state in response",
137148
getFails: false,
@@ -292,6 +303,17 @@ func TestCreateOrUpdateIntakeWaitHandler(t *testing.T) {
292303
State: INTAKERESPONSESTATE_RECONCILING,
293304
},
294305
},
306+
{
307+
desc: "wrong state in response",
308+
getFails: false,
309+
wantErr: true,
310+
wantResp: false,
311+
returnIntake: true,
312+
intakeResponse: &intake.IntakeResponse{
313+
Id: intakeId,
314+
State: "wrong state",
315+
},
316+
},
295317
{
296318
desc: "nil state in response",
297319
getFails: false,
@@ -441,6 +463,17 @@ func TestCreateOrUpdateIntakeUserWaitHandler(t *testing.T) {
441463
State: INTAKEUSERRESPONSESTATE_RECONCILING,
442464
},
443465
},
466+
{
467+
desc: "wrong state in response",
468+
getFails: false,
469+
wantErr: true,
470+
wantResp: false,
471+
returnUser: true,
472+
intakeUserResponse: &intake.IntakeUserResponse{
473+
Id: intakeUserId,
474+
State: "wrong state",
475+
},
476+
},
444477
{
445478
desc: "nil state in response",
446479
getFails: false,

0 commit comments

Comments
 (0)