git-hulk commented on code in PR #232:
URL: 
https://github.com/apache/kvrocks-controller/pull/232#discussion_r1887032349


##########
server/api/raft.go:
##########
@@ -0,0 +1,71 @@
+package api
+
+import (
+       "fmt"
+       "strings"
+
+       "github.com/apache/kvrocks-controller/consts"
+       "github.com/apache/kvrocks-controller/logger"
+       "github.com/apache/kvrocks-controller/server/helper"
+       "github.com/apache/kvrocks-controller/store/engine/raft"
+       "go.uber.org/zap"
+
+       "github.com/gin-gonic/gin"
+)
+
+const (
+       OperationAdd    = "add"
+       OperationRemove = "remove"
+)
+
+type RaftHandler struct{}
+
+type MemberRequest struct {
+       ID        uint64 `json:"id" validate:"required,gt=0"`
+       Operation string `json:"operation" validate:"required"`
+       Peer      string `json:"peer"`
+}
+
+func (r *MemberRequest) validate() error {
+       r.Operation = strings.ToLower(r.Operation)
+       if r.Operation != OperationAdd && r.Operation != OperationRemove {
+               return fmt.Errorf("operation must be one of [%s]",
+                       strings.Join([]string{OperationAdd, OperationRemove}, 
","))
+       }
+       if r.Operation == OperationAdd && r.Peer == "" {
+               return fmt.Errorf("peer should NOT be empty")
+       }
+       return nil
+}
+
+func (handler *RaftHandler) ListPeers(c *gin.Context) {
+       raftNode, _ := c.MustGet(consts.ContextKeyRaftNode).(*raft.Node)
+       peers := raftNode.ListPeers()
+       helper.ResponseOK(c, gin.H{"peers": peers})
+}
+
+func (handler *RaftHandler) UpdatePeer(c *gin.Context) {
+       var req MemberRequest
+       if err := c.BindJSON(&req); err != nil {
+               helper.ResponseBadRequest(c, err)
+               return
+       }
+       if err := req.validate(); err != nil {
+               helper.ResponseBadRequest(c, err)
+               return
+       }
+
+       raftNode, _ := c.MustGet(consts.ContextKeyRaftNode).(*raft.Node)
+       var err error
+       if req.Operation == OperationAdd {
+               err = raftNode.AddPeer(c, req.ID, req.Peer)
+       } else {
+               err = raftNode.RemovePeer(c, req.ID)
+       }
+       if err != nil {
+               helper.ResponseError(c, err)
+       } else {
+               logger.Get().With(zap.Any("request", req)).Info("Update peer 
success")

Review Comment:
   @torwig yes, we intend to record the whole request since we do need to 
record all request fields including `operation`, `id` and `peer`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to