Go CLI: admin model framework (#16252)

This commit is contained in:
Jin Hai
2026-06-23 16:57:05 +08:00
committed by GitHub
parent 2362210caf
commit dec2ce4a60
12 changed files with 1279 additions and 355 deletions

View File

@@ -323,6 +323,190 @@ func (c *CLI) CommonShowProviderCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// CommonShowProviderInstanceCommand shows details of a specific instance
func (c *CLI) CommonShowProviderInstanceCommand(cmd *Command) (ResponseIf, error) {
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to show instance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to show instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("failed to show instance: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// CommonShowProviderInstanceBalanceCommand shows balance of a specific instance
func (c *CLI) CommonShowProviderInstanceBalanceCommand(cmd *Command) (ResponseIf, error) {
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/balance", providerName, instanceName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/balance", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to show instance balance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to show instance balance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("failed to show instance balance: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// CommonListProviderInstances lists all instances of a provider
// LIST INSTANCES FROM PROVIDER <name>
func (c *CLI) CommonListProviderInstances(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances", providerName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances", providerName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to list instances: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list instances: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list instances failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) CommonListInstanceModels(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance_name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/models", providerName, instanceName)
resp, err = c.AdminServerClient.Request("GET", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/models", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("GET", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to list instance models: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list instance models: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("failed to list instance models: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) CommonListModelsCommand(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
@@ -452,6 +636,284 @@ func (c *CLI) CommonShowProviderModelCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
func (c *CLI) CommonCheckProviderWithKey(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok || providerName == "" {
return nil, fmt.Errorf("provider name not provided")
}
region, ok := cmd.Params["region"].(string)
if !ok || region == "" {
return nil, fmt.Errorf("region not provided")
}
apiKey, ok := cmd.Params["api_key"].(string)
if !ok {
return nil, fmt.Errorf("api_key not provided")
}
baseURL, _ := cmd.Params["base_url"].(string)
var apiKeyValue interface{}
if apiKey != "" {
apiKeyValue = apiKey
} else {
apiKeyValue = nil
}
payload := map[string]interface{}{
"region": region,
"api_key": apiKeyValue,
}
if baseURL != "" {
payload["base_url"] = baseURL
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/connection", providerName)
resp, err = c.AdminServerClient.Request("POST", endPoint, "web", nil, payload)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/connection", providerName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("POST", endPoint, "web", nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to check provider connection with key: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to check provider connection: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
func (c *CLI) CommonCheckProviderConnection(cmd *Command) (ResponseIf, error) {
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/connection", providerName, instanceName)
resp, err = c.AdminServerClient.Request("POST", endPoint, "web", nil, nil)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/connection", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("POST", endPoint, "web", nil, nil)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to check provider connection: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to check provider connection: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
// AlterProviderInstanceCommand alters a provider instance
func (c *CLI) CommonAlterProviderInstanceCommand(cmd *Command) (ResponseIf, error) {
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
payload := map[string]interface{}{}
newName, ok := cmd.Params["new_model_name"].(string)
if ok {
payload["model_name"] = newName
}
newAPIKey, ok := cmd.Params["new_api_key"].(string)
if ok {
payload["api_key"] = newAPIKey
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.AdminServerClient.Request("PUT", endPoint, "web", nil, payload)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s", providerName, instanceName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("PUT", endPoint, "web", nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to alter instance: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to alter instance: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
func (c *CLI) CommonEnableOrDisableModel(cmd *Command, status string) (ResponseIf, error) {
modelName, ok := cmd.Params["model_name"].(string)
if !ok {
return nil, fmt.Errorf("model name not provided")
}
instanceName, ok := cmd.Params["instance_name"].(string)
if !ok {
return nil, fmt.Errorf("instance name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider name not provided")
}
payload := map[string]interface{}{
"status": status,
}
var resp *Response
var err error
var endPoint string
switch c.Config.CLIMode {
case AdminMode:
endPoint = fmt.Sprintf("/admin/providers/%s/instances/%s/models/%s", providerName, instanceName, modelName)
resp, err = c.AdminServerClient.Request("PATCH", endPoint, "web", nil, payload)
case APIMode:
endPoint = fmt.Sprintf("/providers/%s/instances/%s/models/%s", providerName, instanceName, modelName)
resp, err = c.APIServerClientMap[c.Config.APIClientConfig.CurrentAPIServer].Request("PATCH", endPoint, "web", nil, payload)
default:
return nil, fmt.Errorf("invalid server type")
}
if err != nil {
return nil, fmt.Errorf("failed to enable/disable model: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to enable/disable model: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
switch c.Config.CLIMode {
case AdminMode:
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
case APIMode:
var result SimpleResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("check provider connection failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
default:
return nil, fmt.Errorf("invalid server type")
}
}
func (c *CLI) SetDefaultModel(cmd *Command) (ResponseIf, error) {
modelType, ok := cmd.Params["model_type"].(string)