Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature/extensions] CreateDetectorAction extends BaseExtensionRestHandler #726

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.opensearch.rest.RestRequest.Method.POST;
import static org.opensearch.rest.RestStatus.BAD_REQUEST;
import static org.opensearch.rest.RestStatus.CREATED;
import static org.opensearch.rest.RestStatus.NOT_FOUND;
import static org.opensearch.rest.RestStatus.OK;

import java.io.ByteArrayInputStream;
Expand All @@ -25,8 +24,6 @@
import java.time.Instant;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ad.AnomalyDetectorExtension;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.indices.AnomalyDetectionIndices;
Expand All @@ -45,17 +42,15 @@
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.extensions.rest.ExtensionRestRequest;
import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestRequest.Method;
import org.opensearch.sdk.ExtensionRestHandler;
import org.opensearch.sdk.BaseExtensionRestHandler;
import org.opensearch.sdk.ExtensionsRunner;
import org.opensearch.sdk.RouteHandler;

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import jakarta.json.stream.JsonParser;

public class RestCreateDetectorAction implements ExtensionRestHandler {
private static final Logger logger = LogManager.getLogger(RestCreateDetectorAction.class);
public class RestCreateDetectorAction extends BaseExtensionRestHandler {

private final OpenSearchClient sdkClient;
private final NamedXContentRegistry xContentRegistry;
Expand All @@ -66,8 +61,8 @@ public RestCreateDetectorAction(ExtensionsRunner runner, AnomalyDetectorExtensio
}

@Override
public List<Route> routes() {
return List.of(new Route(POST, "/detectors"));
protected List<RouteHandler> routeHandlers() {
return List.of(new RouteHandler(POST, "/detectors", (r) -> handlePostRequest(r)));
Copy link
Member Author

@dbwiddis dbwiddis Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@owaiskazi19 FYI, I tried to use the same shortened format as the RestHelloAction but the internal use of client and xContentRegistry inside a Function falls afoul of "must be final" (even though they are defined final!)

So I had to keep it as a standard method and do the lambda version of the Function inline here.

See here as well.

}

private String getAnomalyDetectorMappings() throws IOException {
Expand Down Expand Up @@ -129,20 +124,10 @@ private CreateIndexRequest initAnomalyDetectorIndex() {
return request;
}

@Override
public ExtensionRestResponse handleRequest(ExtensionRestRequest request) {
private ExtensionRestResponse handlePostRequest(ExtensionRestRequest request) {
if (!EnabledSetting.isADPluginEnabled()) {
throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
}
Method method = request.method();

if (!Method.POST.equals(method)) {
return new ExtensionRestResponse(
request,
NOT_FOUND,
"Extension REST action improperly configured to handle " + request.toString()
);
}

XContentParser parser;
AnomalyDetector detector;
Expand Down Expand Up @@ -175,5 +160,4 @@ public ExtensionRestResponse handleRequest(ExtensionRestRequest request) {
}
return new ExtensionRestResponse(request, OK, builder);
}

}