Skip to content

Commit ef19db8

Browse files
committed
Fix some bugs at language level
1. change the method name from restartConsole to restartMonitor 2. avoid of make to define the variables that will be Unmarshaled 3. logging command would print key:val result 4. add more comments 5. change the this pointer name to self
1 parent 12dd953 commit ef19db8

File tree

6 files changed

+364
-333
lines changed

6 files changed

+364
-333
lines changed

api/node.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func (api *NodeApi) show(w http.ResponseWriter, req *http.Request) {
8181

8282
// change the logging state for one node
8383
func (api *NodeApi) put(w http.ResponseWriter, req *http.Request) {
84+
var resp []byte
85+
var err error
8486
vars := mux.Vars(req)
8587
plog.Debug(fmt.Sprintf("Receive %s request %s %v from %s.", req.Method, req.URL.Path, vars, req.RemoteAddr))
8688
if _, ok := req.URL.Query()["state"]; !ok {
@@ -90,10 +92,14 @@ func (api *NodeApi) put(w http.ResponseWriter, req *http.Request) {
9092
state := req.URL.Query()["state"][0]
9193
nodes := make([]string, 0, 1)
9294
nodes = append(nodes, vars["node"])
93-
nodeManager.SetConsoleState(nodes, state)
9495
plog.InfoNode(vars["node"], fmt.Sprintf("The console state has been changed to %s.", state))
96+
result := nodeManager.SetConsoleState(nodes, state)
97+
if resp, err = json.Marshal(result); err != nil {
98+
plog.HandleHttp(w, req, http.StatusInternalServerError, err.Error())
99+
return
100+
}
95101
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
96-
w.WriteHeader(http.StatusAccepted)
102+
fmt.Fprintf(w, "%s\n", resp)
97103
}
98104

99105
// change the logging state for nodes
@@ -107,7 +113,7 @@ func (api *NodeApi) bulkPut(w http.ResponseWriter, req *http.Request) {
107113
return
108114
}
109115
state := req.URL.Query()["state"][0]
110-
storNodes := make(map[string][]storage.Node, 0)
116+
var storNodes map[string][]storage.Node
111117
body, err := ioutil.ReadAll(req.Body)
112118
if err != nil {
113119
plog.HandleHttp(w, req, http.StatusInternalServerError, err.Error())
@@ -137,7 +143,7 @@ func (api *NodeApi) bulkPut(w http.ResponseWriter, req *http.Request) {
137143

138144
func (api *NodeApi) post(w http.ResponseWriter, req *http.Request) {
139145
plog.Debug(fmt.Sprintf("Receive %s request %s from %s.", req.Method, req.URL.Path, req.RemoteAddr))
140-
node := storage.NewNode()
146+
var node storage.Node
141147
body, err := ioutil.ReadAll(req.Body)
142148
if err != nil {
143149
plog.HandleHttp(w, req, http.StatusInternalServerError, err.Error())
@@ -147,7 +153,7 @@ func (api *NodeApi) post(w http.ResponseWriter, req *http.Request) {
147153
plog.HandleHttp(w, req, http.StatusInternalServerError, err.Error())
148154
return
149155
}
150-
if err := json.Unmarshal(body, node); err != nil {
156+
if err := json.Unmarshal(body, &node); err != nil {
151157
plog.HandleHttp(w, req, http.StatusUnprocessableEntity, err.Error())
152158
return
153159
}
@@ -159,7 +165,7 @@ func (api *NodeApi) post(w http.ResponseWriter, req *http.Request) {
159165
plog.HandleHttp(w, req, http.StatusBadRequest, "Driver is not defined")
160166
return
161167
}
162-
httpcode, msg := nodeManager.PostNode(node)
168+
httpcode, msg := nodeManager.PostNode(&node)
163169
if httpcode >= 400 {
164170
plog.HandleHttp(w, req, httpcode, msg)
165171
return
@@ -171,7 +177,7 @@ func (api *NodeApi) post(w http.ResponseWriter, req *http.Request) {
171177
func (api *NodeApi) bulkPost(w http.ResponseWriter, req *http.Request) {
172178
plog.Debug(fmt.Sprintf("Receive %s request %s from %s.", req.Method, req.URL.Path, req.RemoteAddr))
173179
var resp []byte
174-
nodes := make(map[string][]storage.Node)
180+
var nodes map[string][]storage.Node
175181
body, err := ioutil.ReadAll(req.Body)
176182
if err != nil {
177183
plog.HandleHttp(w, req, http.StatusInternalServerError, err.Error())

common/utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ const (
2727
ACTION_NIL = -1
2828

2929
Maxint32 = 1<<31 - 1
30+
31+
RESULT_DELETED = "Deleted"
32+
RESULT_ACCEPTED = "Accepted"
33+
RESULT_UPDATED = "Updated"
34+
RESULT_UNCHANGED = "Unchanged"
35+
RESULT_CREATED = "Created"
3036
)
3137

3238
var (

console/cli.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ func KeyValueToMap(value string, sep string) (map[string]interface{}, error) {
5656
return m, nil
5757
}
5858

59+
func printResult(m interface{}) {
60+
for k, v := range m.(map[string]interface{}) {
61+
fmt.Printf("%s: %s\n", k, v.(string))
62+
}
63+
}
64+
5965
type nodeHost struct {
6066
name string
6167
host string
@@ -191,11 +197,12 @@ func (c *CongoCli) logging(cmd *cobra.Command, args []string) {
191197
fmt.Fprintf(os.Stderr, "Usage: congo logging <node> on/off \n")
192198
os.Exit(1)
193199
}
194-
_, err := congo.Logging(args[0], args[1])
200+
ret, err := congo.Logging(args[0], args[1])
195201
if err != nil {
196202
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
197203
os.Exit(1)
198204
}
205+
printResult(ret)
199206
}
200207

201208
func (c *CongoCli) deleteCommand() *cobra.Command {

0 commit comments

Comments
 (0)