Skip to content

Commit

Permalink
Simplify ClusterOperations
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Kurait <[email protected]>
  • Loading branch information
AndreKurait committed Dec 10, 2024
1 parent 4b1b788 commit 9669ce2
Showing 1 changed file with 0 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.opensearch.migrations.bulkload.framework.SearchClusterContainer;

import lombok.SneakyThrows;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
Expand Down Expand Up @@ -57,55 +53,6 @@ public void createSnapshotRepository(final String repoPath, final String repoNam
}
}

@SneakyThrows
public void deleteAllSnapshotsAndRepository(final String repoName) throws IOException {
// Get all snapshots in the repository
HttpGet getSnapshotsRequest = new HttpGet(clusterUrl + "/_snapshot/" + repoName + "/_all");
getSnapshotsRequest.setHeader("Content-Type", "application/json");

List<String> snapshotNames = new ArrayList<>();
try (var response = httpClient.execute(getSnapshotsRequest)) {
if (response.getCode() == 200) {
String responseBody = EntityUtils.toString(response.getEntity());
// Extract snapshot names from the response
int index = 0;
while ((index = responseBody.indexOf("\"snapshot\":\"", index)) != -1) {
index += "\"snapshot\":\"".length();
int endIndex = responseBody.indexOf("\"", index);
if (endIndex == -1) break;
String snapshotName = responseBody.substring(index, endIndex);
snapshotNames.add(snapshotName);
}
} else if (response.getCode() == 404) {
// Repository does not exist or no snapshots
return;
} else {
throw new IOException("Failed to list snapshots for repository " + repoName + ", status code: " + response.getCode());
}
}

// Delete each snapshot
for (String snapshot : snapshotNames) {
HttpDelete deleteSnapshotRequest = new HttpDelete(clusterUrl + "/_snapshot/" + repoName + "/" + snapshot);
deleteSnapshotRequest.setHeader("Content-Type", "application/json");
try (var response = httpClient.execute(deleteSnapshotRequest)) {
assertThat(response.getCode(), anyOf(equalTo(200), equalTo(202)));
}
}

// Delete the repository
HttpDelete deleteRepoRequest = new HttpDelete(clusterUrl + "/_snapshot/" + repoName);
deleteRepoRequest.setHeader("Content-Type", "application/json");
try (var response = httpClient.execute(deleteRepoRequest)) {
assertThat(response.getCode(), equalTo(200));
}
}

@SneakyThrows
public static void deleteSnapshotDir(SearchClusterContainer container) {
container.execInContainer("sh", "-c", "rm -rf " + SearchClusterContainer.CLUSTER_SNAPSHOT_DIR + "/*");
}

@SneakyThrows
public void restoreSnapshot(final String repository, final String snapshotName) {
var restoreRequest = new HttpPost(clusterUrl + "/_snapshot/" + repository + "/" + snapshotName + "/_restore"+ "?wait_for_completion=true");
Expand Down

0 comments on commit 9669ce2

Please sign in to comment.