Skip to content

Commit d9a792c

Browse files
author
Yago Carlos Fernandez Gou
committed
Introduce waiters for Intake Runner, Intake and Intake User
1 parent 98888df commit d9a792c

File tree

2 files changed

+677
-0
lines changed

2 files changed

+677
-0
lines changed

services/intake/wait/wait.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package wait
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
"time"
9+
10+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
11+
"github.com/stackitcloud/stackit-sdk-go/core/wait"
12+
"github.com/stackitcloud/stackit-sdk-go/services/intake"
13+
)
14+
15+
type APIClientInterface interface {
16+
GetIntakeRunnerExecute(ctx context.Context, projectId, region, intakeRunnerId string) (*intake.IntakeRunnerResponse, error)
17+
GetIntakeExecute(ctx context.Context, projectId, region, intakeId string) (*intake.IntakeResponse, error)
18+
GetIntakeUserExecute(ctx context.Context, projectId, region, intakeId, intakeUserId string) (*intake.IntakeUserResponse, error)
19+
}
20+
21+
func CreateOrUpdateIntakeRunnerWaitHandler(ctx context.Context, a APIClientInterface, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] {
22+
handler := wait.New(func() (waitFinished bool, response *intake.IntakeRunnerResponse, err error) {
23+
runner, err := a.GetIntakeRunnerExecute(ctx, projectId, region, intakeRunnerId)
24+
if err != nil {
25+
return false, nil, err
26+
}
27+
28+
if runner == nil {
29+
return false, nil, fmt.Errorf("API returned a nil response for Intake Runner %s", intakeRunnerId)
30+
}
31+
32+
if runner.Id == nil || runner.State == nil {
33+
return false, nil, fmt.Errorf("could not get ID or State from response for Intake Runner %s", intakeRunnerId)
34+
}
35+
36+
if *runner.Id == intakeRunnerId && *runner.State == intake.INTAKERUNNERRESPONSESTATE_ACTIVE {
37+
return true, runner, nil
38+
}
39+
40+
// The API does not have a dedicated failure state for this resource,
41+
// so we rely on the timeout for cases where it never becomes active.
42+
return false, nil, nil
43+
})
44+
handler.SetTimeout(15 * time.Minute)
45+
return handler
46+
}
47+
48+
func DeleteIntakeRunnerWaitHandler(ctx context.Context, a APIClientInterface, projectId, region, intakeRunnerId string) *wait.AsyncActionHandler[intake.IntakeRunnerResponse] {
49+
handler := wait.New(func() (waitFinished bool, response *intake.IntakeRunnerResponse, err error) {
50+
_, err = a.GetIntakeRunnerExecute(ctx, projectId, region, intakeRunnerId)
51+
if err == nil {
52+
// Resource still exists
53+
return false, nil, nil
54+
}
55+
56+
var oapiError *oapierror.GenericOpenAPIError
57+
if errors.As(err, &oapiError) {
58+
if oapiError.StatusCode == http.StatusNotFound {
59+
// Success: Resource is gone
60+
return true, nil, nil
61+
}
62+
}
63+
// An unexpected error occurred
64+
return false, nil, err
65+
})
66+
handler.SetTimeout(20 * time.Minute)
67+
return handler
68+
}
69+
70+
func CreateOrUpdateIntakeWaitHandler(ctx context.Context, a APIClientInterface, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] {
71+
handler := wait.New(func() (waitFinished bool, response *intake.IntakeResponse, err error) {
72+
ik, err := a.GetIntakeExecute(ctx, projectId, region, intakeId)
73+
if err != nil {
74+
return false, nil, err
75+
}
76+
77+
if ik == nil {
78+
return false, nil, fmt.Errorf("API returned a nil response for Intake %s", intakeId)
79+
}
80+
81+
if ik.Id == nil || ik.State == nil {
82+
return false, nil, fmt.Errorf("could not get ID or State from response for Intake %s", intakeId)
83+
}
84+
85+
if *ik.Id == intakeId && *ik.State == intake.INTAKERESPONSESTATE_ACTIVE {
86+
return true, ik, nil
87+
}
88+
89+
if *ik.Id == intakeId && *ik.State == intake.INTAKERESPONSESTATE_FAILED {
90+
return true, ik, fmt.Errorf("create/update failed for Intake %s", intakeId)
91+
}
92+
93+
return false, nil, nil
94+
})
95+
handler.SetTimeout(20 * time.Minute)
96+
return handler
97+
}
98+
99+
func DeleteIntakeWaitHandler(ctx context.Context, a APIClientInterface, projectId, region, intakeId string) *wait.AsyncActionHandler[intake.IntakeResponse] {
100+
handler := wait.New(func() (waitFinished bool, response *intake.IntakeResponse, err error) {
101+
_, err = a.GetIntakeExecute(ctx, projectId, region, intakeId)
102+
if err == nil {
103+
return false, nil, nil
104+
}
105+
106+
var oapiError *oapierror.GenericOpenAPIError
107+
if errors.As(err, &oapiError) {
108+
if oapiError.StatusCode == http.StatusNotFound {
109+
return true, nil, nil
110+
}
111+
}
112+
return false, nil, err
113+
})
114+
handler.SetTimeout(10 * time.Minute)
115+
return handler
116+
}
117+
118+
func CreateOrUpdateIntakeUserWaitHandler(ctx context.Context, a APIClientInterface, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] {
119+
handler := wait.New(func() (waitFinished bool, response *intake.IntakeUserResponse, err error) {
120+
user, err := a.GetIntakeUserExecute(ctx, projectId, region, intakeId, intakeUserId)
121+
if err != nil {
122+
return false, nil, err
123+
}
124+
125+
if user == nil {
126+
return false, nil, fmt.Errorf("API returned a nil response for Intake User %s", intakeUserId)
127+
}
128+
129+
if user.Id == nil || user.State == nil {
130+
return false, nil, fmt.Errorf("could not get ID or State from response for Intake User %s", intakeUserId)
131+
}
132+
133+
if *user.Id == intakeUserId && *user.State == intake.INTAKEUSERRESPONSESTATE_ACTIVE {
134+
return true, user, nil
135+
}
136+
137+
// The API does not have a dedicated failure state for this resource, we rely on the timeout for cases where
138+
// it never becomes active.
139+
return false, nil, nil
140+
})
141+
handler.SetTimeout(10 * time.Minute)
142+
return handler
143+
}
144+
145+
func DeleteIntakeUserWaitHandler(ctx context.Context, a APIClientInterface, projectId, region, intakeId, intakeUserId string) *wait.AsyncActionHandler[intake.IntakeUserResponse] {
146+
handler := wait.New(func() (waitFinished bool, response *intake.IntakeUserResponse, err error) {
147+
_, err = a.GetIntakeUserExecute(ctx, projectId, region, intakeId, intakeUserId)
148+
if err == nil {
149+
return false, nil, nil
150+
}
151+
152+
var oapiError *oapierror.GenericOpenAPIError
153+
if errors.As(err, &oapiError) {
154+
if oapiError.StatusCode == http.StatusNotFound {
155+
return true, nil, nil
156+
}
157+
}
158+
return false, nil, err
159+
})
160+
handler.SetTimeout(10 * time.Minute)
161+
return handler
162+
}

0 commit comments

Comments
 (0)