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

Renaming RestAPIs while supporting backwards compatibility. #35

Merged
merged 3 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -201,7 +201,9 @@ public class AnomalyDetectorPlugin extends Plugin implements ActionPlugin, Scrip

private static final Logger LOG = LogManager.getLogger(AnomalyDetectorPlugin.class);

public static final String AD_BASE_URI = "/_opendistro/_anomaly_detection";
public static final String AD_BASE_LEGACY = "/_opendistro/_anomaly_detection";
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
public static final String AD_BASE_LEGACY_DETECTORS_URI = AD_BASE_LEGACY + "/detectors";
public static final String AD_BASE_URI = "/_opensearch/_anomaly_detection";
public static final String AD_BASE_DETECTORS_URI = AD_BASE_URI + "/detectors";
public static final String AD_THREAD_POOL_PREFIX = "opendistro.ad.";
public static final String AD_THREAD_POOL_NAME = "ad-threadpool";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;

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

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -59,7 +60,6 @@
import com.amazon.opendistroforelasticsearch.ad.constant.CommonErrorMessages;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector;
import com.amazon.opendistroforelasticsearch.ad.settings.EnabledSetting;
import com.google.common.collect.ImmutableList;

/**
* Abstract class to handle search request.
Expand All @@ -68,15 +68,15 @@ public abstract class AbstractSearchAction<T extends ToXContentObject> extends B

private final String index;
private final Class<T> clazz;
private final String urlPath;
private final List<String> urlPaths;
private final ActionType<SearchResponse> actionType;

private final Logger logger = LogManager.getLogger(AbstractSearchAction.class);

public AbstractSearchAction(String urlPath, String index, Class<T> clazz, ActionType<SearchResponse> actionType) {
public AbstractSearchAction(List<String> urlPaths, String index, Class<T> clazz, ActionType<SearchResponse> actionType) {
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
this.index = index;
this.clazz = clazz;
this.urlPath = urlPath;
this.urlPaths = urlPaths;
this.actionType = actionType;
}

Expand Down Expand Up @@ -126,6 +126,11 @@ public RestResponse buildResponse(SearchResponse response) throws Exception {

@Override
public List<Route> routes() {
return ImmutableList.of(new Route(RestRequest.Method.POST, urlPath), new Route(RestRequest.Method.GET, urlPath));
List<Route> routes = new ArrayList<>();
for (String path : urlPaths) {
routes.add(new Route(RestRequest.Method.POST, path));
routes.add(new Route(RestRequest.Method.GET, path));
}
return routes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
public List<Route> routes() {
return ImmutableList
.of(
// start AD job (legacy)
new Route(
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, DETECTOR_ID, START_JOB)
),
// stop AD job (legacy)
new Route(
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, DETECTOR_ID, STOP_JOB)
),
// start AD job
new Route(
RestRequest.Method.POST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
public List<Route> routes() {
return ImmutableList
.of(
// delete anomaly detector document
new Route(
RestRequest.Method.DELETE,
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, DETECTOR_ID)
),
// delete anomaly detector document
new Route(
RestRequest.Method.DELETE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ private String validateAdExecutionInput(AnomalyDetectorExecutionInput input) {
public List<Route> routes() {
return ImmutableList
.of(
// get AD result, for regular run (legacy)
new Route(
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, DETECTOR_ID, RUN)
),
// get AD result, for regular run
new Route(
RestRequest.Method.POST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,32 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli

@Override
public List<Route> routes() {
String path = String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID);
String path = String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, DETECTOR_ID);
String newPath = String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID);
return ImmutableList
.of(
new Route(RestRequest.Method.GET, path),
new Route(RestRequest.Method.HEAD, path),
new Route(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, DETECTOR_ID, PROFILE)
),
// types is a profile names. See a complete list of supported profiles names in
// com.amazon.opendistroforelasticsearch.ad.model.ProfileName.
new Route(
RestRequest.Method.GET,
String
.format(
Locale.ROOT,
"%s/{%s}/%s/{%s}",
AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI,
DETECTOR_ID,
PROFILE,
TYPE
)
),
new Route(RestRequest.Method.GET, newPath),
new Route(RestRequest.Method.HEAD, newPath),
new Route(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, PROFILE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
public List<Route> routes() {
return ImmutableList
.of(
// Create (legacy)
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
new Route(RestRequest.Method.POST, AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI),
// update (legacy)
new Route(
RestRequest.Method.PUT,
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, DETECTOR_ID)
),
// Create
new Route(RestRequest.Method.POST, AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI),
// update
Expand All @@ -178,7 +185,7 @@ public RestResponse buildResponse(IndexAnomalyDetectorResponse response) throws
response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS)
);
if (restStatus == RestStatus.CREATED) {
String location = String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_URI, response.getId());
String location = String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_LEGACY, response.getId());
bytesRestResponse.addHeader("Location", location);
}
return bytesRestResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ private String validateAdExecutionInput(AnomalyDetectorExecutionInput input) {
public List<RestHandler.Route> routes() {
return ImmutableList
.of(
// preview detector (legacy)
new Route(
RestRequest.Method.POST,
String
.format(
Locale.ROOT,
"%s/{%s}/%s",
AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI,
RestHandlerUtils.DETECTOR_ID,
PREVIEW
)
),
// preview detector
new Route(
RestRequest.Method.POST,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@
import com.amazon.opendistroforelasticsearch.ad.constant.CommonName;
import com.amazon.opendistroforelasticsearch.ad.model.ADTask;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchADTasksAction;
import com.google.common.collect.ImmutableList;

/**
* This class consists of the REST handler to search AD tasks.
*/
public class RestSearchADTasksAction extends AbstractSearchAction<ADTask> {

private static final String URL_PATH = AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI + "/tasks/_search";
private static final String URL_PATH = AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI + "/tasks/_search";
private static final String URL_NEW_PATH = AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI + "/tasks/_search";
private final String SEARCH_ANOMALY_DETECTION_TASKS = "search_anomaly_detection_tasks";

public RestSearchADTasksAction() {
super(URL_PATH, CommonName.DETECTION_STATE_INDEX, ADTask.class, SearchADTasksAction.INSTANCE);
super(ImmutableList.of(URL_PATH, URL_NEW_PATH), CommonName.DETECTION_STATE_INDEX, ADTask.class, SearchADTasksAction.INSTANCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@
import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorAction;
import com.google.common.collect.ImmutableList;

/**
* This class consists of the REST handler to search anomaly detectors.
*/
public class RestSearchAnomalyDetectorAction extends AbstractSearchAction<AnomalyDetector> {

private static final String URL_PATH = AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI + "/_search";
private static final String URL_PATH = AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI + "/_search";
private static final String URL_NEW_PATH = AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI + "/_search";
private final String SEARCH_ANOMALY_DETECTOR_ACTION = "search_anomaly_detector";

public RestSearchAnomalyDetectorAction() {
super(URL_PATH, ANOMALY_DETECTORS_INDEX, AnomalyDetector.class, SearchAnomalyDetectorAction.INSTANCE);
super(
ImmutableList.of(URL_PATH, URL_NEW_PATH),
ANOMALY_DETECTORS_INDEX,
AnomalyDetector.class,
SearchAnomalyDetectorAction.INSTANCE
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ protected RestChannelConsumer prepareRequest(RestRequest request, org.opensearch
public List<RestHandler.Route> routes() {
return ImmutableList
.of(
// get the count of number of detectors (legacy)
new RestHandler.Route(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, COUNT)
),
// get if a detector name exists with name (legacy)
new RestHandler.Route(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI, MATCH)
),
// get the count of number of detectors
new RestHandler.Route(
RestRequest.Method.GET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,24 @@
import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin;
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyResult;
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyResultAction;
import com.google.common.collect.ImmutableList;

/**
* This class consists of the REST handler to search anomaly results.
*/
public class RestSearchAnomalyResultAction extends AbstractSearchAction<AnomalyResult> {

private static final String URL_PATH = AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI + "/results/_search";
private static final String URL_PATH = AnomalyDetectorPlugin.AD_BASE_LEGACY_DETECTORS_URI + "/results/_search";
private static final String URL_NEW_PATH = AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI + "/results/_search";
private final String SEARCH_ANOMALY_DETECTOR_ACTION = "search_anomaly_result";

public RestSearchAnomalyResultAction() {
super(URL_PATH, ALL_AD_RESULTS_INDEX_PATTERN, AnomalyResult.class, SearchAnomalyResultAction.INSTANCE);
super(
ImmutableList.of(URL_PATH, URL_NEW_PATH),
ALL_AD_RESULTS_INDEX_PATTERN,
AnomalyResult.class,
SearchAnomalyResultAction.INSTANCE
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

package com.amazon.opendistroforelasticsearch.ad.rest;

import static com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin.AD_BASE_LEGACY;
import static com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin.AD_BASE_URI;

import java.util.Arrays;
Expand Down Expand Up @@ -143,6 +144,10 @@ private ADStatsRequest getRequest(RestRequest request) {
public List<Route> routes() {
return ImmutableList
.of(
new Route(RestRequest.Method.GET, AD_BASE_LEGACY + "/{nodeId}/stats/"),
new Route(RestRequest.Method.GET, AD_BASE_LEGACY + "/{nodeId}/stats/{stat}"),
new Route(RestRequest.Method.GET, AD_BASE_LEGACY + "/stats/"),
new Route(RestRequest.Method.GET, AD_BASE_LEGACY + "/stats/{stat}"),
new Route(RestRequest.Method.GET, AD_BASE_URI + "/{nodeId}/stats/"),
new Route(RestRequest.Method.GET, AD_BASE_URI + "/{nodeId}/stats/{stat}"),
new Route(RestRequest.Method.GET, AD_BASE_URI + "/stats/"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected AnomalyDetector createRandomAnomalyDetector(Boolean refresh, Boolean w

protected AnomalyDetector createAnomalyDetector(AnomalyDetector detector, Boolean refresh, RestClient client) throws IOException {
Response response = TestHelpers
.makeRequest(client, "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null);
.makeRequest(client, "POST", TestHelpers.AD_BASE_LEGACY_DETECTORS_URI, ImmutableMap.of(), toHttpEntity(detector), null);
assertEquals("Create anomaly detector failed", RestStatus.CREATED, restStatus(response));

Map<String, Object> detectorJson = jsonXContent
Expand Down Expand Up @@ -138,16 +138,31 @@ protected AnomalyDetector createAnomalyDetector(AnomalyDetector detector, Boolea

protected Response startAnomalyDetector(String detectorId, RestClient client) throws IOException {
return TestHelpers
.makeRequest(client, "POST", TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId + "/_start", ImmutableMap.of(), "", null);
.makeRequest(
client,
"POST",
TestHelpers.AD_BASE_LEGACY_DETECTORS_URI + "/" + detectorId + "/_start",
ImmutableMap.of(),
"",
null
);
}

protected Response stopAnomalyDetector(String detectorId, RestClient client) throws IOException {
return TestHelpers
.makeRequest(client, "POST", TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId + "/_stop", ImmutableMap.of(), "", null);
.makeRequest(
client,
"POST",
TestHelpers.AD_BASE_LEGACY_DETECTORS_URI + "/" + detectorId + "/_stop",
ImmutableMap.of(),
"",
null
);
}

protected Response deleteAnomalyDetector(String detectorId, RestClient client) throws IOException {
return TestHelpers.makeRequest(client, "DELETE", TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId, ImmutableMap.of(), "", null);
return TestHelpers
.makeRequest(client, "DELETE", TestHelpers.AD_BASE_LEGACY_DETECTORS_URI + "/" + detectorId, ImmutableMap.of(), "", null);
}

protected Response previewAnomalyDetector(String detectorId, RestClient client, AnomalyDetectorExecutionInput input)
Expand Down Expand Up @@ -187,7 +202,7 @@ public ToXContentObject[] getAnomalyDetector(
.makeRequest(
client,
"GET",
TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId + "?job=" + returnJob + "&task=" + returnTask,
TestHelpers.AD_BASE_LEGACY_DETECTORS_URI + "/" + detectorId + "?job=" + returnJob + "&task=" + returnTask,
null,
"",
ImmutableList.of(header)
Expand Down Expand Up @@ -289,7 +304,14 @@ public Response getDetectorProfile(String detectorId, boolean all, String custom
.makeRequest(
client,
"GET",
TestHelpers.AD_BASE_DETECTORS_URI + "/" + detectorId + "/" + RestHandlerUtils.PROFILE + customizedProfile + "?_all=" + all,
TestHelpers.AD_BASE_LEGACY_DETECTORS_URI
+ "/"
+ detectorId
+ "/"
+ RestHandlerUtils.PROFILE
+ customizedProfile
+ "?_all="
+ all,
null,
"",
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
Expand All @@ -309,7 +331,7 @@ public Response getSearchDetectorCount() throws IOException {
.makeRequest(
client(),
"GET",
TestHelpers.AD_BASE_DETECTORS_URI + "/" + RestHandlerUtils.COUNT,
TestHelpers.AD_BASE_LEGACY_DETECTORS_URI + "/" + RestHandlerUtils.COUNT,
null,
"",
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
Expand All @@ -321,7 +343,7 @@ public Response getSearchDetectorMatch(String name) throws IOException {
.makeRequest(
client(),
"GET",
TestHelpers.AD_BASE_DETECTORS_URI + "/" + RestHandlerUtils.MATCH,
TestHelpers.AD_BASE_LEGACY_DETECTORS_URI + "/" + RestHandlerUtils.MATCH,
ImmutableMap.of("name", name),
"",
ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

package com.amazon.opendistroforelasticsearch.ad;

import static com.amazon.opendistroforelasticsearch.ad.TestHelpers.AD_BASE_DETECTORS_URI;
import static com.amazon.opendistroforelasticsearch.ad.TestHelpers.AD_BASE_LEGACY_DETECTORS_URI;
import static com.amazon.opendistroforelasticsearch.ad.settings.AnomalyDetectorSettings.BATCH_TASK_PIECE_INTERVAL_SECONDS;

import java.io.IOException;
Expand Down Expand Up @@ -76,7 +76,14 @@ public ToXContentObject[] getHistoricalAnomalyDetector(String detectorId, boolea

public ADTaskProfile getADTaskProfile(String detectorId) throws IOException {
Response profileResponse = TestHelpers
.makeRequest(client(), "GET", AD_BASE_DETECTORS_URI + "/" + detectorId + "/_profile/ad_task", ImmutableMap.of(), "", null);
.makeRequest(
client(),
"GET",
AD_BASE_LEGACY_DETECTORS_URI + "/" + detectorId + "/_profile/ad_task",
ImmutableMap.of(),
"",
null
);
return parseADTaskProfile(profileResponse);
}

Expand Down
Loading