Go CLI: admin list configs (#16221)

### What problem does this PR solve?

- list configs;

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

---------

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-06-22 08:19:23 +08:00
committed by GitHub
parent 5039f46999
commit 760229d917
11 changed files with 689 additions and 804 deletions

View File

@@ -191,8 +191,8 @@ func (c *CLI) AdminCreateRoleCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// DropRole deletes the role (admin mode only)
func (c *CLI) DropRole(cmd *Command) (ResponseIf, error) {
// AdminDropRoleCommand deletes the role (admin mode only)
func (c *CLI) AdminDropRoleCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
@@ -211,7 +211,7 @@ func (c *CLI) DropRole(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("failed to drop role: HTTP %d, body: %s", resp.StatusCode, string(resp.Body))
}
var result SimpleResponse
var result CommonDataResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("drop role failed: invalid JSON (%w)", err)
@@ -224,8 +224,8 @@ func (c *CLI) DropRole(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// AlterRole alters the role rights (admin mode only)
func (c *CLI) AlterRole(cmd *Command) (ResponseIf, error) {
// AdminAlterRole alters the role rights (admin mode only)
func (c *CLI) AdminAlterRole(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
@@ -428,8 +428,8 @@ func (c *CLI) AdminCreateUserAPIKeyCommand(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// ActivateUser activates or deactivates a user (admin mode only)
func (c *CLI) ActivateUser(cmd *Command) (ResponseIf, error) {
// AdminActivateUser activates or deactivates a user (admin mode only)
func (c *CLI) AdminActivateUser(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
@@ -478,8 +478,8 @@ func (c *CLI) ActivateUser(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// AlterUserPassword changes a user's password (admin mode only)
func (c *CLI) AlterUserPassword(cmd *Command) (ResponseIf, error) {
// AdminAlterUserPassword changes a user's password (admin mode only)
func (c *CLI) AdminAlterUserPassword(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
@@ -619,8 +619,8 @@ func normalizeVariableRows(rows []map[string]interface{}) {
}
}
// AdminListVariables lists all system variables (admin mode only).
func (c *CLI) AdminListVariables(cmd *Command) (ResponseIf, error) {
// AdminListVariablesCommand lists all system variables (admin mode only).
func (c *CLI) AdminListVariablesCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
@@ -648,6 +648,63 @@ func (c *CLI) AdminListVariables(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// AdminListConfigsCommand lists all system configs (admin mode only).
func (c *CLI) AdminListConfigsCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
resp, err := c.AdminServerClient.Request("GET", "/admin/configs", "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list configs: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list configs: 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 configs failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
normalizeVariableRows(result.Data)
result.Duration = resp.Duration
return &result, nil
}
// AdminListEnvironmentsCommand lists all system environments (admin mode only).
func (c *CLI) AdminListEnvironmentsCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
resp, err := c.AdminServerClient.Request("GET", "/admin/environments", "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list configs: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list configs: 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 configs failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
// AdminShowVariable shows system variables by exact name or name prefix (admin mode only).
func (c *CLI) AdminShowVariable(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
@@ -933,157 +990,6 @@ func (c *CLI) ListUserDatasets(cmd *Command) (ResponseIf, error) {
return nil, nil
}
// GrantPermission grants permission to a role (admin mode only)
func (c *CLI) GrantPermission(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
userName, ok := cmd.Params["user_name"].(string)
if !ok {
return nil, fmt.Errorf("user_name not provided")
}
encodedUserName := common.EncodeEmail(userName)
apiURL := fmt.Sprintf("/admin/users/%s/keys", encodedUserName)
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list tokens: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list tokens: 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 tokens failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
// Remove extra field from data
for _, item := range result.Data {
delete(item, "extra")
}
result.Duration = resp.Duration
return &result, nil
}
// RevokePermission revokes permission from a role (admin mode only)
func (c *CLI) RevokePermission(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
roleName, ok := cmd.Params["role_name"].(string)
if !ok {
return nil, fmt.Errorf("role_name not provided")
}
resource, ok := cmd.Params["resource"].(string)
if !ok {
return nil, fmt.Errorf("resource not provided")
}
actionsRaw, ok := cmd.Params["actions"].([]interface{})
if !ok {
return nil, fmt.Errorf("actions not provided")
}
actions := make([]string, 0, len(actionsRaw))
for _, action := range actionsRaw {
if actionStr, ok := action.(string); ok {
actions = append(actions, actionStr)
}
}
payload := map[string]interface{}{
"resource": resource,
"actions": actions,
}
resp, err := c.AdminServerClient.Request("DELETE", fmt.Sprintf("/admin/roles/%s/permission", roleName), "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to revoke permission: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to revoke permission: 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("revoke permission failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
// Remove extra field from data
for _, item := range result.Data {
delete(item, "extra")
}
result.Duration = resp.Duration
return &result, nil
}
// AlterUserRole alters user's role (admin mode only)
func (c *CLI) AlterUserRole(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
userName, ok := cmd.Params["user_name"].(string)
if !ok {
return nil, fmt.Errorf("user_name not provided")
}
roleName, ok := cmd.Params["role_name"].(string)
if !ok {
return nil, fmt.Errorf("role_name not provided")
}
payload := map[string]interface{}{
"role_name": roleName,
}
encodedUserName := common.EncodeEmail(userName)
apiURL := fmt.Sprintf("/admin/users/%s/role", encodedUserName)
resp, err := c.AdminServerClient.Request("PUT", apiURL, "admin", nil, payload)
if err != nil {
return nil, fmt.Errorf("failed to alter user role: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to alter user role: 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("alter user role failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
// Remove extra field from data
for _, item := range result.Data {
delete(item, "extra")
}
result.Duration = resp.Duration
return &result, nil
}
// ShowUserPermission shows user's permissions (admin mode only)
func (c *CLI) ShowUserPermission(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
@@ -1165,51 +1071,6 @@ func (c *CLI) GenerateAdminToken(cmd *Command) (ResponseIf, error) {
return &result, nil
}
// ListAdminTokens lists all API tokens for a user (admin mode only)
func (c *CLI) ListAdminTokens(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
userName, ok := cmd.Params["user_name"].(string)
if !ok {
return nil, fmt.Errorf("user_name not provided")
}
encodedUserName := common.EncodeEmail(userName)
apiURL := fmt.Sprintf("/admin/users/%s/keys", encodedUserName)
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list tokens: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list tokens: 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 tokens failed: invalid JSON (%w)", err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
// Remove extra field from data
for _, item := range result.Data {
delete(item, "dialog_id")
delete(item, "source")
delete(item, "update_date")
delete(item, "update_time")
delete(item, "create_time")
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) ListAdminTasks(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
@@ -2799,6 +2660,172 @@ func (c *CLI) AdminListUserKeysCommand(cmd *Command) (ResponseIf, error) {
return nil, fmt.Errorf("%s", result.Message)
}
// Remove extra field from data
for _, item := range result.Data {
delete(item, "dialog_id")
delete(item, "source")
delete(item, "update_date")
delete(item, "update_time")
delete(item, "create_time")
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) AdminListUserProvidersCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
userName, ok := cmd.Params["user_name"].(string)
if !ok {
return nil, fmt.Errorf("user_name not provided")
}
encodedUserName := common.EncodeEmail(userName)
apiURL := fmt.Sprintf("/admin/users/%s/providers", encodedUserName)
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list user %s providers: %w", userName, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list user %s providers: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list user %s providers failed: invalid JSON (%w)", userName, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) AdminListUserProviderInstancesCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
userName, ok := cmd.Params["user_name"].(string)
if !ok {
return nil, fmt.Errorf("user_name not provided")
}
providerName, ok := cmd.Params["provider_name"].(string)
if !ok {
return nil, fmt.Errorf("provider_name not provided")
}
encodedUserName := common.EncodeEmail(userName)
apiURL := fmt.Sprintf("/admin/users/%s/providers/%s/instances", encodedUserName, providerName)
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list user %s providers: %w", userName, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list user %s providers: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list user %s providers failed: invalid JSON (%w)", userName, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) AdminListUserProviderInstanceModelsCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
userName, ok := cmd.Params["user_name"].(string)
if !ok {
return nil, fmt.Errorf("user_name not provided")
}
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")
}
encodedUserName := common.EncodeEmail(userName)
apiURL := fmt.Sprintf("/admin/users/%s/providers/%s/instances/%s/models", encodedUserName, providerName, instanceName)
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list user %s provider instance models: %w", userName, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list user %s provider instance models: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list user %s provider instance models failed: invalid JSON (%w)", userName, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}
func (c *CLI) AdminListUserDefaultModelsCommand(cmd *Command) (ResponseIf, error) {
if c.Config.CLIMode != AdminMode || c.AdminServerClient.LoginToken == nil {
return nil, fmt.Errorf("this command is only allowed in ADMIN mode or already login")
}
userName, ok := cmd.Params["user_name"].(string)
if !ok {
return nil, fmt.Errorf("user_name not provided")
}
encodedUserName := common.EncodeEmail(userName)
apiURL := fmt.Sprintf("/admin/users/%s/default-models", encodedUserName)
resp, err := c.AdminServerClient.Request("GET", apiURL, "admin", nil, nil)
if err != nil {
return nil, fmt.Errorf("failed to list user %s default models: %w", userName, err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to list user %s default models: HTTP %d, body: %s", userName, resp.StatusCode, string(resp.Body))
}
var result CommonResponse
if err = json.Unmarshal(resp.Body, &result); err != nil {
return nil, fmt.Errorf("list user %s default models failed: invalid JSON (%w)", userName, err)
}
if result.Code != 0 {
return nil, fmt.Errorf("%s", result.Message)
}
result.Duration = resp.Duration
return &result, nil
}