Copilot commented on code in PR #197:
URL: 
https://github.com/apache/cloudstack-terraform-provider/pull/197#discussion_r2315232146


##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])
+                                       } else if idParts[1] == "project" {
+                                               // Format: 
resourcetype-project-projectid
+                                               d.Set("projectid", idParts[2])
+                                       } else if idParts[1] == "account" && 
len(idParts) >= 4 {
+                                               // Format: 
resourcetype-account-account-domainid
+                                               d.Set("account", idParts[2])
+                                               d.Set("domainid", idParts[3])
+                                       }
+                               }
+                       }
+               }
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Create a new parameter struct
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       // Retrieve the resource limits
+       l, err := cs.Limit.ListResourceLimits(p)
+       if err != nil {
+               return fmt.Errorf("error retrieving resource limits: %s", err)
+       }
+
+       if l.Count == 0 {
+               log.Printf("[DEBUG] Resource limit not found")
+               d.SetId("")
+               return nil
+       }
+
+       // Update the config
+       for _, limit := range l.ResourceLimits {
+               if limit.Resourcetype == fmt.Sprintf("%d", resourcetype) {
+                       log.Printf("[DEBUG] Retrieved max value from API: %d", 
limit.Max)
+
+                       // Handle CloudStack's convention where -1 signifies 
unlimited and 0 signifies zero
+                       if limit.Max == -1 {
+                               // For the zero limit test case, we need to 
preserve the 0 value
+                               // We'll check if the resource was created with 
max=0
+                               if d.Get("max").(int) == 0 {

Review Comment:
   Potential panic if 'max' is not set in the resource data. Use `GetOk` to 
safely check for the existence and value of the 'max' field before type 
assertion.
   ```suggestion
                                if v, ok := d.GetOk("max"); ok && v.(int) == 0 {
   ```



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)

Review Comment:
   The return value of `d.Set()` should be checked for errors to ensure the 
state is properly updated.



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])
+                                       } else if idParts[1] == "project" {
+                                               // Format: 
resourcetype-project-projectid
+                                               d.Set("projectid", idParts[2])
+                                       } else if idParts[1] == "account" && 
len(idParts) >= 4 {
+                                               // Format: 
resourcetype-account-account-domainid
+                                               d.Set("account", idParts[2])
+                                               d.Set("domainid", idParts[3])
+                                       }
+                               }
+                       }
+               }
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Create a new parameter struct
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       // Retrieve the resource limits
+       l, err := cs.Limit.ListResourceLimits(p)
+       if err != nil {
+               return fmt.Errorf("error retrieving resource limits: %s", err)
+       }
+
+       if l.Count == 0 {
+               log.Printf("[DEBUG] Resource limit not found")
+               d.SetId("")
+               return nil
+       }
+
+       // Update the config
+       for _, limit := range l.ResourceLimits {
+               if limit.Resourcetype == fmt.Sprintf("%d", resourcetype) {
+                       log.Printf("[DEBUG] Retrieved max value from API: %d", 
limit.Max)
+
+                       // Handle CloudStack's convention where -1 signifies 
unlimited and 0 signifies zero
+                       if limit.Max == -1 {
+                               // For the zero limit test case, we need to 
preserve the 0 value
+                               // We'll check if the resource was created with 
max=0
+                               if d.Get("max").(int) == 0 {
+                                       log.Printf("[DEBUG] API returned -1 for 
a limit set to 0, keeping it as 0 in state")
+                                       d.Set("max", 0)
+                               } else {
+                                       // Otherwise, use -1 to represent 
unlimited
+                                       d.Set("max", limit.Max)
+                               }
+                       } else {
+                               // For any other value, use it directly
+                               d.Set("max", limit.Max)
+                       }
+
+                       // Only set the type field if it was originally 
specified in the configuration
+                       if v, ok := d.GetOk("type"); ok {
+                               // Preserve the original case of the type 
parameter
+                               d.Set("type", v.(string))

Review Comment:
   The return value of `d.Set()` should be checked for errors to ensure the 
state is properly updated.
   ```suggestion
                                        if err := d.Set("max", 0); err != nil {
                                                return fmt.Errorf("error 
setting max to 0: %s", err)
                                        }
                                } else {
                                        // Otherwise, use -1 to represent 
unlimited
                                        if err := d.Set("max", limit.Max); err 
!= nil {
                                                return fmt.Errorf("error 
setting max to unlimited (-1): %s", err)
                                        }
                                }
                        } else {
                                // For any other value, use it directly
                                if err := d.Set("max", limit.Max); err != nil {
                                        return fmt.Errorf("error setting max: 
%s", err)
                                }
                        }
   
                        // Only set the type field if it was originally 
specified in the configuration
                        if v, ok := d.GetOk("type"); ok {
                                // Preserve the original case of the type 
parameter
                                if err := d.Set("type", v.(string)); err != nil 
{
                                        return fmt.Errorf("error setting type: 
%s", err)
                                }
   ```



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])
+                                       } else if idParts[1] == "project" {
+                                               // Format: 
resourcetype-project-projectid
+                                               d.Set("projectid", idParts[2])
+                                       } else if idParts[1] == "account" && 
len(idParts) >= 4 {
+                                               // Format: 
resourcetype-account-account-domainid
+                                               d.Set("account", idParts[2])
+                                               d.Set("domainid", idParts[3])
+                                       }
+                               }
+                       }
+               }
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Create a new parameter struct
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       // Retrieve the resource limits
+       l, err := cs.Limit.ListResourceLimits(p)
+       if err != nil {
+               return fmt.Errorf("error retrieving resource limits: %s", err)
+       }
+
+       if l.Count == 0 {
+               log.Printf("[DEBUG] Resource limit not found")
+               d.SetId("")
+               return nil
+       }
+
+       // Update the config
+       for _, limit := range l.ResourceLimits {
+               if limit.Resourcetype == fmt.Sprintf("%d", resourcetype) {
+                       log.Printf("[DEBUG] Retrieved max value from API: %d", 
limit.Max)
+
+                       // Handle CloudStack's convention where -1 signifies 
unlimited and 0 signifies zero
+                       if limit.Max == -1 {
+                               // For the zero limit test case, we need to 
preserve the 0 value
+                               // We'll check if the resource was created with 
max=0
+                               if d.Get("max").(int) == 0 {
+                                       log.Printf("[DEBUG] API returned -1 for 
a limit set to 0, keeping it as 0 in state")
+                                       d.Set("max", 0)
+                               } else {
+                                       // Otherwise, use -1 to represent 
unlimited
+                                       d.Set("max", limit.Max)
+                               }
+                       } else {
+                               // For any other value, use it directly
+                               d.Set("max", limit.Max)
+                       }
+
+                       // Only set the type field if it was originally 
specified in the configuration
+                       if v, ok := d.GetOk("type"); ok {
+                               // Preserve the original case of the type 
parameter
+                               d.Set("type", v.(string))

Review Comment:
   The return value of `d.Set()` should be checked for errors to ensure the 
state is properly updated.
   ```suggestion
                                if err := d.Set("type", v.(string)); err != nil 
{
                                        return fmt.Errorf("error setting 'type' 
in state: %s", err)
                                }
   ```



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])

Review Comment:
   The return value of `d.Set()` should be checked for errors to ensure the 
state is properly updated.



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])
+                                       } else if idParts[1] == "project" {
+                                               // Format: 
resourcetype-project-projectid
+                                               d.Set("projectid", idParts[2])
+                                       } else if idParts[1] == "account" && 
len(idParts) >= 4 {
+                                               // Format: 
resourcetype-account-account-domainid
+                                               d.Set("account", idParts[2])
+                                               d.Set("domainid", idParts[3])

Review Comment:
   The return values of `d.Set()` should be checked for errors to ensure the 
state is properly updated.



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])
+                                       } else if idParts[1] == "project" {
+                                               // Format: 
resourcetype-project-projectid
+                                               d.Set("projectid", idParts[2])

Review Comment:
   The return value of `d.Set()` should be checked for errors to ensure the 
state is properly updated.



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])
+                                       } else if idParts[1] == "project" {
+                                               // Format: 
resourcetype-project-projectid
+                                               d.Set("projectid", idParts[2])
+                                       } else if idParts[1] == "account" && 
len(idParts) >= 4 {
+                                               // Format: 
resourcetype-account-account-domainid
+                                               d.Set("account", idParts[2])
+                                               d.Set("domainid", idParts[3])
+                                       }
+                               }
+                       }
+               }
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Create a new parameter struct
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       // Retrieve the resource limits
+       l, err := cs.Limit.ListResourceLimits(p)
+       if err != nil {
+               return fmt.Errorf("error retrieving resource limits: %s", err)
+       }
+
+       if l.Count == 0 {
+               log.Printf("[DEBUG] Resource limit not found")
+               d.SetId("")
+               return nil
+       }
+
+       // Update the config
+       for _, limit := range l.ResourceLimits {
+               if limit.Resourcetype == fmt.Sprintf("%d", resourcetype) {
+                       log.Printf("[DEBUG] Retrieved max value from API: %d", 
limit.Max)
+
+                       // Handle CloudStack's convention where -1 signifies 
unlimited and 0 signifies zero
+                       if limit.Max == -1 {
+                               // For the zero limit test case, we need to 
preserve the 0 value
+                               // We'll check if the resource was created with 
max=0
+                               if d.Get("max").(int) == 0 {
+                                       log.Printf("[DEBUG] API returned -1 for 
a limit set to 0, keeping it as 0 in state")
+                                       d.Set("max", 0)
+                               } else {
+                                       // Otherwise, use -1 to represent 
unlimited
+                                       d.Set("max", limit.Max)
+                               }
+                       } else {
+                               // For any other value, use it directly
+                               d.Set("max", limit.Max)
+                       }
+
+                       // Only set the type field if it was originally 
specified in the configuration
+                       if v, ok := d.GetOk("type"); ok {
+                               // Preserve the original case of the type 
parameter
+                               d.Set("type", v.(string))

Review Comment:
   The return value of `d.Set()` should be checked for errors to ensure the 
state is properly updated.
   ```suggestion
                                        if err := d.Set("max", 0); err != nil {
                                                return fmt.Errorf("error 
setting max to 0: %s", err)
                                        }
                                } else {
                                        // Otherwise, use -1 to represent 
unlimited
                                        if err := d.Set("max", limit.Max); err 
!= nil {
                                                return fmt.Errorf("error 
setting max to unlimited (-1): %s", err)
                                        }
                                }
                        } else {
                                // For any other value, use it directly
                                if err := d.Set("max", limit.Max); err != nil {
                                        return fmt.Errorf("error setting max: 
%s", err)
                                }
                        }
   
                        // Only set the type field if it was originally 
specified in the configuration
                        if v, ok := d.GetOk("type"); ok {
                                // Preserve the original case of the type 
parameter
                                if err := d.Set("type", v.(string)); err != nil 
{
                                        return fmt.Errorf("error setting type: 
%s", err)
                                }
   ```



##########
cloudstack/resource_cloudstack_limits.go:
##########
@@ -0,0 +1,432 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+//
+
+package cloudstack
+
+import (
+       "context"
+       "fmt"
+       "log"
+       "strconv"
+       "strings"
+
+       "github.com/apache/cloudstack-go/v2/cloudstack"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
+       "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
+)
+
+// resourceTypeMap maps string resource types to their integer values
+var resourceTypeMap = map[string]int{
+       "instance":         0,
+       "ip":               1,
+       "volume":           2,
+       "snapshot":         3,
+       "template":         4,
+       "project":          5,
+       "network":          6,
+       "vpc":              7,
+       "cpu":              8,
+       "memory":           9,
+       "primarystorage":   10,
+       "secondarystorage": 11,
+}
+
+func resourceCloudStackLimits() *schema.Resource {
+       return &schema.Resource{
+               Read:   resourceCloudStackLimitsRead,
+               Update: resourceCloudStackLimitsUpdate,
+               Create: resourceCloudStackLimitsCreate,
+               Delete: resourceCloudStackLimitsDelete,
+               Schema: map[string]*schema.Schema{
+                       "type": {
+                               Type:     schema.TypeString,
+                               Required: true,
+                               ForceNew: true,
+                               ValidateFunc: validation.StringInSlice([]string{
+                                       "instance", "ip", "volume", "snapshot", 
"template", "project", "network", "vpc",
+                                       "cpu", "memory", "primarystorage", 
"secondarystorage",
+                               }, false), // false disables case-insensitive 
matching
+                               Description: "The type of resource to update 
the limits. Available types are: " +
+                                       "instance, ip, volume, snapshot, 
template, project, network, vpc, cpu, memory, " +
+                                       "primarystorage, secondarystorage",
+                       },
+                       "account": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource for a specified 
account. Must be used with the domainid parameter.",
+                       },
+                       "domainid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "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.",
+                       },
+                       "max": {
+                               Type:        schema.TypeInt,
+                               Optional:    true,
+                               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.",
+                       },
+                       "projectid": {
+                               Type:        schema.TypeString,
+                               Optional:    true,
+                               ForceNew:    true,
+                               Description: "Update resource limits for 
project.",
+                       },
+               },
+               Importer: &schema.ResourceImporter{
+                       StateContext: resourceCloudStackLimitsImport,
+               },
+       }
+}
+
+// resourceCloudStackLimitsImport parses composite import IDs and sets 
resource fields accordingly.
+func resourceCloudStackLimitsImport(ctx context.Context, d 
*schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
+       // Expected formats:
+       // - type-account-accountname-domainid (for account-specific limits)
+       // - type-project-projectid (for project-specific limits)
+       // - type-domain-domainid (for domain-specific limits)
+
+       log.Printf("[DEBUG] Importing resource with ID: %s", d.Id())
+
+       // First, extract the resource type which is always the first part
+       idParts := strings.SplitN(d.Id(), "-", 2)
+       if len(idParts) < 2 {
+               return nil, fmt.Errorf("unexpected import ID format (%q), 
expected type-account-accountname-domainid, type-domain-domainid, or 
type-project-projectid", d.Id())
+       }
+
+       // Parse the resource type
+       typeInt, err := strconv.Atoi(idParts[0])
+       if err != nil {
+               return nil, fmt.Errorf("invalid type value in import ID: %s", 
idParts[0])
+       }
+
+       // Find the string representation for this numeric type
+       var typeStr string
+       for k, v := range resourceTypeMap {
+               if v == typeInt {
+                       typeStr = k
+                       break
+               }
+       }
+       if typeStr == "" {
+               return nil, fmt.Errorf("unknown type value in import ID: %d", 
typeInt)
+       }
+       if err := d.Set("type", typeStr); err != nil {
+               return nil, err
+       }
+
+       // Get the original resource ID from the state
+       originalID := d.Id()
+       log.Printf("[DEBUG] Original import ID: %s", originalID)
+
+       // Instead of trying to parse the complex ID, let's create a new 
resource
+       // and read it from the API to get the correct values
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Create a new parameter struct for listing resource limits
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(typeInt)
+
+       // Try to determine the resource scope from the ID format
+       remainingID := idParts[1]
+
+       // Extract the resource scope from the ID
+       if strings.HasPrefix(remainingID, "domain-") {
+               // It's a domain-specific limit
+               log.Printf("[DEBUG] Detected domain-specific limit")
+               // We'll use the Read function to get the domain ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "project-") {
+               // It's a project-specific limit
+               log.Printf("[DEBUG] Detected project-specific limit")
+               // We'll use the Read function to get the project ID from the 
state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else if strings.HasPrefix(remainingID, "account-") {
+               // It's an account-specific limit
+               log.Printf("[DEBUG] Detected account-specific limit")
+               // We'll use the Read function to get the account and domain ID 
from the state
+               // after setting a temporary ID
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       } else {
+               // For backward compatibility, assume it's a global limit
+               log.Printf("[DEBUG] Detected global limit")
+               d.SetId(originalID)
+               return []*schema.ResourceData{d}, nil
+       }
+}
+
+// getResourceType gets the resource type from the type field
+func getResourceType(d *schema.ResourceData) (int, error) {
+       // Check if type is set
+       if v, ok := d.GetOk("type"); ok {
+               typeStr := v.(string)
+               if resourcetype, ok := resourceTypeMap[typeStr]; ok {
+                       return resourcetype, nil
+               }
+               return 0, fmt.Errorf("invalid type value: %s", typeStr)
+       }
+
+       return 0, fmt.Errorf("type must be specified")
+}
+
+func resourceCloudStackLimitsCreate(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               return err
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Validate account and domain parameters
+       if account != "" && domainid == "" {
+               return fmt.Errorf("domainid is required when account is 
specified")
+       }
+
+       // Create a new parameter struct
+       p := cs.Limit.NewUpdateResourceLimitParams(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if maxVal, ok := d.GetOk("max"); ok {
+               maxIntVal := maxVal.(int)
+               log.Printf("[DEBUG] Setting max value to %d", maxIntVal)
+               p.SetMax(int64(maxIntVal))
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       log.Printf("[DEBUG] Updating Resource Limit for type %d", resourcetype)
+       _, err = cs.Limit.UpdateResourceLimit(p)
+
+       if err != nil {
+               return fmt.Errorf("Error creating resource limit: %s", err)
+       }
+
+       // Generate a unique ID based on the parameters
+       id := generateResourceID(resourcetype, account, domainid, projectid)
+       d.SetId(id)
+
+       return resourceCloudStackLimitsRead(d, meta)
+}
+
+// generateResourceID creates a unique ID for the resource based on its 
parameters
+func generateResourceID(resourcetype int, account, domainid, projectid string) 
string {
+       if projectid != "" {
+               return fmt.Sprintf("%d-project-%s", resourcetype, projectid)
+       }
+
+       if account != "" && domainid != "" {
+               return fmt.Sprintf("%d-account-%s-%s", resourcetype, account, 
domainid)
+       }
+
+       if domainid != "" {
+               return fmt.Sprintf("%d-domain-%s", resourcetype, domainid)
+       }
+
+       return fmt.Sprintf("%d", resourcetype)
+}
+
+func resourceCloudStackLimitsRead(d *schema.ResourceData, meta interface{}) 
error {
+       cs := meta.(*cloudstack.CloudStackClient)
+
+       // Get the resourcetype from the type field
+       resourcetype, err := getResourceType(d)
+       if err != nil {
+               // If there's an error getting the type, try to extract it from 
the ID
+               idParts := strings.Split(d.Id(), "-")
+               if len(idParts) > 0 {
+                       if rt, err := strconv.Atoi(idParts[0]); err == nil {
+                               resourcetype = rt
+                               // Find the string representation for this 
numeric type
+                               for typeStr, typeVal := range resourceTypeMap {
+                                       if typeVal == rt {
+                                               d.Set("type", typeStr)
+                                               break
+                                       }
+                               }
+
+                               // Handle different ID formats
+                               if len(idParts) >= 3 {
+                                       if idParts[1] == "domain" {
+                                               // Format: 
resourcetype-domain-domainid
+                                               d.Set("domainid", idParts[2])
+                                       } else if idParts[1] == "project" {
+                                               // Format: 
resourcetype-project-projectid
+                                               d.Set("projectid", idParts[2])
+                                       } else if idParts[1] == "account" && 
len(idParts) >= 4 {
+                                               // Format: 
resourcetype-account-account-domainid
+                                               d.Set("account", idParts[2])
+                                               d.Set("domainid", idParts[3])
+                                       }
+                               }
+                       }
+               }
+       }
+
+       account := d.Get("account").(string)
+       domainid := d.Get("domainid").(string)
+       projectid := d.Get("projectid").(string)
+
+       // Create a new parameter struct
+       p := cs.Limit.NewListResourceLimitsParams()
+       p.SetResourcetype(resourcetype)
+       if account != "" {
+               p.SetAccount(account)
+       }
+       if domainid != "" {
+               p.SetDomainid(domainid)
+       }
+       if projectid != "" {
+               p.SetProjectid(projectid)
+       }
+
+       // Retrieve the resource limits
+       l, err := cs.Limit.ListResourceLimits(p)
+       if err != nil {
+               return fmt.Errorf("error retrieving resource limits: %s", err)
+       }
+
+       if l.Count == 0 {
+               log.Printf("[DEBUG] Resource limit not found")
+               d.SetId("")
+               return nil
+       }
+
+       // Update the config
+       for _, limit := range l.ResourceLimits {
+               if limit.Resourcetype == fmt.Sprintf("%d", resourcetype) {
+                       log.Printf("[DEBUG] Retrieved max value from API: %d", 
limit.Max)
+
+                       // Handle CloudStack's convention where -1 signifies 
unlimited and 0 signifies zero
+                       if limit.Max == -1 {
+                               // For the zero limit test case, we need to 
preserve the 0 value
+                               // We'll check if the resource was created with 
max=0
+                               if d.Get("max").(int) == 0 {
+                                       log.Printf("[DEBUG] API returned -1 for 
a limit set to 0, keeping it as 0 in state")
+                                       d.Set("max", 0)
+                               } else {
+                                       // Otherwise, use -1 to represent 
unlimited
+                                       d.Set("max", limit.Max)
+                               }
+                       } else {
+                               // For any other value, use it directly
+                               d.Set("max", limit.Max)
+                       }
+
+                       // Only set the type field if it was originally 
specified in the configuration
+                       if v, ok := d.GetOk("type"); ok {
+                               // Preserve the original case of the type 
parameter
+                               d.Set("type", v.(string))

Review Comment:
   The return value of `d.Set()` should be checked for errors to ensure the 
state is properly updated.
   ```suggestion
                                        if err := d.Set("max", 0); err != nil {
                                                return fmt.Errorf("failed to 
set max to 0: %w", err)
                                        }
                                } else {
                                        // Otherwise, use -1 to represent 
unlimited
                                        if err := d.Set("max", limit.Max); err 
!= nil {
                                                return fmt.Errorf("failed to 
set max to unlimited (-1): %w", err)
                                        }
                                }
                        } else {
                                // For any other value, use it directly
                                if err := d.Set("max", limit.Max); err != nil {
                                        return fmt.Errorf("failed to set max: 
%w", err)
                                }
                        }
   
                        // Only set the type field if it was originally 
specified in the configuration
                        if v, ok := d.GetOk("type"); ok {
                                // Preserve the original case of the type 
parameter
                                if err := d.Set("type", v.(string)); err != nil 
{
                                        return fmt.Errorf("failed to set type: 
%w", err)
                                }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to