Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## Release (2025-YY-XX)
- `core`: [v0.17.2](core/CHANGELOG.md#v0172-2025-05-22)
- **Bugfix:** Access tokens generated via key flow authentication are refreshed 5 seconds before expiration to prevent timing issues with upstream systems which could lead to unexpected 401 error responses
- `iaas`: [v0.25.0](services/iaas/CHANGELOG.md#v0250-2025-06-02)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2669 has to be merged first or just combine these two PRs

- **Feature:** Add waiters for async operations: `CreateSnapshotWaitHandler` and `DeleteSnapshotWaitHandler`

## Release (2025-05-15)
- `alb`:
Expand Down
3 changes: 3 additions & 0 deletions services/iaas/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.25.0 (2025-06-02)
- **Feature:** Add waiters for async operations: `CreateSnapshotWaitHandler` and `DeleteSnapshotWaitHandler`

## v0.23.0 (2025-05-15)
- **Breaking change:** Introduce interfaces for `APIClient` and the request structs

Expand Down
50 changes: 50 additions & 0 deletions services/iaas/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const (
RequestFailedStatus = "FAILED"

XRequestIDHeader = "X-Request-Id"

SnapshotCreateSuccess = "AVAILABLE"
SnapshotCreateFail = "ERROR"
SnapshotDeleteSuccess = "DELETED"
SnapshotDeleteFail = "ERROR"
)

// Interfaces needed for tests
Expand All @@ -46,6 +51,7 @@ type APIClientInterface interface {
GetServerExecute(ctx context.Context, projectId string, serverId string) (*iaas.Server, error)
GetAttachedVolumeExecute(ctx context.Context, projectId string, serverId string, volumeId string) (*iaas.VolumeAttachment, error)
GetImageExecute(ctx context.Context, projectId string, imageId string) (*iaas.Image, error)
GetSnapshotExecute(ctx context.Context, projectId string, snapshotId string) (*iaas.Snapshot, error)
}

// CreateNetworkAreaWaitHandler will wait for network area creation
Expand Down Expand Up @@ -599,3 +605,47 @@ func DeleteImageWaitHandler(ctx context.Context, a APIClientInterface, projectId
handler.SetTimeout(15 * time.Minute)
return handler
}

// CreateSnapshotWaitHandler will wait for snapshot creation
func CreateSnapshotWaitHandler(ctx context.Context, a APIClientInterface, projectId, snapshotId string) *wait.AsyncActionHandler[iaas.Snapshot] {
handler := wait.New(func() (waitFinished bool, response *iaas.Snapshot, err error) {
snapshot, err := a.GetSnapshotExecute(ctx, projectId, snapshotId)
if err != nil {
return false, nil, err
}
if snapshot.Id == nil || snapshot.Status == nil {
return false, nil, fmt.Errorf("could not get snapshot id or status from response for project %s and snapshot %s", projectId, snapshotId)
}
if *snapshot.Id == snapshotId && *snapshot.Status == SnapshotCreateSuccess {
return true, snapshot, nil
}
if *snapshot.Id == snapshotId && *snapshot.Status == SnapshotCreateFail {
return true, snapshot, fmt.Errorf("create failed for snapshot with id %s", snapshotId)
}
return false, nil, nil
})
handler.SetTimeout(45 * time.Minute)
return handler
}

// DeleteSnapshotWaitHandler will wait for snapshot deletion
func DeleteSnapshotWaitHandler(ctx context.Context, a APIClientInterface, projectId, snapshotId string) *wait.AsyncActionHandler[iaas.Snapshot] {
handler := wait.New(func() (waitFinished bool, response *iaas.Snapshot, err error) {
snapshot, err := a.GetSnapshotExecute(ctx, projectId, snapshotId)
if err != nil {
return false, nil, err
}
if snapshot.Id == nil || snapshot.Status == nil {
return false, nil, fmt.Errorf("could not get snapshot id or status from response for project %s and snapshot %s", projectId, snapshotId)
}
if *snapshot.Id == snapshotId && *snapshot.Status == SnapshotDeleteSuccess {
return true, snapshot, nil
}
if *snapshot.Id == snapshotId && *snapshot.Status == SnapshotDeleteFail {
return true, snapshot, fmt.Errorf("delete failed for snapshot with id %s", snapshotId)
}
return false, nil, nil
})
handler.SetTimeout(20 * time.Minute)
return handler
}
150 changes: 150 additions & 0 deletions services/iaas/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type apiClientMocked struct {
isAttached bool
requestAction string
returnResizing bool
getSnapshotFails bool
}

func (a *apiClientMocked) GetNetworkAreaExecute(_ context.Context, _, _ string) (*iaas.NetworkArea, error) {
Expand Down Expand Up @@ -162,6 +163,25 @@ func (a *apiClientMocked) GetImageExecute(_ context.Context, _, _ string) (*iaas
}, nil
}

func (a *apiClientMocked) GetSnapshotExecute(_ context.Context, _, _ string) (*iaas.Snapshot, error) {
if a.isDeleted {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: 404,
}
}

if a.getSnapshotFails {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: 500,
}
}

return &iaas.Snapshot{
Id: utils.Ptr("sid"),
Status: &a.resourceState,
}, nil
}

