Skip to content

Commit 774c10c

Browse files
committed
[cluster] support rename cluster
1 parent 77746ff commit 774c10c

File tree

7 files changed

+119
-8
lines changed

7 files changed

+119
-8
lines changed

cli/command/cluster/cmd.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,23 @@ import (
3131
"github.com/spf13/cobra"
3232
)
3333

34-
func NewClusterCommand(curveadm *cli.DingoAdm) *cobra.Command {
34+
func NewClusterCommand(dingoadm *cli.DingoAdm) *cobra.Command {
3535
cmd := &cobra.Command{
3636
Use: "cluster",
3737
Short: "Manage clusters",
3838
Args: cliutil.NoArgs,
39-
RunE: cliutil.ShowHelp(curveadm.Err()),
39+
RunE: cliutil.ShowHelp(dingoadm.Err()),
4040
}
4141

4242
cmd.AddCommand(
43-
NewAddCommand(curveadm),
44-
NewCheckoutCommand(curveadm),
45-
NewListCommand(curveadm),
46-
NewRemoveCommand(curveadm),
43+
NewAddCommand(dingoadm),
44+
NewCheckoutCommand(dingoadm),
45+
NewListCommand(dingoadm),
46+
NewRemoveCommand(dingoadm),
4747
// TODO(P1): enable export
4848
//NewExportCommand(curveadm),
49-
NewImportCommand(curveadm),
49+
NewImportCommand(dingoadm),
50+
NewRenameCommand(dingoadm),
5051
)
5152
return cmd
5253
}

cli/command/cluster/rename.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2025 dingodb.com Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cluster
18+
19+
import (
20+
"github.com/dingodb/dingoadm/cli/cli"
21+
"github.com/dingodb/dingoadm/internal/errno"
22+
tui "github.com/dingodb/dingoadm/internal/tui/common"
23+
cliutil "github.com/dingodb/dingoadm/internal/utils"
24+
log "github.com/dingodb/dingoadm/pkg/log/glg"
25+
"github.com/spf13/cobra"
26+
)
27+
28+
type renameOptions struct {
29+
clusterOldName string
30+
clusterNewName string
31+
force bool
32+
}
33+
34+
func NewRenameCommand(dingoadm *cli.DingoAdm) *cobra.Command {
35+
var options renameOptions
36+
37+
cmd := &cobra.Command{
38+
Use: "rename CLUSTER [OPTIONS]",
39+
Short: "Rename cluster",
40+
Args: cliutil.ExactArgs(2),
41+
RunE: func(cmd *cobra.Command, args []string) error {
42+
options.clusterOldName = args[0]
43+
options.clusterNewName = args[1]
44+
45+
return runRename(dingoadm, options)
46+
},
47+
DisableFlagsInUseLine: true,
48+
}
49+
50+
flags := cmd.Flags()
51+
flags.BoolVarP(&options.force, "force", "f", false, "Remove cluster by force")
52+
53+
return cmd
54+
}
55+
56+
func runRename(dingoadm *cli.DingoAdm, options renameOptions) error {
57+
// 1) get cluster by name
58+
storage := dingoadm.Storage()
59+
clusterOldName := options.clusterOldName
60+
clusterNewName := options.clusterNewName
61+
clusters, err := storage.GetClusters(clusterOldName) // Get all clusters
62+
if err != nil {
63+
log.Error("Get cluster failed",
64+
log.Field("error", err))
65+
return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err)
66+
} else if len(clusters) == 0 {
67+
return errno.ERR_CLUSTER_NOT_FOUND.
68+
F("cluster name: %s", clusterOldName)
69+
}
70+
71+
// rename cluster name
72+
if options.force {
73+
dingoadm.WriteOut(tui.PromptRenameCluster(clusterOldName, clusterNewName))
74+
} else {
75+
if !tui.ConfirmYes(tui.PromptRenameCluster(clusterOldName, clusterNewName)) {
76+
dingoadm.WriteOut(tui.PromptCancelOpetation("rename cluster"))
77+
return errno.ERR_CANCEL_OPERATION
78+
}
79+
}
80+
81+
if err := dingoadm.Storage().RenameClusterName(clusterOldName, clusterNewName); err != nil {
82+
return errno.ERR_RENAME_CLUSTER_FAILED.E(err)
83+
}
84+
85+
// 3) print success prompt
86+
dingoadm.WriteOutln("Rename cluster '%s' to '%s'", clusterOldName, clusterNewName)
87+
return nil
88+
}

internal/errno/errno.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ var (
215215
ERR_DELETE_CLUSTER_FAILED = EC(111005, "execute SQL failed which delete cluster")
216216
ERR_UPDATE_CLUSTER_TOPOLOGY_FAILED = EC(111006, "execute SQL failed which update cluster topology")
217217
ERR_UPDATE_CLUSTER_POOL_FAILED = EC(111007, "execute SQL failed which update cluster pool")
218+
ERR_RENAME_CLUSTER_FAILED = EC(111008, "execute SQL failed which rename cluster")
218219
// 112: database/SQL (execute SQL statement: containers table)
219220
ERR_INSERT_SERVICE_CONTAINER_ID_FAILED = EC(112000, "execute SQL failed which insert service container id")
220221
ERR_SET_SERVICE_CONTAINER_ID_FAILED = EC(112001, "execute SQL failed which set service container id")

internal/storage/sql.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ var (
153153

154154
// set cluster pool
155155
SetClusterPool = `UPDATE clusters SET topology = ?, pool = ? WHERE id = ?`
156+
157+
// rename cluster name
158+
RenameClusterName = `UPDATE clusters SET name = ? WHERE name = ?`
156159
)
157160

158161
// service

internal/storage/storage.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ func (s *Storage) DeleteCluster(name string) error {
169169
return s.write(DeleteCluster, name)
170170
}
171171

172+
// RenameClusterName update cluster name, and return error if new name exists
173+
func (s *Storage) RenameClusterName(oldName, newName string) error {
174+
clusters, err := s.GetClusters(newName)
175+
if err != nil {
176+
return err
177+
} else if len(clusters) > 0 {
178+
return fmt.Errorf("cluster name already exists: %s", newName)
179+
}
180+
return s.write(RenameClusterName, newName, oldName)
181+
}
182+
172183
func (s *Storage) getClusters(query string, args ...interface{}) ([]Cluster, error) {
173184
result, err := s.db.Query(query, args...)
174185
if err != nil {

internal/tui/common/prompt.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ func PromptRemoveCluster(clusterName string) string {
125125
return prompt.Build()
126126
}
127127

128+
func PromptRenameCluster(clusterOldName string, clusterNewName string) string {
129+
prompt := NewPrompt(color.YellowString(PROMPT_WARNING) + DEFAULT_CONFIRM_PROMPT)
130+
prompt.data["warning"] = fmt.Sprintf("WARNING: cluster '%s' will be renamed to '%s'",
131+
clusterOldName, clusterNewName)
132+
return prompt.Build()
133+
}
134+
128135
func PromptFormat() string {
129136
return color.YellowString(PROMPT_FORMAT)
130137
}

pkg/variable/variables.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,6 @@ func (vars *Variables) Rendering(s string) (string, error) {
153153

154154
func (vars *Variables) Debug() {
155155
for _, v := range vars.m {
156-
log.Info("Variable", log.Field(v.Name, v.Value))
156+
log.Debug("Variable", log.Field(v.Name, v.Value))
157157
}
158158
}

0 commit comments

Comments
 (0)