This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks-controller.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 2de5972  Return its id when creating a new node (#307)
2de5972 is described below

commit 2de5972a0ff8c567c594247c8ceb3909f371dd47
Author: Raphael <[email protected]>
AuthorDate: Tue May 13 20:53:50 2025 +0800

    Return its id when creating a new node (#307)
    
    Co-authored-by: git-hulk <[email protected]>
---
 server/api/node.go      |  4 ++--
 server/api/node_test.go |  7 +++++++
 store/cluster.go        |  4 ++--
 store/cluster_shard.go  | 10 +++++-----
 4 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/server/api/node.go b/server/api/node.go
index 878798b..4407108 100644
--- a/server/api/node.go
+++ b/server/api/node.go
@@ -55,7 +55,7 @@ func (handler *NodeHandler) Create(c *gin.Context) {
                req.Role = store.RoleSlave
        }
        shardIndex, _ := strconv.Atoi(c.Param("shard"))
-       err := cluster.AddNode(shardIndex, req.Addr, req.Role, req.Password)
+       newNode, err := cluster.AddNode(shardIndex, req.Addr, req.Role, 
req.Password)
        if err != nil {
                helper.ResponseError(c, err)
                return
@@ -64,7 +64,7 @@ func (handler *NodeHandler) Create(c *gin.Context) {
                helper.ResponseError(c, err)
                return
        }
-       helper.ResponseCreated(c, nil)
+       helper.ResponseCreated(c, newNode.ID())
 }
 
 func (handler *NodeHandler) Remove(c *gin.Context) {
diff --git a/server/api/node_test.go b/server/api/node_test.go
index d216f81..8629185 100644
--- a/server/api/node_test.go
+++ b/server/api/node_test.go
@@ -68,6 +68,13 @@ func TestNodeBasics(t *testing.T) {
                require.Equal(t, http.StatusOK, recorder.Code)
                handler.Create(ctx)
                require.Equal(t, expectedStatusCode, recorder.Code)
+               if expectedStatusCode == http.StatusCreated {
+                       var rsp struct {
+                               Data string `json:"data"`
+                       }
+                       require.NoError(t, 
json.Unmarshal(recorder.Body.Bytes(), &rsp))
+                       require.Len(t, rsp.Data, store.NodeIDLen)
+               }
        }
 
        runRemove := func(t *testing.T, nodeID string, expectedStatusCode int) {
diff --git a/store/cluster.go b/store/cluster.go
index 8dff6ed..405a475 100644
--- a/store/cluster.go
+++ b/store/cluster.go
@@ -117,9 +117,9 @@ func (cluster *Cluster) GetShard(shardIndex int) (*Shard, 
error) {
        return cluster.Shards[shardIndex], nil
 }
 
-func (cluster *Cluster) AddNode(shardIndex int, addr, role, password string) 
error {
+func (cluster *Cluster) AddNode(shardIndex int, addr, role, password string) 
(*ClusterNode, error) {
        if shardIndex < 0 || shardIndex >= len(cluster.Shards) {
-               return consts.ErrIndexOutOfRange
+               return nil, consts.ErrIndexOutOfRange
        }
        return cluster.Shards[shardIndex].addNode(addr, role, password)
 }
diff --git a/store/cluster_shard.go b/store/cluster_shard.go
index 69ffd4a..bfb60e3 100644
--- a/store/cluster_shard.go
+++ b/store/cluster_shard.go
@@ -99,22 +99,22 @@ func (shard *Shard) IsServicing() bool {
        return shard.IsMigrating()
 }
 
-func (shard *Shard) addNode(addr, role, password string) error {
+func (shard *Shard) addNode(addr, role, password string) (*ClusterNode, error) 
{
        if role != RoleMaster && role != RoleSlave {
-               return fmt.Errorf("%w: role", consts.ErrInvalidArgument)
+               return nil, fmt.Errorf("%w: role", consts.ErrInvalidArgument)
        }
        for _, node := range shard.Nodes {
                if node.Addr() == addr {
-                       return consts.ErrAlreadyExists
+                       return nil, consts.ErrAlreadyExists
                }
        }
        if role == RoleMaster && len(shard.Nodes) > 0 {
-               return fmt.Errorf("master node %w", consts.ErrAlreadyExists)
+               return nil, fmt.Errorf("master node %w", 
consts.ErrAlreadyExists)
        }
        node := NewClusterNode(addr, password)
        node.SetRole(role)
        shard.Nodes = append(shard.Nodes, node)
-       return nil
+       return node, nil
 }
 
 func (shard *Shard) IsMigrating() bool {

Reply via email to