diff --git a/.changelog/18625.txt b/.changelog/18625.txt index 763b47c1eb3a..8474cac8dc1d 100644 --- a/.changelog/18625.txt +++ b/.changelog/18625.txt @@ -1,4 +1,5 @@ ```release-note:improvement -Adds flag -append-filename (which works on two values version and dc) to consul snapshot save command. -Adding the flag -append-filename version,dc will add consul version and consul datacenter in the file name given in the snapshot save command before the file extension. +Adds flag -append-filename (which works on values version, dc, node and status) to consul snapshot save command. +Adding the flag -append-filename version,dc,node,status will add consul version, consul datacenter, node name and leader/follower +(status) in the file name given in the snapshot save command before the file extension. ``` diff --git a/command/snapshot/save/snapshot_save.go b/command/snapshot/save/snapshot_save.go index 99350a947b3f..c9ed61b281f8 100644 --- a/command/snapshot/save/snapshot_save.go +++ b/command/snapshot/save/snapshot_save.go @@ -36,7 +36,7 @@ type cmd struct { func (c *cmd) getAppendFileNameFlag() *flag.FlagSet { fs := flag.NewFlagSet("", flag.ContinueOnError) fs.Var(&c.appendFileNameFlag, "append-filename", "Append filename flag takes two possible values. "+ - "1. version, 2. dc. It appends consul version and datacenter to filename given in command") + "1. version, 2. dc. 3. node 5. status. It appends consul version and datacenter to filename given in command") return fs } @@ -99,6 +99,29 @@ func (c *cmd) Run(args []string) int { } } + if slices.Contains(appendFileNameFlags, "node") { + if config, ok := agentSelfResponse["Config"]; ok { + if nodeName, ok := config["NodeName"]; ok { + fileNameWithoutExt = fileNameWithoutExt + "-" + nodeName.(string) + } + } + } + + if slices.Contains(appendFileNameFlags, "status") { + if status, ok := agentSelfResponse["Stats"]; ok { + if config, ok := status["consul"]; ok { + configMap := config.(map[string]interface{}) + if leader, ok := configMap["leader"]; ok { + if leader == "true" { + fileNameWithoutExt = fileNameWithoutExt + "-" + "leader" + } else { + fileNameWithoutExt = fileNameWithoutExt + "-" + "follower" + } + } + } + } + } + //adding extension back file = fileNameWithoutExt + fileExt } diff --git a/command/snapshot/save/snapshot_save_test.go b/command/snapshot/save/snapshot_save_test.go index c7d7367e418d..8e339cd51f5b 100644 --- a/command/snapshot/save/snapshot_save_test.go +++ b/command/snapshot/save/snapshot_save_test.go @@ -88,12 +88,21 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { dir := testutil.TempDir(t, "snapshot") file := filepath.Join(dir, "backup.tgz") args := []string{ - "-append-filename=version,dc", + "-append-filename=version,dc,node,status", "-http-addr=" + a.HTTPAddr(), file, } - newFilePath := filepath.Join(dir, "backup"+"-"+a.Config.Version+"-"+a.Config.Datacenter+".tgz") + stats := a.Stats() + + status := "follower" + + if stats["consul"]["leader"] == "true" { + status = "leader" + } + + newFilePath := filepath.Join(dir, "backup"+"-"+a.Config.Version+"-"+a.Config.Datacenter+ + "-"+a.Config.NodeName+"-"+status+".tgz") code := c.Run(args) if code != 0 {