Skip to content

Commit b4d1129

Browse files
committed
feat(instance_snapshot): add InstanceSnapshot management with create, read, update, delete, and restore functionalities
1 parent 7ffb277 commit b4d1129

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

instance_snapshot.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package civogo
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"time"
8+
)
9+
10+
// InstanceSnapshot represents a snapshot of an instance
11+
type InstanceSnapshot struct {
12+
ID string `json:"id"`
13+
Name string `json:"name"`
14+
Description string `json:"description,omitempty"`
15+
IncludedVolumes []string `json:"included_volumes,omitempty"`
16+
Status InstanceSnapshotStatus `json:"status,omitempty"`
17+
CreatedAt time.Time `json:"created_at,omitempty"`
18+
}
19+
20+
// InstanceSnapshotStatus represents the status of an instance snapshot
21+
type InstanceSnapshotStatus struct {
22+
State string `json:"state"`
23+
Volumes []InstanceSnapshotVolumeStatus `json:"volumes,omitempty"`
24+
}
25+
26+
// InstanceSnapshotVolumeStatus represents the status of a volume in an instance snapshot
27+
type InstanceSnapshotVolumeStatus struct {
28+
ID string `json:"id"`
29+
State string `json:"state"`
30+
}
31+
32+
// CreateInstanceSnapshotParams represents the parameters for creating a new instance snapshot
33+
type CreateInstanceSnapshotParams struct {
34+
Name string `json:"name"`
35+
Description string `json:"description,omitempty"`
36+
IncludeVolumes bool `json:"include_volumes,omitempty"`
37+
}
38+
39+
// UpdateInstanceSnapshotParams represents the parameters for updating an instance snapshot
40+
type UpdateInstanceSnapshotParams struct {
41+
Name string `json:"name,omitempty"`
42+
Description string `json:"description,omitempty"`
43+
}
44+
45+
// RestoreInstanceSnapshotParams represents the parameters for restoring an instance snapshot
46+
type RestoreInstanceSnapshotParams struct {
47+
Description string `json:"description,omitempty"`
48+
Hostname string `json:"hostname,omitempty"`
49+
PrivateIPv4 string `json:"private_ipv4,omitempty"`
50+
IncludeVolumes bool `json:"include_volumes,omitempty"`
51+
OverwriteExisting bool `json:"overwrite_existing,omitempty"`
52+
}
53+
54+
// CreateInstanceSnapshot creates a new snapshot of an instance
55+
func (c *Client) CreateInstanceSnapshot(instanceID string, params *CreateInstanceSnapshotParams) (*InstanceSnapshot, error) {
56+
url := fmt.Sprintf("/v2/instances/%s/snapshots", instanceID)
57+
resp, err := c.SendPostRequest(url, params)
58+
if err != nil {
59+
return nil, decodeError(err)
60+
}
61+
62+
snapshot := &InstanceSnapshot{}
63+
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(snapshot); err != nil {
64+
return nil, err
65+
}
66+
67+
return snapshot, nil
68+
}
69+
70+
// GetInstanceSnapshot gets a snapshot of an instance by ID or name
71+
func (c *Client) GetInstanceSnapshot(instanceID, snapshotID string) (*InstanceSnapshot, error) {
72+
url := fmt.Sprintf("/v2/instances/%s/snapshots/%s", instanceID, snapshotID)
73+
resp, err := c.SendGetRequest(url)
74+
if err != nil {
75+
return nil, decodeError(err)
76+
}
77+
78+
snapshot := &InstanceSnapshot{}
79+
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(snapshot); err != nil {
80+
return nil, err
81+
}
82+
83+
return snapshot, nil
84+
}
85+
86+
// ListInstanceSnapshots lists all snapshots for an instance
87+
func (c *Client) ListInstanceSnapshots(instanceID string) ([]InstanceSnapshot, error) {
88+
url := fmt.Sprintf("/v2/instances/%s/snapshots", instanceID)
89+
resp, err := c.SendGetRequest(url)
90+
if err != nil {
91+
return nil, decodeError(err)
92+
}
93+
94+
snapshots := make([]InstanceSnapshot, 0)
95+
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&snapshots); err != nil {
96+
return nil, err
97+
}
98+
99+
return snapshots, nil
100+
}
101+
102+
// UpdateInstanceSnapshot updates a snapshot of an instance
103+
func (c *Client) UpdateInstanceSnapshot(instanceID, snapshotID string, params *UpdateInstanceSnapshotParams) (*InstanceSnapshot, error) {
104+
url := fmt.Sprintf("/v2/instances/%s/snapshots/%s", instanceID, snapshotID)
105+
resp, err := c.SendPutRequest(url, params)
106+
if err != nil {
107+
return nil, decodeError(err)
108+
}
109+
110+
snapshot := &InstanceSnapshot{}
111+
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(snapshot); err != nil {
112+
return nil, err
113+
}
114+
115+
return snapshot, nil
116+
}
117+
118+
// DeleteInstanceSnapshot deletes a snapshot of an instance
119+
func (c *Client) DeleteInstanceSnapshot(instanceID, snapshotID string) error {
120+
url := fmt.Sprintf("/v2/instances/%s/snapshots/%s", instanceID, snapshotID)
121+
_, err := c.SendDeleteRequest(url)
122+
if err != nil {
123+
return decodeError(err)
124+
}
125+
126+
return nil
127+
}
128+
129+
// RestoreInstanceSnapshot restores a snapshot of an instance
130+
func (c *Client) RestoreInstanceSnapshot(instanceID, snapshotID string, params *RestoreInstanceSnapshotParams) (*ResourceSnapshotRestore, error) {
131+
url := fmt.Sprintf("/v2/instances/%s/snapshots/%s/restore", instanceID, snapshotID)
132+
body, err := c.SendPostRequest(url, params)
133+
if err != nil {
134+
return nil, decodeError(err)
135+
}
136+
var restoreInfo ResourceSnapshotRestore
137+
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&restoreInfo); err != nil {
138+
return nil, decodeError(err)
139+
}
140+
141+
return &restoreInfo, nil
142+
}

0 commit comments

Comments
 (0)