-
Notifications
You must be signed in to change notification settings - Fork 3
add CRUD operations for Volume Snapshots #73
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
Open
disperate
wants to merge
10
commits into
master
Choose a base branch
from
julian/add-snapshot-crud-operations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c42dbd3
add crud for volume snapshots
disperate 680e79b
improve integration test name to match other tests
disperate dda9c21
use embedded structs for zone and tags
disperate 97ec39e
include volume snapshots in tags integration test
disperate 0f13af9
add omitempty to future-proof the sdk for api changes
disperate a172488
move update operation to separate integration test
disperate 96c1eaa
use testZone for resource creation in integration tests
disperate 31eefaa
add status and check for volume status deleting in waitForSnapshotDel…
disperate 4b198cd
remove unit tests, they only test genric_service.go behaviour
disperate c2b65ca
use generic interface definitions
disperate 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
Some comments aren't visible on the classic Files Changed page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| //go:build integration | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/cloudscale-ch/cloudscale-go-sdk/v6" | ||
| ) | ||
|
|
||
| func TestIntegrationVolumeSnapshot_CRUD(t *testing.T) { | ||
| integrationTest(t) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| // A source volume is needed to create a snapshot. | ||
| volumeCreateRequest := &cloudscale.VolumeRequest{ | ||
| Name: "test-volume-for-snapshot", | ||
| SizeGB: 50, | ||
| Type: "ssd", | ||
| ZonalResourceRequest: cloudscale.ZonalResourceRequest{ | ||
| Zone: testZone, | ||
| }, | ||
| } | ||
| volume, err := client.Volumes.Create(ctx, volumeCreateRequest) | ||
| if err != nil { | ||
| t.Fatalf("Volume.Create: %v", err) | ||
| } | ||
|
|
||
| snapshotCreateRequest := &cloudscale.VolumeSnapshotRequest{ | ||
| Name: "test-snapshot", | ||
| SourceVolume: volume.UUID, | ||
| } | ||
| snapshot, err := client.VolumeSnapshots.Create(ctx, snapshotCreateRequest) | ||
| if err != nil { | ||
| t.Fatalf("VolumeSnapshots.Create: %v", err) | ||
| } | ||
|
|
||
| retrieved, err := client.VolumeSnapshots.Get(ctx, snapshot.UUID) | ||
| if err != nil { | ||
| t.Fatalf("VolumeSnapshots.Get: %v", err) | ||
| } | ||
| if retrieved.UUID != snapshot.UUID { | ||
| t.Errorf("Expected UUID %s, got %s", snapshot.UUID, retrieved.UUID) | ||
| } | ||
| if retrieved.Name != "test-snapshot" { | ||
| t.Errorf("Expected retrieved snapshot name 'test-snapshot', got '%s'", retrieved.Name) | ||
| } | ||
|
|
||
| snapshots, err := client.VolumeSnapshots.List(ctx) | ||
| if err != nil { | ||
| t.Fatalf("VolumeSnapshots.List: %v", err) | ||
| } | ||
| if len(snapshots) == 0 { | ||
| t.Error("Expected at least one snapshot") | ||
| } | ||
|
|
||
| if err := client.VolumeSnapshots.Delete(ctx, snapshot.UUID); err != nil { | ||
| t.Fatalf("Warning: failed to delete snapshot %s: %v", snapshot.UUID, err) | ||
| } | ||
|
|
||
| // Wait for snapshot to be fully deleted before deleting volume | ||
| err = waitForSnapshotDeletion(ctx, snapshot.UUID, 10) | ||
| if err != nil { | ||
| t.Fatalf("Snapshot deletion timeout: %v", err) | ||
| } | ||
|
|
||
| if err := client.Volumes.Delete(ctx, volume.UUID); err != nil { | ||
| t.Fatalf("Warning: failed to delete volume %s: %v", volume.UUID, err) | ||
| } | ||
| } | ||
|
|
||
| func TestIntegrationVolumeSnapshot_Update(t *testing.T) { | ||
| integrationTest(t) | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| // A source volume is needed to create a snapshot. | ||
| volumeCreateRequest := &cloudscale.VolumeRequest{ | ||
| Name: "test-volume-for-snapshot", | ||
| SizeGB: 50, | ||
| Type: "ssd", | ||
| ZonalResourceRequest: cloudscale.ZonalResourceRequest{ | ||
| Zone: testZone, | ||
| }, | ||
| } | ||
| volume, err := client.Volumes.Create(ctx, volumeCreateRequest) | ||
| if err != nil { | ||
| t.Fatalf("Volume.Create: %v", err) | ||
| } | ||
|
|
||
| snapshotCreateRequest := &cloudscale.VolumeSnapshotRequest{ | ||
| Name: "test-snapshot", | ||
| SourceVolume: volume.UUID, | ||
| } | ||
| snapshot, err := client.VolumeSnapshots.Create(ctx, snapshotCreateRequest) | ||
| if err != nil { | ||
| t.Fatalf("VolumeSnapshots.Create: %v", err) | ||
| } | ||
|
|
||
| snapshotUpdateRequest := &cloudscale.VolumeSnapshotUpdateRequest{ | ||
| Name: "updated-snapshot", | ||
| } | ||
| err = client.VolumeSnapshots.Update(ctx, snapshot.UUID, snapshotUpdateRequest) | ||
| if err != nil { | ||
| t.Fatalf("VolumeSnapshots.Update: %v", err) | ||
| } | ||
disperate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Get snapshot again to verify the update | ||
| updatedSnapshot, err := client.VolumeSnapshots.Get(ctx, snapshot.UUID) | ||
| if err != nil { | ||
| t.Fatalf("VolumeSnapshots.Get after update: %v", err) | ||
| } | ||
| if updatedSnapshot.Name != "updated-snapshot" { | ||
| t.Errorf("Expected updated snapshot name 'updated-snapshot', got '%s'", updatedSnapshot.Name) | ||
| } | ||
|
|
||
| if err := client.VolumeSnapshots.Delete(ctx, snapshot.UUID); err != nil { | ||
| t.Fatalf("Warning: failed to delete snapshot %s: %v", snapshot.UUID, err) | ||
| } | ||
|
|
||
| // Wait for snapshot to be fully deleted before deleting volume | ||
| err = waitForSnapshotDeletion(ctx, snapshot.UUID, 10) | ||
| if err != nil { | ||
| t.Fatalf("Snapshot deletion timeout: %v", err) | ||
| } | ||
|
|
||
| if err := client.Volumes.Delete(ctx, volume.UUID); err != nil { | ||
| t.Fatalf("Warning: failed to delete volume %s: %v", volume.UUID, err) | ||
| } | ||
| } | ||
|
|
||
| // waitForSnapshotDeletion polls the API until the snapshot no longer exists | ||
| func waitForSnapshotDeletion(ctx context.Context, snapshotUUID string, maxWaitSeconds int) error { | ||
| for i := 0; i < maxWaitSeconds; i++ { | ||
| snapshot, err := client.VolumeSnapshots.Get(ctx, snapshotUUID) | ||
| if err != nil { | ||
|
|
||
| if apiErr, ok := err.(*cloudscale.ErrorResponse); ok { | ||
| if apiErr.StatusCode == 404 { | ||
| // if we get a 404 error, snapshot is gone, deletion completed | ||
| return nil | ||
| } | ||
| } | ||
| // some other error occurred | ||
| return err | ||
| } | ||
|
|
||
| // if snapshot still exists, it must be in state deleting | ||
| if snapshot.Status != "deleting" { | ||
| return fmt.Errorf( | ||
| "snapshot %s exists but is in unexpected state %q while waiting for deletion", | ||
| snapshotUUID, | ||
| snapshot.Status, | ||
| ) | ||
| } | ||
|
|
||
| // snapshot still exists, wait 1 second and try again | ||
| time.Sleep(1 * time.Second) | ||
| } | ||
| return fmt.Errorf("snapshot %s still exists after %d seconds", snapshotUUID, maxWaitSeconds) | ||
| } | ||
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,35 @@ | ||
| package cloudscale | ||
|
|
||
| const volumeSnapshotsBasePath = "v1/volume-snapshots" | ||
|
|
||
| type VolumeSnapshot struct { | ||
| ZonalResource | ||
| TaggedResource | ||
| HREF string `json:"href,omitempty"` | ||
| UUID string `json:"uuid,omitempty"` | ||
| Name string `json:"name,omitempty"` | ||
| SizeGB int `json:"size_gb,omitempty"` | ||
| CreatedAt string `json:"created_at,omitempty"` | ||
| Volume VolumeStub `json:"volume,omitempty"` | ||
| Status string `json:"status,omitempty"` | ||
| } | ||
|
|
||
| type VolumeSnapshotRequest struct { | ||
| TaggedResourceRequest | ||
| Name string `json:"name,omitempty"` | ||
| SourceVolume string `json:"source_volume,omitempty"` | ||
| } | ||
|
|
||
| type VolumeSnapshotUpdateRequest struct { | ||
| TaggedResourceRequest | ||
| Name string `json:"name,omitempty"` | ||
| } | ||
|
|
||
| type VolumeSnapshotService interface { | ||
| GenericCreateService[VolumeSnapshot, VolumeSnapshotRequest] | ||
| GenericGetService[VolumeSnapshot] | ||
| GenericListService[VolumeSnapshot] | ||
| GenericUpdateService[VolumeSnapshot, VolumeSnapshotUpdateRequest] | ||
| GenericDeleteService[VolumeSnapshot] | ||
| GenericWaitForService[VolumeSnapshot] | ||
| } |
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.