instance_disks.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 // InstanceDisk represents an Instance Disk object
12 type InstanceDisk struct {
13 ID int `json:"id"`
14 Label string `json:"label"`
15 Status DiskStatus `json:"status"`
16 Size int `json:"size"`
17 Filesystem DiskFilesystem `json:"filesystem"`
18 Created *time.Time `json:"-"`
19 Updated *time.Time `json:"-"`
20
21 // NOTE: Disk encryption may not currently be available to all users.
22 DiskEncryption InstanceDiskEncryption `json:"disk_encryption"`
23 }
24
25 // DiskFilesystem constants start with Filesystem and include Linode API Filesystems
26 type DiskFilesystem string
27
28 // DiskFilesystem constants represent the filesystems types an Instance Disk may use
29 const (
30 FilesystemRaw DiskFilesystem = "raw"
31 FilesystemSwap DiskFilesystem = "swap"
32 FilesystemExt3 DiskFilesystem = "ext3"
33 FilesystemExt4 DiskFilesystem = "ext4"
34 FilesystemInitrd DiskFilesystem = "initrd"
35 )
36
37 // DiskStatus constants have the prefix "Disk" and include Linode API Instance Disk Status
38 type DiskStatus string
39
40 // DiskStatus constants represent the status values an Instance Disk may have
41 const (
42 DiskReady DiskStatus = "ready"
43 DiskNotReady DiskStatus = "not ready"
44 DiskDeleting DiskStatus = "deleting"
45 )
46
47 // InstanceDiskCreateOptions are InstanceDisk settings that can be used at creation
48 type InstanceDiskCreateOptions struct {
49 Label string `json:"label"`
50 Size int `json:"size"`
51
52 // Image is optional, but requires RootPass if provided
53 Image string `json:"image,omitempty"`
54 RootPass string `json:"root_pass,omitempty"`
55
56 Filesystem string `json:"filesystem,omitempty"`
57 AuthorizedKeys []string `json:"authorized_keys,omitempty"`
58 AuthorizedUsers []string `json:"authorized_users,omitempty"`
59 StackscriptID int `json:"stackscript_id,omitempty"`
60 StackscriptData map[string]string `json:"stackscript_data,omitempty"`
61 }
62
63 // InstanceDiskUpdateOptions are InstanceDisk settings that can be used in updates
64 type InstanceDiskUpdateOptions struct {
65 Label string `json:"label"`
66 }
67
68 type InstanceDiskCloneOptions struct{}
69
70 // ListInstanceDisks lists InstanceDisks
71 func (c *Client) ListInstanceDisks(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceDisk, error) {
72 return getPaginatedResults[InstanceDisk](ctx, c, formatAPIPath("linode/instances/%d/disks", linodeID), opts)
73 }
74
75 // UnmarshalJSON implements the json.Unmarshaler interface
76 func (i *InstanceDisk) UnmarshalJSON(b []byte) error {
77 type Mask InstanceDisk
78
79 p := struct {
80 *Mask
81
82 Created *parseabletime.ParseableTime `json:"created"`
83 Updated *parseabletime.ParseableTime `json:"updated"`
84 }{
85 Mask: (*Mask)(i),
86 }
87
88 if err := json.Unmarshal(b, &p); err != nil {
89 return err
90 }
91
92 i.Created = (*time.Time)(p.Created)
93 i.Updated = (*time.Time)(p.Updated)
94
95 return nil
96 }
97
98 // GetInstanceDisk gets the template with the provided ID
99 func (c *Client) GetInstanceDisk(ctx context.Context, linodeID int, diskID int) (*InstanceDisk, error) {
100 e := formatAPIPath("linode/instances/%d/disks/%d", linodeID, diskID)
101 return doGETRequest[InstanceDisk](ctx, c, e)
102 }
103
104 // CreateInstanceDisk creates a new InstanceDisk for the given Instance
105 func (c *Client) CreateInstanceDisk(ctx context.Context, linodeID int, opts InstanceDiskCreateOptions) (*InstanceDisk, error) {
106 e := formatAPIPath("linode/instances/%d/disks", linodeID)
107 return doPOSTRequest[InstanceDisk](ctx, c, e, opts)
108 }
109
110 // UpdateInstanceDisk creates a new InstanceDisk for the given Instance
111 func (c *Client) UpdateInstanceDisk(ctx context.Context, linodeID int, diskID int, opts InstanceDiskUpdateOptions) (*InstanceDisk, error) {
112 e := formatAPIPath("linode/instances/%d/disks/%d", linodeID, diskID)
113 return doPUTRequest[InstanceDisk](ctx, c, e, opts)
114 }
115
116 // RenameInstanceDisk renames an InstanceDisk
117 func (c *Client) RenameInstanceDisk(ctx context.Context, linodeID int, diskID int, label string) (*InstanceDisk, error) {
118 return c.UpdateInstanceDisk(ctx, linodeID, diskID, InstanceDiskUpdateOptions{Label: label})
119 }
120
121 // ResizeInstanceDisk resizes the size of the Instance disk
122 func (c *Client) ResizeInstanceDisk(ctx context.Context, linodeID int, diskID int, size int) error {
123 opts := map[string]any{
124 "size": size,
125 }
126
127 e := formatAPIPath("linode/instances/%d/disks/%d/resize", linodeID, diskID)
128
129 return doPOSTRequestNoResponseBody(ctx, c, e, opts)
130 }
131
132 // PasswordResetInstanceDisk resets the "root" account password on the Instance disk
133 func (c *Client) PasswordResetInstanceDisk(ctx context.Context, linodeID int, diskID int, password string) error {
134 opts := map[string]any{
135 "password": password,
136 }
137
138 e := formatAPIPath("linode/instances/%d/disks/%d/password", linodeID, diskID)
139
140 return doPOSTRequestNoResponseBody(ctx, c, e, opts)
141 }
142
143 // DeleteInstanceDisk deletes a Linode Instance Disk
144 func (c *Client) DeleteInstanceDisk(ctx context.Context, linodeID int, diskID int) error {
145 e := formatAPIPath("linode/instances/%d/disks/%d", linodeID, diskID)
146 return doDELETERequest(ctx, c, e)
147 }
148
149 // CloneInstanceDisk clones the given InstanceDisk for the given Instance
150 func (c *Client) CloneInstanceDisk(ctx context.Context, linodeID, diskID int, opts InstanceDiskCloneOptions) (*InstanceDisk, error) {
151 e := formatAPIPath("linode/instances/%d/disks/%d/clone", linodeID, diskID)
152 return doPOSTRequest[InstanceDisk](ctx, c, e, opts)
153 }
154