Skip to content

Conversation

@zeinageb
Copy link

Problem

The current Terraform Cost Budget resource does not perform sufficient validation during the terraform plan phase. Configuration errors (especially around tag filters and missing budget entries) are only detected during terraform apply, resulting in user confusion and wasted time

Solution

Implemented ResourceWithValidateConfig interface to add client-side validation that mirrors the API's validation logic from dd-source. Validation now runs during terraform plan, catching configuration errors before any API calls are made

Changes

1. Client-Side Validation (resource_datadog_cost_budget.go)

Added ValidateConfig() method that validates:

  • Tags validation:
    - Tags must have 0, 1, or 2 elements
    - Tags must be unique (no duplicate tags in by {tag1,tag2})
  • Month validation:
    - start_month > 0 and format YYYYMM
    - end_month > 0 and format YYYYMM
    - end_month >= start_month
  • Entry validation:
    - Entry month must be within budget period
    - Entry amount must be >= 0
    - Entry tag_filters count must match tags in metrics_query
    - Each tag_key must be one of the tags from metrics_query
    - Each unique tag combination must have entries for all months in the budget period

2. Helper Functions

Added two helper functions copied from dd-source to maintain consistency:

  • extractTagsFromQuery(): Parses tags from by {tag1,tag2} clause
  • calculateMonthCount(): Calculates number of months in budget period

Testing

Created test files to test different scenarios we might have

Valid Configuration (Passes Validation)

Test case: test_valid_budget/main.tf
Hierarchical budget with 12 months, 2 tags, 1 combinationResult: terraform plan succeeds

image

Invalid Configurations (Caught at Plan Time)

Test case 1: test_invalid_tag_key/main.tf

metrics_query = "sum:aws.cost{*} by {account}"tag_filters { tag_key = "wrong_tag" # ERROR: Not in metrics_query}
image

Test case 2: test_missing_months/main.tf

start_month = 202501, and end_month = 202503# Only has entry for 202501, missing 202502 & 202503

image

Test case 3: test_wrong_tag_count/main.tf

metrics_query = "sum:aws.cost{*} by {team,account}" # 2 tagstag_filters { tag_key = "team" # ERROR: Missing 'account' tag filter}

image

@zeinageb zeinageb marked this pull request as ready for review November 27, 2025 12:20
@zeinageb zeinageb requested review from a team as code owners November 27, 2025 12:20
@zeinageb zeinageb changed the title [[CCV-4035] Add Validation for terraform plan Flow [CCV-4035] Add Validation for terraform plan Flow Nov 27, 2025
@zeinageb zeinageb changed the title [CCV-4035] Add Validation for terraform plan Flow [CCV-4035] [datadog_cost_budget] Add Validation for terraform plan Flow Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants