Skip to content

Commit

Permalink
add validate rest handler
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Lou <mloufra@amazon.com>
  • Loading branch information
mloufra committed Nov 16, 2022
1 parent bcd5a3e commit b7dbd48
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.opensearch.ad.rest.RestCreateDetectorAction;
import org.opensearch.ad.rest.RestGetDetectorAction;
import org.opensearch.ad.rest.RestValidateDetectorAction;
import org.opensearch.ad.settings.AnomalyDetectorSettings;
import org.opensearch.ad.settings.EnabledSetting;
import org.opensearch.client.opensearch.OpenSearchClient;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ExtensionSettings getExtensionSettings() {

@Override
public List<ExtensionRestHandler> getExtensionRestHandlers() {
return List.of(new RestCreateDetectorAction(), new RestGetDetectorAction());
return List.of(new RestCreateDetectorAction(), new RestGetDetectorAction(), new RestValidateDetectorAction());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.opensearch.ad.rest;

import static org.opensearch.rest.RestRequest.Method.POST;
import static org.opensearch.rest.RestStatus.NOT_FOUND;
import static org.opensearch.rest.RestStatus.OK;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ad.constant.CommonErrorMessages;
import org.opensearch.ad.settings.EnabledSetting;
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;

public class RestValidateDetectorAction implements ExtensionRestHandler {
private final Logger logger = LogManager.getLogger(RestValidateDetectorAction.class);

@Override
public List<Route> routes() {
return List.of(new Route(POST, "/detectors"));
}

@Override
public ExtensionRestResponse handleRequest(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()
);
}
// do things with request
return new ExtensionRestResponse(request, OK, "placeholder");
}
}

0 comments on commit b7dbd48

Please sign in to comment.