Repository navigation
fix(terraform): drop litellm_key from state on 404 instead of failing the plan - #40443
Conversation
… the plan Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
|
Greptile SummaryThis PR allows Terraform refresh to treat a missing LiteLLM key as an absent resource rather than aborting the plan.
Confidence Score: 5/5The PR appears safe to merge because it narrowly handles missing keys during refresh while preserving failures for all other HTTP statuses. No actionable failures remain: the typed error preserves existing caller-visible messages, the endpoint’s 404 contract represents an absent key, and tests cover both the intended 404 behavior and a non-404 failure.
|
| Filename | Overview |
|---|---|
| terraform/provider/litellm/client.go | Adds typed HTTP errors and maps key-info 404 responses to an absent key without changing handling for other statuses. |
| terraform/provider/litellm/resource_key.go | Logs when a missing key is removed from Terraform state through the existing nil-key branch. |
| terraform/provider/litellm/resource_key_test.go | Covers state removal on 404 and preservation of the existing failure behavior and state ID on 500 responses. |
Reviews (1): Last reviewed commit: "fix(terraform): drop litellm_key from st..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
5264f8e
into
litellm_internal_staging
TLDR
Problem this solves:
litellm_keydeleted or rotated outside Terraform 404s on refreshterraform plan, blocking every other resource tooHow it solves it:
/key/info404 on refresh now clears the key from stateUser Flow
Before: a platform team manages virtual keys with Terraform, someone deletes one key in the Admin UI, and from then on every plan in that workspace errors out
litellm_key.tf_managed(aliastf-managed-lit-6394) andlitellm_team.unrelated, both create fine{"deleted_keys":["tf-managed-lit-6394"]}Key not found in databaseterraform planand getError: error reading key: API request failed with status code 404 ..., exit 1, with no plan for any resourceAfter: the same plan succeeds and shows the missing key as
+ create, so the team can recreate it (or import the rotated one) without hand-editing state{"deleted_keys":["tf-managed-lit-6394"]}Key not found in databaseterraform plan, it exits 0 withPlan: 1 to add, 0 to change, 0 to destroy.andlitellm_key.tf_managed will be createdterraform applycreates the key again, and the nextterraform plansaysNo changes.+ create. Since the rotated key still holds the alias, they runterraform import litellm_key.tf_managed <new token_id>and the next plan saysNo changes.Relevant issues
Fixes BerriAI/terraform-provider-litellm#51
Linear ticket
Resolves LIT-6394
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
cd terraform/provider && go test -timeout 120s ./...@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Root cause:
sendRequestfolded every non-200 into a plainfmt.Errorf, soGetKeycould never tell a 404 apart from any other failure andresourceKeyReadalways returned a hard diagnostic; the existingif key == nil { d.SetId("") }branch was unreachable. The fix returns a typedapiError{StatusCode, Body}fromsendRequest(same message text as before, so every other caller is unchanged), andGetKeymaps onlyStatusCode == 404to(nil, nil)Setup, shared by both arms: local proxy on http://localhost:4000 with Postgres, started from this checkout with
PYTHONPATH=<checkout> uv run --no-sync litellm --config litellm/proxy/dev_config.yaml --port 4000 --use_prisma_db_push. Terraform 1.13.4 with adev_overridesCLI config pointing at a provider binary built withgo buildfrom the named commit (asserted withstrings <binary> | grep "not found, removing from state": 0 hits for Before, 7 for After). Samemain.tfand the same state file across arms:Before (096984b)
Key deleted out of band
curl -s -X POST -H "Authorization: Bearer $MASTER" -H 'Content-Type: application/json' http://localhost:4000/key/delete -d '{"key_aliases":["tf-managed-lit-6394"]}'curl -s -w '\n%{http_code}\n' -H "Authorization: Bearer $MASTER" 'http://localhost:4000/key/info?key=4c6036efed3c5f8ace928128479bf6aee089206b1f6f723a15b44ee9df816eb6'terraform plan -no-colorKey rotated out of band
curl -s -X POST -H "Authorization: Bearer $MASTER" -H 'Content-Type: application/json' http://localhost:4000/key/regenerate -d '{"key":"<current key>"}'returns a newtoken_id7a356d5d..., and/key/infofor the old id31e0c87d...returns 404 as aboveterraform plan -no-colorNon-404 error (provider
api_key = "sk-wrong")terraform plan -no-colorAfter (69105f2)
Key deleted out of band
curl -s -X POST -H "Authorization: Bearer $MASTER" -H 'Content-Type: application/json' http://localhost:4000/key/delete -d '{"key_aliases":["tf-managed-lit-6394"]}'curl -s -w '\n%{http_code}\n' -H "Authorization: Bearer $MASTER" 'http://localhost:4000/key/info?key=7a356d5da23abe5e8e16f3e054b94c0946ed0821e2269844f53d2cd7bd0c03fc'terraform plan -no-colorterraform apply -auto-approve -no-colorterraform plan -no-colorKey rotated out of band
31e0c87d...,/key/infofor it returns 404 as aboveterraform plan -no-colorterraform apply -auto-approve -no-color(the rotated key still owns the alias, so the proxy rejects the recreate; this is the existing alias-uniqueness rule, unchanged by this PR)terraform import litellm_key.tf_managed 7a356d5da23abe5e8e16f3e054b94c0946ed0821e2269844f53d2cd7bd0c03fcterraform plan -no-colorNon-404 error (provider
api_key = "sk-wrong")terraform plan -no-colorTests:
TestResourceKeyReadDropsMissingKeyFromStateandTestResourceKeyReadStillFailsOnNon404Errorsinterraform/provider/litellm/resource_key_test.go. Mutation check: revertingclient.goto the merge base, keeping the oldfmt.ErrorfinsendRequest, or wideningisNotFoundto anyapiErroreach fail exactly one of the two tests.gofmt -l,go vet ./...,go build ./..., andgo test -timeout 120s ./...pass on the tipType
🐛 Bug Fix
Caveats (if any)
Low
terraform importof the new token_id, since the rotated key keeps the aliaslitellm_keyread path changes. Other resources already handled 404 withd.SetId("")Final Attestation
Link to Devin session: https://app.devin.ai/sessions/a3cc1cc6dd8b45928c3c6e0258317eae
Open in Devin Desktop: https://app.devin.ai/desktop/session/a3cc1cc6dd8b45928c3c6e0258317eae?variant=devin
Requested by: @yassin-berriai