Skip to content

Commit

Permalink
tools: fix set namespace in pd-ctl (#1701)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx authored and sre-bot committed Aug 29, 2019
1 parent 14b91e8 commit 15e2674
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 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
54 changes: 30 additions & 24 deletions tools/pd-ctl/pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package command
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -77,8 +76,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 +114,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 +129,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,15 +152,19 @@ func tryURLs(cmd *cobra.Command, f DoFunc) {
}
break
}
if err != nil {
cmd.Println("after trying all endpoints, no endpoint is available, the last error we met:", err)
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 printResponseError(r *http.Response) error {
fmt.Printf("[%d]:", r.StatusCode)
_, err := io.Copy(os.Stdout, r.Body)
return err
func getEndpoints(cmd *cobra.Command) []string {
addrs, err := cmd.Flags().GetString("pd")
if err != nil {
cmd.Println("get pd address failed, should set flag with '-u'")
os.Exit(1)
}
return strings.Split(addrs, ",")
}

func postJSON(cmd *cobra.Command, prefix string, input map[string]interface{}) {
Expand All @@ -174,23 +174,29 @@ func postJSON(cmd *cobra.Command, prefix string, input map[string]interface{}) {
return
}

tryURLs(cmd, func(endpoint string) error {
endpoints := getEndpoints(cmd)
err = tryURLs(cmd, endpoints, func(endpoint string) error {
var msg []byte
var r *http.Response
url := endpoint + "/" + prefix
r, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data))
r, err = dialClient.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
if err := printResponseError(r); err != nil {
cmd.Println(err)
msg, err = ioutil.ReadAll(r.Body)
if err != nil {
return err
}
return nil
return errors.Errorf("[%d] %s", r.StatusCode, msg)
}
cmd.Println("success!")
return nil
})

if err != nil {
cmd.Println(err)
}
cmd.Println("Success!")
}

// UsageTemplate will used to generate a help information
Expand Down

0 comments on commit 15e2674

Please sign in to comment.