forked from shapeblue/cloudstack-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvolumes.go
More file actions
155 lines (136 loc) · 4.37 KB
/
volumes.go
File metadata and controls
155 lines (136 loc) · 4.37 KB
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
package cloud
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/apache/cloudstack-go/v2/cloudstack"
"k8s.io/klog/v2"
"github.com/cloudstack/cloudstack-csi-driver/pkg/util"
)
func (c *client) listVolumes(p *cloudstack.ListVolumesParams) (*Volume, error) {
l, err := c.Volume.ListVolumes(p)
if err != nil {
return nil, err
}
if l.Count == 0 {
return nil, ErrNotFound
}
if l.Count > 1 {
return nil, ErrTooManyResults
}
vol := l.Volumes[0]
v := Volume{
ID: vol.Id,
Name: vol.Name,
Size: vol.Size,
DiskOfferingID: vol.Diskofferingid,
ZoneID: vol.Zoneid,
VirtualMachineID: vol.Virtualmachineid,
DeviceID: strconv.FormatInt(vol.Deviceid, 10),
}
return &v, nil
}
func (c *client) GetVolumeByID(ctx context.Context, volumeID string) (*Volume, error) {
logger := klog.FromContext(ctx)
p := c.Volume.NewListVolumesParams()
p.SetId(volumeID)
logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{
"id": volumeID,
})
return c.listVolumes(p)
}
func (c *client) GetVolumeByName(ctx context.Context, name string) (*Volume, error) {
logger := klog.FromContext(ctx)
p := c.Volume.NewListVolumesParams()
p.SetName(name)
logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{
"name": name,
})
return c.listVolumes(p)
}
func (c *client) CreateVolume(ctx context.Context, diskOfferingID, zoneID, name string, sizeInGB int64) (string, error) {
logger := klog.FromContext(ctx)
p := c.Volume.NewCreateVolumeParams()
p.SetDiskofferingid(diskOfferingID)
p.SetZoneid(zoneID)
p.SetName(name)
p.SetSize(sizeInGB)
logger.V(2).Info("CloudStack API call", "command", "CreateVolume", "params", map[string]string{
"diskofferingid": diskOfferingID,
"zoneid": zoneID,
"name": name,
"size": strconv.FormatInt(sizeInGB, 10),
})
vol, err := c.Volume.CreateVolume(p)
if err != nil {
return "", err
}
return vol.Id, nil
}
func (c *client) DeleteVolume(ctx context.Context, id string) error {
logger := klog.FromContext(ctx)
p := c.Volume.NewDeleteVolumeParams(id)
logger.V(2).Info("CloudStack API call", "command", "DeleteVolume", "params", map[string]string{
"id": id,
})
_, err := c.Volume.DeleteVolume(p)
if err != nil && strings.Contains(err.Error(), "4350") {
// CloudStack error InvalidParameterValueException
return ErrNotFound
}
return err
}
func (c *client) AttachVolume(ctx context.Context, volumeID, vmID string) (string, error) {
logger := klog.FromContext(ctx)
p := c.Volume.NewAttachVolumeParams(volumeID, vmID)
logger.V(2).Info("CloudStack API call", "command", "AttachVolume", "params", map[string]string{
"id": volumeID,
"virtualmachineid": vmID,
})
r, err := c.Volume.AttachVolume(p)
if err != nil {
return "", err
}
return strconv.FormatInt(r.Deviceid, 10), nil
}
func (c *client) DetachVolume(ctx context.Context, volumeID string) error {
logger := klog.FromContext(ctx)
p := c.Volume.NewDetachVolumeParams()
p.SetId(volumeID)
logger.V(2).Info("CloudStack API call", "command", "DetachVolume", "params", map[string]string{
"id": volumeID,
})
_, err := c.Volume.DetachVolume(p)
return err
}
// ExpandVolume expands the volume to new size.
func (c *client) ExpandVolume(ctx context.Context, volumeID string, newSizeInGB int64) error {
logger := klog.FromContext(ctx)
volume, _, err := c.Volume.GetVolumeByID(volumeID)
if err != nil {
return fmt.Errorf("failed to retrieve volume '%s': %w", volumeID, err)
}
if volume.State != "Allocated" && volume.State != "Ready" {
return fmt.Errorf("volume '%s' is not in 'Allocated' or 'Ready' state to get resized", volumeID)
}
currentSize := volume.Size
currentSizeInGB := util.RoundUpBytesToGB(currentSize)
volumeName := volume.Name
p := c.Volume.NewResizeVolumeParams(volumeID)
p.SetId(volumeID)
p.SetSize(newSizeInGB)
logger.V(2).Info("CloudStack API call", "command", "ExpandVolume", "params", map[string]string{
"name": volumeName,
"volumeid": volumeID,
"current_size": strconv.FormatInt(currentSizeInGB, 10),
"requested_size": strconv.FormatInt(newSizeInGB, 10),
})
// Execute the API call to resize the volume.
_, err = c.Volume.ResizeVolume(p)
if err != nil {
// Handle the error accordingly
return fmt.Errorf("failed to expand volume '%s': %w", volumeID, err)
}
return nil
}