Skip to content

Commit 0b9d7fc

Browse files
committed
feat: update CloudStack limits handling for zero resources and unlimited resources
Implimenting copilot suggestions for d.GetOk
1 parent e3b3d90 commit 0b9d7fc

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

cloudstack/data_source_cloudstack_limits.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ func dataSourceCloudStackLimits() *schema.Resource {
3838
Optional: true,
3939
ValidateFunc: validation.StringInSlice([]string{
4040
"instance", "ip", "volume", "snapshot", "template", "project", "network", "vpc",
41-
"cpu", "memory", "primarystorage", "secondarystorage", "publicip", "eip", "autoscalevmgroup",
41+
"cpu", "memory", "primarystorage", "secondarystorage",
4242
}, false), // false disables case-insensitive matching
4343
Description: "The type of resource to list the limits. Available types are: " +
4444
"instance, ip, volume, snapshot, template, project, network, vpc, cpu, memory, " +
45-
"primarystorage, secondarystorage, publicip, eip, autoscalevmgroup",
45+
"primarystorage, secondarystorage",
4646
},
4747
"account": {
4848
Type: schema.TypeString,

cloudstack/resource_cloudstack_limits.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func resourceCloudStackLimits() *schema.Resource {
7979
"max": {
8080
Type: schema.TypeInt,
8181
Optional: true,
82-
Description: "Maximum resource limit. Use -1 for unlimited resource limit.",
82+
Description: "Maximum resource limit. Use -1 for unlimited resource limit. A value of 0 means zero resources are allowed, though the CloudStack API may return -1 for a limit set to 0.",
8383
},
8484
"projectid": {
8585
Type: schema.TypeString,
@@ -118,7 +118,6 @@ func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) er
118118

119119
account := d.Get("account").(string)
120120
domainid := d.Get("domainid").(string)
121-
max := d.Get("max").(int)
122121
projectid := d.Get("projectid").(string)
123122

124123
// Validate account and domain parameters
@@ -134,8 +133,10 @@ func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) er
134133
if domainid != "" {
135134
p.SetDomainid(domainid)
136135
}
137-
if max != 0 {
138-
p.SetMax(int64(max))
136+
if maxVal, ok := d.GetOk("max"); ok {
137+
maxIntVal := maxVal.(int)
138+
log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
139+
p.SetMax(int64(maxIntVal))
139140
}
140141
if projectid != "" {
141142
p.SetProjectid(projectid)
@@ -226,7 +227,15 @@ func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) erro
226227
// Update the config
227228
for _, limit := range l.ResourceLimits {
228229
if limit.Resourcetype == fmt.Sprintf("%d", resourcetype) {
229-
d.Set("max", limit.Max)
230+
log.Printf("[DEBUG] Retrieved max value from API: %d", limit.Max)
231+
232+
// If the user set max to 0 but the API returned -1, keep it as 0 in the state
233+
if limit.Max == -1 && d.Get("max").(int) == 0 {
234+
log.Printf("[DEBUG] API returned -1 for a limit set to 0, keeping it as 0 in state")
235+
d.Set("max", 0)
236+
} else {
237+
d.Set("max", limit.Max)
238+
}
230239

231240
// Only set the type field if it was originally specified in the configuration
232241
if v, ok := d.GetOk("type"); ok {
@@ -251,7 +260,6 @@ func resourceCloudStackLimitsUpdate(d *schema.ResourceData, meta interface{}) er
251260

252261
account := d.Get("account").(string)
253262
domainid := d.Get("domainid").(string)
254-
max := d.Get("max").(int)
255263
projectid := d.Get("projectid").(string)
256264

257265
// Create a new parameter struct
@@ -262,8 +270,10 @@ func resourceCloudStackLimitsUpdate(d *schema.ResourceData, meta interface{}) er
262270
if domainid != "" {
263271
p.SetDomainid(domainid)
264272
}
265-
if max != 0 {
266-
p.SetMax(int64(max))
273+
if maxVal, ok := d.GetOk("max"); ok {
274+
maxIntVal := maxVal.(int)
275+
log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
276+
p.SetMax(int64(maxIntVal))
267277
}
268278
if projectid != "" {
269279
p.SetProjectid(projectid)

cloudstack/resource_cloudstack_limits_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,26 @@ func TestAccCloudStackLimits_memory(t *testing.T) {
307307
})
308308
}
309309

310+
func TestAccCloudStackLimits_zero(t *testing.T) {
311+
resource.Test(t, resource.TestCase{
312+
PreCheck: func() { testAccPreCheck(t) },
313+
Providers: testAccProviders,
314+
CheckDestroy: testAccCheckCloudStackLimitsDestroy,
315+
Steps: []resource.TestStep{
316+
{
317+
Config: testAccCloudStackLimits_domain + testAccCloudStackLimits_zero,
318+
Check: resource.ComposeTestCheckFunc(
319+
testAccCheckCloudStackLimitsExists("cloudstack_limits.zero_limit"),
320+
resource.TestCheckResourceAttr(
321+
"cloudstack_limits.zero_limit", "type", "instance"),
322+
resource.TestCheckResourceAttr(
323+
"cloudstack_limits.zero_limit", "max", "0"),
324+
),
325+
},
326+
},
327+
})
328+
}
329+
310330
func TestAccCloudStackLimits_secondarystorage(t *testing.T) {
311331
resource.Test(t, resource.TestCase{
312332
PreCheck: func() { testAccPreCheck(t) },
@@ -343,7 +363,7 @@ func TestAccCloudStackLimits_import(t *testing.T) {
343363
ResourceName: "cloudstack_limits.foo",
344364
ImportState: true,
345365
ImportStateVerify: true,
346-
ImportStateVerifyIgnore: []string{"domainid", "type", "type", "max"},
366+
ImportStateVerifyIgnore: []string{"domainid", "type", "max"},
347367
},
348368
},
349369
})
@@ -448,6 +468,16 @@ resource "cloudstack_limits" "memory_limit" {
448468
}
449469
`
450470

471+
const testAccCloudStackLimits_zero = `
472+
# Testing setting a limit to 0 (zero resources allowed)
473+
# Note: The CloudStack API may return -1 for a limit set to 0, but the provider maintains the 0 value in state
474+
resource "cloudstack_limits" "zero_limit" {
475+
type = "instance"
476+
max = 0
477+
domainid = cloudstack_domain.test_domain.id
478+
}
479+
`
480+
451481
const testAccCloudStackLimits_secondarystorage = `
452482
resource "cloudstack_limits" "secondarystorage_limit" {
453483
type = "secondarystorage"

website/docs/d/limits.html.markdown

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ The following arguments are supported:
4949
* `memory`
5050
* `primarystorage`
5151
* `secondarystorage`
52-
* `publicip`
53-
* `eip`
54-
* `autoscalevmgroup`
5552
* `account` - (Optional) List resources by account. Must be used with the `domainid` parameter.
5653
* `domainid` - (Optional) List only resources belonging to the domain specified.
5754
* `projectid` - (Optional) List resource limits by project.
@@ -63,7 +60,7 @@ The following attributes are exported:
6360
* `limits` - A list of resource limits. Each limit has the following attributes:
6461
* `resourcetype` - The type of resource.
6562
* `resourcetypename` - The name of the resource type.
66-
* `max` - The maximum number of the resource.
63+
* `max` - The maximum number of the resource. A value of `-1` indicates unlimited resources. A value of `0` means zero resources are allowed, though the CloudStack API may return `-1` for a limit set to `0`.
6764
* `account` - The account of the resource limit.
6865
* `domain` - The domain name of the resource limit.
6966
* `domainid` - The domain ID of the resource limit.

website/docs/r/limits.html.markdown

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,10 @@ The following arguments are supported:
5858
* `memory`
5959
* `primarystorage`
6060
* `secondarystorage`
61-
* `publicip`
62-
* `eip`
63-
* `autoscalevmgroup`
6461

6562
* `account` - (Optional, ForceNew) Update resource for a specified account. Must be used with the `domainid` parameter.
6663
* `domainid` - (Optional, ForceNew) Update resource limits for all accounts in specified domain. If used with the `account` parameter, updates resource limits for a specified account in specified domain.
67-
* `max` - (Optional) Maximum resource limit. Use `-1` for unlimited resource limit.
64+
* `max` - (Optional) Maximum resource limit. Use `-1` for unlimited resource limit. A value of `0` means zero resources are allowed, though the CloudStack API may return `-1` for a limit set to `0`.
6865
* `projectid` - (Optional, ForceNew) Update resource limits for project.
6966

7067
## Attributes Reference

0 commit comments

Comments
 (0)