forked from shapeblue/cloudstack-csi-driver
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfake.go
More file actions
126 lines (102 loc) · 3.05 KB
/
fake.go
File metadata and controls
126 lines (102 loc) · 3.05 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
// Package fake provides a fake implementation of the cloud
// connector interface, to be used in tests.
package fake
import (
"context"
"github.com/hashicorp/go-uuid"
"github.com/cloudstack/cloudstack-csi-driver/pkg/cloud"
"github.com/cloudstack/cloudstack-csi-driver/pkg/util"
)
const zoneID = "a1887604-237c-4212-a9cd-94620b7880fa"
type fakeConnector struct {
node *cloud.VM
volumesByID map[string]cloud.Volume
volumesByName map[string]cloud.Volume
}
// New returns a new fake implementation of the
// CloudStack connector.
func New() cloud.Interface {
volume := cloud.Volume{
ID: "ace9f28b-3081-40c1-8353-4cc3e3014072",
Name: "vol-1",
Size: 10,
DiskOfferingID: "9743fd77-0f5d-4ef9-b2f8-f194235c769c",
ZoneID: zoneID,
VirtualMachineID: "",
DeviceID: "",
}
node := &cloud.VM{
ID: "0d7107a3-94d2-44e7-89b8-8930881309a5",
ZoneID: zoneID,
}
return &fakeConnector{
node: node,
volumesByID: map[string]cloud.Volume{volume.ID: volume},
volumesByName: map[string]cloud.Volume{volume.Name: volume},
}
}
func (f *fakeConnector) GetVMByID(_ context.Context, vmID string) (*cloud.VM, error) {
if vmID == f.node.ID {
return f.node, nil
}
return nil, cloud.ErrNotFound
}
func (f *fakeConnector) GetNodeInfo(_ context.Context, _ string) (*cloud.VM, error) {
return f.node, nil
}
func (f *fakeConnector) ListZonesID(_ context.Context) ([]string, error) {
return []string{zoneID}, nil
}
func (f *fakeConnector) GetVolumeByID(_ context.Context, volumeID string) (*cloud.Volume, error) {
vol, ok := f.volumesByID[volumeID]
if ok {
return &vol, nil
}
return nil, cloud.ErrNotFound
}
func (f *fakeConnector) GetVolumeByName(_ context.Context, name string) (*cloud.Volume, error) {
vol, ok := f.volumesByName[name]
if ok {
return &vol, nil
}
return nil, cloud.ErrNotFound
}
func (f *fakeConnector) CreateVolume(_ context.Context, diskOfferingID, zoneID, name string, sizeInGB int64) (string, error) {
id, _ := uuid.GenerateUUID()
vol := cloud.Volume{
ID: id,
Name: name,
Size: util.GigaBytesToBytes(sizeInGB),
DiskOfferingID: diskOfferingID,
ZoneID: zoneID,
}
f.volumesByID[vol.ID] = vol
f.volumesByName[vol.Name] = vol
return vol.ID, nil
}
func (f *fakeConnector) DeleteVolume(_ context.Context, id string) error {
if vol, ok := f.volumesByID[id]; ok {
name := vol.Name
delete(f.volumesByName, name)
}
delete(f.volumesByID, id)
return nil
}
func (f *fakeConnector) AttachVolume(_ context.Context, _, _ string) (string, error) {
return "1", nil
}
func (f *fakeConnector) DetachVolume(_ context.Context, _ string) error {
return nil
}
func (f *fakeConnector) ExpandVolume(_ context.Context, volumeID string, newSizeInGB int64) error {
if vol, ok := f.volumesByID[volumeID]; ok {
newSizeInBytes := newSizeInGB * 1024 * 1024 * 1024
if newSizeInBytes > vol.Size {
vol.Size = newSizeInBytes
f.volumesByID[volumeID] = vol
f.volumesByName[vol.Name] = vol
}
return nil
}
return cloud.ErrNotFound
}