Skip to content

Commit b3a4218

Browse files
Convert watcher license object to LicensedFeature (#78710) (#78795)
* Convert watcher license object to LicensedFeature (#78710) This commit moves the watcher license checks to use the new LicensedFeature class. * checkstyle Co-authored-by: Elastic Machine <[email protected]>
1 parent 3e3d974 commit b3a4218

File tree

8 files changed

+48
-25
lines changed

8 files changed

+48
-25
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/license/XPackLicenseState.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public enum Feature {
4949
SECURITY_AUTHORIZATION_REALM(OperationMode.PLATINUM, true),
5050
SECURITY_AUTHORIZATION_ENGINE(OperationMode.PLATINUM, true),
5151

52-
WATCHER(OperationMode.STANDARD, true),
53-
// TODO: should just check WATCHER directly?
5452
MONITORING_CLUSTER_ALERTS(OperationMode.STANDARD, true),
5553
MONITORING_UPDATE_RETENTION(OperationMode.STANDARD, false),
5654

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.core.watcher;
9+
10+
import org.elasticsearch.license.License;
11+
import org.elasticsearch.license.LicensedFeature;
12+
13+
public class WatcherConstants {
14+
15+
public static final LicensedFeature.Momentary WATCHER_FEATURE =
16+
LicensedFeature.momentary(null, "watcher", License.OperationMode.STANDARD);
17+
18+
// no construction
19+
private WatcherConstants() {}
20+
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/actions/throttler/ActionThrottler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.elasticsearch.core.Nullable;
1010
import org.elasticsearch.core.TimeValue;
1111
import org.elasticsearch.license.XPackLicenseState;
12+
import org.elasticsearch.xpack.core.watcher.WatcherConstants;
1213
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
1314

1415
import java.time.Clock;
@@ -39,7 +40,7 @@ public TimeValue throttlePeriod() {
3940

4041
@Override
4142
public Result throttle(String actionId, WatchExecutionContext ctx) {
42-
if (licenseState.checkFeature(XPackLicenseState.Feature.WATCHER) == false) {
43+
if (WatcherConstants.WATCHER_FEATURE.check(licenseState) == false) {
4344
return Result.throttle(LICENSE, "watcher license does not allow action execution");
4445
}
4546
if (periodThrottler != null) {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/watcher/actions/throttler/WatchThrottlerTests.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
*/
77
package org.elasticsearch.xpack.core.watcher.actions.throttler;
88

9-
import org.elasticsearch.license.XPackLicenseState;
10-
import org.elasticsearch.license.XPackLicenseState.Feature;
9+
import org.elasticsearch.license.MockLicenseState;
1110
import org.elasticsearch.test.ESTestCase;
11+
import org.elasticsearch.xpack.core.watcher.WatcherConstants;
1212
import org.elasticsearch.xpack.core.watcher.execution.WatchExecutionContext;
1313

1414
import static org.hamcrest.Matchers.is;
@@ -25,8 +25,8 @@ public void testThrottleDueToAck() throws Exception {
2525
when(periodThrottler.throttle("_action", ctx)).thenReturn(Throttler.Result.NO);
2626
Throttler.Result expectedResult = Throttler.Result.throttle(Throttler.Type.ACK, "_reason");
2727
when(ackThrottler.throttle("_action", ctx)).thenReturn(expectedResult);
28-
XPackLicenseState licenseState = mock(XPackLicenseState.class);
29-
when(licenseState.checkFeature(Feature.WATCHER)).thenReturn(true);
28+
MockLicenseState licenseState = mock(MockLicenseState.class);
29+
when(licenseState.isAllowed(WatcherConstants.WATCHER_FEATURE)).thenReturn(true);
3030
ActionThrottler throttler = new ActionThrottler(periodThrottler, ackThrottler, licenseState);
3131
Throttler.Result result = throttler.throttle("_action", ctx);
3232
assertThat(result, notNullValue());
@@ -40,8 +40,8 @@ public void testThrottleDueToPeriod() throws Exception {
4040
Throttler.Result expectedResult = Throttler.Result.throttle(Throttler.Type.PERIOD, "_reason");
4141
when(periodThrottler.throttle("_action", ctx)).thenReturn(expectedResult);
4242
when(ackThrottler.throttle("_action", ctx)).thenReturn(Throttler.Result.NO);
43-
XPackLicenseState licenseState = mock(XPackLicenseState.class);
44-
when(licenseState.checkFeature(Feature.WATCHER)).thenReturn(true);
43+
MockLicenseState licenseState = mock(MockLicenseState.class);
44+
when(licenseState.isAllowed(WatcherConstants.WATCHER_FEATURE)).thenReturn(true);
4545
ActionThrottler throttler = new ActionThrottler(periodThrottler, ackThrottler, licenseState);
4646
Throttler.Result result = throttler.throttle("_action", ctx);
4747
assertThat(result, notNullValue());
@@ -56,8 +56,8 @@ public void testThrottleDueAckAndPeriod() throws Exception {
5656
when(periodThrottler.throttle("_action", ctx)).thenReturn(periodResult);
5757
Throttler.Result ackResult = Throttler.Result.throttle(Throttler.Type.ACK, "_reason_ack");
5858
when(ackThrottler.throttle("_action", ctx)).thenReturn(ackResult);
59-
XPackLicenseState licenseState = mock(XPackLicenseState.class);
60-
when(licenseState.checkFeature(Feature.WATCHER)).thenReturn(true);
59+
MockLicenseState licenseState = mock(MockLicenseState.class);
60+
when(licenseState.isAllowed(WatcherConstants.WATCHER_FEATURE)).thenReturn(true);
6161
ActionThrottler throttler = new ActionThrottler(periodThrottler, ackThrottler, licenseState);
6262
Throttler.Result result = throttler.throttle("_action", ctx);
6363
assertThat(result, notNullValue());
@@ -71,8 +71,8 @@ public void testNoThrottle() throws Exception {
7171
WatchExecutionContext ctx = mock(WatchExecutionContext.class);
7272
when(periodThrottler.throttle("_action", ctx)).thenReturn(Throttler.Result.NO);
7373
when(ackThrottler.throttle("_action", ctx)).thenReturn(Throttler.Result.NO);
74-
XPackLicenseState licenseState = mock(XPackLicenseState.class);
75-
when(licenseState.checkFeature(Feature.WATCHER)).thenReturn(true);
74+
MockLicenseState licenseState = mock(MockLicenseState.class);
75+
when(licenseState.isAllowed(WatcherConstants.WATCHER_FEATURE)).thenReturn(true);
7676
ActionThrottler throttler = new ActionThrottler(periodThrottler, ackThrottler, licenseState);
7777
Throttler.Result result = throttler.throttle("_action", ctx);
7878
assertThat(result, notNullValue());
@@ -84,8 +84,8 @@ public void testWithoutPeriod() throws Exception {
8484
WatchExecutionContext ctx = mock(WatchExecutionContext.class);
8585
Throttler.Result ackResult = mock(Throttler.Result.class);
8686
when(ackThrottler.throttle("_action", ctx)).thenReturn(ackResult);
87-
XPackLicenseState licenseState = mock(XPackLicenseState.class);
88-
when(licenseState.checkFeature(Feature.WATCHER)).thenReturn(true);
87+
MockLicenseState licenseState = mock(MockLicenseState.class);
88+
when(licenseState.isAllowed(WatcherConstants.WATCHER_FEATURE)).thenReturn(true);
8989
ActionThrottler throttler = new ActionThrottler(null, ackThrottler, licenseState);
9090
Throttler.Result result = throttler.throttle("_action", ctx);
9191
assertThat(result, notNullValue());
@@ -97,8 +97,8 @@ public void testThatRestrictedLicenseReturnsCorrectResult() throws Exception {
9797
WatchExecutionContext ctx = mock(WatchExecutionContext.class);
9898
Throttler.Result ackResult = mock(Throttler.Result.class);
9999
when(ackThrottler.throttle("_action", ctx)).thenReturn(ackResult);
100-
XPackLicenseState licenseState = mock(XPackLicenseState.class);
101-
when(licenseState.checkFeature(Feature.WATCHER)).thenReturn(false);
100+
MockLicenseState licenseState = mock(MockLicenseState.class);
101+
when(licenseState.isAllowed(WatcherConstants.WATCHER_FEATURE)).thenReturn(false);
102102
ActionThrottler throttler = new ActionThrottler(null, ackThrottler, licenseState);
103103
Throttler.Result result = throttler.throttle("_action", ctx);
104104
assertThat(result, notNullValue());

x-pack/plugin/watcher/src/internalClusterTest/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.elasticsearch.xpack.core.XPackSettings;
4747
import org.elasticsearch.xpack.core.security.SecurityField;
4848
import org.elasticsearch.xpack.core.ssl.SSLService;
49+
import org.elasticsearch.xpack.core.watcher.WatcherConstants;
4950
import org.elasticsearch.xpack.core.watcher.WatcherState;
5051
import org.elasticsearch.xpack.core.watcher.client.WatcherClient;
5152
import org.elasticsearch.xpack.core.watcher.execution.ExecutionState;
@@ -530,7 +531,7 @@ protected void startWatcher() throws Exception {
530531
protected void ensureLicenseEnabled() throws Exception {
531532
assertBusy(() -> {
532533
for (XPackLicenseState licenseState : internalCluster().getInstances(XPackLicenseState.class)) {
533-
assertThat(licenseState.checkFeature(XPackLicenseState.Feature.WATCHER), is(true));
534+
assertThat(WatcherConstants.WATCHER_FEATURE.check(licenseState), is(true));
534535
}
535536
});
536537
}

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherFeatureSet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.xpack.core.XPackFeatureSet;
1717
import org.elasticsearch.xpack.core.XPackField;
1818
import org.elasticsearch.xpack.core.XPackSettings;
19+
import org.elasticsearch.xpack.core.watcher.WatcherConstants;
1920
import org.elasticsearch.xpack.core.watcher.WatcherFeatureSetUsage;
2021
import org.elasticsearch.xpack.core.watcher.client.WatcherClient;
2122
import org.elasticsearch.xpack.core.watcher.common.stats.Counters;
@@ -50,7 +51,7 @@ public String name() {
5051

5152
@Override
5253
public boolean available() {
53-
return licenseState != null && licenseState.isAllowed(XPackLicenseState.Feature.WATCHER);
54+
return licenseState != null && WatcherConstants.WATCHER_FEATURE.checkWithoutTracking(licenseState);
5455
}
5556

5657
@Override

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/WatcherTransportAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.threadpool.ThreadPool;
1919
import org.elasticsearch.transport.TransportService;
2020
import org.elasticsearch.xpack.core.XPackField;
21+
import org.elasticsearch.xpack.core.watcher.WatcherConstants;
2122

2223
abstract class WatcherTransportAction<Request extends ActionRequest, Response extends ActionResponse>
2324
extends HandledTransportAction<Request, Response> {
@@ -36,7 +37,7 @@ protected String executor() {
3637

3738
@Override
3839
protected final void doExecute(Task task, final Request request, ActionListener<Response> listener) {
39-
if (licenseState.checkFeature(XPackLicenseState.Feature.WATCHER)) {
40+
if (WatcherConstants.WATCHER_FEATURE.check(licenseState)) {
4041
doExecute(request, listener);
4142
} else {
4243
listener.onFailure(LicenseUtils.newComplianceException(XPackField.WATCHER));

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherFeatureSetTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1616
import org.elasticsearch.common.settings.Settings;
1717
import org.elasticsearch.common.util.concurrent.ThreadContext;
18+
import org.elasticsearch.common.xcontent.ObjectPath;
1819
import org.elasticsearch.common.xcontent.ToXContent;
1920
import org.elasticsearch.common.xcontent.XContentBuilder;
20-
import org.elasticsearch.license.XPackLicenseState;
21+
import org.elasticsearch.license.MockLicenseState;
2122
import org.elasticsearch.test.ESTestCase;
2223
import org.elasticsearch.threadpool.ThreadPool;
2324
import org.elasticsearch.xpack.core.XPackFeatureSet;
25+
import org.elasticsearch.xpack.core.watcher.WatcherConstants;
2426
import org.elasticsearch.xpack.core.watcher.WatcherFeatureSetUsage;
2527
import org.elasticsearch.xpack.core.watcher.WatcherMetadata;
2628
import org.elasticsearch.xpack.core.watcher.common.stats.Counters;
27-
import org.elasticsearch.common.xcontent.ObjectPath;
2829
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
2930
import org.elasticsearch.xpack.core.watcher.transport.actions.stats.WatcherStatsAction;
3031
import org.elasticsearch.xpack.core.watcher.transport.actions.stats.WatcherStatsResponse;
@@ -47,12 +48,12 @@
4748

4849
public class WatcherFeatureSetTests extends ESTestCase {
4950

50-
private XPackLicenseState licenseState;
51+
private MockLicenseState licenseState;
5152
private Client client;
5253

5354
@Before
5455
public void init() throws Exception {
55-
licenseState = mock(XPackLicenseState.class);
56+
licenseState = mock(MockLicenseState.class);
5657
client = mock(Client.class);
5758
ThreadPool threadPool = mock(ThreadPool.class);
5859
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
@@ -63,7 +64,7 @@ public void init() throws Exception {
6364
public void testAvailable() {
6465
WatcherFeatureSet featureSet = new WatcherFeatureSet(Settings.EMPTY, licenseState, client);
6566
boolean available = randomBoolean();
66-
when(licenseState.isAllowed(XPackLicenseState.Feature.WATCHER)).thenReturn(available);
67+
when(licenseState.isAllowed(WatcherConstants.WATCHER_FEATURE)).thenReturn(available);
6768
assertThat(featureSet.available(), is(available));
6869
}
6970

0 commit comments

Comments
 (0)