Skip to content

Commit

Permalink
Merge 5072958 into backport/fix-snapshot-save-append-file/curiously-m…
Browse files Browse the repository at this point in the history
…aximum-mite
  • Loading branch information
hc-github-team-consul-core authored Sep 6, 2023
2 parents b4c5744 + 5072958 commit 573743b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
18 changes: 12 additions & 6 deletions command/snapshot/save/snapshot_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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") {
Expand Down
27 changes: 13 additions & 14 deletions command/snapshot/save/snapshot_save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,34 +88,33 @@ 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)

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 {
Expand Down

0 comments on commit 573743b

Please sign in to comment.