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 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 @@ -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 LEGACY_AD_BASE = "/_opendistro/_anomaly_detection";
public static final String LEGACY_OPENDISTRO_AD_BASE_URI = LEGACY_AD_BASE + "/detectors";
public static final String AD_BASE_URI = "/_plugins/_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,8 +32,10 @@
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;

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

import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.action.ActionType;
Expand All @@ -59,7 +61,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 +69,23 @@ 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 List<Pair<String, String>> deprecatedPaths;
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,
List<Pair<String, String>> deprecatedPaths,
String index,
Class<T> clazz,
ActionType<SearchResponse> actionType
) {
this.index = index;
this.clazz = clazz;
this.urlPath = urlPath;
this.urlPaths = urlPaths;
this.deprecatedPaths = deprecatedPaths;
this.actionType = actionType;
}

Expand Down Expand Up @@ -126,6 +135,26 @@ 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;
}

@Override
public List<ReplacedRoute> replacedRoutes() {
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
List<ReplacedRoute> replacedRoutes = new ArrayList<>();
for (Pair<String, String> deprecatedPath : deprecatedPaths) {
replacedRoutes
.add(
new ReplacedRoute(RestRequest.Method.POST, deprecatedPath.getKey(), RestRequest.Method.POST, deprecatedPath.getValue())
);
replacedRoutes
.add(new ReplacedRoute(RestRequest.Method.GET, deprecatedPath.getKey(), RestRequest.Method.GET, deprecatedPath.getValue()));

}
return replacedRoutes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,26 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli

@Override
public List<Route> routes() {
return ImmutableList.of();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return ImmutableList
.of(
// start AD job
new Route(
// start AD Job
new ReplacedRoute(
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, START_JOB)
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, START_JOB),
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI, DETECTOR_ID, START_JOB)
),
// stop AD job
new Route(
// stop AD Job
new ReplacedRoute(
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, STOP_JOB),
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, STOP_JOB)
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI, DETECTOR_ID, STOP_JOB)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,19 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli

@Override
public List<Route> routes() {
return ImmutableList.of();
saratvemulapalli marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return ImmutableList
.of(
// delete anomaly detector document
new Route(
new ReplacedRoute(
RestRequest.Method.DELETE,
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID),
RestRequest.Method.DELETE,
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID)
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI, DETECTOR_ID)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,19 @@ private String validateAdExecutionInput(AnomalyDetectorExecutionInput input) {

@Override
public List<Route> routes() {
return ImmutableList.of();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return ImmutableList
.of(
// get AD result, for regular run
new Route(
new ReplacedRoute(
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, RUN),
RestRequest.Method.POST,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, RUN)
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI, DETECTOR_ID, RUN)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,38 @@ 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);
return ImmutableList.of();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
String path = String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_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(
new ReplacedRoute(RestRequest.Method.GET, newPath, RestRequest.Method.GET, path),
new ReplacedRoute(RestRequest.Method.HEAD, newPath, RestRequest.Method.HEAD, path),
new ReplacedRoute(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, PROFILE)
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, PROFILE),
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/{%s}/%s", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_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(
new ReplacedRoute(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/{%s}/%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, PROFILE, TYPE),
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/{%s}/%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID, PROFILE, TYPE)
String
.format(
Locale.ROOT,
"%s/{%s}/%s/{%s}",
AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI,
DETECTOR_ID,
PROFILE,
TYPE
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,26 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli

@Override
public List<Route> routes() {
return ImmutableList.of();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return ImmutableList
.of(
// Create
new Route(RestRequest.Method.POST, AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI),
// update
new Route(
new ReplacedRoute(
RestRequest.Method.POST,
AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI,
RestRequest.Method.POST,
AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI
),
// Update
new ReplacedRoute(
RestRequest.Method.PUT,
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID),
RestRequest.Method.PUT,
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, DETECTOR_ID)
String.format(Locale.ROOT, "%s/{%s}", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI, DETECTOR_ID)
)
);
}
Expand All @@ -178,7 +190,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.LEGACY_AD_BASE, response.getId());
bytesRestResponse.addHeader("Location", location);
}
return bytesRestResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ private String validateAdExecutionInput(AnomalyDetectorExecutionInput input) {

@Override
public List<RestHandler.Route> routes() {
return ImmutableList.of();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return ImmutableList
.of(
// preview detector
new Route(
// Preview Detector
new ReplacedRoute(
RestRequest.Method.POST,
String
.format(
Expand All @@ -133,6 +138,15 @@ public List<RestHandler.Route> routes() {
AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI,
RestHandlerUtils.DETECTOR_ID,
PREVIEW
),
RestRequest.Method.POST,
String
.format(
Locale.ROOT,
"%s/{%s}/%s",
AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI,
RestHandlerUtils.DETECTOR_ID,
PREVIEW
)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,31 @@

package com.amazon.opendistroforelasticsearch.ad.rest;

import org.apache.commons.lang3.tuple.Pair;

import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin;
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 LEGACY_URL_PATH = AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI + "/tasks/_search";
private static final String URL_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(),
ImmutableList.of(Pair.of(URL_PATH, LEGACY_URL_PATH)),
CommonName.DETECTION_STATE_INDEX,
ADTask.class,
SearchADTasksAction.INSTANCE
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,30 @@

import static com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX;

import org.apache.commons.lang3.tuple.Pair;

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 LEGACY_URL_PATH = AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI + "/_search";
private static final String URL_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(),
ImmutableList.of(Pair.of(URL_PATH, LEGACY_URL_PATH)),
ANOMALY_DETECTORS_INDEX,
AnomalyDetector.class,
SearchAnomalyDetectorAction.INSTANCE
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,26 @@ protected RestChannelConsumer prepareRequest(RestRequest request, org.opensearch

@Override
public List<RestHandler.Route> routes() {
return ImmutableList.of();
}

@Override
public List<RestHandler.ReplacedRoute> replacedRoutes() {
return ImmutableList
.of(
// get the count of number of detectors
new RestHandler.Route(
new ReplacedRoute(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, COUNT)
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, COUNT),
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI, COUNT)
),
// get if a detector name exists with name
new RestHandler.Route(
new ReplacedRoute(
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, MATCH),
RestRequest.Method.GET,
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, MATCH)
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.LEGACY_OPENDISTRO_AD_BASE_URI, MATCH)
)
);
}
Expand Down
Loading