Skip to content

Commit 139c90a

Browse files
committed
Add network area waiters
1 parent 1836f7f commit 139c90a

File tree

2 files changed

+295
-0
lines changed

2 files changed

+295
-0
lines changed

services/iaas/wait/wait.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package wait
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
9+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
10+
"github.com/stackitcloud/stackit-sdk-go/core/wait"
11+
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
12+
)
13+
14+
const (
15+
CreateSuccess = "CREATED"
16+
)
17+
18+
// Interfaces needed for tests
19+
type APIClientInterface interface {
20+
GetNetworkAreaExecute(ctx context.Context, organizationId, areaId string) (*iaas.NetworkArea, error)
21+
// GetNetworkExecute(ctx context.Context, projectId, networkId string) (*iaas.Network, error)
22+
}
23+
24+
// CreateNetworkAreaWaitHandler will wait for network area creation
25+
func CreateNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.NetworkArea] {
26+
handler := wait.New(func() (waitFinished bool, response *iaas.NetworkArea, err error) {
27+
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
28+
if err != nil {
29+
return false, area, err
30+
}
31+
if area.AreaId == nil || area.State == nil {
32+
return false, area, fmt.Errorf("create failed for netwrok area with id %s, the response is not valid: the id or the state are missing", areaId)
33+
}
34+
if *area.AreaId == areaId && *area.State == CreateSuccess {
35+
return true, area, nil
36+
}
37+
return false, area, nil
38+
})
39+
handler.SetTimeout(10 * time.Minute)
40+
return handler
41+
}
42+
43+
// UpdateNetworkAreaWaitHandler will wait for network area update
44+
func UpdateNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.NetworkArea] {
45+
handler := wait.New(func() (waitFinished bool, response *iaas.NetworkArea, err error) {
46+
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
47+
if err != nil {
48+
return false, area, err
49+
}
50+
if area.AreaId == nil || area.State == nil {
51+
return false, nil, fmt.Errorf("update failed for network area with id %s, the response is not valid: the id or the state are missing", areaId)
52+
}
53+
// The state returns to "CREATED" after a successful update is completed
54+
if *area.AreaId == areaId && *area.State == CreateSuccess {
55+
return true, area, nil
56+
}
57+
return false, area, nil
58+
})
59+
handler.SetSleepBeforeWait(2 * time.Second)
60+
handler.SetTimeout(10 * time.Minute)
61+
return handler
62+
}
63+
64+
// DeleteNetworkAreaWaitHandler will wait for network area deletion
65+
func DeleteNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.NetworkArea] {
66+
handler := wait.New(func() (waitFinished bool, response *iaas.NetworkArea, err error) {
67+
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
68+
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
69+
if !ok {
70+
return false, area, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError")
71+
}
72+
if oapiErr.StatusCode != http.StatusNotFound {
73+
return false, area, err
74+
}
75+
return true, nil, nil
76+
})
77+
handler.SetTimeout(10 * time.Minute)
78+
return handler
79+
}

