Skip to content

Commit 133f7cc

Browse files
committed
Add support for project
1 parent d597e80 commit 133f7cc

File tree

7 files changed

+34
-20
lines changed

7 files changed

+34
-20
lines changed

pkg/cloud/affinity_groups.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cloud
1818

1919
import (
20+
"github.com/apache/cloudstack-go/v2/cloudstack"
2021
"github.com/pkg/errors"
2122
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
2223
)
@@ -42,7 +43,7 @@ type AffinityGroupIface interface {
4243

4344
func (c *client) FetchAffinityGroup(group *AffinityGroup) (reterr error) {
4445
if group.ID != "" {
45-
affinityGroup, count, err := c.cs.AffinityGroup.GetAffinityGroupByID(group.ID)
46+
affinityGroup, count, err := c.cs.AffinityGroup.GetAffinityGroupByID(group.ID, cloudstack.WithProject(c.config.ProjectID))
4647
if err != nil {
4748
// handle via multierr
4849
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
@@ -57,7 +58,7 @@ func (c *client) FetchAffinityGroup(group *AffinityGroup) (reterr error) {
5758
}
5859
}
5960
if group.Name != "" {
60-
affinityGroup, count, err := c.cs.AffinityGroup.GetAffinityGroupByName(group.Name)
61+
affinityGroup, count, err := c.cs.AffinityGroup.GetAffinityGroupByName(group.Name, cloudstack.WithProject(c.config.ProjectID))
6162
if err != nil {
6263
// handle via multierr
6364
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
@@ -78,6 +79,7 @@ func (c *client) GetOrCreateAffinityGroup(group *AffinityGroup) (retErr error) {
7879
if err := c.FetchAffinityGroup(group); err != nil { // Group not found?
7980
p := c.cs.AffinityGroup.NewCreateAffinityGroupParams(group.Name, group.Type)
8081
p.SetName(group.Name)
82+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
8183
resp, err := c.cs.AffinityGroup.CreateAffinityGroup(p)
8284
if err != nil {
8385
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
@@ -92,6 +94,7 @@ func (c *client) DeleteAffinityGroup(group *AffinityGroup) (retErr error) {
9294
p := c.cs.AffinityGroup.NewDeleteAffinityGroupParams()
9395
setIfNotEmpty(group.ID, p.SetId)
9496
setIfNotEmpty(group.Name, p.SetName)
97+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
9598
_, retErr = c.cs.AffinityGroup.DeleteAffinityGroup(p)
9699
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(retErr)
97100
return retErr
@@ -101,7 +104,7 @@ type affinityGroups []AffinityGroup
101104

102105
func (c *client) getCurrentAffinityGroups(csMachine *infrav1.CloudStackMachine) (affinityGroups, error) {
103106
// Start by fetching VM details which includes an array of currently associated affinity groups.
104-
if virtM, count, err := c.cs.VirtualMachine.GetVirtualMachineByID(*csMachine.Spec.InstanceID); err != nil {
107+
if virtM, count, err := c.cs.VirtualMachine.GetVirtualMachineByID(*csMachine.Spec.InstanceID, cloudstack.WithProject(c.config.ProjectID)); err != nil {
105108
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
106109
return nil, err
107110
} else if count > 1 {

pkg/cloud/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Config struct {
5353
APIKey string `yaml:"api-key"`
5454
SecretKey string `yaml:"secret-key"`
5555
VerifySSL string `yaml:"verify-ssl"`
56+
ProjectID string `yaml:"project-id"`
5657
}
5758

5859
type client struct {

pkg/cloud/instance.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func setMachineDataFromVMMetrics(vmResponse *cloudstack.VirtualMachinesMetric, c
5858
func (c *client) ResolveVMInstanceDetails(csMachine *infrav1.CloudStackMachine) error {
5959
// Attempt to fetch by ID.
6060
if csMachine.Spec.InstanceID != nil {
61-
vmResp, count, err := c.cs.VirtualMachine.GetVirtualMachinesMetricByID(*csMachine.Spec.InstanceID)
61+
vmResp, count, err := c.cs.VirtualMachine.GetVirtualMachinesMetricByID(*csMachine.Spec.InstanceID, cloudstack.WithProject(c.config.ProjectID))
6262
if err != nil && !strings.Contains(strings.ToLower(err.Error()), "no match found") {
6363
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
6464
return err
@@ -72,7 +72,7 @@ func (c *client) ResolveVMInstanceDetails(csMachine *infrav1.CloudStackMachine)
7272

7373
// Attempt fetch by name.
7474
if csMachine.Name != "" {
75-
vmResp, count, err := c.cs.VirtualMachine.GetVirtualMachinesMetricByName(csMachine.Name) // add opts usage
75+
vmResp, count, err := c.cs.VirtualMachine.GetVirtualMachinesMetricByName(csMachine.Name, cloudstack.WithProject(c.config.ProjectID))
7676
if err != nil && !strings.Contains(strings.ToLower(err.Error()), "no match") {
7777
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
7878
return err
@@ -88,7 +88,7 @@ func (c *client) ResolveVMInstanceDetails(csMachine *infrav1.CloudStackMachine)
8888

8989
func (c *client) ResolveServiceOffering(csMachine *infrav1.CloudStackMachine, zoneID string) (offering cloudstack.ServiceOffering, retErr error) {
9090
if len(csMachine.Spec.Offering.ID) > 0 {
91-
csOffering, count, err := c.cs.ServiceOffering.GetServiceOfferingByID(csMachine.Spec.Offering.ID)
91+
csOffering, count, err := c.cs.ServiceOffering.GetServiceOfferingByID(csMachine.Spec.Offering.ID, cloudstack.WithProject(c.config.ProjectID))
9292
if err != nil {
9393
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
9494
return cloudstack.ServiceOffering{}, multierror.Append(retErr, errors.Wrapf(
@@ -104,7 +104,7 @@ func (c *client) ResolveServiceOffering(csMachine *infrav1.CloudStackMachine, zo
104104
}
105105
return *csOffering, nil
106106
}
107-
csOffering, count, err := c.cs.ServiceOffering.GetServiceOfferingByName(csMachine.Spec.Offering.Name, cloudstack.WithZone(zoneID))
107+
csOffering, count, err := c.cs.ServiceOffering.GetServiceOfferingByName(csMachine.Spec.Offering.Name, cloudstack.WithZone(zoneID), cloudstack.WithProject(c.config.ProjectID))
108108
if err != nil {
109109
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
110110
return cloudstack.ServiceOffering{}, multierror.Append(retErr, errors.Wrapf(
@@ -122,7 +122,7 @@ func (c *client) ResolveTemplate(
122122
zoneID string,
123123
) (templateID string, retErr error) {
124124
if len(csMachine.Spec.Template.ID) > 0 {
125-
csTemplate, count, err := c.cs.Template.GetTemplateByID(csMachine.Spec.Template.ID, "executable")
125+
csTemplate, count, err := c.cs.Template.GetTemplateByID(csMachine.Spec.Template.ID, "executable", cloudstack.WithProject(c.config.ProjectID))
126126
if err != nil {
127127
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
128128
return "", multierror.Append(retErr, errors.Wrapf(
@@ -138,7 +138,7 @@ func (c *client) ResolveTemplate(
138138
}
139139
return csMachine.Spec.Template.ID, nil
140140
}
141-
templateID, count, err := c.cs.Template.GetTemplateID(csMachine.Spec.Template.Name, "executable", zoneID)
141+
templateID, count, err := c.cs.Template.GetTemplateID(csMachine.Spec.Template.Name, "executable", zoneID, cloudstack.WithProject(c.config.ProjectID))
142142
if err != nil {
143143
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
144144
return "", multierror.Append(retErr, errors.Wrapf(
@@ -156,7 +156,7 @@ func (c *client) ResolveTemplate(
156156
func (c *client) ResolveDiskOffering(csMachine *infrav1.CloudStackMachine, zoneID string) (diskOfferingID string, retErr error) {
157157
diskOfferingID = csMachine.Spec.DiskOffering.ID
158158
if len(csMachine.Spec.DiskOffering.Name) > 0 {
159-
diskID, count, err := c.cs.DiskOffering.GetDiskOfferingID(csMachine.Spec.DiskOffering.Name, cloudstack.WithZone(zoneID))
159+
diskID, count, err := c.cs.DiskOffering.GetDiskOfferingID(csMachine.Spec.DiskOffering.Name, cloudstack.WithZone(zoneID), cloudstack.WithProject(c.config.ProjectID))
160160
if err != nil {
161161
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
162162
return "", multierror.Append(retErr, errors.Wrapf(
@@ -183,7 +183,7 @@ func (c *client) ResolveDiskOffering(csMachine *infrav1.CloudStackMachine, zoneI
183183
}
184184

185185
func verifyDiskoffering(csMachine *infrav1.CloudStackMachine, c *client, diskOfferingID string, retErr error) (string, error) {
186-
csDiskOffering, count, err := c.cs.DiskOffering.GetDiskOfferingByID(diskOfferingID)
186+
csDiskOffering, count, err := c.cs.DiskOffering.GetDiskOfferingByID(diskOfferingID, cloudstack.WithProject(c.config.ProjectID))
187187
if err != nil {
188188
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
189189
return "", multierror.Append(retErr, errors.Wrapf(
@@ -300,6 +300,7 @@ func (c *client) DeployVM(
300300
setIfNotEmpty(csMachine.Name, p.SetName)
301301
setIfNotEmpty(capiMachine.Name, p.SetDisplayname)
302302
setIfNotEmpty(diskOfferingID, p.SetDiskofferingid)
303+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
303304
setIntIfPositive(csMachine.Spec.DiskOffering.CustomSize, p.SetSize)
304305

305306
setIfNotEmpty(csMachine.Spec.SSHKey, p.SetKeypair)
@@ -330,7 +331,7 @@ func (c *client) DeployVM(
330331
// CloudStack may have created the VM even though it reported an error. We attempt to
331332
// retrieve the VM so we can populate the CloudStackMachine for the user to manually
332333
// clean up.
333-
vm, findErr := findVirtualMachine(c.cs.VirtualMachine, templateID, fd, csMachine)
334+
vm, findErr := findVirtualMachine(c.cs.VirtualMachine, templateID, fd, csMachine, c.config.ProjectID)
334335
if findErr != nil {
335336
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(findErr)
336337
return fmt.Errorf("%v; find virtual machine: %v", err, findErr)
@@ -394,13 +395,14 @@ func findVirtualMachine(
394395
client cloudstack.VirtualMachineServiceIface,
395396
templateID string,
396397
failureDomain *infrav1.CloudStackFailureDomain,
397-
machine *infrav1.CloudStackMachine,
398+
machine *infrav1.CloudStackMachine, projectID string,
398399
) (*cloudstack.VirtualMachine, error) {
399400
params := client.NewListVirtualMachinesParams()
400401
params.SetTemplateid(templateID)
401402
params.SetZoneid(failureDomain.Spec.Zone.ID)
402403
params.SetNetworkid(failureDomain.Spec.Zone.Network.ID)
403404
params.SetName(machine.Name)
405+
setIfNotEmpty(projectID, params.SetProjectid)
404406

405407
response, err := client.ListVirtualMachines(params)
406408
if err != nil {
@@ -453,6 +455,7 @@ func (c *client) listVMInstanceDatadiskVolumeIDs(instanceID string) ([]string, e
453455
p.SetVirtualmachineid(instanceID)
454456
// VM root volumes are destroyed automatically, no need to explicitly include
455457
p.SetType("DATADISK")
458+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
456459

457460
listVOLResp, err := c.csAsync.Volume.ListVolumes(p)
458461
if err != nil {

pkg/cloud/isolated_network.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (c *client) AssociatePublicIPAddress(
7676
p := c.cs.Address.NewAssociateIpAddressParams()
7777
p.SetIpaddress(isoNet.Spec.ControlPlaneEndpoint.Host)
7878
p.SetNetworkid(isoNet.Spec.ID)
79+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
7980
if _, err := c.cs.Address.AssociateIpAddress(p); err != nil {
8081
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
8182
return errors.Wrapf(err,
@@ -102,6 +103,7 @@ func (c *client) CreateIsolatedNetwork(fd *infrav1.CloudStackFailureDomain, isoN
102103
// Do isolated network creation.
103104
p := c.cs.Network.NewCreateNetworkParams(isoNet.Spec.Name, offeringID, fd.Spec.Zone.ID)
104105
p.SetDisplaytext(isoNet.Spec.Name)
106+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
105107
resp, err := c.cs.Network.CreateNetwork(p)
106108
if err != nil {
107109
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
@@ -148,6 +150,7 @@ func (c *client) GetPublicIP(
148150
p.SetAllocatedonly(false)
149151
p.SetZoneid(fd.Spec.Zone.ID)
150152
setIfNotEmpty(ip, p.SetIpaddress)
153+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
151154
publicAddresses, err := c.cs.Address.ListPublicIpAddresses(p)
152155
if err != nil {
153156
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
@@ -168,7 +171,7 @@ func (c *client) GetPublicIP(
168171

169172
// GetIsolatedNetwork gets an isolated network in the relevant Zone.
170173
func (c *client) GetIsolatedNetwork(isoNet *infrav1.CloudStackIsolatedNetwork) (retErr error) {
171-
netDetails, count, err := c.cs.Network.GetNetworkByName(isoNet.Spec.Name)
174+
netDetails, count, err := c.cs.Network.GetNetworkByName(isoNet.Spec.Name, cloudstack.WithProject(c.config.ProjectID))
172175
if err != nil {
173176
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
174177
retErr = multierror.Append(retErr, errors.Wrapf(err, "could not get Network ID from %s", isoNet.Spec.Name))
@@ -180,7 +183,7 @@ func (c *client) GetIsolatedNetwork(isoNet *infrav1.CloudStackIsolatedNetwork) (
180183
return nil
181184
}
182185

183-
netDetails, count, err = c.cs.Network.GetNetworkByID(isoNet.Spec.ID)
186+
netDetails, count, err = c.cs.Network.GetNetworkByID(isoNet.Spec.ID, cloudstack.WithProject(c.config.ProjectID))
184187
if err != nil {
185188
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
186189
return multierror.Append(retErr, errors.Wrapf(err, "could not get Network by ID %s", isoNet.Spec.ID))
@@ -199,6 +202,7 @@ func (c *client) ResolveLoadBalancerRuleDetails(
199202
) error {
200203
p := c.cs.LoadBalancer.NewListLoadBalancerRulesParams()
201204
p.SetPublicipid(isoNet.Status.PublicIPID)
205+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
202206
loadBalancerRules, err := c.cs.LoadBalancer.ListLoadBalancerRules(p)
203207
if err != nil {
204208
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
@@ -370,7 +374,7 @@ func (c *client) DeleteNetworkIfNotInUse(csCluster *infrav1.CloudStackCluster, n
370374
func (c *client) DisassociatePublicIPAddressIfNotInUse(isoNet *infrav1.CloudStackIsolatedNetwork) (retError error) {
371375
if tagsAllowDisposal, err := c.DoClusterTagsAllowDisposal(ResourceTypeIPAddress, isoNet.Status.PublicIPID); err != nil {
372376
return err
373-
} else if publicIP, _, err := c.cs.Address.GetPublicIpAddressByID(isoNet.Status.PublicIPID); err != nil {
377+
} else if publicIP, _, err := c.cs.Address.GetPublicIpAddressByID(isoNet.Status.PublicIPID, cloudstack.WithProject(c.config.ProjectID)); err != nil {
374378
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
375379
return err
376380
} else if publicIP == nil || publicIP.Issourcenat { // Can't disassociate an address if it's the source NAT address.

pkg/cloud/network.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cloud
1818

1919
import (
20+
"github.com/apache/cloudstack-go/v2/cloudstack"
2021
"github.com/hashicorp/go-multierror"
2122
"github.com/pkg/errors"
2223
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
@@ -51,7 +52,7 @@ func (c *client) ResolveNetwork(net *infrav1.Network) (retErr error) {
5152
// TODO rebuild this to consider cases with networks in many zones.
5253
// Use ListNetworks instead.
5354
netName := net.Name
54-
netDetails, count, err := c.cs.Network.GetNetworkByName(netName)
55+
netDetails, count, err := c.cs.Network.GetNetworkByName(netName, cloudstack.WithProject(c.config.ProjectID))
5556
if err != nil {
5657
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
5758
retErr = multierror.Append(retErr, errors.Wrapf(err, "could not get Network ID from %s", netName))
@@ -65,7 +66,7 @@ func (c *client) ResolveNetwork(net *infrav1.Network) (retErr error) {
6566
}
6667

6768
// Now get network details.
68-
netDetails, count, err = c.cs.Network.GetNetworkByID(net.ID)
69+
netDetails, count, err = c.cs.Network.GetNetworkByID(net.ID, cloudstack.WithProject(c.config.ProjectID))
6970
if err != nil {
7071
return multierror.Append(retErr, errors.Wrapf(err, "could not get Network by ID %s", net.ID))
7172
} else if count != 1 {

pkg/cloud/tags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func (c *client) GetTags(resourceType ResourceType, resourceID string) (map[stri
129129
p.SetResourceid(resourceID)
130130
p.SetResourcetype(string(resourceType))
131131
p.SetListall(true)
132+
setIfNotEmpty(c.config.ProjectID, p.SetProjectid)
132133
listTagResponse, err := c.cs.Resourcetags.ListTags(p)
133134
if err != nil {
134135
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)

pkg/cloud/zone.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package cloud
1818

1919
import (
20+
"github.com/apache/cloudstack-go/v2/cloudstack"
2021
"github.com/hashicorp/go-multierror"
2122
"github.com/pkg/errors"
2223
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
@@ -55,7 +56,7 @@ func (c *client) ResolveZone(zSpec *infrav1.CloudStackZoneSpec) (retErr error) {
5556
// ResolveNetworkForZone fetches details on Zone's specified network.
5657
func (c *client) ResolveNetworkForZone(zSpec *infrav1.CloudStackZoneSpec) (retErr error) {
5758
netName := zSpec.Network.Name
58-
netDetails, count, err := c.cs.Network.GetNetworkByName(netName)
59+
netDetails, count, err := c.cs.Network.GetNetworkByName(netName, cloudstack.WithProject(c.config.ProjectID))
5960
if err != nil {
6061
c.customMetrics.EvaluateErrorAndIncrementAcsReconciliationErrorCounter(err)
6162
retErr = multierror.Append(retErr, errors.Wrapf(err, "could not get Network ID from %v", netName))
@@ -69,7 +70,7 @@ func (c *client) ResolveNetworkForZone(zSpec *infrav1.CloudStackZoneSpec) (retEr
6970
}
7071

7172
// Now get network details.
72-
netDetails, count, err = c.cs.Network.GetNetworkByID(zSpec.Network.ID)
73+
netDetails, count, err = c.cs.Network.GetNetworkByID(zSpec.Network.ID, cloudstack.WithProject(c.config.ProjectID))
7374
if err != nil {
7475
return multierror.Append(retErr, errors.Wrapf(err, "could not get Network by ID %s", zSpec.Network.ID))
7576
} else if count != 1 {

0 commit comments

Comments
 (0)