-
Notifications
You must be signed in to change notification settings - Fork 48
Add cloudstack_limits
data source and resource
#197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Implemented data source for retrieving CloudStack resource limits. - Added resource management for setting and updating resource limits for accounts, domains, and projects. - Updated documentation for cloudstack_limits with usage examples and argument references.
…ted resources Implimenting copilot suggestions for d.GetOk
Co-authored-by: Copilot <[email protected]>
- update resourceCloudStackLimitsRead to handle different ID formats - rewrite resourceCloudStackLimitsImport to handle different ID formats - Support -1 (Unlimited) and 0 (zero) limits
Optional: true, | ||
Description: "List resources by account. Must be used with the domainid parameter.", | ||
}, | ||
"domainid": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"domainid": { | |
"domain_id": { |
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"domainid": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"domainid": { | |
"domain_id": { |
For consistency with rest of the project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds CloudStack resource limits management capabilities to the Terraform CloudStack provider by introducing both a data source and resource for managing resource limits for accounts, domains, and projects.
Key changes include:
- Implementation of
cloudstack_limits
resource for managing CloudStack resource limits via the updateResourceLimit API - Implementation of
cloudstack_limits
data source for querying existing resource limits - Comprehensive test coverage for various limit types, scopes, and edge cases
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.
Show a summary per file
File | Description |
---|---|
cloudstack/provider.go |
Registers the new data source and resource with the provider |
cloudstack/resource_cloudstack_limits.go |
Core implementation of the limits resource with CRUD operations and import functionality |
cloudstack/data_source_cloudstack_limits.go |
Data source implementation for querying resource limits |
cloudstack/resource_cloudstack_limits_test.go |
Comprehensive test suite covering various scenarios and resource types |
website/docs/r/limits.html.markdown |
Resource documentation with examples and argument reference |
website/docs/d/limits.html.markdown |
Data source documentation with usage examples |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
if d.Get("max").(int) == 0 { | |
if v, ok := d.GetOk("max"); ok && v.(int) == 0 { |
Copilot uses AI. Check for mistakes.
// Find the string representation for this numeric type | ||
for typeStr, typeVal := range resourceTypeMap { | ||
if typeVal == rt { | ||
d.Set("type", typeStr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of d.Set()
should be checked for errors to ensure the state is properly updated.
Copilot uses AI. Check for mistakes.
if len(idParts) >= 3 { | ||
if idParts[1] == "domain" { | ||
// Format: resourcetype-domain-domainid | ||
d.Set("domainid", idParts[2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of d.Set()
should be checked for errors to ensure the state is properly updated.
Copilot uses AI. Check for mistakes.
d.Set("domainid", idParts[2]) | ||
} else if idParts[1] == "project" { | ||
// Format: resourcetype-project-projectid | ||
d.Set("projectid", idParts[2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of d.Set()
should be checked for errors to ensure the state is properly updated.
Copilot uses AI. Check for mistakes.
d.Set("account", idParts[2]) | ||
d.Set("domainid", idParts[3]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return values of d.Set()
should be checked for errors to ensure the state is properly updated.
Copilot uses AI. Check for mistakes.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of d.Set()
should be checked for errors to ensure the state is properly updated.
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)) | |
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) | |
} |
Copilot uses AI. Check for mistakes.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of d.Set()
should be checked for errors to ensure the state is properly updated.
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)) | |
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) | |
} |
Copilot uses AI. Check for mistakes.
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of d.Set()
should be checked for errors to ensure the state is properly updated.
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)) | |
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) | |
} |
Copilot uses AI. Check for mistakes.
// 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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value of d.Set()
should be checked for errors to ensure the state is properly updated.
d.Set("type", v.(string)) | |
if err := d.Set("type", v.(string)); err != nil { | |
return fmt.Errorf("error setting 'type' in state: %s", err) | |
} |
Copilot uses AI. Check for mistakes.
Adding limits as a terraform managed resource option -> https://cloudstack.apache.org/api/apidocs-4.20/apis/updateResourceLimit.html
Contributes to #82
Using this code for example: