Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit de513db

Browse files
authored
Merge pull request #118 from github/elasticsearch/SnapshotAllIndices-additional-query-params
Snapshot all Indices Additional Query Params
2 parents c03609c + 7aa74a5 commit de513db

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
tmp/*
1515
.vscode/settings.json
1616
.idea
17+
18+
oryxBuildBinary

es.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,37 @@ func (c *Client) SnapshotAllIndices(repository string, snapshot string) error {
11741174
return err
11751175
}
11761176

1177+
// Take a snapshot of all indices on the cluster to the given repository with body params
1178+
//
1179+
// Use case: You want to backup all of the indices on the cluster to the given repository with body params
1180+
func (c *Client) SnapshotAllIndicesWithBodyParams(repository string, snapshot string, bodyParams map[string]interface{}) error {
1181+
if repository == "" {
1182+
return errors.New("empty string for repository is not allowed")
1183+
}
1184+
1185+
if snapshot == "" {
1186+
return errors.New("empty string for snapshot is not allowed")
1187+
}
1188+
1189+
parsedJSON, parsingErr := json.Marshal(bodyParams)
1190+
1191+
if parsingErr != nil {
1192+
return parsingErr
1193+
}
1194+
1195+
agent := c.buildPutRequest(fmt.Sprintf("_snapshot/%s/%s", repository, snapshot))
1196+
1197+
if bodyParams != nil {
1198+
agent = agent.
1199+
Set("Content-Type", "application/json").
1200+
Send(string(parsedJSON))
1201+
}
1202+
1203+
_, err := handleErrWithBytes(agent)
1204+
1205+
return err
1206+
}
1207+
11771208
// Restore an index or indices on the cluster
11781209
//
11791210
// Use case: You want to restore a particular index or indices onto your cluster with a new name.

es_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,73 @@ func TestSnapshotAllIndices(t *testing.T) {
13811381
}
13821382
}
13831383

1384+
func TestSnapshotAllIndicesWithAdditionalParameters(t *testing.T) {
1385+
testSetup := &ServerSetup{
1386+
Method: "PUT",
1387+
Path: "/_snapshot/backup-repo/snapshot1",
1388+
Body: `{"metadata":{"taken_because":"backup before upgrading","taken_by":"user123"}}`,
1389+
Response: `{"acknowledged": true }`,
1390+
}
1391+
1392+
host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
1393+
defer ts.Close()
1394+
client := NewClient(host, port)
1395+
1396+
bodyParams := map[string]interface{}{
1397+
"metadata": map[string]interface{}{
1398+
"taken_by": "user123",
1399+
"taken_because": "backup before upgrading",
1400+
},
1401+
}
1402+
1403+
err := client.SnapshotAllIndicesWithBodyParams("backup-repo", "snapshot1", bodyParams)
1404+
1405+
if err != nil {
1406+
t.Fatalf("Got error taking snapshot: %s", err)
1407+
}
1408+
}
1409+
1410+
func TestSnapshotAllIndicesWithAdditionalParametersIncludeGlobalState(t *testing.T) {
1411+
testSetup := &ServerSetup{
1412+
Method: "PUT",
1413+
Path: "/_snapshot/backup-repo/snapshot1",
1414+
Body: `{"include_global_state":true}`,
1415+
Response: `{"acknowledged": true }`,
1416+
}
1417+
1418+
host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
1419+
defer ts.Close()
1420+
client := NewClient(host, port)
1421+
1422+
bodyParams := map[string]interface{}{
1423+
"include_global_state": true,
1424+
}
1425+
1426+
err := client.SnapshotAllIndicesWithBodyParams("backup-repo", "snapshot1", bodyParams)
1427+
1428+
if err != nil {
1429+
t.Fatalf("Got error taking snapshot: %s", err)
1430+
}
1431+
}
1432+
1433+
func TestSnapshotAllIndicesWithAdditionalParametersNilValue(t *testing.T) {
1434+
testSetup := &ServerSetup{
1435+
Method: "PUT",
1436+
Path: "/_snapshot/backup-repo/snapshot1",
1437+
Response: `{"acknowledged": true }`,
1438+
}
1439+
1440+
host, port, ts := setupTestServers(t, []*ServerSetup{testSetup})
1441+
defer ts.Close()
1442+
client := NewClient(host, port)
1443+
1444+
err := client.SnapshotAllIndicesWithBodyParams("backup-repo", "snapshot1", nil)
1445+
1446+
if err != nil {
1447+
t.Fatalf("Should be able to take Nil body params: %s", err)
1448+
}
1449+
}
1450+
13841451
func TestRestoreSnapshotIndices_ErrorConditions(t *testing.T) {
13851452
tt := []struct {
13861453
Name string

0 commit comments

Comments
 (0)