Skip to content

Commit

Permalink
one task
Browse files Browse the repository at this point in the history
Signed-off-by: Sarthak Aggarwal <sarthagg@amazon.com>
  • Loading branch information
sarthakaggarwal97 committed Jan 25, 2024
1 parent 462fe9c commit 7d989b9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,6 @@ protected boolean preserveIndicesUponCompletion() {
return true;
}

@Before
public void testSetup() {
final String bwcsuiteString = System.getProperty("tests.rest.bwcsuite");
System.out.println(bwcsuiteString);
// Assume.assumeTrue("Test cannot be run outside the BWC gradle task 'bwcTestSuite' or its dependent tasks", bwcsuiteString != null);
CLUSTER_TYPE = ClusterType.parse(bwcsuiteString);
CLUSTER_NAME = System.getProperty("tests.clustername");

System.out.println(CLUSTER_TYPE);
System.out.println(CLUSTER_NAME);
}

@Override
protected final boolean preserveClusterUponCompletion() {
return true;
Expand Down Expand Up @@ -295,8 +283,8 @@ private void createIndexIfNotExists(String index) throws IOException {
+ " }\n"
+ "}";
if (!resourceExists(index)) {
Response response = RestHelper.makeRequest(client(), "PUT", index, RestHelper.toHttpEntity(settings));
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
// Response response = RestHelper.makeRequest(client(), "PUT", index, RestHelper.toHttpEntity(settings));
// assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.customcodecs.bwc.helper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.opensearch.client.Request;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.Response;
import org.opensearch.client.RestClient;
import org.opensearch.client.WarningsHandler;

import static org.apache.hc.core5.http.ContentType.APPLICATION_JSON;

public class RestHelper {

private static final Logger log = LogManager.getLogger(RestHelper.class);

public static HttpEntity toHttpEntity(String jsonString) {
return new StringEntity(jsonString, APPLICATION_JSON);
}

public static Response get(RestClient client, String url) throws IOException {
return makeRequest(client, "GET", url, null, null);
}

public static Response makeRequest(RestClient client, String method, String endpoint, HttpEntity entity) throws IOException {
return makeRequest(client, method, endpoint, entity, null);
}

public static Response makeRequest(RestClient client, String method, String endpoint, HttpEntity entity, List<Header> headers)
throws IOException {
log.info("Making request " + method + " " + endpoint + ", with headers " + headers);

Request request = new Request(method, endpoint);

RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
options.setWarningsHandler(WarningsHandler.PERMISSIVE);
if (headers != null) {
headers.forEach(header -> options.addHeader(header.getName(), header.getValue()));
}
request.setOptions(options.build());

if (entity != null) {
request.setEntity(entity);
}

Response response = client.performRequest(request);
log.info("Recieved response " + response.getStatusLine());
return response;
}

public static List<Response> requestAgainstAllNodes(RestClient client, String method, String endpoint, HttpEntity entity)
throws IOException {
return requestAgainstAllNodes(client, method, endpoint, entity, null);
}

public static List<Response> requestAgainstAllNodes(
RestClient client,
String method,
String endpoint,
HttpEntity entity,
List<Header> headers
) throws IOException {
int nodeCount = client.getNodes().size();
List<Response> responses = new ArrayList<>();
while (nodeCount-- > 0) {
responses.add(makeRequest(client, method, endpoint, entity, headers));
}
return responses;
}

public static Header getAuthorizationHeader(String username, String password) {
return new BasicHeader("Authorization", "Basic " + username + ":" + password);
}
}
///*
// * SPDX-License-Identifier: Apache-2.0
// *
// * The OpenSearch Contributors require contributions made to
// * this file be licensed under the Apache-2.0 license or a
// * compatible open source license.
// */
//
//package org.opensearch.customcodecs.bwc.helper;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.List;
//
//import org.apache.hc.core5.http.Header;
//import org.apache.hc.core5.http.HttpEntity;
//import org.apache.hc.core5.http.io.entity.StringEntity;
//import org.apache.hc.core5.http.message.BasicHeader;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//
//import org.opensearch.client.Request;
//import org.opensearch.client.RequestOptions;
//import org.opensearch.client.Response;
//import org.opensearch.client.RestClient;
//import org.opensearch.client.WarningsHandler;
//
//import static org.apache.hc.core5.http.ContentType.APPLICATION_JSON;
//
//public class RestHelper {
//
// private static final Logger log = LogManager.getLogger(RestHelper.class);
//
// public static HttpEntity toHttpEntity(String jsonString) {
// return new StringEntity(jsonString, APPLICATION_JSON);
// }
//
// public static Response get(RestClient client, String url) throws IOException {
// return makeRequest(client, "GET", url, null, null);
// }
//
// public static Response makeRequest(RestClient client, String method, String endpoint, HttpEntity entity) throws IOException {
// return makeRequest(client, method, endpoint, entity, null);
// }
//
// public static Response makeRequest(RestClient client, String method, String endpoint, HttpEntity entity, List<Header> headers)
// throws IOException {
// log.info("Making request " + method + " " + endpoint + ", with headers " + headers);
//
// Request request = new Request(method, endpoint);
//
// RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
// options.setWarningsHandler(WarningsHandler.PERMISSIVE);
// if (headers != null) {
// headers.forEach(header -> options.addHeader(header.getName(), header.getValue()));
// }
// request.setOptions(options.build());
//
// if (entity != null) {
// request.setEntity(entity);
// }
//
// Response response = client.performRequest(request);
// log.info("Recieved response " + response.getStatusLine());
// return response;
// }
//
// public static List<Response> requestAgainstAllNodes(RestClient client, String method, String endpoint, HttpEntity entity)
// throws IOException {
// return requestAgainstAllNodes(client, method, endpoint, entity, null);
// }
//
// public static List<Response> requestAgainstAllNodes(
// RestClient client,
// String method,
// String endpoint,
// HttpEntity entity,
// List<Header> headers
// ) throws IOException {
// int nodeCount = client.getNodes().size();
// List<Response> responses = new ArrayList<>();
// while (nodeCount-- > 0) {
// responses.add(makeRequest(client, method, endpoint, entity, headers));
// }
// return responses;
// }
//
// public static Header getAuthorizationHeader(String username, String password) {
// return new BasicHeader("Authorization", "Basic " + username + ":" + password);
// }
//}

0 comments on commit 7d989b9

Please sign in to comment.