Skip to content

Commit 18ef184

Browse files
committed
Remove all occurrence of global projectID in cloudstack client because we can set it as default option
1 parent f79a93f commit 18ef184

File tree

4 files changed

+17
-36
lines changed

4 files changed

+17
-36
lines changed

pkg/cloud/cloud.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"errors"
2727

2828
"github.com/apache/cloudstack-go/v2/cloudstack"
29+
"k8s.io/klog/v2"
2930
)
3031

3132
// Interface is the CloudStack client interface.
@@ -70,6 +71,7 @@ type Volume struct {
7071
DeviceID string
7172
}
7273

74+
// Volume represents a CloudStack snapshot.
7375
type Snapshot struct {
7476
ID string
7577
Name string
@@ -99,12 +101,18 @@ var (
99101
// client is the implementation of Interface.
100102
type client struct {
101103
*cloudstack.CloudStackClient
102-
projectID string
103104
}
104105

105106
// New creates a new cloud connector, given its configuration.
106107
func New(config *Config) Interface {
107108
csClient := cloudstack.NewAsyncClient(config.APIURL, config.APIKey, config.SecretKey, config.VerifySSL)
108109

109-
return &client{csClient, config.ProjectID}
110+
// Set the project id to every request.
111+
// This is possible because we just could work in one project with the previous implementation.
112+
if config.ProjectID != "" {
113+
csClient.DefaultOptions(cloudstack.WithProject(config.ProjectID))
114+
klog.Background().V(2).Info("Set projectID to cloud connector", "projectID", config.ProjectID)
115+
}
116+
117+
return &client{csClient}
110118
}

pkg/cloud/snapshots.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ func (c *client) GetSnapshotByID(ctx context.Context, snapshotID string) (*Snaps
3434
if snapshotID != "" {
3535
p.SetId(snapshotID)
3636
}
37-
if c.projectID != "" {
38-
p.SetProjectid(c.projectID)
39-
}
4037
logger.V(2).Info("CloudStack API call", "command", "ListSnapshots", "params", map[string]string{
41-
"id": snapshotID,
42-
"projectid": c.projectID,
38+
"id": snapshotID,
4339
})
4440
l, err := c.Snapshot.ListSnapshots(p)
4541
if err != nil {
@@ -112,12 +108,8 @@ func (c *client) GetSnapshotByName(ctx context.Context, name string) (*Snapshot,
112108
}
113109
p := c.Snapshot.NewListSnapshotsParams()
114110
p.SetName(name)
115-
if c.projectID != "" {
116-
p.SetProjectid(c.projectID)
117-
}
118111
logger.V(2).Info("CloudStack API call", "command", "ListSnapshots", "params", map[string]string{
119-
"name": name,
120-
"projectid": c.projectID,
112+
"name": name,
121113
})
122114
l, err := c.Snapshot.ListSnapshots(p)
123115
if err != nil {
@@ -152,13 +144,9 @@ func (c *client) ListSnapshots(ctx context.Context, volumeID, snapshotID string)
152144
if volumeID != "" {
153145
p.SetVolumeid(volumeID)
154146
}
155-
if c.projectID != "" {
156-
p.SetProjectid(c.projectID)
157-
}
158147
logger.V(2).Info("CloudStack API call", "command", "ListSnapshots", "params", map[string]string{
159-
"id": snapshotID,
160-
"volumeid": volumeID,
161-
"projectid": c.projectID,
148+
"id": snapshotID,
149+
"volumeid": volumeID,
162150
})
163151
l, err := c.Snapshot.ListSnapshots(p)
164152
if err != nil {

pkg/cloud/vms.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ import (
2929
func (c *client) GetVMByID(ctx context.Context, vmID string) (*VM, error) {
3030
logger := klog.FromContext(ctx)
3131
logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{
32-
"id": vmID,
33-
"projectID": c.projectID,
32+
"id": vmID,
3433
})
3534

3635
return c.getVMByParam(ctx, func(p *cloudstack.ListVirtualMachinesParams) {
@@ -41,8 +40,7 @@ func (c *client) GetVMByID(ctx context.Context, vmID string) (*VM, error) {
4140
func (c *client) getVMByName(ctx context.Context, name string) (*VM, error) {
4241
logger := klog.FromContext(ctx)
4342
logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{
44-
"name": name,
45-
"projectID": c.projectID,
43+
"name": name,
4644
})
4745

4846
return c.getVMByParam(ctx, func(p *cloudstack.ListVirtualMachinesParams) {
@@ -53,10 +51,6 @@ func (c *client) getVMByName(ctx context.Context, name string) (*VM, error) {
5351
func (c *client) getVMByParam(ctx context.Context, setParams func(p *cloudstack.ListVirtualMachinesParams)) (*VM, error) {
5452
p := c.VirtualMachine.NewListVirtualMachinesParams()
5553

56-
if c.projectID != "" {
57-
p.SetProjectid(c.projectID)
58-
}
59-
6054
// set params for virtual machine list
6155
setParams(p)
6256

pkg/cloud/volumes.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ func (c *client) GetVolumeByID(ctx context.Context, volumeID string) (*Volume, e
6262
logger := klog.FromContext(ctx)
6363
p := c.Volume.NewListVolumesParams()
6464
p.SetId(volumeID)
65-
if c.projectID != "" {
66-
p.SetProjectid(c.projectID)
67-
}
6865
logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{
69-
"id": volumeID,
70-
"projectid": c.projectID,
66+
"id": volumeID,
7167
})
7268

7369
return c.listVolumes(p)
@@ -91,15 +87,11 @@ func (c *client) CreateVolume(ctx context.Context, diskOfferingID, zoneID, name
9187
p.SetZoneid(zoneID)
9288
p.SetName(name)
9389
p.SetSize(sizeInGB)
94-
if c.projectID != "" {
95-
p.SetProjectid(c.projectID)
96-
}
9790
logger.V(2).Info("CloudStack API call", "command", "CreateVolume", "params", map[string]string{
9891
"diskofferingid": diskOfferingID,
9992
"zoneid": zoneID,
10093
"name": name,
10194
"size": strconv.FormatInt(sizeInGB, 10),
102-
"projectid": c.projectID,
10395
})
10496
vol, err := c.Volume.CreateVolume(p)
10597
if err != nil {
@@ -199,7 +191,6 @@ func (c *client) CreateVolumeFromSnapshot(ctx context.Context, zoneID, name, pro
199191
"name": name,
200192
"size": strconv.FormatInt(sizeInGB, 10),
201193
"snapshotid": snapshotID,
202-
"projectid": projectID,
203194
"zoneid": zoneID,
204195
})
205196
// Execute the API call to create volume from snapshot

0 commit comments

Comments
 (0)