forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_disks.go
175 lines (140 loc) · 5.67 KB
/
instance_disks.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package linodego
import (
"context"
"encoding/json"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
// InstanceDisk represents an Instance Disk object
type InstanceDisk struct {
ID int `json:"id"`
Label string `json:"label"`
Status DiskStatus `json:"status"`
Size int `json:"size"`
Filesystem DiskFilesystem `json:"filesystem"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
// NOTE: Disk encryption may not currently be available to all users.
DiskEncryption InstanceDiskEncryption `json:"disk_encryption"`
}
// DiskFilesystem constants start with Filesystem and include Linode API Filesystems
type DiskFilesystem string
// DiskFilesystem constants represent the filesystems types an Instance Disk may use
const (
FilesystemRaw DiskFilesystem = "raw"
FilesystemSwap DiskFilesystem = "swap"
FilesystemExt3 DiskFilesystem = "ext3"
FilesystemExt4 DiskFilesystem = "ext4"
FilesystemInitrd DiskFilesystem = "initrd"
)
// DiskStatus constants have the prefix "Disk" and include Linode API Instance Disk Status
type DiskStatus string
// DiskStatus constants represent the status values an Instance Disk may have
const (
DiskReady DiskStatus = "ready"
DiskNotReady DiskStatus = "not ready"
DiskDeleting DiskStatus = "deleting"
)
// InstanceDiskCreateOptions are InstanceDisk settings that can be used at creation
type InstanceDiskCreateOptions struct {
Label string `json:"label"`
Size int `json:"size"`
// Image is optional, but requires RootPass if provided
Image string `json:"image,omitempty"`
RootPass string `json:"root_pass,omitempty"`
Filesystem string `json:"filesystem,omitempty"`
AuthorizedKeys []string `json:"authorized_keys,omitempty"`
AuthorizedUsers []string `json:"authorized_users,omitempty"`
StackscriptID int `json:"stackscript_id,omitempty"`
StackscriptData map[string]string `json:"stackscript_data,omitempty"`
}
// InstanceDiskUpdateOptions are InstanceDisk settings that can be used in updates
type InstanceDiskUpdateOptions struct {
Label string `json:"label"`
}
type InstanceDiskCloneOptions struct{}
// ListInstanceDisks lists InstanceDisks
func (c *Client) ListInstanceDisks(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceDisk, error) {
response, err := getPaginatedResults[InstanceDisk](ctx, c, formatAPIPath("linode/instances/%d/disks", linodeID), opts)
if err != nil {
return nil, err
}
return response, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *InstanceDisk) UnmarshalJSON(b []byte) error {
type Mask InstanceDisk
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
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)
return nil
}
// GetInstanceDisk gets the template with the provided ID
func (c *Client) GetInstanceDisk(ctx context.Context, linodeID int, diskID int) (*InstanceDisk, error) {
e := formatAPIPath("linode/instances/%d/disks/%d", linodeID, diskID)
response, err := doGETRequest[InstanceDisk](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// CreateInstanceDisk creates a new InstanceDisk for the given Instance
func (c *Client) CreateInstanceDisk(ctx context.Context, linodeID int, opts InstanceDiskCreateOptions) (*InstanceDisk, error) {
e := formatAPIPath("linode/instances/%d/disks", linodeID)
response, err := doPOSTRequest[InstanceDisk](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
// UpdateInstanceDisk creates a new InstanceDisk for the given Instance
func (c *Client) UpdateInstanceDisk(ctx context.Context, linodeID int, diskID int, opts InstanceDiskUpdateOptions) (*InstanceDisk, error) {
e := formatAPIPath("linode/instances/%d/disks/%d", linodeID, diskID)
response, err := doPUTRequest[InstanceDisk](ctx, c, e, opts)
if err != nil {
return nil, err
}
return response, nil
}
// RenameInstanceDisk renames an InstanceDisk
func (c *Client) RenameInstanceDisk(ctx context.Context, linodeID int, diskID int, label string) (*InstanceDisk, error) {
return c.UpdateInstanceDisk(ctx, linodeID, diskID, InstanceDiskUpdateOptions{Label: label})
}
// ResizeInstanceDisk resizes the size of the Instance disk
func (c *Client) ResizeInstanceDisk(ctx context.Context, linodeID int, diskID int, size int) error {
opts := map[string]any{
"size": size,
}
e := formatAPIPath("linode/instances/%d/disks/%d/resize", linodeID, diskID)
_, err := doPOSTRequest[InstanceDisk](ctx, c, e, opts)
return err
}
// PasswordResetInstanceDisk resets the "root" account password on the Instance disk
func (c *Client) PasswordResetInstanceDisk(ctx context.Context, linodeID int, diskID int, password string) error {
opts := map[string]any{
"password": password,
}
e := formatAPIPath("linode/instances/%d/disks/%d/password", linodeID, diskID)
_, err := doPOSTRequest[InstanceDisk](ctx, c, e, opts)
return err
}
// DeleteInstanceDisk deletes a Linode Instance Disk
func (c *Client) DeleteInstanceDisk(ctx context.Context, linodeID int, diskID int) error {
e := formatAPIPath("linode/instances/%d/disks/%d", linodeID, diskID)
err := doDELETERequest(ctx, c, e)
return err
}
// CloneInstanceDisk clones the given InstanceDisk for the given Instance
func (c *Client) CloneInstanceDisk(ctx context.Context, linodeID, diskID int, opts InstanceDiskCloneOptions) (*InstanceDisk, error) {
e := formatAPIPath("linode/instances/%d/disks/%d/clone", linodeID, diskID)
return doPOSTRequest[InstanceDisk](ctx, c, e, opts)
}