diff --git a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/GraphFeatureSet.java b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/GraphFeatureSet.java index b83db912369..008a6942e11 100644 --- a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/GraphFeatureSet.java +++ b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/GraphFeatureSet.java @@ -10,7 +10,6 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.xpack.XPackFeatureSet; import java.io.IOException; @@ -27,7 +26,7 @@ public class GraphFeatureSet implements XPackFeatureSet { public GraphFeatureSet(Settings settings, @Nullable GraphLicensee licensee, NamedWriteableRegistry namedWriteableRegistry) { this.enabled = Graph.enabled(settings); this.licensee = licensee; - namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); + namedWriteableRegistry.register(Usage.class, Usage.writeableName(Graph.NAME), Usage::new); } @Override @@ -51,14 +50,12 @@ public class GraphFeatureSet implements XPackFeatureSet { } @Override - public Usage usage() { + public XPackFeatureSet.Usage usage() { return new Usage(available(), enabled()); } static class Usage extends XPackFeatureSet.Usage { - static final String WRITEABLE_NAME = writeableName(Graph.NAME); - public Usage(StreamInput input) throws IOException { super(input); } @@ -66,19 +63,5 @@ public class GraphFeatureSet implements XPackFeatureSet { public Usage(boolean available, boolean enabled) { super(Graph.NAME, available, enabled); } - - @Override - public String getWriteableName() { - return WRITEABLE_NAME; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field(Field.AVAILABLE, available) - .field(Field.ENABLED, enabled) - .endObject(); - - } } } diff --git a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/MonitoringFeatureSet.java b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/MonitoringFeatureSet.java index 7d528bb9f12..c28118df0dd 100644 --- a/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/MonitoringFeatureSet.java +++ b/elasticsearch/x-pack/marvel/src/main/java/org/elasticsearch/marvel/MonitoringFeatureSet.java @@ -10,17 +10,15 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Writeable; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.marvel.agent.exporter.Exporter; import org.elasticsearch.marvel.agent.exporter.Exporters; -import org.elasticsearch.marvel.agent.exporter.http.HttpExporter; -import org.elasticsearch.marvel.agent.exporter.local.LocalExporter; import org.elasticsearch.xpack.XPackFeatureSet; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; /** * @@ -29,7 +27,7 @@ public class MonitoringFeatureSet implements XPackFeatureSet { private final boolean enabled; private final MonitoringLicensee licensee; - private final org.elasticsearch.marvel.agent.exporter.Exporters exporters; + private final Exporters exporters; @Inject public MonitoringFeatureSet(Settings settings, @Nullable MonitoringLicensee licensee, @Nullable Exporters exporters, @@ -37,7 +35,7 @@ public class MonitoringFeatureSet implements XPackFeatureSet { this.enabled = MonitoringSettings.ENABLED.get(settings); this.licensee = licensee; this.exporters = exporters; - namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); + namedWriteableRegistry.register(Usage.class, Usage.writeableName(Monitoring.NAME), Usage::new); } @Override @@ -61,123 +59,53 @@ public class MonitoringFeatureSet implements XPackFeatureSet { } @Override - public Usage usage() { + public XPackFeatureSet.Usage usage() { return new Usage(available(), enabled(), exportersUsage(exporters)); } - static Usage.Exporters exportersUsage(Exporters exporters) { + static Map exportersUsage(Exporters exporters) { if (exporters == null) { return null; } - int local = 0; - int http = 0; - int unknown = 0; + Map usage = new HashMap<>(); for (Exporter exporter : exporters) { if (exporter.config().enabled()) { - switch (exporter.type()) { - case LocalExporter.TYPE: - local++; - break; - case HttpExporter.TYPE: - http++; - break; - default: - unknown++; - } + String type = exporter.type(); + int count = (Integer) usage.getOrDefault(type, 0); + usage.put(type, count + 1); } } - return new Usage.Exporters(local, http, unknown); + return usage; } static class Usage extends XPackFeatureSet.Usage { - private static String WRITEABLE_NAME = writeableName(Monitoring.NAME); + private static final String ENABLED_EXPORTERS_XFIELD = "enabled_exporters"; - private @Nullable - Exporters exporters; + private @Nullable Map exporters; public Usage(StreamInput in) throws IOException { super(in); - exporters = new Exporters(in); + exporters = in.readMap(); } - public Usage(boolean available, boolean enabled, Exporters exporters) { + public Usage(boolean available, boolean enabled, Map exporters) { super(Monitoring.NAME, available, enabled); this.exporters = exporters; } - @Override - public boolean available() { - return available; - } - - @Override - public boolean enabled() { - return enabled; - } - - @Override - public String getWriteableName() { - return WRITEABLE_NAME; - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); - out.writeOptionalWriteable(exporters); + out.writeMap(exporters); } @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(Field.AVAILABLE, available); - builder.field(Field.ENABLED, enabled); + protected void innerXContent(XContentBuilder builder, Params params) throws IOException { + super.innerXContent(builder, params); if (exporters != null) { - builder.field(Field.ENABLED_EXPORTERS, exporters); + builder.field(ENABLED_EXPORTERS_XFIELD, exporters); } - return builder.endObject(); - } - - static class Exporters implements Writeable, ToXContent { - - private final int enabledLocalExporters; - private final int enabledHttpExporters; - private final int enabledUnknownExporters; - - public Exporters(StreamInput in) throws IOException { - this(in.readVInt(), in.readVInt(), in.readVInt()); - } - - public Exporters(int enabledLocalExporters, int enabledHttpExporters, int enabledUnknownExporters) { - this.enabledLocalExporters = enabledLocalExporters; - this.enabledHttpExporters = enabledHttpExporters; - this.enabledUnknownExporters = enabledUnknownExporters; - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(enabledLocalExporters); - out.writeVInt(enabledHttpExporters); - out.writeVInt(enabledUnknownExporters); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(Field.LOCAL, enabledLocalExporters); - builder.field(Field.HTTP, enabledHttpExporters); - if (enabledUnknownExporters > 0) { - builder.field(Field.UNKNOWN, enabledUnknownExporters); - } - return builder.endObject(); - } - } - - interface Field extends XPackFeatureSet.Usage.Field { - String ENABLED_EXPORTERS = "enabled_exporters"; - String LOCAL = "_local"; - String HTTP = "http"; - String UNKNOWN = "_unknown"; } } diff --git a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MonitoringFeatureSetTests.java b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MonitoringFeatureSetTests.java index 84f37f47fd2..108527eb584 100644 --- a/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MonitoringFeatureSetTests.java +++ b/elasticsearch/x-pack/marvel/src/test/java/org/elasticsearch/marvel/MonitoringFeatureSetTests.java @@ -7,10 +7,20 @@ package org.elasticsearch.marvel; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.marvel.agent.exporter.Exporter; import org.elasticsearch.marvel.agent.exporter.Exporters; +import org.elasticsearch.marvel.agent.exporter.http.HttpExporter; +import org.elasticsearch.marvel.agent.exporter.local.LocalExporter; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.XPackFeatureSet; +import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource; import org.junit.Before; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.core.Is.is; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.eq; @@ -42,21 +52,85 @@ public class MonitoringFeatureSetTests extends ESTestCase { public void testAvailable() throws Exception { MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licensee, exporters, namedWriteableRegistry); boolean available = randomBoolean(); - when(licensee.available()).thenReturn(available); + when(licensee.isAvailable()).thenReturn(available); assertThat(featureSet.available(), is(available)); } - public void testEnabled() throws Exception { + public void testEnabledSetting() throws Exception { boolean enabled = randomBoolean(); Settings.Builder settings = Settings.builder(); - if (enabled) { - if (randomBoolean()) { - settings.put("xpack.monitoring.enabled", enabled); - } - } else { - settings.put("xpack.monitoring.enabled", enabled); - } + settings.put("xpack.monitoring.enabled", enabled); MonitoringFeatureSet featureSet = new MonitoringFeatureSet(settings.build(), licensee, exporters, namedWriteableRegistry); assertThat(featureSet.enabled(), is(enabled)); } + + public void testEnabledDefault() throws Exception { + MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licensee, exporters, namedWriteableRegistry); + assertThat(featureSet.enabled(), is(true)); + } + + public void testUsage() throws Exception { + + List exporterList = new ArrayList<>(); + int localCount = randomIntBetween(0, 5); + for (int i = 0; i < localCount; i++) { + Exporter exporter = mockExporter(LocalExporter.TYPE, true); + exporterList.add(exporter); + if (randomBoolean()) { + exporter = mockExporter(LocalExporter.TYPE, false); + exporterList.add(exporter); + } + } + int httpCount = randomIntBetween(0, 5); + for (int i = 0; i < httpCount; i++) { + Exporter exporter = mockExporter(HttpExporter.TYPE, true); + exporterList.add(exporter); + if (randomBoolean()) { + exporter = mockExporter(HttpExporter.TYPE, false); + exporterList.add(exporter); + } + } + int xCount = randomIntBetween(0, 5); + String xType = randomAsciiOfLength(10); + for (int i = 0; i < xCount; i++) { + Exporter exporter = mockExporter(xType, true); + exporterList.add(exporter); + if (randomBoolean()) { + exporter = mockExporter(xType, false); + exporterList.add(exporter); + } + } + when(exporters.iterator()).thenReturn(exporterList.iterator()); + + MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licensee, exporters, namedWriteableRegistry); + XPackFeatureSet.Usage usage = featureSet.usage(); + assertThat(usage.name(), is(featureSet.name())); + assertThat(usage.enabled(), is(featureSet.enabled())); + XContentSource source = new XContentSource(usage); + assertThat(source.getValue("enabled_exporters"), is(notNullValue())); + if (localCount > 0) { + assertThat(source.getValue("enabled_exporters.local"), is(localCount)); + } else { + assertThat(source.getValue("enabled_exporters.local"), is(nullValue())); + } + if (httpCount > 0) { + assertThat(source.getValue("enabled_exporters.http"), is(httpCount)); + } else { + assertThat(source.getValue("enabled_exporters.http"), is(nullValue())); + } + if (xCount > 0) { + assertThat(source.getValue("enabled_exporters." + xType), is(xCount)); + } else { + assertThat(source.getValue("enabled_exporters." + xType), is(nullValue())); + } + } + + private Exporter mockExporter(String type, boolean enabled) { + Exporter exporter = mock(Exporter.class); + when(exporter.type()).thenReturn(type); + Exporter.Config enabledConfig = mock(Exporter.Config.class); + when(enabledConfig.enabled()).thenReturn(enabled); + when(exporter.config()).thenReturn(enabledConfig); + return exporter; + } } diff --git a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/SecurityFeatureSet.java b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/SecurityFeatureSet.java index 07eb6c86cf0..d721531659e 100644 --- a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/SecurityFeatureSet.java +++ b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/SecurityFeatureSet.java @@ -40,7 +40,7 @@ public class SecurityFeatureSet implements XPackFeatureSet { this.enabled = Security.enabled(settings); this.licenseState = licenseState; this.realms = realms; - namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); + namedWriteableRegistry.register(Usage.class, Usage.writeableName(Security.NAME), Usage::new); } @Override @@ -64,7 +64,7 @@ public class SecurityFeatureSet implements XPackFeatureSet { } @Override - public Usage usage() { + public XPackFeatureSet.Usage usage() { List> enabledRealms = buildEnabledRealms(realms); return new Usage(available(), enabled(), enabledRealms); } @@ -86,7 +86,8 @@ public class SecurityFeatureSet implements XPackFeatureSet { static class Usage extends XPackFeatureSet.Usage { - private static final String WRITEABLE_NAME = writeableName(Security.NAME); + private static final String ENABLED_REALMS_XFIELD = "enabled_realms"; + private List> enabledRealms; public Usage(StreamInput in) throws IOException { @@ -99,11 +100,6 @@ public class SecurityFeatureSet implements XPackFeatureSet { this.enabledRealms = enabledRealms; } - @Override - public String getWriteableName() { - return WRITEABLE_NAME; - } - @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); @@ -111,18 +107,11 @@ public class SecurityFeatureSet implements XPackFeatureSet { } @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - builder.field(Field.AVAILABLE, available); - builder.field(Field.ENABLED, enabled); + protected void innerXContent(XContentBuilder builder, Params params) throws IOException { + super.innerXContent(builder, params); if (enabled) { - builder.field(Field.ENABLED_REALMS, enabledRealms); + builder.field(ENABLED_REALMS_XFIELD, enabledRealms); } - return builder.endObject(); - } - - interface Field extends XPackFeatureSet.Usage.Field { - String ENABLED_REALMS = "enabled_realms"; } } } diff --git a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/FileRealm.java b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/FileRealm.java index 473cc11bffc..3a17b6ec243 100644 --- a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/FileRealm.java +++ b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/FileRealm.java @@ -61,7 +61,7 @@ public class FileRealm extends CachingUsernamePasswordRealm { public Map usageStats() { Map stats = super.usageStats(); // here we can determine the size based on the in mem user store - stats.put("size", UserbaseScale.resolve(userPasswdStore.usersCount())); + stats.put("size", UserbaseSize.resolve(userPasswdStore.usersCount())); return stats; } diff --git a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java index a977be5f7e1..c95616e9f94 100644 --- a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java +++ b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/CachingUsernamePasswordRealm.java @@ -168,7 +168,7 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm @Override public Map usageStats() { Map stats = super.usageStats(); - stats.put("size", UserbaseScale.resolve(cache.count()).toString()); + stats.put("size", UserbaseSize.resolve(cache.count()).toString()); return stats; } diff --git a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealm.java b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealm.java index 1b7138f7845..57886b923d6 100644 --- a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealm.java +++ b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealm.java @@ -39,21 +39,25 @@ public abstract class UsernamePasswordRealm extends Realm } } - public enum UserbaseScale { + public enum UserbaseSize { + TINY, SMALL, MEDIUM, LARGE, XLARGE; - public static UserbaseScale resolve(int count) { + public static UserbaseSize resolve(int count) { if (count < 10) { + return TINY; + } + if (count < 100) { return SMALL; } - if (count < 50) { + if (count < 500) { return MEDIUM; } - if (count < 250) { + if (count < 1000) { return LARGE; } return XLARGE; diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/SecurityFeatureSetTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/SecurityFeatureSetTests.java index f07afb60f88..be1a6412f2e 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/SecurityFeatureSetTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/SecurityFeatureSetTests.java @@ -10,6 +10,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.shield.authc.Realm; import org.elasticsearch.shield.authc.Realms; import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xpack.XPackFeatureSet; import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource; import org.junit.Before; @@ -55,20 +56,20 @@ public class SecurityFeatureSetTests extends ESTestCase { assertThat(featureSet.available(), is(available)); } - public void testEnabled() throws Exception { + public void testEnabledSetting() throws Exception { boolean enabled = randomBoolean(); - Settings.Builder settings = Settings.builder(); - if (enabled) { - if (randomBoolean()) { - settings.put("xpack.security.enabled", enabled); - } - } else { - settings.put("xpack.security.enabled", enabled); - } - SecurityFeatureSet featureSet = new SecurityFeatureSet(settings.build(), licenseState, realms, namedWriteableRegistry); + Settings settings = Settings.builder() + .put("xpack.security.enabled", enabled) + .build(); + SecurityFeatureSet featureSet = new SecurityFeatureSet(settings, licenseState, realms, namedWriteableRegistry); assertThat(featureSet.enabled(), is(enabled)); } + public void testEnabledDefault() throws Exception { + SecurityFeatureSet featureSet = new SecurityFeatureSet(Settings.EMPTY, licenseState, realms, namedWriteableRegistry); + assertThat(featureSet.enabled(), is(true)); + } + public void testUsage() throws Exception { boolean available = randomBoolean(); @@ -77,13 +78,7 @@ public class SecurityFeatureSetTests extends ESTestCase { Settings.Builder settings = Settings.builder(); boolean enabled = randomBoolean(); - if (enabled) { - if (randomBoolean()) { - settings.put("xpack.security.enabled", enabled); - } - } else { - settings.put("xpack.security.enabled", enabled); - } + settings.put("xpack.security.enabled", enabled); List realmsList= new ArrayList<>(); @@ -100,7 +95,7 @@ public class SecurityFeatureSetTests extends ESTestCase { when(realms.iterator()).thenReturn(realmsList.iterator()); SecurityFeatureSet featureSet = new SecurityFeatureSet(settings.build(), licenseState, realms, namedWriteableRegistry); - SecurityFeatureSet.Usage usage = featureSet.usage(); + XPackFeatureSet.Usage usage = featureSet.usage(); assertThat(usage, is(notNullValue())); assertThat(usage.name(), is(Security.NAME)); assertThat(usage.enabled(), is(enabled)); diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryRealmUsageTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryRealmUsageTests.java index 2eeb5ad9dd6..ed71448bad5 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryRealmUsageTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectoryRealmUsageTests.java @@ -37,7 +37,7 @@ public class ActiveDirectoryRealmUsageTests extends AbstractActiveDirectoryInteg assertThat(stats, hasEntry("type", "active_directory")); assertThat(stats, hasEntry("name", "ad-test")); assertThat(stats, hasEntry("order", realm.order())); - assertThat(stats, hasEntry("size", "small")); + assertThat(stats, hasEntry("size", "tiny")); assertThat(stats, hasEntry("ssl", true)); assertThat(stats, hasEntry("load_balance_type", loadBalanceType)); } diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java index 7a7f5500731..80fb786688a 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/esnative/ReservedRealmTests.java @@ -120,7 +120,7 @@ public class ReservedRealmTests extends ESTestCase { } public void testHelperMethods() { - final User expectedUser = randomFrom((User) XPackUser.INSTANCE, (User) KibanaUser.INSTANCE); + final User expectedUser = randomFrom((User) XPackUser.INSTANCE, KibanaUser.INSTANCE); final String principal = expectedUser.principal(); assertThat(ReservedRealm.isReserved(principal), is(true)); assertThat(ReservedRealm.getUser(principal), sameInstance(expectedUser)); diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/FileRealmTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/FileRealmTests.java index e1c42bc5775..29a953199a3 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/FileRealmTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/FileRealmTests.java @@ -180,7 +180,7 @@ public class FileRealmTests extends ESTestCase { assertThat(usage, hasEntry("type", "file")); assertThat(usage, hasEntry("name", "file-realm")); assertThat(usage, hasEntry("order", order)); - assertThat(usage, hasEntry("size", UsernamePasswordRealm.UserbaseScale.resolve(userCount))); + assertThat(usage, hasEntry("size", UsernamePasswordRealm.UserbaseSize.resolve(userCount))); } diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTests.java index 409ae31c7d0..7ab06210a42 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/ldap/LdapRealmTests.java @@ -250,7 +250,7 @@ public class LdapRealmTests extends LdapTestCase { assertThat(stats, hasEntry("type", "ldap")); assertThat(stats, hasEntry("name", "ldap-realm")); assertThat(stats, hasEntry("order", realm.order())); - assertThat(stats, hasEntry("size", "small")); + assertThat(stats, hasEntry("size", "tiny")); assertThat(stats, hasEntry("ssl", false)); assertThat(stats, hasEntry("user_search", userSearch)); } diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealmTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealmTests.java index f2dc36def2b..8549a18f220 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealmTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/support/UsernamePasswordRealmTests.java @@ -18,25 +18,27 @@ public class UsernamePasswordRealmTests extends ESTestCase { public void testUserbaseScaelResolve() throws Exception { int count = randomIntBetween(0, 1000); - UsernamePasswordRealm.UserbaseScale scale = UsernamePasswordRealm.UserbaseScale.resolve(count); + UsernamePasswordRealm.UserbaseSize size = UsernamePasswordRealm.UserbaseSize.resolve(count); if (count < 10) { - assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.SMALL)); - } else if (count < 50) { - assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.MEDIUM)); - } else if (count < 250) { - assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.LARGE)); + assertThat(size, is(UsernamePasswordRealm.UserbaseSize.SMALL)); + } else if (count < 100) { + assertThat(size, is(UsernamePasswordRealm.UserbaseSize.SMALL)); + } else if (count < 500) { + assertThat(size, is(UsernamePasswordRealm.UserbaseSize.MEDIUM)); + } else if (count < 1000) { + assertThat(size, is(UsernamePasswordRealm.UserbaseSize.LARGE)); } else { - assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.XLARGE)); + assertThat(size, is(UsernamePasswordRealm.UserbaseSize.XLARGE)); } } public void testUserbaseScaleToString() throws Exception { - UsernamePasswordRealm.UserbaseScale scale = randomFrom(UsernamePasswordRealm.UserbaseScale.values()); - String value = scale.toString(); - if (scale == UsernamePasswordRealm.UserbaseScale.XLARGE) { + UsernamePasswordRealm.UserbaseSize size = randomFrom(UsernamePasswordRealm.UserbaseSize.values()); + String value = size.toString(); + if (size == UsernamePasswordRealm.UserbaseSize.XLARGE) { assertThat(value , is("x-large")); } else { - assertThat(value , is(scale.name().toLowerCase(Locale.ROOT))); + assertThat(value , is(size.name().toLowerCase(Locale.ROOT))); } } } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackFeatureSet.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackFeatureSet.java index ef0717c985d..ccbe45d984f 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackFeatureSet.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/XPackFeatureSet.java @@ -9,6 +9,7 @@ import org.elasticsearch.common.io.stream.NamedWriteable; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; @@ -29,6 +30,9 @@ public interface XPackFeatureSet { abstract class Usage implements ToXContent, NamedWriteable { + private static final String AVAILABLE_XFIELD = "available"; + private static final String ENABLED_XFIELD = "enabled"; + protected final String name; protected final boolean available; protected final boolean enabled; @@ -55,18 +59,30 @@ public interface XPackFeatureSet { return enabled; } + @Override + public String getWriteableName() { + return writeableName(name); + } + @Override public void writeTo(StreamOutput out) throws IOException { out.writeBoolean(available); out.writeBoolean(enabled); } - protected interface Field { - String AVAILABLE = "available"; - String ENABLED = "enabled"; + @Override + public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); + innerXContent(builder, params); + return builder.endObject(); } - protected static String writeableName(String featureName) { + protected void innerXContent(XContentBuilder builder, Params params) throws IOException { + builder.field(AVAILABLE_XFIELD, available); + builder.field(ENABLED_XFIELD, enabled); + } + + public static String writeableName(String featureName) { return "xpack.usage." + featureName; } } diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherFeatureSet.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherFeatureSet.java index d57f918469b..dd97c2f6e47 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherFeatureSet.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherFeatureSet.java @@ -10,7 +10,6 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.xpack.XPackFeatureSet; import java.io.IOException; @@ -27,7 +26,7 @@ public class WatcherFeatureSet implements XPackFeatureSet { public WatcherFeatureSet(Settings settings, @Nullable WatcherLicensee licensee, NamedWriteableRegistry namedWriteableRegistry) { this.enabled = Watcher.enabled(settings); this.licensee = licensee; - namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); + namedWriteableRegistry.register(Usage.class, Usage.writeableName(Watcher.NAME), Usage::new); } @Override @@ -51,14 +50,12 @@ public class WatcherFeatureSet implements XPackFeatureSet { } @Override - public Usage usage() { + public XPackFeatureSet.Usage usage() { return new Usage(available(), enabled()); } static class Usage extends XPackFeatureSet.Usage { - private static final String WRITEABLE_NAME = writeableName(Watcher.NAME); - public Usage(StreamInput input) throws IOException { super(input); } @@ -67,18 +64,5 @@ public class WatcherFeatureSet implements XPackFeatureSet { super(Watcher.NAME, available, enabled); } - @Override - public String getWriteableName() { - return WRITEABLE_NAME; - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject() - .field(Field.AVAILABLE, available) - .field(Field.ENABLED, enabled) - .endObject(); - - } } } diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherFeatureSetTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherFeatureSetTests.java index 1e90de702b4..3eb671a08aa 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherFeatureSetTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherFeatureSetTests.java @@ -39,7 +39,7 @@ public class WatcherFeatureSetTests extends ESTestCase { public void testAvailable() throws Exception { WatcherFeatureSet featureSet = new WatcherFeatureSet(Settings.EMPTY, licensee, namedWriteableRegistry); boolean available = randomBoolean(); - when(licensee.available()).thenReturn(available); + when(licensee.isAvailable()).thenReturn(available); assertThat(featureSet.available(), is(available)); }