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 6 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
28 changes: 28 additions & 0 deletions es.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,34 @@ func (c *Client) SnapshotAllIndices(repository string, snapshot string) error {
return err
}

sarwaan001 marked this conversation as resolved.
Show resolved Hide resolved
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")
}

if bodyParams == nil {
sarwaan001 marked this conversation as resolved.
Show resolved Hide resolved
return errors.New("no body params provided, please use SnapshotAllIndices Function instead")
}

parsedJSON, parsingErr := json.Marshal(bodyParams)

if parsingErr != nil {
return parsingErr
}

agent := c.buildPutRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot)).
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
68 changes: 68 additions & 0 deletions es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,74 @@ 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 TestSnapshotAllIndicesWithAdditionalParametersErr(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)

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

if err == nil {
t.Fatalf("should have thrown an error")
}
}

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