Skip to content

Commit

Permalink
[Feature/extensions] Add Action for Validate detector API call (#732)
Browse files Browse the repository at this point in the history
* add validate rest handler

Signed-off-by: Frank Lou <[email protected]>

* add another new router

Signed-off-by: Frank Lou <[email protected]>

* add one more route

Signed-off-by: Frank Lou <[email protected]>

* change route

Signed-off-by: Frank Lou <[email protected]>

* change route path

Signed-off-by: Frank Lou <[email protected]>

* change route path

Signed-off-by: Frank Lou <[email protected]>

* change route path

Signed-off-by: Frank Lou <[email protected]>

* change route path

Signed-off-by: Frank Lou <[email protected]>

Signed-off-by: Frank Lou <[email protected]>
Co-authored-by: Daniel Widdis <[email protected]>
  • Loading branch information
mloufra and dbwiddis authored Nov 18, 2022
1 parent a845bdb commit 71010ca
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 @@ -21,6 +21,7 @@
import org.opensearch.ad.model.DetectorInternalState;
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 All @@ -43,7 +44,7 @@ public AnomalyDetectorExtension() {

@Override
public List<ExtensionRestHandler> getExtensionRestHandlers() {
return List.of(new RestCreateDetectorAction(extensionsRunner, this), new RestGetDetectorAction());
return List.of(new RestCreateDetectorAction(extensionsRunner, this), 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/_validate"), new Route(POST, "/detectors/_validate/{type}"));
}

@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 71010ca

Please sign in to comment.