Skip to content

Commit

Permalink
Merge pull request #118 from github/elasticsearch/SnapshotAllIndices-…
Browse files Browse the repository at this point in the history
…additional-query-params

Snapshot all Indices Additional Query Params
  • Loading branch information
sarwaan001 authored Dec 30, 2024
2 parents c03609c + 7aa74a5 commit de513db
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
tmp/*
.vscode/settings.json
.idea

oryxBuildBinary
31 changes: 31 additions & 0 deletions es.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,37 @@ func (c *Client) SnapshotAllIndices(repository string, snapshot string) error {
return err
}

// Take a snapshot of all indices on the cluster to the given repository with body params
//
// Use case: You want to backup all of the indices on the cluster to the given repository with body params
func (c *Client) SnapshotAllIndicesWithBodyParams(repository string, snapshot string, bodyParams map[string]interface{}) error {
if repository == "" {
return errors.New("empty string for repository is not allowed")
}

if snapshot == "" {
return errors.New("empty string for snapshot is not allowed")
}

parsedJSON, parsingErr := json.Marshal(bodyParams)

if parsingErr != nil {
return parsingErr
}

agent := c.buildPutRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot))

if bodyParams != nil {
agent = agent.
Set("Content-Type", "application/json").
Send(string(parsedJSON))
}

_, err := handleErrWithBytes(agent)

return err
}

// Restore an index or indices on the cluster
//
// Use case: You want to restore a particular index or indices onto your cluster with a new name.
Expand Down
67 changes: 67 additions & 0 deletions es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,73 @@ func TestSnapshotAllIndices(t *testing.T) {
}
}

func TestSnapshotAllIndicesWithAdditionalParameters(t *testing.T) {
testSetup := &ServerSetup{
Method: "PUT",
Path: "/_snapshot/backup-repo/snapshot1",
Body: `{"metadata":{"taken_because":"backup before upgrading","taken_by":"user123"}}`,
Response: `{"acknowledged": true }`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

bodyParams := map[string]interface{}{
"metadata": map[string]interface{}{
"taken_by": "user123",
"taken_because": "backup before upgrading",
},
}

err := client.SnapshotAllIndicesWithBodyParams("backup-repo", "snapshot1", bodyParams)

if err != nil {
t.Fatalf("Got error taking snapshot: %s", err)
}
}

func TestSnapshotAllIndicesWithAdditionalParametersIncludeGlobalState(t *testing.T) {
testSetup := &ServerSetup{
Method: "PUT",
Path: "/_snapshot/backup-repo/snapshot1",
Body: `{"include_global_state":true}`,
Response: `{"acknowledged": true }`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

bodyParams := map[string]interface{}{
"include_global_state": true,
}

err := client.SnapshotAllIndicesWithBodyParams("backup-repo", "snapshot1", bodyParams)

if err != nil {
t.Fatalf("Got error taking snapshot: %s", err)
}
}

func TestSnapshotAllIndicesWithAdditionalParametersNilValue(t *testing.T) {
testSetup := &ServerSetup{
Method: "PUT",
Path: "/_snapshot/backup-repo/snapshot1",
Response: `{"acknowledged": true }`,
}

host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
defer ts.Close()
client := NewClient(host, port)

err := client.SnapshotAllIndicesWithBodyParams("backup-repo", "snapshot1", nil)

if err != nil {
t.Fatalf("Should be able to take Nil body params: %s", err)
}
}

func TestRestoreSnapshotIndices_ErrorConditions(t *testing.T) {
tt := []struct {
Name string
Expand Down

0 comments on commit de513db

Please sign in to comment.