[CCV-4000] [datadog_cost_budget] Fix update creating duplicate instead of updating existing #3341
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.
What
Fix
datadog_cost_budgetresource Update operation creating duplicate budgets instead of updating existing onesWhy
When users run
terraform applyto update an existing budget, the provider was creating a new budget with a different ID instead of modifying the existing one. This issue was reported by Indeed customerRoot Cause
The
Update()function inresource_datadog_cost_budget.gowas only reading fromreq.Plan, which contains the user's desired changes from their.tffile. However, Terraform doesn't include the resource ID in the plan (since users don't specify it in their config, it's marked asOptional: truein the schema)The ID is only available in
req.State(fromterraform.tfstatefile). without the ID, theUpsertBudgetAPI call creates a new resource instead of updating the existing one.The Fix
Added code to:
Testing
!! Please read the testing process below to understand how this was verified!!
before anything, wanted to test if problem from API or Terraform so:
Tested the API directly with curl to confirm it handles updates correctly when ID is provided:
Returns same ID (API works correctly)This confirmed the bug was in the Terraform provider, not the API!
Test 1: Verify Update Fix (Create -> Update Workflow)
This test confirms the update bug is fixed
Commands:
cd terraform_examplesStep 1: Create main.tf
Step 2: Create budget
Step 3: Modify main.tf (change name and amounts to 150)
... edit file ...Step 4: Update budget
terraform apply -auto-approveID stayed same: so fix is verified
Result:
Before fix: ID changed on update (new budget created)
After fix: ID stays the same (existing budget updated)
Test 2: Check for Schema Discrepancies (Import -> Plan)
This test confirms no other discrepancies exist between UI-created budgets and Terraform schema
Commands:
Step 1: Import a UI-created budget (has tag filters, multiple entries so we check if problem with tags)
terraform import datadog_cost_budget.ui_created ... (type its id)Import successful
Step 2: See what was imported
terraform show > imported_budget.txtShows 12 monthly entries, each with tag_filters
Step 3: Copy imported config to import_test.tf
Step 4: Check for drift
terraform planOutput: "No changes. Your infrastructure matches the configuration"
Result: