Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot all Indices Additional Query Params #118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

sarwaan001 marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading