litellm/terraform/provider/litellm/utils.go
Yassin Kortam b06f4345e4
fix(terraform): accept 2xx from the proxy in mcp server, model, key and org requests (#46550)
The proxy answers POST /v1/mcp/server with 201 and DELETE /v1/mcp/server/{id} with 202, but handleMCPAPIResponse, handleAPIResponse, Client.sendRequest and resourceLiteLLMMCPServerDelete treated every status other than 200 as a failure. terraform apply created the server on the proxy, then reported an error and never wrote the resource to state, so the next apply created a duplicate, and destroy failed on the 202.

Accept any 2xx in the four status checks, treat an empty or null 2xx body in handleAPIResponse as an empty ModelResponse, and cover each path with httptest tables that fail on the 200-only comparison.

Re-lands the diff from #40723 on current main.

Co-authored-by: matthew-hull-bright <matthew.hull@brightsg.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-16 00:40:24 +00:00

344 lines
9.9 KiB
Go

package litellm
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func isModelNotFoundError(errResp ErrorResponse) bool {
if msg, ok := errResp.Error.Message.(string); ok {
if strings.Contains(msg, "model not found") {
return true
}
}
if msgMap, ok := errResp.Error.Message.(map[string]interface{}); ok {
if errStr, ok := msgMap["error"].(string); ok {
if strings.Contains(errStr, "Model with id=") && strings.Contains(errStr, "not found in db") {
return true
}
}
}
// Check Detail.Error field for LiteLLM proxy error format
if errResp.Detail.Error != "" {
if strings.Contains(errResp.Detail.Error, "not found on litellm proxy") {
return true
}
}
return false
}
func handleAPIResponse(resp *http.Response, reqBody interface{}, client *Client) (*ModelResponse, error) {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
var errResp ErrorResponse
if err := json.Unmarshal(bodyBytes, &errResp); err == nil {
if isModelNotFoundError(errResp) {
return nil, fmt.Errorf("model_not_found")
}
}
reqBodyBytes, _ := json.Marshal(reqBody)
return nil, fmt.Errorf("API request failed: Status: %s, Response: %s, Request: %s",
resp.Status, client.redactSensitiveData(string(bodyBytes)), client.redactSensitiveData(string(reqBodyBytes)))
}
var envelope struct {
Data []json.RawMessage `json:"data"`
}
if err := json.Unmarshal(bodyBytes, &envelope); err == nil && len(envelope.Data) > 0 {
bodyBytes = envelope.Data[0]
}
if len(bodyBytes) == 0 || string(bodyBytes) == "null" {
return nil, nil
}
var modelResp ModelResponse
if err := json.Unmarshal(bodyBytes, &modelResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %v", err)
}
return &modelResp, nil
}
// hashedKeyToken normalizes a raw sk- API key to its SHA-256 token hash, the
// identifier the proxy stores and accepts, so the plaintext key never lands
// in request URLs, resource IDs, or proxy access logs. Values that are
// already hashed pass through unchanged.
func hashedKeyToken(key string) string {
if !strings.HasPrefix(key, "sk-") {
return key
}
sum := sha256.Sum256([]byte(key))
return hex.EncodeToString(sum[:])
}
// MakeRequest is a helper function to make HTTP requests
func MakeRequest(client *Client, method, endpoint string, body interface{}) (*http.Response, error) {
var req *http.Request
var err error
if body != nil {
jsonData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}
req, err = http.NewRequest(method, fmt.Sprintf("%s%s", client.APIBase, endpoint), bytes.NewBuffer(jsonData))
} else {
req, err = http.NewRequest(method, fmt.Sprintf("%s%s", client.APIBase, endpoint), nil)
}
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-api-key", client.APIKey)
return client.httpClient.Do(req)
}
// Helper functions to handle potential nil values from the API response
func GetStringValue(apiValue, defaultValue string) string {
if apiValue != "" {
return apiValue
}
return defaultValue
}
func GetIntValue(apiValue, defaultValue int) int {
if apiValue != 0 {
return apiValue
}
return defaultValue
}
func GetFloatValue(apiValue, defaultValue float64) float64 {
if apiValue != 0 {
return apiValue
}
return defaultValue
}
func GetBoolValue(apiValue, defaultValue bool) bool {
return apiValue
}
// handleMCPAPIResponse handles API responses specifically for MCP server operations
func handleMCPAPIResponse(resp *http.Response, result interface{}, client *Client) error {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
var errResp ErrorResponse
if err := json.Unmarshal(bodyBytes, &errResp); err == nil {
if isMCPServerNotFoundError(errResp) {
return fmt.Errorf("mcp_server_not_found")
}
}
return fmt.Errorf("API request failed: Status: %s, Response: %s",
resp.Status, client.redactSensitiveData(string(bodyBytes)))
}
if err := json.Unmarshal(bodyBytes, result); err != nil {
return fmt.Errorf("failed to parse response: %v", err)
}
return nil
}
// isMCPServerNotFoundError checks if the error response indicates an MCP server not found
func isMCPServerNotFoundError(errResp ErrorResponse) bool {
if msg, ok := errResp.Error.Message.(string); ok {
if strings.Contains(msg, "mcp server not found") || strings.Contains(msg, "server not found") {
return true
}
}
if msgMap, ok := errResp.Error.Message.(map[string]interface{}); ok {
if errStr, ok := msgMap["error"].(string); ok {
if strings.Contains(errStr, "MCP server with id=") && strings.Contains(errStr, "not found") {
return true
}
}
}
// Check Detail.Error field for LiteLLM proxy error format
if errResp.Detail.Error != "" {
if strings.Contains(errResp.Detail.Error, "not found") {
return true
}
}
return false
}
// isCredentialNotFoundError checks if the error response indicates a credential not found
func isCredentialNotFoundError(errResp ErrorResponse) bool {
if msg, ok := errResp.Error.Message.(string); ok {
if strings.Contains(msg, "credential not found") {
return true
}
}
if msgMap, ok := errResp.Error.Message.(map[string]interface{}); ok {
if errStr, ok := msgMap["error"].(string); ok {
if strings.Contains(errStr, "Credential with name=") && strings.Contains(errStr, "not found") {
return true
}
}
}
// Check Detail.Error field for LiteLLM proxy error format
if errResp.Detail.Error != "" {
if strings.Contains(errResp.Detail.Error, "credential not found") {
return true
}
}
return false
}
var errCredentialConflict = errors.New("credential_conflict")
func isLegacyCredentialConflictError(errResp ErrorResponse) bool {
isConflict := func(msg string) bool {
return strings.Contains(msg, "Unique constraint failed") && strings.Contains(msg, "credential_name")
}
if msg, ok := errResp.Error.Message.(string); ok && isConflict(msg) {
return true
}
if msgMap, ok := errResp.Error.Message.(map[string]interface{}); ok {
if errStr, ok := msgMap["error"].(string); ok && isConflict(errStr) {
return true
}
}
return isConflict(errResp.Detail.Error)
}
// handleCredentialAPIResponse handles API responses specifically for credential operations
func handleCredentialAPIResponse(resp *http.Response, result interface{}, client *Client) error {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("credential_not_found")
}
if resp.StatusCode == http.StatusConflict {
return errCredentialConflict
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
var errResp ErrorResponse
if err := json.Unmarshal(bodyBytes, &errResp); err == nil {
if isCredentialNotFoundError(errResp) {
return fmt.Errorf("credential_not_found")
}
if isLegacyCredentialConflictError(errResp) {
return errCredentialConflict
}
}
return fmt.Errorf("API request failed: Status: %s, Response: %s",
resp.Status, client.redactSensitiveData(string(bodyBytes)))
}
// For credential operations, we might get a simple string response or a credential object
if result != nil {
if err := json.Unmarshal(bodyBytes, result); err != nil {
// If parsing fails, it might be a simple string response which is fine for create/update/delete
return nil
}
}
return nil
}
// isVectorStoreNotFoundError checks if the error response indicates a vector store not found
func isVectorStoreNotFoundError(errResp ErrorResponse) bool {
if msg, ok := errResp.Error.Message.(string); ok {
if strings.Contains(msg, "vector store not found") {
return true
}
}
if msgMap, ok := errResp.Error.Message.(map[string]interface{}); ok {
if errStr, ok := msgMap["error"].(string); ok {
if strings.Contains(errStr, "Vector store with id=") && strings.Contains(errStr, "not found") {
return true
}
}
}
// Check Detail.Error field for LiteLLM proxy error format
if errResp.Detail.Error != "" {
if strings.Contains(errResp.Detail.Error, "vector store not found") {
return true
}
}
return false
}
// handleVectorStoreAPIResponse handles API responses specifically for vector store operations
func handleVectorStoreAPIResponse(resp *http.Response, result interface{}, client *Client) error {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("vector_store_not_found")
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
var errResp ErrorResponse
if err := json.Unmarshal(bodyBytes, &errResp); err == nil {
if isVectorStoreNotFoundError(errResp) {
return fmt.Errorf("vector_store_not_found")
}
}
return fmt.Errorf("API request failed: Status: %s, Response: %s",
resp.Status, client.redactSensitiveData(string(bodyBytes)))
}
if result != nil {
if err := json.Unmarshal(bodyBytes, result); err != nil {
return fmt.Errorf("failed to parse response: %v", err)
}
}
return nil
}
// writeOnlyString returns the config value of a write-only string attribute.
// Terraform never stores write-only values in plan or state, so d.Get sees
// null in a real run; the raw config is the only place the value lives.
func writeOnlyString(d *schema.ResourceData, attr string) string {
raw, diags := d.GetRawConfigAt(cty.GetAttrPath(attr))
if !diags.HasError() && raw.IsKnown() && !raw.IsNull() && raw.Type() == cty.String {
return raw.AsString()
}
v, _ := d.Get(attr).(string)
return v
}