-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathvolume_snapshot.go
65 lines (56 loc) · 2.01 KB
/
volume_snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)
}