Skip to content

Commit

Permalink
feat: add CloudStack project support
Browse files Browse the repository at this point in the history
  • Loading branch information
hrak committed Aug 20, 2024
1 parent 7e48373 commit 7d7ac73
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ api-url = <CloudStack API URL>
api-key = <CloudStack API Key>
secret-key = <CloudStack API Secret>
ssl-no-verify = <Disable SSL certificate validation: true or false (optional)>
project-id = <CloudStack project ID (optional)>
```

Create a secret named `cloudstack-secret` in namespace `kube-system`:
Expand Down
8 changes: 6 additions & 2 deletions pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ var (
// client is the implementation of Interface.
type client struct {
*cloudstack.CloudStackClient
projectID string
}

// New creates a new cloud connector, given its configuration.
func New(config *Config) Interface {
csClient := cloudstack.NewAsyncClient(config.APIURL, config.APIKey, config.SecretKey, config.VerifySSL)
csClient := &client{
projectID: config.ProjectID,
}
csClient.CloudStackClient = cloudstack.NewAsyncClient(config.APIURL, config.APIKey, config.SecretKey, config.VerifySSL)

return &client{csClient}
return csClient
}
2 changes: 2 additions & 0 deletions pkg/cloud/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Config struct {
APIKey string
SecretKey string
VerifySSL bool
ProjectID string
}

// csConfig wraps the config for the CloudStack cloud provider.
Expand Down Expand Up @@ -42,5 +43,6 @@ func ReadConfig(configFilePath string) (*Config, error) {
APIKey: cfg.Global.APIKey,
SecretKey: cfg.Global.SecretKey,
VerifySSL: !cfg.Global.SSLNoVerify,
ProjectID: cfg.Global.ProjectID,
}, nil
}
6 changes: 6 additions & 0 deletions pkg/cloud/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func (c *client) GetVMByID(ctx context.Context, vmID string) (*VM, error) {
logger := klog.FromContext(ctx)
p := c.VirtualMachine.NewListVirtualMachinesParams()
p.SetId(vmID)
if c.projectID != "" {
p.SetProjectid(c.projectID)
}
logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{
"id": vmID,
})
Expand All @@ -35,6 +38,9 @@ func (c *client) getVMByName(ctx context.Context, name string) (*VM, error) {
logger := klog.FromContext(ctx)
p := c.VirtualMachine.NewListVirtualMachinesParams()
p.SetName(name)
if c.projectID != "" {
p.SetProjectid(c.projectID)
}
logger.V(2).Info("CloudStack API call", "command", "ListVirtualMachines", "params", map[string]string{
"name": name,
})
Expand Down
9 changes: 9 additions & 0 deletions pkg/cloud/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func (c *client) GetVolumeByID(ctx context.Context, volumeID string) (*Volume, e
logger := klog.FromContext(ctx)
p := c.Volume.NewListVolumesParams()
p.SetId(volumeID)
if c.projectID != "" {
p.SetProjectid(c.projectID)
}
logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{
"id": volumeID,
})
Expand All @@ -52,6 +55,9 @@ func (c *client) GetVolumeByName(ctx context.Context, name string) (*Volume, err
logger := klog.FromContext(ctx)
p := c.Volume.NewListVolumesParams()
p.SetName(name)
if c.projectID != "" {
p.SetProjectid(c.projectID)
}
logger.V(2).Info("CloudStack API call", "command", "ListVolumes", "params", map[string]string{
"name": name,
})
Expand All @@ -66,6 +72,9 @@ func (c *client) CreateVolume(ctx context.Context, diskOfferingID, zoneID, name
p.SetZoneid(zoneID)
p.SetName(name)
p.SetSize(sizeInGB)
if c.projectID != "" {
p.SetProjectid(c.projectID)
}
logger.V(2).Info("CloudStack API call", "command", "CreateVolume", "params", map[string]string{
"diskofferingid": diskOfferingID,
"zoneid": zoneID,
Expand Down

0 comments on commit 7d7ac73

Please sign in to comment.