diff --git a/server/apiv2/handlers/tso_keyspace_group.go b/server/apiv2/handlers/tso_keyspace_group.go index 0e8c039d521..f5df812e428 100644 --- a/server/apiv2/handlers/tso_keyspace_group.go +++ b/server/apiv2/handlers/tso_keyspace_group.go @@ -135,6 +135,12 @@ func GetKeyspaceGroups(c *gin.Context) { c.IndentedJSON(http.StatusOK, kgs) } +// GetKeyspaceGroupPrimaryResponse defines the response for getting primary node of keyspace group. +type GetKeyspaceGroupPrimaryResponse struct { + ID uint32 `json:"id"` + Primary string `json:"primary"` +} + // GetKeyspaceGroupByID gets keyspace group by ID. func GetKeyspaceGroupByID(c *gin.Context) { id, err := validateKeyspaceGroupID(c) @@ -150,13 +156,17 @@ func GetKeyspaceGroupByID(c *gin.Context) { return } - if c.Query("get_param") == "primary" { + fields := c.Query("fields") // split by comma if need to add more fields + if fields == "primary" { primary, err := manager.GetKeyspaceGroupPrimaryByID(id) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, err.Error()) return } - c.JSON(http.StatusOK, primary) + c.JSON(http.StatusOK, &GetKeyspaceGroupPrimaryResponse{ + ID: id, + Primary: primary, + }) return } diff --git a/tests/pdctl/keyspace/keyspace_group_test.go b/tests/pdctl/keyspace/keyspace_group_test.go index 50da5a3fd77..2ec200b0493 100644 --- a/tests/pdctl/keyspace/keyspace_group_test.go +++ b/tests/pdctl/keyspace/keyspace_group_test.go @@ -504,9 +504,9 @@ func TestShowKeyspaceGroupPrimary(t *testing.T) { args := []string{"-u", pdAddr, "keyspace-group", "primary", defaultKeyspaceGroupID} output, err := pdctl.ExecuteCommand(cmd, args...) re.NoError(err) - addr := strings.ReplaceAll(string(output), "\"", "") - addr = strings.ReplaceAll(addr, "\n", "") - return s1.GetAddr() == addr || s2.GetAddr() == addr + var resp handlers.GetKeyspaceGroupPrimaryResponse + json.Unmarshal(output, &resp) + return s1.GetAddr() == resp.Primary || s2.GetAddr() == resp.Primary }) // split keyspace group. @@ -536,9 +536,9 @@ func TestShowKeyspaceGroupPrimary(t *testing.T) { args := []string{"-u", pdAddr, "keyspace-group", "primary", "1"} output, err := pdctl.ExecuteCommand(cmd, args...) re.NoError(err) - addr := strings.ReplaceAll(string(output), "\"", "") - addr = strings.ReplaceAll(addr, "\n", "") - return s1.GetAddr() == addr || s2.GetAddr() == addr + var resp handlers.GetKeyspaceGroupPrimaryResponse + json.Unmarshal(output, &resp) + return s1.GetAddr() == resp.Primary || s2.GetAddr() == resp.Primary }) re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/keyspace/acceleratedAllocNodes")) diff --git a/tools/pd-ctl/pdctl/command/keyspace_group_command.go b/tools/pd-ctl/pdctl/command/keyspace_group_command.go index 3b75bdc9636..615251c31f3 100644 --- a/tools/pd-ctl/pdctl/command/keyspace_group_command.go +++ b/tools/pd-ctl/pdctl/command/keyspace_group_command.go @@ -357,7 +357,7 @@ func showKeyspaceGroupPrimaryCommandFunc(cmd *cobra.Command, args []string) { cmd.Printf("Failed to parse the keyspace group ID: %s\n", err) return } - r, err := doRequest(cmd, fmt.Sprintf("%s/%s?get_param=primary", keyspaceGroupsPrefix, args[0]), http.MethodGet, http.Header{}) + r, err := doRequest(cmd, fmt.Sprintf("%s/%s?fields=primary", keyspaceGroupsPrefix, args[0]), http.MethodGet, http.Header{}) if err != nil { cmd.Printf("Failed to get the keyspace group primary information: %s\n", err) return