From 822f853366e6d2c7cff4e4e51d1aabe01f4d3b8c Mon Sep 17 00:00:00 2001 From: Louis Vauterin Date: Tue, 22 Sep 2026 13:15:43 +0200 Subject: [PATCH] fix(provider): accept 2xx status in handleResponse for access group create handleResponse (resource_team.go) only accepted exactly HTTP 200, but POST /v1/access_group (and its /v1/unified_access_group alias) legitimately answers 201 Created. litellm_access_group and litellm_unified_access_group create both succeeded on the proxy and failed in the provider, leaving the group out of state and forcing an import to recover on the next apply's 409 for the now-duplicate name. Same fix and shape as #40723, which widened this exact check in sendRequest/handleAPIResponse/handleMCPAPIResponse for mcp_server, model, key and organization_member. handleResponse is the one shared status-check helper that fix didn't reach -- it's a different function in a different file (resource_team.go, not client.go/utils.go), so this is fully independent of that PR and can land before, after, or alongside it with no conflict. handleResponse is also used by agent, budget, guardrail, organization, prompt, search_tool, tag, team, team_block, key_block, team_member(_add) and user -- all unaffected in practice, since every one of their own endpoints already answers exactly 200. Widening the check costs them nothing and only changes behavior for the two resources that were actually broken. Verified: go test ./... passes, including a new TestHandleResponseAcceptsFullSuccessRange table test covering 200/201/202/204 (accepted) and 400/404/409/500 (still rejected), mirroring #40723's own TestHandleAPIResponseAcceptsFullSuccessRange. Co-Authored-By: Claude Sonnet 5 --- terraform/provider/CHANGELOG.md | 1 + terraform/provider/litellm/resource_team.go | 2 +- .../provider/litellm/resource_team_test.go | 45 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/terraform/provider/CHANGELOG.md b/terraform/provider/CHANGELOG.md index 8f40ed6dfb7..165e4ea7861 100644 --- a/terraform/provider/CHANGELOG.md +++ b/terraform/provider/CHANGELOG.md @@ -57,6 +57,7 @@ longer signal it. - **key**: Updates no longer send an empty `budget_duration`, which the proxy rejects with a 400; any update to a key without a configured `budget_duration` previously failed outright - **key**: A config-supplied `key` value (write-only) is now forwarded to `/key/generate`; previously it was silently dropped and the proxy generated a random key instead - **security**: The `litellm_key` data source and `litellm_key_block` resource normalize raw `sk-` keys to their SHA-256 token hash before building request URLs and resource IDs, so plaintext keys no longer land in reverse-proxy access logs, Terraform plan output, or state IDs +- **access_group, unified_access_group**: create now accepts any 2xx response instead of requiring exactly HTTP 200; `POST /v1/access_group` (and its `/v1/unified_access_group` alias) legitimately returns 201, so `litellm_access_group` and `litellm_unified_access_group` create previously succeeded on the proxy but failed in the provider, leaving the group out of state and forcing a `terraform import` to recover on the next apply's 409. Same fix and shape as the one already applied to `mcp_server`/`model`/`key`/`organization_member`; `handleResponse` (shared by several other resources) was the one status-check helper that fix didn't reach ### Changed diff --git a/terraform/provider/litellm/resource_team.go b/terraform/provider/litellm/resource_team.go index bf7d2508077..622fec7d0a0 100644 --- a/terraform/provider/litellm/resource_team.go +++ b/terraform/provider/litellm/resource_team.go @@ -466,7 +466,7 @@ func toStringSlice(v interface{}) []string { } func handleResponse(resp *http.Response, action string) error { - if resp.StatusCode != http.StatusOK { + if resp.StatusCode < 200 || resp.StatusCode >= 300 { body, _ := io.ReadAll(resp.Body) return fmt.Errorf("error %s: %s - %s", action, resp.Status, string(body)) } diff --git a/terraform/provider/litellm/resource_team_test.go b/terraform/provider/litellm/resource_team_test.go index 35d60401d30..36008dd13f3 100644 --- a/terraform/provider/litellm/resource_team_test.go +++ b/terraform/provider/litellm/resource_team_test.go @@ -434,3 +434,48 @@ func TestTeamLimitTypesSentOnCreateOnly(t *testing.T) { } } } + +// handleResponse is shared by several resources (team, access_group, +// unified_access_group, agent, budget, guardrail, organization, prompt, +// search_tool, tag, team_block, key_block, team_member(_add), user), but only +// access_group/unified_access_group's create actually hits a non-200 success +// status in practice: POST /v1/access_group (and its /v1/unified_access_group +// alias) answers 201, same shape as the mcp_server/model/key/organization_member +// bug already fixed for handleAPIResponse/handleMCPAPIResponse/sendRequest. +func TestHandleResponseAcceptsFullSuccessRange(t *testing.T) { + tests := []struct { + name string + statusCode int + wantErr bool + }{ + {name: "200 OK", statusCode: http.StatusOK, wantErr: false}, + {name: "201 Created", statusCode: http.StatusCreated, wantErr: false}, + {name: "202 Accepted", statusCode: http.StatusAccepted, wantErr: false}, + {name: "204 No Content", statusCode: http.StatusNoContent, wantErr: false}, + {name: "400 Bad Request", statusCode: http.StatusBadRequest, wantErr: true}, + {name: "404 Not Found", statusCode: http.StatusNotFound, wantErr: true}, + {name: "409 Conflict", statusCode: http.StatusConflict, wantErr: true}, + {name: "500 Internal Server Error", statusCode: http.StatusInternalServerError, wantErr: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rec := httptest.NewRecorder() + rec.WriteHeader(tt.statusCode) + rec.WriteString(`{"access_group_id":"ag-1","access_group_name":"uag-baseline"}`) + resp := rec.Result() + + err := handleResponse(resp, "creating unified access group") + + if tt.wantErr { + if err == nil { + t.Fatalf("handleResponse returned no error for status %d", tt.statusCode) + } + return + } + if err != nil { + t.Fatalf("handleResponse returned unexpected error for status %d: %v", tt.statusCode, err) + } + }) + } +}