-
Notifications
You must be signed in to change notification settings - Fork 24
Add waiters and example for the image operations on iaasalpha
#1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74169c3
feat: Add waiters and example for the image operations
joaopalet 3a3190e
fix: Lint fixes
joaopalet 04aa15c
Update services/iaasalpha/wait/wait.go
joaopalet a32d9d8
Update services/iaasalpha/wait/wait.go
joaopalet 1c79d33
Update services/iaasalpha/wait/wait.go
joaopalet 46ac26f
Update examples/iaasalpha/image/image.go
joaopalet 2615926
feat: Improve descriptions and comments
joaopalet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "github.com/stackitcloud/stackit-sdk-go/core/config" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha/wait" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Specify the and project ID | ||
| projectId := "PROJECT_ID" | ||
| imageFilePath := "PATH/TO/IMAGE" // Should be a path to a valid image file, e.g. "./my-image.qcow2" | ||
| imageDiskFormat := "DISK_FORMAT" // E.g. "qcow2", "raw", "iso" | ||
|
|
||
| // Create a new API client, that uses default authentication and configuration | ||
| iaasalphaClient, err := iaasalpha.NewAPIClient( | ||
| config.WithRegion("eu01"), | ||
| ) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Creating API client: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| ctx := context.Background() | ||
|
|
||
| // Create an image | ||
| createImagePayload := iaasalpha.CreateImagePayload{ | ||
| Name: utils.Ptr("my-image"), | ||
| DiskFormat: utils.Ptr(imageDiskFormat), | ||
| } | ||
| imageCreateResp, err := iaasalphaClient.CreateImage(ctx, projectId).CreateImagePayload(createImagePayload).Execute() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `CreateImage`: %v\n", err) | ||
| os.Exit(1) | ||
| } else { | ||
| fmt.Printf("[iaasalpha API] Image %q has been successfully created.\n", *imageCreateResp.Id) | ||
| } | ||
|
|
||
| // Upload the image by making a PUT request to upload URL | ||
| fileContents, err := os.ReadFile(imageFilePath) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when reading file: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| req, err := http.NewRequest(http.MethodPut, *imageCreateResp.UploadUrl, bytes.NewReader(fileContents)) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when creating request: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| req.Header.Set("Content-Type", "application/octet-stream") | ||
|
|
||
| fmt.Printf("[iaasalpha API] Uploading image %q...\n", *imageCreateResp.Id) | ||
| client := &http.Client{} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when making request: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when uploading image: %v\n", resp.Status) | ||
| os.Exit(1) | ||
| } else { | ||
| fmt.Printf("[iaasalpha API] Image %q has been successfully uploaded.\n", *imageCreateResp.Id) | ||
| } | ||
|
|
||
| // Wait for image to become available | ||
| image, err := wait.ImageUploadWaitHandler(ctx, iaasalphaClient, projectId, *imageCreateResp.Id).WaitWithContext(ctx) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for creation: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| fmt.Printf("[iaasalpha API] Image %q is available.\n", *image.Id) | ||
|
|
||
| // Delete the image | ||
| err = iaasalphaClient.DeleteImage(ctx, projectId, *imageCreateResp.Id).Execute() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `DeleteImage`: %v\n", err) | ||
| } else { | ||
| fmt.Printf("[iaasalpha API] public IP %q has been successfully deleted.\n", *imageCreateResp.Id) | ||
joaopalet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Wait for image to be deleted | ||
| _, err = wait.DeleteImageWaitHandler(ctx, iaasalphaClient, projectId, *imageCreateResp.Id).WaitWithContext(ctx) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for deletion: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| fmt.Printf("[iaasalpha API] Image %q has been successfully deleted.\n", *imageCreateResp.Id) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.