-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisk_volume.go
308 lines (275 loc) · 9.27 KB
/
disk_volume.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Proof of Concepts for the Cloud-Barista Multi-Cloud Project.
// * Cloud-Barista: https://github.com/cloud-barista
//
// This code is based on the following material: https://github.com/mindjiver/gopherstack (MIT License)
//
// KT Cloud SDK go
//
// by ETRI, 2024.01.
package ktcloudsdk
import (
"net/url"
)
// KT Cloud (G1/G2 Platform) Disk Volume API : https://cloud.kt.com/docs/open-api-guide/g/computing/disk-volume
type CreateVolumeReqInfo struct {
Name string // Required. Volume Name.
DiskOfferingId string // Required. DiskOfferingId
ZoneId string // Required
UsagePlanType string // default : hourly
Account string
DomainId string
Size string // Custom size of Volume
SnapshotId string
VMId string // It is only valid if the 'SnapshotId' field is present, and allows the volume to be automatically connected to the VM after creation.
ProductCode string // ### Create volume using product abbreviations (ex. STG 100G, SSD 300G, etc.)
// ### If the 'ProductCode' field is used, the 'DiskOfferingId' field value is ignored.
IOPS string
}
type ListVolumeReqInfo struct {
Account string
DomainId string
IsRecursive bool // Default : false
ID string // Volume ID
Keyword string
Name string // Volume Name
Page string
PageSize string
Type string
VMId string
ZoneId string
Install bool // Default : false
}
type ResizeVolumeReqInfo struct {
ID string // Required. Volume ID
VMId string // Required
Size string // Required. Only 50(Linux series only), 80, and 100 are available.
IsLinux string // Required. 'Y' for Linux series, 'N' for Windows series
}
type AttachVolumeReqInfo struct {
ID string // Required. Volume ID
VMId string // Required
DeviceId string // ID of VM Device where Volume is connected. When unused, a Device ID that can be used sequentially is selected automatically.
}
type DetachVolumeReqInfo struct {
ID string // Required. Volume ID
VMId string
DeviceId string
}
// # Create a Disk Volume
// DiskOfferingId : https://cloud.kt.com/docs/open-api-guide/g/computing/disk-volume
func (c KtCloudClient) CreateVolume(req CreateVolumeReqInfo) (CreateVolumeResponse, error) {
var resp CreateVolumeResponse
params := url.Values{}
params.Set("name", req.Name)
params.Set("diskofferingid", req.DiskOfferingId)
params.Set("zoneid", req.ZoneId)
if req.UsagePlanType != "" {
params.Set("usageplantype", req.UsagePlanType)
}
if req.Account != "" {
params.Set("account", req.Account)
}
if req.DomainId != "" {
params.Set("domainid", req.DomainId)
}
if req.Size != "" {
params.Set("size", req.Size)
}
if req.SnapshotId != "" {
params.Set("snapshotid", req.SnapshotId)
}
if req.VMId != "" {
params.Set("virtualmachineid", req.VMId)
}
if req.ProductCode != "" {
params.Set("productcode", req.ProductCode)
}
if req.IOPS != "" {
params.Set("iops", req.IOPS)
}
response, err := NewRequest(c, "createVolume", params)
if err != nil {
return resp, err
}
resp = response.(CreateVolumeResponse)
return resp, nil
}
// # List Disk Volumes
func (c KtCloudClient) ListVolumes(req ListVolumeReqInfo) (ListVolumesResponse, error) {
var resp ListVolumesResponse
params := url.Values{}
if req.Account != "" {
params.Set("account", req.Account)
}
if req.DomainId != "" {
params.Set("domainid", req.DomainId)
}
if req.IsRecursive {
params.Set("isrecursive", "true")
}
if req.ID != "" {
params.Set("id", req.ID)
}
if req.Keyword != "" {
params.Set("keyword", req.Keyword)
}
if req.Name != "" {
params.Set("name", req.Name)
}
if req.Page != "" {
params.Set("page", req.Page)
}
if req.PageSize != "" {
params.Set("pagesize", req.PageSize)
}
if req.Type != "" {
params.Set("type", req.Type)
}
if req.VMId != "" {
params.Set("virtualmachineid", req.VMId)
}
if req.ZoneId != "" {
params.Set("zoneid", req.ZoneId)
}
if req.Install {
params.Set("install", "true")
}
response, err := NewRequest(c, "listVolumes", params)
if err != nil {
return resp, err
}
resp = response.(ListVolumesResponse)
return resp, nil
}
// # Resize(Change) Disk Volume Size (This is only for Bootalbe Disk of a VM.)
func (c KtCloudClient) ResizeVolume(req ResizeVolumeReqInfo) (ResizeVolumeResponse, error) {
var resp ResizeVolumeResponse
params := url.Values{}
params.Set("id", req.ID) // Volume ID
params.Set("vmid", req.VMId)
params.Set("size", req.Size)
params.Set("isLinux", req.IsLinux)
response, err := NewRequest(c, "resizeVolume", params)
if err != nil {
return resp, err
}
resp = response.(ResizeVolumeResponse)
return resp, nil
}
// # Delete a Disk Volume
func (c KtCloudClient) DeleteVolume(id string) (DeleteVolumeResponse, error) {
var resp DeleteVolumeResponse
params := url.Values{}
params.Set("id", id)
response, err := NewRequest(c, "deleteVolume", params)
if err != nil {
return resp, err
}
resp = response.(DeleteVolumeResponse)
return resp, nil
}
// # Attach a Disk Volume to VM
func (c KtCloudClient) AttachVolume(req AttachVolumeReqInfo) (AttachVolumeResponse, error) {
var resp AttachVolumeResponse
params := url.Values{}
params.Set("id", req.ID) // Volume ID
params.Set("virtualmachineid", req.VMId) // Not 'vmid' but 'virtualmachineid'
if req.DeviceId != "" {
params.Set("deviceid", req.DeviceId)
}
response, err := NewRequest(c, "attachVolume", params)
if err != nil {
return resp, err
}
resp = response.(AttachVolumeResponse)
return resp, nil
}
// # Detach a Disk Volume from VM
func (c KtCloudClient) DetachVolume(req DetachVolumeReqInfo) (DetachVolumeResponse, error) {
var resp DetachVolumeResponse
params := url.Values{}
params.Set("id", req.ID) // Volume ID
if req.VMId != "" {
params.Set("virtualmachineid", req.VMId) // Not 'vmid' but 'virtualmachineid'
}
if req.DeviceId != "" {
params.Set("deviceid", req.DeviceId)
}
response, err := NewRequest(c, "detachVolume", params)
if err != nil {
return resp, err
}
resp = response.(DetachVolumeResponse)
return resp, nil
}
type Volume struct {
ID string `json:"id"`
Name string `json:"name"`
ZoneId string `json:"zoneid"`
ZoneName string `json:"zonename"`
Type string `json:"type"`
DeviceId int `json:"deviceid"`
VMId string `json:"virtualmachineid"`
VMName string `json:"vmname"`
VMDisplayName string `json:"vmdisplayname"`
VMState string `json:"vmstate"`
TemplateId string `json:"templateid"` // Volume with the OS installed
TemplateName string `json:"templatename"` // Volume with the OS installed
TemplateDisplayText string `json:"templatedisplaytext"` // Volume with the OS installed
ProvisioningType string `json:"provisioningtype"`
Size int64 `json:"size"`
MinIOPS int64 `json:"miniops"`
MaxIOPS int64 `json:"maxiops"`
Created string `json:"created"`
State string `json:"state"`
Account string `json:"account"`
DomainId string `json:"domainid"`
Domain string `json:"domain"`
StorageType string `json:"storagetype"`
DiskOfferingId string `json:"diskofferingid"`
DiskOfferingName string `json:"diskofferingname"`
DiskOfferingDisplayText string `json:"diskofferingdisplaytext"`
ServiceofferingId string `json:"serviceofferingid"` // In case, Type value : ROOT
ServiceofferingName string `json:"serviceofferingname"` // In case, Type value : ROOT
ServiceofferingDisplayText string `json:"serviceofferingdisplaytext"` // In case, Type value : ROOT
AttachedTime string `json:"attached"`
Destroyed bool `json:"destroyed"`
IsExtractable bool `json:"isextractable"`
QuiesceVM bool `json:"quiescevm"`
Tags []string `json:"tags"`
UsagePlanType string `json:"usageplantype"`
VolumeType string `json:"volumetype"`
}
type CreateVolumeResponse struct {
Createvolumeresponse struct {
Volume Volume `json:"volume"`
JobId string `json:"jobid"`
ID string `json:"id"`
} `json:"createvolumeresponse"`
}
type ListVolumesResponse struct {
Listvolumesresponse struct {
Volume []Volume `json:"volume"`
Count int `json:"count"`
} `json:"listvolumesresponse"`
}
type ResizeVolumeResponse struct {
Resizevolumeresponse struct {
JobId string `json:"jobid"`
} `json:"resizevolumeresponse"`
}
type DeleteVolumeResponse struct {
Deletevolumeresponse struct {
Success string `json:"success"` // 'string' type of value!! 'true' or 'false'
} `json:"deletevolumeresponse"`
}
type AttachVolumeResponse struct {
Attachvolumeresponse struct {
JobId string `json:"jobid"`
} `json:"attachvolumeresponse"`
}
type DetachVolumeResponse struct {
Detachvolumeresponse struct {
JobId string `json:"jobid"`
} `json:"detachvolumeresponse"`
}