diff --git a/examples/iaas/attach_volume/attach_volume.go b/examples/iaas/attach_volume/attach_volume.go index 122e4f08f..753ad3902 100644 --- a/examples/iaas/attach_volume/attach_volume.go +++ b/examples/iaas/attach_volume/attach_volume.go @@ -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" ) @@ -23,32 +26,35 @@ func main() { os.Exit(1) } + var httpResp *http.Response + ctx := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp) + 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)