diff --git a/command/snapshot/save/snapshot_save.go b/command/snapshot/save/snapshot_save.go index a15147baa4c7..0b776e11bf4b 100644 --- a/command/snapshot/save/snapshot_save.go +++ b/command/snapshot/save/snapshot_save.go @@ -75,9 +75,7 @@ func (c *cmd) Run(args []string) int { if len(appendFileNameFlags) != 0 && len(c.appendFileNameFlag.String()) > 0 { agentSelfResponse, err := client.Agent().Self() - operatorHealthResponse, error := client.Operator().AutopilotServerHealth(nil) - - if err != nil && error != nil { + if err != nil { c.UI.Error(fmt.Sprintf("Error connecting to Consul agent and fetching datacenter/version: %s", err)) return 1 } @@ -86,11 +84,19 @@ func (c *cmd) Run(args []string) int { fileNameWithoutExt := strings.TrimSuffix(file, fileExt) if slices.Contains(appendFileNameFlags, "version") { - if config, ok := agentSelfResponse["Config"]; ok { - if version, ok := config["Version"]; ok { - fileNameWithoutExt = fileNameWithoutExt + "-" + version.(string) + operatorHealthResponse, err := client.Operator().AutopilotServerHealth(nil) + if err != nil { + c.UI.Error(fmt.Sprintf("Error fetching version of Consul agent Leader: %s", err)) + return 1 + } + version := "" + for _, server := range operatorHealthResponse.Servers { + if server.Leader { + version = server.Version + break } } + fileNameWithoutExt = fileNameWithoutExt + "-" + version } if slices.Contains(appendFileNameFlags, "dc") { diff --git a/command/snapshot/save/snapshot_save_test.go b/command/snapshot/save/snapshot_save_test.go index a5fce4133dec..702d43a08ca7 100644 --- a/command/snapshot/save/snapshot_save_test.go +++ b/command/snapshot/save/snapshot_save_test.go @@ -88,19 +88,11 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { dir := testutil.TempDir(t, "snapshot") file := filepath.Join(dir, "backup.tgz") args := []string{ - "-append-filename=version,dc,node,status", + "-append-filename=version,dc", "-http-addr=" + a.HTTPAddr(), file, } - stats := a.Stats() - - status := "follower" - - if stats["consul"]["leader"] == "true" { - status = "leader" - } - // We need to use the self endpoint here for ENT, which returns the product suffix (+ent) self, err := client.Agent().Self() require.NoError(t, err) @@ -108,14 +100,21 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { cfg, ok := self["Config"] require.True(t, ok) - versionAny, ok := cfg["Version"] + dc, ok := cfg["Datacenter"] require.True(t, ok) - version, ok := versionAny.(string) - require.True(t, ok) + datacenter := dc.(string) + + operatorHealth, err := client.Operator().AutopilotServerHealth(nil) + + version := "" + for _, server := range operatorHealth.Servers { + if server.Leader { + version = server.Version + } + } - newFilePath := filepath.Join(dir, "backup"+"-"+version+"-"+a.Config.Datacenter+ - "-"+a.Config.NodeName+"-"+status+".tgz") + newFilePath := filepath.Join(dir, "backup"+"-"+version+"-"+datacenter+".tgz") code := c.Run(args) if code != 0 {