Skip to content

Commit bfa4713

Browse files
committed
Refactor cloud client to use project id default with other cloudstack methods
1 parent bfbf5f6 commit bfa4713

File tree

4 files changed

+54
-87
lines changed

4 files changed

+54
-87
lines changed

pkg/cloud/cloud.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ var (
101101
// client is the implementation of Interface.
102102
type client struct {
103103
*cloudstack.CloudStackClient
104+
projectID string // Used by some specific cloudstack api calls
104105
}
105106

106107
// New creates a new cloud connector, given its configuration.
@@ -114,5 +115,5 @@ func New(config *Config) Interface {
114115
klog.Background().V(2).Info("Set projectID to cloud connector", "projectID", config.ProjectID)
115116
}
116117

117-
return &client{csClient}
118+
return &client{csClient, config.ProjectID}
118119
}

pkg/cloud/snapshots.go

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,23 @@ import (
3030

3131
func (c *client) GetSnapshotByID(ctx context.Context, snapshotID string) (*Snapshot, error) {
3232
logger := klog.FromContext(ctx)
33-
p := c.Snapshot.NewListSnapshotsParams()
34-
p.SetId(snapshotID)
35-
logger.V(2).Info("CloudStack API call", "command", "ListSnapshots", "params", map[string]string{
33+
logger.V(2).Info("CloudStack API call", "command", "GetSnapshotByID", "params", map[string]string{
3634
"id": snapshotID,
3735
})
38-
l, err := c.Snapshot.ListSnapshots(p)
36+
37+
snapshot, _, err := c.Snapshot.GetSnapshotByID(snapshotID)
3938
if err != nil {
4039
return nil, err
4140
}
42-
if l.Count == 0 {
43-
return nil, ErrNotFound
44-
}
45-
if l.Count > 1 {
46-
return nil, ErrTooManyResults
47-
}
48-
snapshot := l.Snapshots[0]
49-
s := Snapshot{
41+
42+
return &Snapshot{
5043
ID: snapshot.Id,
5144
Name: snapshot.Name,
5245
DomainID: snapshot.Domainid,
5346
ProjectID: snapshot.Projectid,
5447
ZoneID: snapshot.Zoneid,
5548
VolumeID: snapshot.Volumeid,
56-
}
57-
58-
return &s, nil
49+
}, nil
5950
}
6051

6152
func (c *client) CreateSnapshot(ctx context.Context, volumeID, name string) (*Snapshot, error) {
@@ -72,7 +63,7 @@ func (c *client) CreateSnapshot(ctx context.Context, volumeID, name string) (*Sn
7263
return nil, status.Errorf(codes.Internal, "Error %v", err)
7364
}
7465

75-
snap := Snapshot{
66+
return &Snapshot{
7667
ID: snapshot.Id,
7768
Name: snapshot.Name,
7869
Size: snapshot.Virtualsize,
@@ -81,9 +72,7 @@ func (c *client) CreateSnapshot(ctx context.Context, volumeID, name string) (*Sn
8172
ZoneID: snapshot.Zoneid,
8273
VolumeID: snapshot.Volumeid,
8374
CreatedAt: snapshot.Created,
84-
}
85-
86-
return &snap, nil
75+
}, nil
8776
}
8877

8978
func (c *client) DeleteSnapshot(_ context.Context, snapshotID string) error {
@@ -99,39 +88,28 @@ func (c *client) DeleteSnapshot(_ context.Context, snapshotID string) error {
9988

10089
func (c *client) GetSnapshotByName(ctx context.Context, name string) (*Snapshot, error) {
10190
logger := klog.FromContext(ctx)
102-
p := c.Snapshot.NewListSnapshotsParams()
103-
p.SetName(name)
104-
logger.V(2).Info("CloudStack API call", "command", "ListSnapshots", "params", map[string]string{
91+
logger.V(2).Info("CloudStack API call", "command", "GetSnapshotByName", "params", map[string]string{
10592
"name": name,
10693
})
107-
l, err := c.Snapshot.ListSnapshots(p)
94+
snapshot, _, err := c.Snapshot.GetSnapshotByName(name)
10895
if err != nil {
10996
return nil, err
11097
}
111-
if l.Count == 0 {
112-
return nil, ErrNotFound
113-
}
114-
if l.Count > 1 {
115-
return nil, ErrTooManyResults
116-
}
117-
snapshot := l.Snapshots[0]
118-
s := Snapshot{
98+
99+
return &Snapshot{
119100
ID: snapshot.Id,
120101
Name: snapshot.Name,
121102
DomainID: snapshot.Domainid,
122103
ProjectID: snapshot.Projectid,
123104
ZoneID: snapshot.Zoneid,
124105
VolumeID: snapshot.Volumeid,
125106
CreatedAt: snapshot.Created,
126-
}
127-
128-
return &s, nil
107+
}, nil
129108
}
130109

131110
func (c *client) ListSnapshots(ctx context.Context, volumeID, snapshotID string) ([]*Snapshot, error) {
132111
logger := klog.FromContext(ctx)
133112
p := c.Snapshot.NewListSnapshotsParams()
134-
135113
// snapshotID is optional: csi.ListSnapshotsRequest
136114
if snapshotID != "" {
137115
p.SetId(snapshotID)
@@ -141,6 +119,11 @@ func (c *client) ListSnapshots(ctx context.Context, volumeID, snapshotID string)
141119
p.SetVolumeid(volumeID)
142120
}
143121

122+
// There is no list function that uses the client default project id
123+
if c.projectID != "" {
124+
p.SetProjectid(c.projectID)
125+
}
126+
144127
logger.V(2).Info("CloudStack API call", "command", "ListSnapshots", "params", map[string]string{
145128
"id": snapshotID,
146129
"volumeid": volumeID,

pkg/cloud/vms.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,36 @@ package cloud
2222
import (
2323
"context"
2424

25-
"github.com/apache/cloudstack-go/v2/cloudstack"
2625
"k8s.io/klog/v2"
2726
)
2827

2928
func (c *client) GetVMByID(ctx context.Context, vmID string) (*VM, error) {
3029
logger := klog.FromContext(ctx)
31-
logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{
30+
logger.V(2).Info("CloudStack API call", "command", "GetVirtualMachineByID", "params", map[string]string{
3231
"id": vmID,
3332
})
3433

35-
return c.getVMByParam(ctx, func(p *cloudstack.ListVirtualMachinesParams) {
36-
p.SetId(vmID)
37-
})
34+
vm, _, err := c.VirtualMachine.GetVirtualMachineByID(vmID)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
return &VM{
40+
ID: vm.Id,
41+
ZoneID: vm.Zoneid,
42+
}, nil
3843
}
3944

4045
func (c *client) getVMByName(ctx context.Context, name string) (*VM, error) {
4146
logger := klog.FromContext(ctx)
42-
logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{
47+
logger.V(2).Info("CloudStack API call", "command", "GetVirtualMachineByName", "params", map[string]string{
4348
"name": name,
4449
})
4550

46-
return c.getVMByParam(ctx, func(p *cloudstack.ListVirtualMachinesParams) {
47-
p.SetName(name)
48-
})
49-
}
50-
51-
func (c *client) getVMByParam(ctx context.Context, setParams func(p *cloudstack.ListVirtualMachinesParams)) (*VM, error) {
52-
p := c.VirtualMachine.NewListVirtualMachinesParams()
53-
54-
// set params for virtual machine list
55-
setParams(p)
56-
57-
l, err := c.VirtualMachine.ListVirtualMachines(p)
51+
vm, _, err := c.VirtualMachine.GetVirtualMachineByName(name)
5852
if err != nil {
5953
return nil, err
6054
}
61-
if l.Count == 0 {
62-
return nil, ErrNotFound
63-
}
64-
if l.Count > 1 {
65-
return nil, ErrTooManyResults
66-
}
67-
vm := l.VirtualMachines[0]
68-
klog.FromContext(ctx).V(2).Info("Returning VM", "vmID", vm.Id, "zoneID", vm.Zoneid)
6955

7056
return &VM{
7157
ID: vm.Id,

pkg/cloud/volumes.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,8 @@ import (
3131
"github.com/cloudstack/cloudstack-csi-driver/pkg/util"
3232
)
3333

34-
func (c *client) listVolumes(p *cloudstack.ListVolumesParams) (*Volume, error) {
35-
l, err := c.Volume.ListVolumes(p)
36-
if err != nil {
37-
return nil, err
38-
}
39-
if l.Count == 0 {
40-
return nil, ErrNotFound
41-
}
42-
if l.Count > 1 {
43-
return nil, ErrTooManyResults
44-
}
45-
vol := l.Volumes[0]
46-
v := Volume{
34+
func mapVolume(vol *cloudstack.Volume) *Volume {
35+
return &Volume{
4736
ID: vol.Id,
4837
Name: vol.Name,
4938
Size: vol.Size,
@@ -54,30 +43,36 @@ func (c *client) listVolumes(p *cloudstack.ListVolumesParams) (*Volume, error) {
5443
VirtualMachineID: vol.Virtualmachineid,
5544
DeviceID: strconv.FormatInt(vol.Deviceid, 10),
5645
}
57-
58-
return &v, nil
5946
}
6047

6148
func (c *client) GetVolumeByID(ctx context.Context, volumeID string) (*Volume, error) {
6249
logger := klog.FromContext(ctx)
63-
p := c.Volume.NewListVolumesParams()
64-
p.SetId(volumeID)
65-
logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{
50+
logger.V(2).Info("CloudStack API call", "command", "GetVolumeByID", "params", map[string]string{
6651
"id": volumeID,
6752
})
6853

69-
return c.listVolumes(p)
54+
volume, _, err := c.Volume.GetVolumeByID(volumeID)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
return mapVolume(volume), nil
7060
}
7161

7262
func (c *client) GetVolumeByName(ctx context.Context, name string) (*Volume, error) {
7363
logger := klog.FromContext(ctx)
7464
p := c.Volume.NewListVolumesParams()
7565
p.SetName(name)
76-
logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{
66+
logger.V(2).Info("CloudStack API call", "command", "GetVolumeByName", "params", map[string]string{
7767
"name": name,
7868
})
7969

80-
return c.listVolumes(p)
70+
volume, _, err := c.Volume.GetVolumeByName(name)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
return mapVolume(volume), nil
8176
}
8277

8378
func (c *client) CreateVolume(ctx context.Context, diskOfferingID, zoneID, name string, sizeInGB int64) (string, error) {
@@ -87,6 +82,10 @@ func (c *client) CreateVolume(ctx context.Context, diskOfferingID, zoneID, name
8782
p.SetZoneid(zoneID)
8883
p.SetName(name)
8984
p.SetSize(sizeInGB)
85+
// There is no create function that uses the client default project id
86+
if c.projectID != "" {
87+
p.SetProjectid(c.projectID)
88+
}
9089
logger.V(2).Info("CloudStack API call", "command", "CreateVolume", "params", map[string]string{
9190
"diskofferingid": diskOfferingID,
9291
"zoneid": zoneID,
@@ -200,7 +199,7 @@ func (c *client) CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, pro
200199
return nil, fmt.Errorf("failed to create volume from snapshot '%s': %w", snapshotID, err)
201200
}
202201

203-
v := Volume{
202+
return &Volume{
204203
ID: vol.Id,
205204
Name: vol.Name,
206205
Size: vol.Size,
@@ -210,7 +209,5 @@ func (c *client) CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, pro
210209
ZoneID: vol.Zoneid,
211210
VirtualMachineID: vol.Virtualmachineid,
212211
DeviceID: strconv.FormatInt(vol.Deviceid, 10),
213-
}
214-
215-
return &v, nil
212+
}, nil
216213
}

0 commit comments

Comments
 (0)