From 0c268771dfc306d784c73c1a52a7f77ba7b8d746 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Fri, 27 Dec 2024 20:59:13 +0000 Subject: [PATCH 1/8] fix: added gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 689f7dd..bc6d29c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ tmp/* .vscode/settings.json .idea + +oryxBuildBinary From aef7e71367ba2e7f85df1fb570633e86b8eb5144 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Sun, 29 Dec 2024 06:52:43 +0000 Subject: [PATCH 2/8] feature: added SnapshotAllIndicesWithBodyParams function --- es.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/es.go b/es.go index 48e0292..5ae72f6 100644 --- a/es.go +++ b/es.go @@ -1174,6 +1174,34 @@ func (c *Client) SnapshotAllIndices(repository string, snapshot string) error { return err } +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 { + 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(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. From 903b29b0a61f7349dc5f9387fb6d9206702e1503 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:13:31 +0000 Subject: [PATCH 3/8] fix: changed bytes to string --- es.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/es.go b/es.go index 5ae72f6..d4a39a9 100644 --- a/es.go +++ b/es.go @@ -1195,7 +1195,7 @@ func (c *Client) SnapshotAllIndicesWithBodyParams(repository string, snapshot st agent := c.buildPutRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot)). Set("Content-Type", "application/json"). - Send(parsedJson) + Send(string(parsedJson)) _, err := handleErrWithBytes(agent) From 3eaed8f8e5254150d0a108c0c15874dd77604028 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:13:51 +0000 Subject: [PATCH 4/8] fix: added test to test if snapshot all indices works as intended --- es_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/es_test.go b/es_test.go index e71b6be..73842a9 100644 --- a/es_test.go +++ b/es_test.go @@ -1381,6 +1381,51 @@ 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 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 From bf45fdcb54588d13d27ef73fd171f94da577e280 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:29:17 +0000 Subject: [PATCH 5/8] fix: linting --- es.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/es.go b/es.go index d4a39a9..8f4c704 100644 --- a/es.go +++ b/es.go @@ -1187,7 +1187,7 @@ func (c *Client) SnapshotAllIndicesWithBodyParams(repository string, snapshot st return errors.New("no body params provided, please use SnapshotAllIndices Function instead") } - parsedJson, parsingErr := json.Marshal(bodyParams) + parsedJSON, parsingErr := json.Marshal(bodyParams) if parsingErr != nil { return parsingErr @@ -1195,7 +1195,7 @@ func (c *Client) SnapshotAllIndicesWithBodyParams(repository string, snapshot st agent := c.buildPutRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot)). Set("Content-Type", "application/json"). - Send(string(parsedJson)) + Send(string(parsedJSON)) _, err := handleErrWithBytes(agent) From 309cd67e5d55c7ef23df325df245fdb4f6815615 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Sun, 29 Dec 2024 07:39:24 +0000 Subject: [PATCH 6/8] chore: added additional test to see if map[string]bool works correctly --- es_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/es_test.go b/es_test.go index 73842a9..fc135b0 100644 --- a/es_test.go +++ b/es_test.go @@ -1407,6 +1407,29 @@ func TestSnapshotAllIndicesWithAdditionalParameters(t *testing.T) { } } +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", From 026716eddab602157b6722a9887804dc3945bce5 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Mon, 30 Dec 2024 16:44:11 +0000 Subject: [PATCH 7/8] fix: added docstring --- es.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/es.go b/es.go index 8f4c704..3def58c 100644 --- a/es.go +++ b/es.go @@ -1174,6 +1174,9 @@ 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") From 7aa74a5d236546aaad4105d6734a49f229e21cf0 Mon Sep 17 00:00:00 2001 From: Sarwaan Ansari <31755174+sarwaan001@users.noreply.github.com> Date: Mon, 30 Dec 2024 16:57:12 +0000 Subject: [PATCH 8/8] fix: allowed nil values --- es.go | 14 +++++++------- es_test.go | 7 +++---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/es.go b/es.go index 3def58c..7f6d20d 100644 --- a/es.go +++ b/es.go @@ -1186,19 +1186,19 @@ func (c *Client) SnapshotAllIndicesWithBodyParams(repository string, snapshot st return errors.New("empty string for snapshot is not allowed") } - if bodyParams == nil { - 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)) + 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) diff --git a/es_test.go b/es_test.go index fc135b0..7fc3d4c 100644 --- a/es_test.go +++ b/es_test.go @@ -1430,11 +1430,10 @@ func TestSnapshotAllIndicesWithAdditionalParametersIncludeGlobalState(t *testing } } -func TestSnapshotAllIndicesWithAdditionalParametersErr(t *testing.T) { +func TestSnapshotAllIndicesWithAdditionalParametersNilValue(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 }`, } @@ -1444,8 +1443,8 @@ func TestSnapshotAllIndicesWithAdditionalParametersErr(t *testing.T) { err := client.SnapshotAllIndicesWithBodyParams("backup-repo", "snapshot1", nil) - if err == nil { - t.Fatalf("should have thrown an error") + if err != nil { + t.Fatalf("Should be able to take Nil body params: %s", err) } }