Skip to content

Commit

Permalink
Update PR to incorporate NamedRoute change
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Jul 10, 2023
1 parent 1fb4296 commit 20577ac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import org.opensearch.jobscheduler.rest.request.GetJobDetailsRequest;
import org.opensearch.jobscheduler.spi.schedule.IntervalSchedule;
import org.opensearch.jobscheduler.spi.schedule.Schedule;
import org.opensearch.rest.NamedRoute;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.rest.RestStatus;
import org.opensearch.sdk.ExtensionsRunner;
import org.opensearch.sdk.SDKClient;
Expand All @@ -58,6 +60,7 @@
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Random;
Expand Down Expand Up @@ -85,10 +88,20 @@ public RestRemoteHelloAction(ExtensionsRunner runner) {
}

@Override
public List<RouteHandler> routeHandlers() {
public List<NamedRoute> routes() {
return List.of(
new RouteHandler(GET, "/hello/{name}", handleRemoteGetRequest),
new RouteHandler(PUT, "/schedule/hello", handleScheduleRequest)
new NamedRoute.Builder().method(GET)
.path("/hello/{name}")
.handler(handleRemoteGetRequest)
.uniqueName(routePrefix("remote_greet_with_name"))
.legacyActionNames(Collections.emptySet())
.build(),
new NamedRoute.Builder().method(GET)
.path("/schedule/hello")
.handler(handleScheduleRequest)
.uniqueName(routePrefix("scheduled_greet"))
.legacyActionNames(Collections.emptySet())
.build()
);
}

Expand All @@ -100,7 +113,7 @@ private void registerJobDetails(SDKClient.SDKRestClient client) throws IOExcepti
requestBody.field(GetJobDetailsRequest.JOB_TYPE, GreetJob.PARSE_FIELD_NAME);
requestBody.field(GetJobDetailsRequest.JOB_PARAMETER_ACTION, HWJobParameterAction.class.getName());
requestBody.field(GetJobDetailsRequest.JOB_RUNNER_ACTION, HWJobRunnerAction.class.getName());
requestBody.field(GetJobDetailsRequest.EXTENSION_UNIQUE_ID, extensionsRunner.getUniqueId());
requestBody.field(GetJobDetailsRequest.EXTENSION_UNIQUE_ID, extensionsRunner.getSdkTransportService().getUniqueId());
requestBody.endObject();

Request request = new Request("PUT", String.format(Locale.ROOT, "%s/%s", JobSchedulerPlugin.JS_BASE_URI, "_job_details"));
Expand Down Expand Up @@ -156,7 +169,7 @@ public <T> T fromJson(String json, Class<T> clazz) {
return mapper.deserialize(parser, clazz);
}

private Function<RestRequest, ExtensionRestResponse> handleScheduleRequest = (request) -> {
private Function<RestRequest, RestResponse> handleScheduleRequest = (request) -> {
SDKClient client = extensionsRunner.getSdkClient();
SDKClient.SDKRestClient restClient = client.initializeRestClient();
OpenSearchClient javaClient = client.initializeJavaClient();
Expand Down Expand Up @@ -223,7 +236,7 @@ public <T> T fromJson(String json, Class<T> clazz) {
return new ExtensionRestResponse(request, OK, "GreetJob successfully scheduled");
};

private Function<RestRequest, ExtensionRestResponse> handleRemoteGetRequest = (request) -> {
private Function<RestRequest, RestResponse> handleRemoteGetRequest = (request) -> {
SDKClient client = extensionsRunner.getSdkClient();

String name = request.param("name");
Expand All @@ -250,7 +263,7 @@ public <T> T fromJson(String json, Class<T> clazz) {
TimeUnit.SECONDS
).get();
if (!response.isSuccess()) {
return new ExtensionRestResponse(request, OK, "Remote extension reponse failed: " + response.getResponseBytesAsString());
return new ExtensionRestResponse(request, OK, "Remote extension response failed: " + response.getResponseBytesAsString());
}
// Parse out the expected response class from the bytes
SampleResponse sampleResponse = new SampleResponse(StreamInput.wrap(response.getResponseBytes()));
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/resources/sample/helloworld-settings.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extensionName: hello-world
extensionName: hw
hostAddress: 127.0.0.1
hostPort: 4532
opensearchAddress: 127.0.0.1
Expand Down
11 changes: 0 additions & 11 deletions src/main/resources/sample/helloworld-settings.yml

This file was deleted.

0 comments on commit 20577ac

Please sign in to comment.