fix(terraform): omit empty team_id from /key/update payload

UpdateKey always sent team_id, so a key with no team sent team_id: "" and
the proxy rejected it with a 500 ("Team object not found for team change
validation"). Any update or import of a teamless key failed outright.

Only send team_id when it is set, matching how budget_duration is already
handled.
This commit is contained in:
Julian Löffler 2026-09-11 09:18:31 +02:00
parent 9a715df212
commit ed6f8216d8
No known key found for this signature in database
2 changed files with 34 additions and 1 deletions

View file

@ -125,7 +125,6 @@ func (c *Client) UpdateKey(key *Key) (*Key, error) {
// Create a new map with only the fields that can be updated
updateData := map[string]interface{}{
"key": key.Key,
"team_id": key.TeamID,
"key_alias": key.KeyAlias,
"aliases": key.Aliases,
"permissions": key.Permissions,
@ -144,6 +143,12 @@ func (c *Client) UpdateKey(key *Key) (*Key, error) {
updateData["model_tpm_limit"] = key.ModelTPMLimit
}
// The proxy rejects an empty team_id with a 500 ("Team object not found"),
// so only send it when set.
if key.TeamID != "" {
updateData["team_id"] = key.TeamID
}
// The proxy rejects an empty-string budget_duration with a 400, so only
// send it when set.
if key.BudgetDuration != "" {

View file

@ -319,6 +319,34 @@ func TestUpdateKeyOmitsEmptyBudgetDuration(t *testing.T) {
}
}
// The proxy 500s on team_id: "" with "Team object not found", so a teamless key
// must omit it from the update payload entirely.
func TestUpdateKeyOmitsEmptyTeamID(t *testing.T) {
var captured map[string]interface{}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
json.Unmarshal(body, &captured)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"key": "sk-test"}`))
}))
defer srv.Close()
client := NewClient(srv.URL, "test-key", true)
if _, err := client.UpdateKey(&Key{Key: "sk-test"}); err != nil {
t.Fatalf("UpdateKey returned error: %v", err)
}
if _, present := captured["team_id"]; present {
t.Errorf("update payload contains empty team_id: %v", captured["team_id"])
}
if _, err := client.UpdateKey(&Key{Key: "sk-test", TeamID: "team-1"}); err != nil {
t.Fatalf("UpdateKey returned error: %v", err)
}
if captured["team_id"] != "team-1" {
t.Errorf("team_id = %v, want team-1", captured["team_id"])
}
}
func TestResourceKeyUpdateFailureKeepsPriorState(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")