-
Notifications
You must be signed in to change notification settings - Fork 82
/
instance_snapshots.go
143 lines (119 loc) · 5 KB
/
instance_snapshots.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package linodego
import (
"context"
"encoding/json"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
// InstanceBackupsResponse response struct for backup snapshot
type InstanceBackupsResponse struct {
Automatic []*InstanceSnapshot `json:"automatic"`
Snapshot *InstanceBackupSnapshotResponse `json:"snapshot"`
}
// InstanceBackupSnapshotResponse fields are those representing Instance Backup Snapshots
type InstanceBackupSnapshotResponse struct {
Current *InstanceSnapshot `json:"current"`
InProgress *InstanceSnapshot `json:"in_progress"`
}
// RestoreInstanceOptions fields are those accepted by InstanceRestore
type RestoreInstanceOptions struct {
LinodeID int `json:"linode_id"`
Overwrite bool `json:"overwrite"`
}
// InstanceSnapshot represents a linode backup snapshot
type InstanceSnapshot struct {
ID int `json:"id"`
Label string `json:"label"`
Status InstanceSnapshotStatus `json:"status"`
Type string `json:"type"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Finished *time.Time `json:"-"`
Configs []string `json:"configs"`
Disks []*InstanceSnapshotDisk `json:"disks"`
Available bool `json:"available"`
}
// InstanceSnapshotDisk fields represent the source disk of a Snapshot
type InstanceSnapshotDisk struct {
Label string `json:"label"`
Size int `json:"size"`
Filesystem string `json:"filesystem"`
}
// InstanceSnapshotStatus constants start with Snapshot and include Linode API Instance Backup Snapshot status values
type InstanceSnapshotStatus string
// InstanceSnapshotStatus constants reflect the current status of an Instance Snapshot
var (
SnapshotPaused InstanceSnapshotStatus = "paused"
SnapshotPending InstanceSnapshotStatus = "pending"
SnapshotRunning InstanceSnapshotStatus = "running"
SnapshotNeedsPostProcessing InstanceSnapshotStatus = "needsPostProcessing"
SnapshotSuccessful InstanceSnapshotStatus = "successful"
SnapshotFailed InstanceSnapshotStatus = "failed"
SnapshotUserAborted InstanceSnapshotStatus = "userAborted"
)
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *InstanceSnapshot) UnmarshalJSON(b []byte) error {
type Mask InstanceSnapshot
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
Finished *parseabletime.ParseableTime `json:"finished"`
}{
Mask: (*Mask)(i),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
i.Created = (*time.Time)(p.Created)
i.Updated = (*time.Time)(p.Updated)
i.Finished = (*time.Time)(p.Finished)
return nil
}
// GetInstanceSnapshot gets the snapshot with the provided ID
func (c *Client) GetInstanceSnapshot(ctx context.Context, linodeID int, snapshotID int) (*InstanceSnapshot, error) {
e := formatAPIPath("linode/instances/%d/backups/%d", linodeID, snapshotID)
response, err := doGETRequest[InstanceSnapshot](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// CreateInstanceSnapshot Creates or Replaces the snapshot Backup of a Linode. If a previous snapshot exists for this Linode, it will be deleted.
func (c *Client) CreateInstanceSnapshot(ctx context.Context, linodeID int, label string) (*InstanceSnapshot, error) {
opts := map[string]string{"label": label}
e := formatAPIPath("linode/instances/%d/backups", linodeID)
response, err := doPOSTRequest[InstanceSnapshot](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
// GetInstanceBackups gets the Instance's available Backups.
// This is not called ListInstanceBackups because a single object is returned, matching the API response.
func (c *Client) GetInstanceBackups(ctx context.Context, linodeID int) (*InstanceBackupsResponse, error) {
e := formatAPIPath("linode/instances/%d/backups", linodeID)
response, err := doGETRequest[InstanceBackupsResponse](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// EnableInstanceBackups Enables backups for the specified Linode.
func (c *Client) EnableInstanceBackups(ctx context.Context, linodeID int) error {
e := formatAPIPath("linode/instances/%d/backups/enable", linodeID)
_, err := doPOSTRequest[InstanceBackup, any](ctx, c, e)
return err
}
// CancelInstanceBackups Cancels backups for the specified Linode.
func (c *Client) CancelInstanceBackups(ctx context.Context, linodeID int) error {
e := formatAPIPath("linode/instances/%d/backups/cancel", linodeID)
_, err := doPOSTRequest[InstanceBackup, any](ctx, c, e)
return err
}
// RestoreInstanceBackup Restores a Linode's Backup to the specified Linode.
func (c *Client) RestoreInstanceBackup(ctx context.Context, linodeID int, backupID int, opts RestoreInstanceOptions) error {
e := formatAPIPath("linode/instances/%d/backups/%d/restore", linodeID, backupID)
_, err := doPOSTRequest[InstanceBackup](ctx, c, e, opts)
return err
}