This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
cds.go
366 lines (325 loc) · 9.34 KB
/
cds.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package bcc
import (
"bytes"
"encoding/json"
"fmt"
"k8s.io/cloud-provider-baiducloud/pkg/cloud-sdk/bce"
)
type Volume struct {
Id string `json:"id"`
Name string `json:"name"`
DiskSizeInGB int `json:"diskSizeInGB"`
PaymentTiming string `json:"paymentTiming"`
CreateTime string `json:"createTime"`
ExpireTime string `json:"expireTime"`
Status VolumeStatus `json:"status"`
VolumeType VolumeType `json:"type"`
StorageType StorageType `json:"storageType"`
Desc string `json:"desc"`
Attachments []VolumeAttachment `json:"attachments"`
ZoneName string `json:"zoneName"`
IsSystemVolume bool `json:"isSystemVolume"`
}
type VolumeStatus string
const (
VOLUMESTATUS_CREATE VolumeStatus = "Creating"
VOLUMESTATUS_AVAILABLE VolumeStatus = "Available"
VOLUMESTATUS_ATTACHING VolumeStatus = "Attaching"
VOLUMESTATUS_NOTAVALIABLE VolumeStatus = "NotAvailable"
VOLUMESTATUS_INUSE VolumeStatus = "InUse"
VOLUMESTATUS_DETACHING VolumeStatus = "Detaching"
VOLUMESTATUS_DELETING VolumeStatus = "Deleting"
VOLUMESTATUS_DELETED VolumeStatus = "Deleted"
VOLUMESTATUS_SCALING VolumeStatus = "Scaling"
VOLUMESTATUS_EXPIRED VolumeStatus = "Expired"
VOLUMESTATUS_ERROR VolumeStatus = "Error"
VOLUMESTATUS_SNAPSHOTPROCESSING VolumeStatus = "SnapshotProcessing"
VOLUMESTATUS_IMAGEPROCESSING VolumeStatus = "ImageProcessing"
)
type VolumeType string
const (
VOLUME_TYPE_SYSTEM VolumeType = "System"
VOLUME_TYPE_EPHEMERAL VolumeType = "Ephemeral"
VOLUME_TYPE_CDS VolumeType = "Cds"
)
type StorageType string
const (
STORAGE_TYPE_STD1 StorageType = "std1"
STORAGE_TYPE_HP1 StorageType = "hp1"
STORAGE_TYPE_SATA StorageType = "sata"
STORAGE_TYPE_SSD StorageType = "ssd"
)
// VolumeAttachment define attach info
type VolumeAttachment struct {
VolumeId string `json:"volumeId"`
InstanceId string `json:"instanceId"`
// mount path
Device string `json:"device"`
}
// CdsPreMountInfo define premount
type CdsPreMountInfo struct {
MountPath string `json:"mountPath"`
CdsConfig []DiskSizeConfig `json:"cdsConfig"`
}
// DiskSizeConfig define distsize config
type DiskSizeConfig struct {
Size string `json:"size"`
SnapshotID string `json:"snapshotID"`
SnapshotName string `json:"snapshotName"`
VolumeType string `json:"volumeType"`
StorageType string `json:"storageType"`
LogicalZone string `json:"logicalZone"`
}
// DeleteVolume Delete a volume
func (c *Client) DeleteVolume(volumeId string) error {
if volumeId == "" {
return fmt.Errorf("DeleteVolume need a id")
}
req, err := bce.NewRequest("DELETE", c.GetURL("v2/volume"+"/"+volumeId, nil), nil)
if err != nil {
return err
}
_, err = c.SendRequest(req, nil)
if err != nil {
return err
}
return nil
}
type CreateVolumeArgs struct {
PurchaseCount int `json:"purchaseCount,omitempty"`
CdsSizeInGB int `json:"cdsSizeInGB"`
StorageType StorageType `json:"storageType"`
Billing *bce.Billing `json:"billing"`
SnapshotId string `json:"snapshotId,omitempty"`
ZoneName string `json:"zoneName,omitempty"`
}
type CreateVolumeResponse struct {
VolumeIds []string `json:"volumeIds,omitempty"`
}
func (args *CreateVolumeArgs) validate() error {
if args == nil {
return fmt.Errorf("CreateVolumeArgs need args")
}
if args.StorageType == "" {
return fmt.Errorf("CreateVolumeArgs need StorageType")
}
if args.Billing == nil {
return fmt.Errorf("CreateVolumeArgs need Billing")
}
if args.CdsSizeInGB == 0 {
return fmt.Errorf("CreateVolumeArgs need CdsSizeInGB")
}
return nil
}
// CreateVolumes create a volume
func (c *Client) CreateVolumes(args *CreateVolumeArgs) ([]string, error) {
err := args.validate()
if err != nil {
return nil, err
}
params := map[string]string{
"clientToken": c.GenerateClientToken(),
}
postContent, err := json.Marshal(args)
if err != nil {
return nil, err
}
req, err := bce.NewRequest("POST", c.GetURL("v2/volume", params), bytes.NewBuffer(postContent))
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, nil)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var blbsResp *CreateVolumeResponse
err = json.Unmarshal(bodyContent, &blbsResp)
if err != nil {
return nil, err
}
return blbsResp.VolumeIds, nil
}
type GetVolumeListArgs struct {
InstanceId string
ZoneName string
}
type GetVolumeListResponse struct {
Volumes []Volume `json:"volumes"`
Marker string `json:"marker"`
IsTruncated bool `json:"isTruncated"`
NextMarker string `json:"nextMarker"`
MaxKeys int `json:"maxKeys"`
}
// GetVolumeList get all volumes
func (c *Client) GetVolumeList(args *GetVolumeListArgs) ([]Volume, error) {
if args == nil {
args = &GetVolumeListArgs{}
}
params := map[string]string{
"zoneName": args.ZoneName,
"instanceId": args.InstanceId,
}
req, err := bce.NewRequest("GET", c.GetURL("v2/volume", params), nil)
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, nil)
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var blbsResp *GetVolumeListResponse
err = json.Unmarshal(bodyContent, &blbsResp)
if err != nil {
return nil, err
}
return blbsResp.Volumes, nil
}
type DescribeVolumeResponse struct {
Volume *Volume `json:"volume"`
}
// DescribeVolume describe a volume
// More info see https://cloud.baidu.com/doc/BCC/API.html#.E6.9F.A5.E8.AF.A2.E7.A3.81.E7.9B.98.E8.AF.A6.E6.83.85
func (c *Client) DescribeVolume(id string) (*Volume, error) {
if id == "" {
return nil, fmt.Errorf("DescribeVolume need a id")
}
req, err := bce.NewRequest("GET", c.GetURL("v2/volume"+"/"+id, nil), nil)
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, nil)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var ins DescribeVolumeResponse
err = json.Unmarshal(bodyContent, &ins)
if err != nil {
return nil, err
}
return ins.Volume, nil
}
// AttachCDSVolumeArgs describe attachcds args
type AttachCDSVolumeArgs struct {
VolumeId string `json:"-"`
InstanceId string `json:"instanceId"`
}
type AttachCDSVolumeResponse struct {
VolumeAttachment *VolumeAttachment `json:"volumeAttachment"`
}
func (args *AttachCDSVolumeArgs) validate() error {
if args == nil {
return fmt.Errorf("AttachCDSVolumeArgs need args")
}
if args.VolumeId == "" {
return fmt.Errorf("AttachCDSVolumeArgs need VolumeId")
}
if args.InstanceId == "" {
return fmt.Errorf("AttachCDSVolumeArgs need InstanceId")
}
return nil
}
// AttachCDSVolume attach a cds to vm
func (c *Client) AttachCDSVolume(args *AttachCDSVolumeArgs) (*VolumeAttachment, error) {
err := args.validate()
if err != nil {
return nil, err
}
params := map[string]string{
"attach": "",
"clientToken": c.GenerateClientToken(),
}
postContent, err := json.Marshal(args)
if err != nil {
return nil, err
}
req, err := bce.NewRequest("PUT", c.GetURL("v2/volume"+"/"+args.VolumeId, params), bytes.NewBuffer(postContent))
if err != nil {
return nil, err
}
resp, err := c.SendRequest(req, nil)
if err != nil {
return nil, err
}
bodyContent, err := resp.GetBodyContent()
if err != nil {
return nil, err
}
var blbsResp AttachCDSVolumeResponse
err = json.Unmarshal(bodyContent, &blbsResp)
if err != nil {
return nil, err
}
return blbsResp.VolumeAttachment, nil
}
// DetachCDSVolume detach a cds
// TODO: if a volume is ddetaching, need to wait
func (c *Client) DetachCDSVolume(args *AttachCDSVolumeArgs) error {
err := args.validate()
if err != nil {
return err
}
params := map[string]string{
"detach": "",
"clientToken": c.GenerateClientToken(),
}
postContent, err := json.Marshal(args)
if err != nil {
return err
}
req, err := bce.NewRequest("PUT", c.GetURL("v2/volume"+"/"+args.VolumeId, params), bytes.NewBuffer(postContent))
if err != nil {
return err
}
_, err = c.SendRequest(req, nil)
if err != nil {
return err
}
return nil
}
// DeleteCDS delete a cds
func (c *Client) DeleteCDS(volumeID string) error {
if volumeID == "" {
return fmt.Errorf("DeleteCDS need a volumeId")
}
params := map[string]string{
"clientToken": c.GenerateClientToken(),
}
req, err := bce.NewRequest("DELETE", c.GetURL("v2/volume"+"/"+volumeID, params), nil)
if err != nil {
return err
}
_, err = c.SendRequest(req, nil)
if err != nil {
return err
}
return nil
}
// RollbackVolume rollback a volume
// TODO
func (c *Client) RollbackVolume() {
}
// PurchaseReservedVolume purchaseReserved a volume
// TODO
func (c *Client) PurchaseReservedVolume() {
}