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

Update REST resources to follow new OpenSearch naming convention #14

Merged
merged 1 commit into from
May 20, 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 @@ -30,6 +30,7 @@
import static java.util.Collections.unmodifiableList;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -59,28 +60,69 @@ public class PerformanceAnalyzerClusterConfigAction extends BaseRestHandler {
public static final String CURRENT = "currentPerformanceAnalyzerClusterState";
public static final String BATCH_METRICS_RETENTION_PERIOD_MINUTES =
"batchMetricsRetentionPeriodMinutes";
public static final String PA_CLUSTER_CONFIG_PATH =
"/_opendistro/_performanceanalyzer/cluster/config";
public static final String ENABLED = "enabled";
public static final String SHARDS_PER_COLLECTION = "shardsPerCollection";

public static final String PA_CLUSTER_CONFIG_PATH = RestConfig.PA_BASE_URI + "/cluster/config";
public static final String RCA_CLUSTER_CONFIG_PATH =
"/_opendistro/_performanceanalyzer/rca/cluster/config";
RestConfig.PA_BASE_URI + "/rca/cluster/config";
public static final String LOGGING_CLUSTER_CONFIG_PATH =
"/_opendistro/_performanceanalyzer/logging/cluster/config";
RestConfig.PA_BASE_URI + "/logging/cluster/config";
public static final String BATCH_METRICS_CLUSTER_CONFIG_PATH =
"/_opendistro/_performanceanalyzer/batch/cluster/config";
public static final String ENABLED = "enabled";
public static final String SHARDS_PER_COLLECTION = "shardsPerCollection";
RestConfig.PA_BASE_URI + "/batch/cluster/config";

private static final List<Route> ROUTES =
public static final String LEGACY_PA_CLUSTER_CONFIG_PATH =
RestConfig.LEGACY_PA_BASE_URI + "/cluster/config";
public static final String LEGACY_RCA_CLUSTER_CONFIG_PATH =
RestConfig.LEGACY_PA_BASE_URI + "/rca/cluster/config";
public static final String LEGACY_LOGGING_CLUSTER_CONFIG_PATH =
RestConfig.LEGACY_PA_BASE_URI + "/logging/cluster/config";
public static final String LEGACY_BATCH_METRICS_CLUSTER_CONFIG_PATH =
RestConfig.LEGACY_PA_BASE_URI + "/batch/cluster/config";

private static final List<ReplacedRoute> REPLACED_ROUTES =
unmodifiableList(
asList(
new Route(RestRequest.Method.GET, PA_CLUSTER_CONFIG_PATH),
new Route(RestRequest.Method.POST, PA_CLUSTER_CONFIG_PATH),
new Route(RestRequest.Method.GET, RCA_CLUSTER_CONFIG_PATH),
new Route(RestRequest.Method.POST, RCA_CLUSTER_CONFIG_PATH),
new Route(RestRequest.Method.GET, LOGGING_CLUSTER_CONFIG_PATH),
new Route(RestRequest.Method.POST, LOGGING_CLUSTER_CONFIG_PATH),
new Route(RestRequest.Method.GET, BATCH_METRICS_CLUSTER_CONFIG_PATH),
new Route(RestRequest.Method.POST, BATCH_METRICS_CLUSTER_CONFIG_PATH)));
new ReplacedRoute(
RestRequest.Method.GET,
PA_CLUSTER_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_PA_CLUSTER_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
PA_CLUSTER_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_PA_CLUSTER_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.GET,
RCA_CLUSTER_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_RCA_CLUSTER_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
RCA_CLUSTER_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_RCA_CLUSTER_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.GET,
LOGGING_CLUSTER_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_LOGGING_CLUSTER_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
LOGGING_CLUSTER_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_LOGGING_CLUSTER_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.GET,
BATCH_METRICS_CLUSTER_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_BATCH_METRICS_CLUSTER_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
BATCH_METRICS_CLUSTER_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_BATCH_METRICS_CLUSTER_CONFIG_PATH)));

