Skip to content

Commit

Permalink
fix: allowed nil values
Browse files Browse the repository at this point in the history
  • Loading branch information
sarwaan001 committed Dec 30, 2024
1 parent 026716e commit 7aa74a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
14 changes: 7 additions & 7 deletions es.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 3 additions & 4 deletions es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`,
}

Expand All @@ -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)
}
}

Expand Down

0 comments on commit 7aa74a5

Please sign in to comment.