-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from civo/feat/vol-snapshots
Feat/vol snapshots
- Loading branch information
Showing
4 changed files
with
358 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,65 @@ | ||
package civogo | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// VolumeSnapshot is the point-in-time copy of a Volume | ||
type VolumeSnapshot struct { | ||
Name string `json:"name"` | ||
SnapshotID string `json:"snapshot_id"` | ||
SnapshotDescription string `json:"snapshot_description"` | ||
VolumeID string `json:"volume_id"` | ||
InstanceID string `json:"instance_id,omitempty"` | ||
SourceVolumeName string `json:"source_volume_name"` | ||
RestoreSize int `json:"restore_size"` | ||
State string `json:"state"` | ||
CreationTime string `json:"creation_time,omitempty"` | ||
} | ||
|
||
// VolumeSnapshotConfig is the configuration for creating a new VolumeSnapshot | ||
type VolumeSnapshotConfig struct { | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
Region string `json:"region"` | ||
} | ||
|
||
// ListVolumeSnapshots returns all snapshots owned by the calling API account | ||
func (c *Client) ListVolumeSnapshots() ([]VolumeSnapshot, error) { | ||
resp, err := c.SendGetRequest("/v2/snapshots?resource_type=volume") | ||
if err != nil { | ||
return nil, decodeError(err) | ||
} | ||
|
||
var volumeSnapshots = make([]VolumeSnapshot, 0) | ||
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshots); err != nil { | ||
return nil, err | ||
} | ||
|
||
return volumeSnapshots, nil | ||
} | ||
|
||
// GetVolumeSnapshot finds a volume by the full ID | ||
func (c *Client) GetVolumeSnapshot(id string) (VolumeSnapshot, error) { | ||
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/snapshots/%s?resource_type=volume", id)) | ||
if err != nil { | ||
return VolumeSnapshot{}, decodeError(err) | ||
} | ||
var volumeSnapshot = VolumeSnapshot{} | ||
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshot); err != nil { | ||
return VolumeSnapshot{}, err | ||
} | ||
return volumeSnapshot, nil | ||
} | ||
|
||
// DeleteVolumeSnapshot deletes a volume snapshot | ||
func (c *Client) DeleteVolumeSnapshot(id string) (*SimpleResponse, error) { | ||
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/snapshots/%s", id)) | ||
if err != nil { | ||
return nil, decodeError(err) | ||
} | ||
|
||
return c.DecodeSimpleResponse(resp) | ||
} |
This file contains 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,106 @@ | ||
package civogo | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestListVolumeSnapshots(t *testing.T) { | ||
client, server, _ := NewClientForTesting(map[string]string{ | ||
"/v2/snapshots?region=TEST&resource_type=volume": `[{ | ||
"name": "test-snapshot", | ||
"snapshot_id": "12345", | ||
"snapshot_description": "snapshot for test", | ||
"volume_id": "12345", | ||
"source_volume_name": "test-volume", | ||
"instance_id": "ins1234", | ||
"restore_size": 20, | ||
"state": "available", | ||
"creation_time": "2020-01-01T00:00:00Z" | ||
}]`, | ||
}) | ||
defer server.Close() | ||
|
||
got, err := client.ListVolumeSnapshots() | ||
|
||
if err != nil { | ||
t.Errorf("Request returned an error: %s", err) | ||
return | ||
} | ||
|
||
expected := []VolumeSnapshot{ | ||
{ | ||
Name: "test-snapshot", | ||
SnapshotID: "12345", | ||
SnapshotDescription: "snapshot for test", | ||
VolumeID: "12345", | ||
SourceVolumeName: "test-volume", | ||
InstanceID: "ins1234", | ||
RestoreSize: 20, | ||
State: "available", | ||
CreationTime: "2020-01-01T00:00:00Z", | ||
}, | ||
} | ||
|
||
if !reflect.DeepEqual(got, expected) { | ||
t.Errorf("Expected %+v, got %+v", expected, got) | ||
} | ||
} | ||
|
||
func TestGetVolumeSnapshot(t *testing.T) { | ||
client, server, _ := NewClientForTesting(map[string]string{ | ||
"/v2/snapshots/snapshot-uuid?region=TEST&resource_type=volume": `{ | ||
"name": "test-snapshot", | ||
"snapshot_id": "snapshot-uuid", | ||
"snapshot_description": "snapshot for testing", | ||
"volume_id": "12345", | ||
"source_volume_name": "test-volume", | ||
"instance_id": "ins1234", | ||
"restore_size": 20, | ||
"state": "available", | ||
"creation_time": "2020-01-01T00:00:00Z" | ||
}`, | ||
}) | ||
defer server.Close() | ||
got, err := client.GetVolumeSnapshot("snapshot-uuid") | ||
|
||
if err != nil { | ||
t.Errorf("Request returned an error: %s", err) | ||
return | ||
} | ||
|
||
expected := VolumeSnapshot{ | ||
Name: "test-snapshot", | ||
SnapshotID: "snapshot-uuid", | ||
SnapshotDescription: "snapshot for testing", | ||
VolumeID: "12345", | ||
SourceVolumeName: "test-volume", | ||
InstanceID: "ins1234", | ||
RestoreSize: 20, | ||
State: "available", | ||
CreationTime: "2020-01-01T00:00:00Z", | ||
} | ||
|
||
if !reflect.DeepEqual(got, expected) { | ||
t.Errorf("Expected %+v, got %+v", expected, got) | ||
} | ||
} | ||
|
||
func TestDeleteVolumeSnapshot(t *testing.T) { | ||
client, server, _ := NewClientForTesting(map[string]string{ | ||
"/v2/snapshots/12346": `{"result": "success"}`, | ||
}) | ||
defer server.Close() | ||
got, err := client.DeleteVolumeSnapshot("12346") | ||
|
||
if err != nil { | ||
t.Errorf("Request returned an error: %s", err) | ||
return | ||
} | ||
|
||
expected := &SimpleResponse{Result: "success"} | ||
|
||
if !reflect.DeepEqual(got, expected) { | ||
t.Errorf("Expected %+v, got %+v", expected, got) | ||
} | ||
} |
This file contains 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