[CCV-4035] [datadog_cost_budget] Add Validation for terraform plan Flow #3349
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
ResourceWithValidateConfiginterface 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 madeChanges
1. Client-Side Validation (
resource_datadog_cost_budget.go)Added
ValidateConfig()method that validates:- Tags must have 0, 1, or 2 elements
- Tags must be unique (no duplicate tags in by {tag1,tag2})
- start_month > 0 and format YYYYMM
- end_month > 0 and format YYYYMM
- end_month >= start_month
- 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} clausecalculateMonthCount(): Calculates number of months in budget periodTesting
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
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}Test case 2: test_missing_months/main.tf
start_month = 202501, and end_month = 202503# Only has entry for 202501, missing 202502 & 202503
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}