func TestCreateNetworkAreaWaitHandler(t *testing.T) {
tests := []struct {
desc string
Expand Down Expand Up @@ -1504,3 +1524,133 @@ func TestDeleteImageWaitHandler(t *testing.T) {
})
}
}

func TestCreateSnapshotWaitHandler(t *testing.T) {
tests := []struct {
desc string
getFails bool
resourceState string
wantErr bool
wantResp bool
}{
{
desc: "create_succeeded",
getFails: false,
resourceState: SnapshotCreateSuccess,
wantErr: false,
wantResp: true,
},
{
desc: "error_status",
getFails: false,
resourceState: SnapshotCreateFail,
wantErr: true,
wantResp: true,
},
{
desc: "get_fails",
getFails: true,
resourceState: "",
wantErr: true,
wantResp: false,
},
{
desc: "timeout",
getFails: false,
resourceState: "ANOTHER_STATUS",
wantErr: true,
wantResp: true,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
apiClient := &apiClientMocked{
getSnapshotFails: tt.getFails,
resourceState: tt.resourceState,
}

var wantRes *iaas.Snapshot
if tt.wantResp {
wantRes = &iaas.Snapshot{
Id: utils.Ptr("sid"),
Status: utils.Ptr(tt.resourceState),
}
}

handler := CreateSnapshotWaitHandler(context.Background(), apiClient, "pid", "sid")
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())

if (err != nil) != tt.wantErr {
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
}
if !cmp.Equal(gotRes, wantRes) {
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
}
})
}
}

func TestDeleteSnapshotWaitHandler(t *testing.T) {
tests := []struct {
desc string
getFails bool
resourceState string
wantErr bool
wantResp bool
}{
{
desc: "delete_succeeded",
getFails: false,
resourceState: SnapshotDeleteSuccess,
wantErr: false,
wantResp: true,
},
{
desc: "error_status",
getFails: false,
resourceState: SnapshotDeleteFail,
wantErr: true,
wantResp: true,
},
{
desc: "get_fails",
getFails: true,
resourceState: "",
wantErr: true,
wantResp: false,
},
{
desc: "timeout",
getFails: false,
resourceState: "ANOTHER_STATUS",
wantErr: true,
wantResp: true,
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
apiClient := &apiClientMocked{
getSnapshotFails: tt.getFails,
resourceState: tt.resourceState,
}

var wantRes *iaas.Snapshot
if tt.wantResp {
wantRes = &iaas.Snapshot{
Id: utils.Ptr("sid"),
Status: utils.Ptr(tt.resourceState),
}
}

handler := DeleteSnapshotWaitHandler(context.Background(), apiClient, "pid", "sid")
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())

if (err != nil) != tt.wantErr {
t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr)
}
if !cmp.Equal(gotRes, wantRes) {
t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes)
}
})
}
}
Loading