private final PerformanceAnalyzerClusterSettingHandler clusterSettingHandler;
private final NodeStatsSettingHandler nodeStatsSettingHandler;
Expand All @@ -105,7 +147,12 @@ public String getName() {

@Override
public List<Route> routes() {
return ROUTES;
return Collections.emptyList();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return REPLACED_ROUTES;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static java.util.Collections.unmodifiableList;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -63,24 +64,63 @@ public class PerformanceAnalyzerConfigAction extends BaseRestHandler {
"batchMetricsRetentionPeriodMinutes";
public static final String PERFORMANCE_ANALYZER_CONFIG_ACTION =
"PerformanceAnalyzer_Config_Action";
public static final String RCA_CONFIG_PATH = "/_opendistro/_performanceanalyzer/rca/config";
public static final String PA_CONFIG_PATH = "/_opendistro/_performanceanalyzer/config";
public static final String LOGGING_CONFIG_PATH =
"/_opendistro/_performanceanalyzer/logging/config";
public static final String BATCH_METRICS_CONFIG_PATH =
"/_opendistro/_performanceanalyzer/batch/config";

private static final List<Route> ROUTES =

public static final String RCA_CONFIG_PATH = RestConfig.PA_BASE_URI + "/rca/config";
public static final String PA_CONFIG_PATH = RestConfig.PA_BASE_URI + "/config";
public static final String LOGGING_CONFIG_PATH = RestConfig.PA_BASE_URI + "/logging/config";
public static final String BATCH_METRICS_CONFIG_PATH = RestConfig.PA_BASE_URI + "/batch/config";

public static final String LEGACY_RCA_CONFIG_PATH =
Copy link
Member

Choose a reason for hiding this comment

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

These LEGACY_PA_BASE_URI should really be LEGACY_OPENDISTRO_PA_BASE_URI. We weren't super clear in the doc, I fixed it in opensearch-project/opensearch-plugins#33.

RestConfig.LEGACY_PA_BASE_URI + "/rca/config";
public static final String LEGACY_PA_CONFIG_PATH = RestConfig.LEGACY_PA_BASE_URI + "/config";
public static final String LEGACY_LOGGING_CONFIG_PATH =
RestConfig.LEGACY_PA_BASE_URI + "/logging/config";
public static final String LEGACY_BATCH_METRICS_CONFIG_PATH =
RestConfig.LEGACY_PA_BASE_URI + "/batch/config";

private static final List<ReplacedRoute> REPLACED_ROUTES =
unmodifiableList(
asList(
new Route(RestRequest.Method.GET, PA_CONFIG_PATH),
new Route(RestRequest.Method.POST, PA_CONFIG_PATH),
new Route(RestRequest.Method.GET, RCA_CONFIG_PATH),
new Route(RestRequest.Method.POST, RCA_CONFIG_PATH),
new Route(RestRequest.Method.GET, LOGGING_CONFIG_PATH),
new Route(RestRequest.Method.POST, LOGGING_CONFIG_PATH),
new Route(RestRequest.Method.GET, BATCH_METRICS_CONFIG_PATH),
new Route(RestRequest.Method.POST, BATCH_METRICS_CONFIG_PATH)));
new ReplacedRoute(
RestRequest.Method.GET,
PA_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_PA_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
PA_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_PA_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.GET,
RCA_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_RCA_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
RCA_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_RCA_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.GET,
LOGGING_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_LOGGING_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
LOGGING_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_LOGGING_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.GET,
BATCH_METRICS_CONFIG_PATH,
RestRequest.Method.GET,
LEGACY_BATCH_METRICS_CONFIG_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
BATCH_METRICS_CONFIG_PATH,
RestRequest.Method.POST,
LEGACY_BATCH_METRICS_CONFIG_PATH)));

public static PerformanceAnalyzerConfigAction getInstance() {
return instance;
Expand All @@ -93,7 +133,12 @@ public static void setInstance(

@Override
public List<Route> routes() {
return ROUTES;
return Collections.emptyList();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return REPLACED_ROUTES;
}

@Inject
Expand All @@ -119,7 +164,8 @@ protected RestChannelConsumer prepareRequest(final RestRequest request, final No
performanceAnalyzerController.isPerformanceAnalyzerEnabled());
if (value instanceof Boolean) {
boolean shouldEnable = (Boolean) value;
if (request.path().contains(RCA_CONFIG_PATH)) {
if (request.path().contains(RCA_CONFIG_PATH)
|| request.path().contains(LEGACY_RCA_CONFIG_PATH)) {
// If RCA needs to be turned on, we need to have PA turned on also.
// If this is not the case, return error.
if (shouldEnable
Expand All @@ -129,15 +175,17 @@ protected RestChannelConsumer prepareRequest(final RestRequest request, final No
}

performanceAnalyzerController.updateRcaState(shouldEnable);
} else if (request.path().contains(LOGGING_CONFIG_PATH)) {
} else if (request.path().contains(LOGGING_CONFIG_PATH)
|| request.path().contains(LEGACY_LOGGING_CONFIG_PATH)) {
if (shouldEnable
&& !performanceAnalyzerController.isPerformanceAnalyzerEnabled()) {
return getChannelConsumerWithError(
"Error: PA not enabled. Enable PA before turning Logging on");
}

performanceAnalyzerController.updateLoggingState(shouldEnable);
} else if (request.path().contains(BATCH_METRICS_CONFIG_PATH)) {
} else if (request.path().contains(BATCH_METRICS_CONFIG_PATH)
|| request.path().contains(LEGACY_BATCH_METRICS_CONFIG_PATH)) {
if (shouldEnable
&& !performanceAnalyzerController.isPerformanceAnalyzerEnabled()) {
return getChannelConsumerWithError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,26 @@ public class PerformanceAnalyzerOverridesClusterConfigAction extends BaseRestHan
private static final Logger LOG =
LogManager.getLogger(PerformanceAnalyzerOverridesClusterConfigAction.class);
public static final String PA_CONFIG_OVERRIDES_PATH =
"/_opendistro/_performanceanalyzer/override/cluster/config";
RestConfig.PA_BASE_URI + "/override/cluster/config";
public static final String LEGACY_PA_CONFIG_OVERRIDES_PATH =
RestConfig.LEGACY_PA_BASE_URI + "/override/cluster/config";
private static final String OVERRIDES_FIELD = "overrides";
private static final String REASON_FIELD = "reason";
public static final String OVERRIDE_TRIGGERED_FIELD = "override triggered";

private static final List<Route> ROUTES =
private static final List<ReplacedRoute> REPLACED_ROUTES =
unmodifiableList(
asList(
new Route(RestRequest.Method.GET, PA_CONFIG_OVERRIDES_PATH),
new Route(RestRequest.Method.POST, PA_CONFIG_OVERRIDES_PATH)));
new ReplacedRoute(
RestRequest.Method.GET,
PA_CONFIG_OVERRIDES_PATH,
RestRequest.Method.GET,
LEGACY_PA_CONFIG_OVERRIDES_PATH),
new ReplacedRoute(
RestRequest.Method.POST,
PA_CONFIG_OVERRIDES_PATH,
RestRequest.Method.POST,
LEGACY_PA_CONFIG_OVERRIDES_PATH)));

private final ConfigOverridesClusterSettingHandler configOverridesClusterSettingHandler;
private final ConfigOverridesWrapper overridesWrapper;
Expand All @@ -81,7 +91,12 @@ public PerformanceAnalyzerOverridesClusterConfigAction(

@Override
public List<Route> routes() {
return ROUTES;
return Collections.emptyList();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return REPLACED_ROUTES;
}

/** @return the name of this handler. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,22 @@ public class PerformanceAnalyzerResourceProvider extends BaseRestHandler {
LogManager.getLogger(PerformanceAnalyzerResourceProvider.class);

private static final int HTTP_CLIENT_CONNECTION_TIMEOUT_MILLIS = 200;
private static final String AGENT_PATH = "/_opendistro/_performanceanalyzer/_agent/";
private static final String AGENT_PATH = RestConfig.PA_BASE_URI + "/_agent/";
private static final String LEGACY_AGENT_PATH = RestConfig.LEGACY_PA_BASE_URI + "/_agent/";
private static final String DEFAULT_PORT_NUMBER = "9600";

private String portNumber;
private final boolean isHttpsEnabled;
private static Set<String> SUPPORTED_REDIRECTIONS =
ImmutableSet.of("rca", "metrics", "batch", "actions");
private static final List<Route> ROUTES =
Collections.unmodifiableList(
Collections.singletonList(
new Route(Method.GET, AGENT_PATH + "{redirectEndpoint}")));

private static final List<ReplacedRoute> REPLACED_ROUTES =
Collections.singletonList(
new ReplacedRoute(
Method.GET,
AGENT_PATH + "{redirectEndpoint}",
Method.GET,
LEGACY_AGENT_PATH + "{redirectEndpoint}"));

@Inject
public PerformanceAnalyzerResourceProvider(Settings settings, RestController controller) {
Expand Down Expand Up @@ -141,7 +146,12 @@ public String getName() {
/** {@inheritDoc} */
@Override
public List<Route> routes() {
return ROUTES;
return Collections.emptyList();
}

@Override
public List<ReplacedRoute> replacedRoutes() {
return REPLACED_ROUTES;
}

@Override
Expand Down Expand Up @@ -234,7 +244,7 @@ public URL getAgentUri(RestRequest request) throws IOException {
String redirectEndpoint = request.param("redirectEndpoint");
String urlScheme = isHttpsEnabled ? "https://" : "http://";
String redirectBasePath =
urlScheme + "localhost:" + portNumber + "/_opendistro/_performanceanalyzer/";
urlScheme + "localhost:" + portNumber + RestConfig.PA_BASE_URI + "/";
// Need to register all params in OpenSearch request else opensearch throws
// illegal_argument_exception
for (String key : request.params().keySet()) {
Expand All @@ -243,7 +253,15 @@ public URL getAgentUri(RestRequest request) throws IOException {

// Add Handler whenever add new redirectAgent path
if (SUPPORTED_REDIRECTIONS.contains(redirectEndpoint)) {
String uri = redirectBasePath + request.uri().split(AGENT_PATH)[1];
String uri = null;
if (request.uri().contains(AGENT_PATH)) {
uri = redirectBasePath + request.uri().split(AGENT_PATH)[1];
} else if (request.uri().contains(LEGACY_AGENT_PATH)) {
uri = redirectBasePath + request.uri().split(LEGACY_AGENT_PATH)[1];
} else {
throw new IOException();
}

return new URL(uri);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package org.opensearch.performanceanalyzer.http_action.config;

/** Shared constants and configuration used by the REST controllers defined by this plugin. */
public class RestConfig {
public static final String PA_BASE_URI = "/_plugins/_performanceanalyzer";
public static final String LEGACY_PA_BASE_URI = "/_opendistro/_performanceanalyzer";
}
Loading