Skip to content

Commit 659e317

Browse files
committed
add delete scf org waiter
1 parent e7e8d28 commit 659e317

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

services/scf/VERSION

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

services/scf/wait/wait.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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/scf"
13+
)
14+
15+
const STATUS_DELETING_FAILED = "deleting_failed"
16+
17+
// Interfaces needed for tests
18+
type APIClientInterface interface {
19+
GetOrganizationExecute(ctx context.Context, projectId, region, orgId string) (*scf.Organization, error)
20+
}
21+
22+
// DeleteOrganizationWaitHandler will wait for Organization deletion
23+
func DeleteOrganizationWaitHandler(ctx context.Context, a APIClientInterface, projectId, region, orgId string) *wait.AsyncActionHandler[scf.Organization] {
24+
handler := wait.New(func() (waitFinished bool, response *scf.Organization, err error) {
25+
s, err := a.GetOrganizationExecute(ctx, projectId, region, orgId)
26+
if err != nil {
27+
var oapiErr *oapierror.GenericOpenAPIError
28+
ok := errors.As(err, &oapiErr)
29+
if ok && oapiErr.StatusCode == http.StatusNotFound {
30+
return true, s, nil
31+
}
32+
return false, s, err
33+
}
34+
if *s.Status == STATUS_DELETING_FAILED {
35+
return true, nil, fmt.Errorf("delete failed for Organization with id %s", orgId)
36+
}
37+
return false, s, nil
38+
})
39+
handler.SetTimeout(20 * time.Minute)
40+
return handler
41+
}

services/scf/wait/wait_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package wait
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/google/uuid"
9+
10+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
11+
"github.com/stackitcloud/stackit-sdk-go/services/scf"
12+
)
13+
14+
var PROJECT_ID = uuid.New().String()
15+
var INSTANCE_ID = uuid.New().String()
16+
17+
const REGION = "eu01"
18+
19+
type apiClientMocked struct {
20+
getFails bool
21+
errorCode int
22+
returnInstance bool
23+
projectId string
24+
instanceId string
25+
getGitResponse *scf.Organization
26+
}
27+
28+
func (a *apiClientMocked) GetOrganizationExecute(_ context.Context, _, _, _ string) (*scf.Organization, error) {
29+
if a.getFails {
30+
return nil, &oapierror.GenericOpenAPIError{
31+
StatusCode: a.errorCode,
32+
}
33+
}
34+
if !a.returnInstance {
35+
return nil, nil
36+
}
37+
return a.getGitResponse, nil
38+
}
39+
40+
func TestDeleteOrganizationWaitHandler(t *testing.T) {
41+
statusDeletingFailed := "deleting_failed"
42+
tests := []struct {
43+
desc string
44+
wantErr bool
45+
wantReturnedInstance bool
46+
getFails bool
47+
errorCode int
48+
returnInstance bool
49+
getOrgResponse *scf.Organization
50+
}{
51+
{
52+
desc: "Instance deletion failed with error",
53+
wantErr: true,
54+
getFails: true,
55+
},
56+
{
57+
desc: "Instance is not found",
58+
wantErr: false,
59+
getFails: true,
60+
errorCode: 404,
61+
},
62+
{
63+
desc: "Instance is in error state",
64+
wantErr: true,
65+
returnInstance: true,
66+
getOrgResponse: &scf.Organization{
67+
Status: &statusDeletingFailed,
68+
},
69+
},
70+
}
71+
for _, tt := range tests {
72+
t.Run(tt.desc, func(t *testing.T) {
73+
apiClient := &apiClientMocked{
74+
projectId: PROJECT_ID,
75+
instanceId: INSTANCE_ID,
76+
getFails: tt.getFails,
77+
errorCode: tt.errorCode,
78+
returnInstance: tt.returnInstance,
79+
getGitResponse: tt.getOrgResponse,
80+
}
81+
82+
handler := DeleteOrganizationWaitHandler(context.Background(), apiClient, apiClient.projectId, REGION, apiClient.instanceId)
83+
response, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
84+
85+
if (err != nil) != tt.wantErr {
86+
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
87+
}
88+
if (response != nil) != tt.wantReturnedInstance {
89+
t.Fatalf("handler gotRes = %v, want nil", response)
90+
}
91+
})
92+
}
93+
}

0 commit comments

Comments
 (0)