Skip to content

Conversation

@jeanvetorello
Copy link
Contributor

CloudStack Quota API Implementation

Summary

This PR implements comprehensive support for CloudStack Quota APIs in the Terraform CloudStack provider, enabling users to manage quota tariffs and monitor quota usage through Terraform.

Features Added

Data Sources

  • cloudstack_quota - Retrieve quota summary for accounts/domains
  • cloudstack_quota_enabled - Check if quota plugin is enabled
  • cloudstack_quota_tariff - Query existing quota tariffs with filtering

Resources

  • cloudstack_quota_tariff - Full CRUD operations for quota tariff management
    • Create/Update/Delete tariffs
    • Activation rules support
    • Import existing tariffs
    • Schema validation

Technical Implementation

Core Components

  • Complete CRUD operations using CloudStack Go client v2.18.1
  • Robust error handling and validation
  • Lifecycle management with ForceNew attributes
  • Import functionality for existing resources

Schema Features

  • Usage Types: Support for all CloudStack usage types (1-25)
  • Activation Rules: Complex pricing rules based on service offerings
  • Validation: Input validation for negative values and invalid types
  • Lifecycle: Proper resource recreation when needed

Testing Coverage

Acceptance Tests (11 total)

  • Data Sources: Basic and filtered queries for all data sources
  • Resource CRUD: Create, Read, Update, Delete, Import operations
  • Validation: Invalid input handling and error scenarios
  • Activation Rules: Simple and complex pricing rules

Manual Testing Scenarios

  • ✅ Real CloudStack integration verified
  • ✅ All CRUD operations tested
  • ✅ Complex activation rules validated
  • ✅ Import/export functionality confirmed
  • ✅ Edge cases and error handling tested

Documentation

Website Documentation

  • Complete markdown documentation for all resources and data sources
  • Usage examples and configuration options
  • Import instructions and best practices

Examples

  • 11 example configurations covering:
    • Basic tariff creation
    • Multiple tariffs management
    • Complex activation rules
    • Lifecycle management
    • Import scenarios
    • Validation examples

Code Quality

Standards Compliance

  • Follows Terraform provider patterns
  • Consistent with existing CloudStack provider code
  • Proper Go formatting and conventions
  • Comprehensive error handling

Dependencies

  • Uses existing CloudStack Go client
  • No new external dependencies
  • Compatible with current provider architecture

Use Cases

This implementation enables:

  1. Cost Management: Define pricing for CloudStack resources
  2. Billing Automation: Automated tariff management through Terraform
  3. Multi-tenant Pricing: Different pricing rules per service offering
  4. Quota Monitoring: Track quota usage across accounts/domains

Breaking Changes

None - This is a new feature addition with no impact on existing functionality.

Checklist

  • All tests pass
  • Documentation complete
  • Examples provided
  • Real CloudStack integration tested
  • Import functionality working
  • Schema validation implemented
  • Error handling robust

Related APIs

Implements CloudStack APIs:

  • quotaSummary
  • quotaIsEnabled
  • quotaTariffList
  • quotaTariffCreate
  • quotaTariffUpdate
  • quotaTariffDelete

How to Test

# Run acceptance tests
TF_ACC=1 go test -v ./cloudstack -run "TestAccCloudStackQuota"

# Test specific resource
TF_ACC=1 go test -v ./cloudstack -run "TestAccCloudStackQuotaTariff"

@CodeBleu CodeBleu added enhancement New feature or request status:needs-testing labels Oct 15, 2025
@kiranchavala
Copy link
Collaborator

@jeanvetorello could you fix the failing tests

--- PASS: TestAccServiceOfferingUnconstrained (2.41s)
=== RUN TestDiffTags
--- PASS: TestDiffTags (0.00s)
FAIL
FAIL github.com/terraform-providers/terraform-provider-cloudstack/cloudstack 1294.491s
FAIL
make: *** [GNUmakefile:34: testacc] Error 1
Error: Process completed with exit code 2.

Copy link
Collaborator

@kiranchavala kiranchavala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM , tested manually

root@ubuntu2404:~/terrfomconfig# cat main.tf
provider "cloudstack" {
  api_url    = var.cloudstack_api_url
  api_key    = var.cloudstack_api_key
  secret_key = var.cloudstack_secret_key

}



resource "cloudstack_quota_tariff" "test" {
  name            = "Complex Activation Rule Tariff"
  usage_type      = 1
  value           = 0.25
  description     = "Test tariff with complex activation rule"
  activation_rule = "serviceOffering.id == 'f3e56622-8cad-475d-92aa-5495b31768fd' && zone.name == 'ref-trl-9690-k-Mol8-kiran-chavala'"
}

root@ubuntu2404:~/terrfomconfig# terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # cloudstack_quota_tariff.test will be created
  + resource "cloudstack_quota_tariff" "test" {
      + activation_rule = "serviceOffering.id == 'f3e56622-8cad-475d-92aa-5495b31768fd' && zone.name == 'ref-trl-9690-k-Mol8-kiran-chavala'"
      + currency        = (known after apply)
      + description     = "Test tariff with complex activation rule"
      + effective_date  = (known after apply)
      + id              = (known after apply)
      + name            = "Complex Activation Rule Tariff"
      + position        = (known after apply)
      + removed         = (known after apply)
      + usage_name      = (known after apply)
      + usage_type      = 1
      + usage_unit      = (known after apply)
      + value           = 0.25
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudstack_quota_tariff.test: Creating...
cloudstack_quota_tariff.test: Creation complete after 0s [id=085fb27c-2c73-4359-a315-0ef46d63a98f]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

mysql> select * from quota_tariff where id=23 \G;
*************************** 1. row ***************************
                 id: 23
         usage_type: 1
         usage_name: RUNNING_VM
         usage_unit: Compute*Month
usage_discriminator: None
     currency_value: 0.25000
       effective_on: 2025-10-17 06:52:56
         updated_on: 2025-10-17 06:52:56
         updated_by: 1
               uuid: 085fb27c-2c73-4359-a315-0ef46d63a98f
               name: Complex Activation Rule Tariff
        description: Test tariff with complex activation rule
    activation_rule: serviceOffering.id == 'f3e56622-8cad-475d-92aa-5495b31768fd' && zone.name == 'ref-trl-9690-k-Mol8-kiran-chavala'
            removed: NULL
           end_date: NULL
           position: 1
1 row in set (0.00 sec)

Copy link
Contributor

@DaanHoogland DaanHoogland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clgtm

@kiranchavala kiranchavala merged commit c50eed8 into apache:main Oct 17, 2025
24 checks passed
@jeanvetorello jeanvetorello deleted the feature/cloudstack-quota-api branch October 17, 2025 13:18
@kiranchavala kiranchavala added this to the v0.6.0 milestone Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants