instance_snapshots.go raw
1 package linodego
2
3 import (
4 "context"
5 "encoding/json"
6 "time"
7
8 "github.com/linode/linodego/internal/parseabletime"
9 )
10
11 // InstanceBackupsResponse response struct for backup snapshot
12 type InstanceBackupsResponse struct {
13 Automatic []*InstanceSnapshot `json:"automatic"`
14 Snapshot *InstanceBackupSnapshotResponse `json:"snapshot"`
15 }
16
17 // InstanceBackupSnapshotResponse fields are those representing Instance Backup Snapshots
18 type InstanceBackupSnapshotResponse struct {
19 Current *InstanceSnapshot `json:"current"`
20 InProgress *InstanceSnapshot `json:"in_progress"`
21 }
22
23 // RestoreInstanceOptions fields are those accepted by InstanceRestore
24 type RestoreInstanceOptions struct {
25 LinodeID int `json:"linode_id"`
26 Overwrite bool `json:"overwrite"`
27 }
28
29 // InstanceSnapshot represents a linode backup snapshot
30 type InstanceSnapshot struct {
31 ID int `json:"id"`
32 Label string `json:"label"`
33 Status InstanceSnapshotStatus `json:"status"`
34 Type string `json:"type"`
35 Created *time.Time `json:"-"`
36 Updated *time.Time `json:"-"`
37 Finished *time.Time `json:"-"`
38 Configs []string `json:"configs"`
39 Disks []*InstanceSnapshotDisk `json:"disks"`
40 Available bool `json:"available"`
41 }
42
43 // InstanceSnapshotDisk fields represent the source disk of a Snapshot
44 type InstanceSnapshotDisk struct {
45 Label string `json:"label"`
46 Size int `json:"size"`
47 Filesystem string `json:"filesystem"`
48 }
49
50 // InstanceSnapshotStatus constants start with Snapshot and include Linode API Instance Backup Snapshot status values
51 type InstanceSnapshotStatus string
52
53 // InstanceSnapshotStatus constants reflect the current status of an Instance Snapshot
54 var (
55 SnapshotPaused InstanceSnapshotStatus = "paused"
56 SnapshotPending InstanceSnapshotStatus = "pending"
57 SnapshotRunning InstanceSnapshotStatus = "running"
58 SnapshotNeedsPostProcessing InstanceSnapshotStatus = "needsPostProcessing"
59 SnapshotSuccessful InstanceSnapshotStatus = "successful"
60 SnapshotFailed InstanceSnapshotStatus = "failed"
61 SnapshotUserAborted InstanceSnapshotStatus = "userAborted"
62 )
63
64 // UnmarshalJSON implements the json.Unmarshaler interface
65 func (i *InstanceSnapshot) UnmarshalJSON(b []byte) error {
66 type Mask InstanceSnapshot
67
68 p := struct {
69 *Mask
70
71 Created *parseabletime.ParseableTime `json:"created"`
72 Updated *parseabletime.ParseableTime `json:"updated"`
73 Finished *parseabletime.ParseableTime `json:"finished"`
74 }{
75 Mask: (*Mask)(i),
76 }
77
78 if err := json.Unmarshal(b, &p); err != nil {
79 return err
80 }
81
82 i.Created = (*time.Time)(p.Created)
83 i.Updated = (*time.Time)(p.Updated)
84 i.Finished = (*time.Time)(p.Finished)
85
86 return nil
87 }
88
89 // GetInstanceSnapshot gets the snapshot with the provided ID
90 func (c *Client) GetInstanceSnapshot(ctx context.Context, linodeID int, snapshotID int) (*InstanceSnapshot, error) {
91 e := formatAPIPath("linode/instances/%d/backups/%d", linodeID, snapshotID)
92 return doGETRequest[InstanceSnapshot](ctx, c, e)
93 }
94
95 // CreateInstanceSnapshot Creates or Replaces the snapshot Backup of a Linode. If a previous snapshot exists for this Linode, it will be deleted.
96 func (c *Client) CreateInstanceSnapshot(ctx context.Context, linodeID int, label string) (*InstanceSnapshot, error) {
97 opts := map[string]string{"label": label}
98
99 e := formatAPIPath("linode/instances/%d/backups", linodeID)
100
101 return doPOSTRequest[InstanceSnapshot](ctx, c, e, opts)
102 }
103
104 // GetInstanceBackups gets the Instance's available Backups.
105 // This is not called ListInstanceBackups because a single object is returned, matching the API response.
106 func (c *Client) GetInstanceBackups(ctx context.Context, linodeID int) (*InstanceBackupsResponse, error) {
107 e := formatAPIPath("linode/instances/%d/backups", linodeID)
108 return doGETRequest[InstanceBackupsResponse](ctx, c, e)
109 }
110
111 // EnableInstanceBackups Enables backups for the specified Linode.
112 func (c *Client) EnableInstanceBackups(ctx context.Context, linodeID int) error {
113 e := formatAPIPath("linode/instances/%d/backups/enable", linodeID)
114 return doPOSTRequestNoRequestResponseBody(ctx, c, e)
115 }
116
117 // CancelInstanceBackups Cancels backups for the specified Linode.
118 func (c *Client) CancelInstanceBackups(ctx context.Context, linodeID int) error {
119 e := formatAPIPath("linode/instances/%d/backups/cancel", linodeID)
120 return doPOSTRequestNoRequestResponseBody(ctx, c, e)
121 }
122
123 // RestoreInstanceBackup Restores a Linode's Backup to the specified Linode.
124 func (c *Client) RestoreInstanceBackup(ctx context.Context, linodeID int, backupID int, opts RestoreInstanceOptions) error {
125 e := formatAPIPath("linode/instances/%d/backups/%d/restore", linodeID, backupID)
126 return doPOSTRequestNoResponseBody(ctx, c, e, opts)
127 }
128