-
Notifications
You must be signed in to change notification settings - Fork 0
/
member_command.go
186 lines (171 loc) · 4.85 KB
/
member_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package command
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/spf13/cobra"
)
var (
membersPrefix = "pd/api/v1/members"
leaderMemberPrefix = "pd/api/v1/leader"
)
// NewMemberCommand return a member subcommand of rootCmd
func NewMemberCommand() *cobra.Command {
m := &cobra.Command{
Use: "member [leader|delete|leader_priority]",
Short: "show the pd member status",
Run: showMemberCommandFunc,
}
m.AddCommand(NewLeaderMemberCommand())
m.AddCommand(NewDeleteMemberCommand())
m.AddCommand(&cobra.Command{
Use: "leader_priority <member_name> <priority>",
Short: "set the member's priority to be elected as etcd leader",
Run: setLeaderPriorityFunc,
})
return m
}
// NewDeleteMemberCommand return a delete subcommand of memberCmd
func NewDeleteMemberCommand() *cobra.Command {
d := &cobra.Command{
Use: "delete <subcommand>",
Short: "delete a member",
}
d.AddCommand(&cobra.Command{
Use: "name <member_name>",
Short: "delete a member by name",
Run: deleteMemberByNameCommandFunc,
})
d.AddCommand(&cobra.Command{
Use: "id <member_id>",
Short: "delete a member by id",
Run: deleteMemberByIDCommandFunc,
})
return d
}
// NewLeaderMemberCommand return a leader subcommand of memberCmd
func NewLeaderMemberCommand() *cobra.Command {
d := &cobra.Command{
Use: "leader <subcommand>",
Short: "leader commands",
}
d.AddCommand(&cobra.Command{
Use: "show",
Short: "show the leader member status",
Run: getLeaderMemberCommandFunc,
})
d.AddCommand(&cobra.Command{
Use: "resign",
Short: "resign current leader pd's leadership",
Run: resignLeaderCommandFunc,
})
d.AddCommand(&cobra.Command{
Use: "transfer <member_name>",
Short: "transfer leadership to another pd",
Run: transferPDLeaderCommandFunc,
})
return d
}
func showMemberCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, membersPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get pd members: %s\n", err)
return
}
fmt.Println(r)
}
func deleteMemberByNameCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Usage: member delete <member_name>")
return
}
prefix := membersPrefix + "/name/" + args[0]
_, err := doRequest(cmd, prefix, http.MethodDelete)
if err != nil {
fmt.Printf("Failed to delete member %s: %s\n", args[0], err)
return
}
fmt.Println("Success!")
}
func deleteMemberByIDCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Usage: member delete id <member_id>")
return
}
prefix := membersPrefix + "/id/" + args[0]
_, err := doRequest(cmd, prefix, http.MethodDelete)
if err != nil {
fmt.Printf("Failed to delete member %s: %s\n", args[0], err)
return
}
fmt.Println("Success!")
}
func getLeaderMemberCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, leaderMemberPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get the leader of pd members: %s\n", err)
return
}
fmt.Println(r)
}
func resignLeaderCommandFunc(cmd *cobra.Command, args []string) {
prefix := leaderMemberPrefix + "/resign"
_, err := doRequest(cmd, prefix, http.MethodPost)
if err != nil {
fmt.Printf("Failed to resign: %s\n", err)
return
}
fmt.Println("Success!")
}
func transferPDLeaderCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("Usage: leader transfer <member_name>")
return
}
prefix := leaderMemberPrefix + "/transfer/" + args[0]
_, err := doRequest(cmd, prefix, http.MethodPost)
if err != nil {
fmt.Printf("Failed to trasfer leadership: %s\n", err)
return
}
fmt.Println("Success!")
}
func setLeaderPriorityFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("Usage: leader_priority <member_name> <priority>")
return
}
prefix := membersPrefix + "/name/" + args[0]
priority, err := strconv.ParseFloat(args[1], 64)
if err != nil {
fmt.Printf("failed to parse priority: %v\n", err)
return
}
data := map[string]interface{}{"leader-priority": priority}
reqData, _ := json.Marshal(data)
req, err := getRequest(cmd, prefix, http.MethodPost, "application/json", bytes.NewBuffer(reqData))
if err != nil {
fmt.Printf("failed to set leader priority: %v\n", err)
return
}
_, err = dail(req)
if err != nil {
fmt.Printf("failed to set leader priority: %v\n", err)
return
}
fmt.Println("Success!")
}