diff --git a/.gitignore b/.gitignore index 689f7dd..bc6d29c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ tmp/* .vscode/settings.json .idea + +oryxBuildBinary diff --git a/es.go b/es.go index 48e0292..7f6d20d 100644 --- a/es.go +++ b/es.go @@ -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. diff --git a/es_test.go b/es_test.go index e71b6be..7fc3d4c 100644 --- a/es_test.go +++ b/es_test.go @@ -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