Skip to content
Merged
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
28 changes: 17 additions & 11 deletions examples/iaas/attach_volume/attach_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"time"

"github.com/stackitcloud/stackit-sdk-go/core/runtime"
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api/wait"
)
Expand All @@ -23,32 +26,35 @@ func main() {
os.Exit(1)
}

var httpResp *http.Response
ctx := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp)
Comment thread
cgoetz-inovex marked this conversation as resolved.

payload := iaas.AddVolumeToServerPayload{}
_, err = iaasClient.DefaultAPI.AddVolumeToServer(context.Background(), projectId, region, serverId, volumeId).AddVolumeToServerPayload(payload).Execute()
_, err = iaasClient.DefaultAPI.AddVolumeToServer(ctx, projectId, region, serverId, volumeId).AddVolumeToServerPayload(payload).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `AddVolumeToServer`: %v\n", err)
} else {
fmt.Printf("[iaas API] Triggered attachment of volume with ID %q.\n", volumeId)
os.Exit(1)
}
fmt.Printf("[iaas API] Triggered attachment of volume with ID %q.\n", volumeId)

xRequestId := httpResp.Header.Get(wait.XRequestIDHeader)
// Wait for attachment of the volume
_, err = wait.AddVolumeToServerWaitHandler(context.Background(), iaasClient.DefaultAPI, projectId, region, serverId, volumeId).WaitWithContext(context.Background())
_, err = wait.ProjectRequestWaitHandler(ctx, iaasClient.DefaultAPI, projectId, region, xRequestId).SetSleepBeforeWait(500 * time.Millisecond).WaitWithContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for attachment: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Volume %q has been successfully attached to the server %s.\n", volumeId, serverId)

err = iaasClient.DefaultAPI.RemoveVolumeFromServer(context.Background(), projectId, region, serverId, volumeId).Execute()
err = iaasClient.DefaultAPI.RemoveVolumeFromServer(ctx, projectId, region, serverId, volumeId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `RemoveVolumeFromServer`: %v\n", err)
} else {
fmt.Printf("[iaas API] Triggered removal of attachment of volume with ID %q.\n", volumeId)
os.Exit(1)
}
fmt.Printf("[iaas API] Triggered removal of attachment of volume with ID %q.\n", volumeId)

// Wait for dettachment of the volume
_, err = wait.RemoveVolumeFromServerWaitHandler(context.Background(), iaasClient.DefaultAPI, projectId, region, serverId, volumeId).WaitWithContext(context.Background())
xRequestId = httpResp.Header.Get(wait.XRequestIDHeader)
// Wait for detachment of the volume
_, err = wait.ProjectRequestWaitHandler(ctx, iaasClient.DefaultAPI, projectId, region, xRequestId).SetSleepBeforeWait(500 * time.Millisecond).WaitWithContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for removal of attachment of volume: %v\n", err)
os.Exit(1)
Expand Down
Loading