Skip to content

Commit

Permalink
fix set namespace in pd-ctl
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Aug 23, 2019
1 parent 864c221 commit 1bebe1b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
5 changes: 5 additions & 0 deletions tests/pdctl/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package config_test

import (
"encoding/json"
"strings"
"testing"

"github.com/coreos/go-semver/semver"
Expand Down Expand Up @@ -96,6 +97,10 @@ func (s *configTestSuite) TestConfig(c *C) {
c.Assert(json.Unmarshal(output, &clusterVersion), IsNil)
c.Assert(clusterVersion, DeepEquals, svr.GetClusterVersion())

args2 = []string{"-u", pdAddr, "config", "set", "namespace", "ts1", "region-schedule-limit", "128"}
_, output, err = pdctl.ExecuteCommandC(cmd, args2...)
c.Assert(err, IsNil)
c.Assert(strings.Contains(string(output), "Failed"), IsTrue)
// config show namespace <name> && config set namespace <type> <key> <value>
args = []string{"-u", pdAddr, "table_ns", "create", "ts1"}
_, _, err = pdctl.ExecuteCommandC(cmd, args...)
Expand Down
2 changes: 1 addition & 1 deletion tools/pd-ctl/pdctl/command/config_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func setNamespaceConfigCommandFunc(cmd *cobra.Command, args []string) {
prefix := path.Join(namespacePrefix, name)
err := postConfigDataWithPath(cmd, opt, val, prefix)
if err != nil {
cmd.Printf("Failed to set namespace:%s config: %s\n", name, err)
cmd.Printf("Failed to set namespace: %s error: %s\n", name, err)
return
}
cmd.Println("Success!")
Expand Down
32 changes: 20 additions & 12 deletions tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ func doRequest(cmd *cobra.Command, prefix string, method string,
o(b)
}
var resp string
var err error
tryURLs(cmd, func(endpoint string) error {

endpoints := getEndpoints(cmd)
err := tryURLs(cmd, endpoints, func(endpoint string) error {
var err error
url := endpoint + "/" + prefix
if method == "" {
method = http.MethodGet
Expand Down Expand Up @@ -113,7 +115,7 @@ func dail(req *http.Request) (string, error) {
if err != nil {
return "", err
}
return fmt.Sprintf("[%d] %s", resp.StatusCode, msg), nil
return "", errors.Errorf("[%d] %s", resp.StatusCode, msg)
}

content, err := ioutil.ReadAll(resp.Body)
Expand All @@ -128,13 +130,8 @@ type DoFunc func(endpoint string) error

// tryURLs issues requests to each URL and tries next one if there
// is an error
func tryURLs(cmd *cobra.Command, f DoFunc) {
addrs, err := cmd.Flags().GetString("pd")
if err != nil {
cmd.Println("get pd address failed, should set flag with '-u'")
os.Exit(1)
}
endpoints := strings.Split(addrs, ",")
func tryURLs(cmd *cobra.Command, endpoints []string, f DoFunc) error {
var err error
for _, endpoint := range endpoints {
var u *url.URL
u, err = url.Parse(endpoint)
Expand All @@ -156,9 +153,19 @@ func tryURLs(cmd *cobra.Command, f DoFunc) {
}
break
}
if len(endpoints) > 1 && err != nil {
err = errors.Errorf("after trying all endpoints, no endpoint is available, the last error we met: %s", err)
}
return err
}

func getEndpoints(cmd *cobra.Command) []string {
addrs, err := cmd.Flags().GetString("pd")
if err != nil {
cmd.Println("after trying all endpoints, no endpoint is available, the last error we met:", err)
cmd.Println("get pd address failed, should set flag with '-u'")
os.Exit(1)
}
return strings.Split(addrs, ",")
}

func printResponseError(r *http.Response) error {
Expand All @@ -174,7 +181,8 @@ func postJSON(cmd *cobra.Command, prefix string, input map[string]interface{}) {
return
}

tryURLs(cmd, func(endpoint string) error {
endpoints := getEndpoints(cmd)
tryURLs(cmd, endpoints, func(endpoint string) error {
url := endpoint + "/" + prefix
r, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
Expand Down

0 comments on commit 1bebe1b

Please sign in to comment.