services/iaas/wait/wait_test.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package wait
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
10+
"github.com/stackitcloud/stackit-sdk-go/core/utils"
11+
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
12+
)
13+
14+
type apiClientMocked struct {
15+
getFails bool
16+
isDeleted bool
17+
resourceState string
18+
}
19+
20+
func (a *apiClientMocked) GetNetworkAreaExecute(_ context.Context, _, _ string) (*iaas.NetworkArea, error) {
21+
if a.isDeleted {
22+
return nil, &oapierror.GenericOpenAPIError{
23+
StatusCode: 404,
24+
}
25+
}
26+
27+
if a.getFails {
28+
return nil, &oapierror.GenericOpenAPIError{
29+
StatusCode: 500,
30+
}
31+
}
32+
33+
return &iaas.NetworkArea{
34+
AreaId: utils.Ptr("naid"),
35+
State: &a.resourceState,
36+
}, nil
37+
}
38+
39+
func TestCreateNetworkAreaWaitHandler(t *testing.T) {
40+
tests := []struct {
41+
desc string
42+
getFails bool
43+
resourceState string
44+
wantErr bool
45+
wantResp bool
46+
}{
47+
{
48+
desc: "create_succeeded",
49+
getFails: false,
50+
resourceState: CreateSuccess,
51+
wantErr: false,
52+
wantResp: true,
53+
},
54+
{
55+
desc: "get_fails",
56+
getFails: true,
57+
resourceState: "",
58+
wantErr: true,
59+
wantResp: false,
60+
},
61+
{
62+
desc: "timeout",
63+
getFails: false,
64+
resourceState: "ANOTHER STATE",
65+
wantErr: true,
66+
wantResp: true,
67+
},
68+
}
69+
for _, tt := range tests {
70+
t.Run(tt.desc, func(t *testing.T) {
71+
apiClient := &apiClientMocked{
72+
getFails: tt.getFails,
73+
resourceState: tt.resourceState,
74+
}
75+
76+
var wantRes *iaas.NetworkArea
77+
if tt.wantResp {
78+
wantRes = &iaas.NetworkArea{
79+
AreaId: utils.Ptr("naid"),
80+
State: &tt.resourceState,
81+
}
82+
}
83+
84+
handler := CreateNetworkAreaWaitHandler(context.Background(), apiClient, "oid", "naid")
85+
86+
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
87+
88+
if (err != nil) != tt.wantErr {
89+
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
90+
}
91+
if !cmp.Equal(gotRes, wantRes) {
92+
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
93+
}
94+
})
95+
}
96+
}
97+
98+
func TestUpdateNetworkAreaWaitHandler(t *testing.T) {
99+
tests := []struct {
100+
desc string
101+
getFails bool
102+
resourceState string
103+
wantErr bool
104+
wantResp bool
105+
}{
106+
{
107+
desc: "update_succeeded",
108+
getFails: false,
109+
resourceState: CreateSuccess,
110+
wantErr: false,
111+
wantResp: true,
112+
},
113+
{
114+
desc: "get_fails",
115+
getFails: true,
116+
resourceState: "",
117+
wantErr: true,
118+
wantResp: false,
119+
},
120+
{
121+
desc: "timeout",
122+
getFails: false,
123+
resourceState: "ANOTHER STATE",
124+
wantErr: true,
125+
wantResp: true,
126+
},
127+
}
128+
for _, tt := range tests {
129+
t.Run(tt.desc, func(t *testing.T) {
130+
apiClient := &apiClientMocked{
131+
getFails: tt.getFails,
132+
resourceState: tt.resourceState,
133+
}
134+
135+
var wantRes *iaas.NetworkArea
136+
if tt.wantResp {
137+
wantRes = &iaas.NetworkArea{
138+
AreaId: utils.Ptr("naid"),
139+
State: &tt.resourceState,
140+
}
141+
}
142+
143+
handler := UpdateNetworkAreaWaitHandler(context.Background(), apiClient, "oid", "naid")
144+
145+
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
146+
147+
if (err != nil) != tt.wantErr {
148+
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
149+
}
150+
if !cmp.Equal(gotRes, wantRes) {
151+
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
152+
}
153+
})
154+
}
155+
}
156+
157+
func TestDeleteNetworkAreaWaitHandler(t *testing.T) {
158+
tests := []struct {
159+
desc string
160+
getFails bool
161+
isDeleted bool
162+
resourceState string
163+
wantErr bool
164+
wantResp bool
165+
}{
166+
{
167+
desc: "delete_succeeded",
168+
getFails: false,
169+
isDeleted: true,
170+
wantErr: false,
171+
wantResp: false,
172+
},
173+
{
174+
desc: "get_fails",
175+
getFails: true,
176+
resourceState: "",
177+
wantErr: true,
178+
wantResp: false,
179+
},
180+
{
181+
desc: "timeout",
182+
getFails: false,
183+
resourceState: "ANOTHER STATE",
184+
wantErr: true,
185+
wantResp: true,
186+
},
187+
}
188+
for _, tt := range tests {
189+
t.Run(tt.desc, func(t *testing.T) {
190+
apiClient := &apiClientMocked{
191+
getFails: tt.getFails,
192+
isDeleted: tt.isDeleted,
193+
resourceState: tt.resourceState,
194+
}
195+
196+
var wantRes *iaas.NetworkArea
197+
if tt.wantResp {
198+
wantRes = &iaas.NetworkArea{
199+
AreaId: utils.Ptr("naid"),
200+
State: &tt.resourceState,
201+
}
202+
}
203+
204+
handler := DeleteNetworkAreaWaitHandler(context.Background(), apiClient, "oid", "naid")
205+
206+
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
207+
208+
if (err != nil) != tt.wantErr {
209+
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
210+
}
211+
if !cmp.Equal(gotRes, wantRes) {
212+
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
213+
}
214+
})
215+
}
216+
}

0 commit comments

Comments
 (0)