addressed review comments

Original commit: elastic/x-pack-elasticsearch@01552f5e82
This commit is contained in:
uboness 2016-05-19 09:19:15 +01:00
parent 084179f457
commit 27db7c40b1
16 changed files with 174 additions and 199 deletions

View File

@ -10,7 +10,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.XPackFeatureSet; import org.elasticsearch.xpack.XPackFeatureSet;
import java.io.IOException; import java.io.IOException;
@ -27,7 +26,7 @@ public class GraphFeatureSet implements XPackFeatureSet {
public GraphFeatureSet(Settings settings, @Nullable GraphLicensee licensee, NamedWriteableRegistry namedWriteableRegistry) { public GraphFeatureSet(Settings settings, @Nullable GraphLicensee licensee, NamedWriteableRegistry namedWriteableRegistry) {
this.enabled = Graph.enabled(settings); this.enabled = Graph.enabled(settings);
this.licensee = licensee; this.licensee = licensee;
namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); namedWriteableRegistry.register(Usage.class, Usage.writeableName(Graph.NAME), Usage::new);
} }
@Override @Override
@ -51,14 +50,12 @@ public class GraphFeatureSet implements XPackFeatureSet {
} }
@Override @Override
public Usage usage() { public XPackFeatureSet.Usage usage() {
return new Usage(available(), enabled()); return new Usage(available(), enabled());
} }
static class Usage extends XPackFeatureSet.Usage { static class Usage extends XPackFeatureSet.Usage {
static final String WRITEABLE_NAME = writeableName(Graph.NAME);
public Usage(StreamInput input) throws IOException { public Usage(StreamInput input) throws IOException {
super(input); super(input);
} }
@ -66,19 +63,5 @@ public class GraphFeatureSet implements XPackFeatureSet {
public Usage(boolean available, boolean enabled) { public Usage(boolean available, boolean enabled) {
super(Graph.NAME, available, 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();
}
} }
} }

View File

@ -10,17 +10,15 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.marvel.agent.exporter.Exporter; import org.elasticsearch.marvel.agent.exporter.Exporter;
import org.elasticsearch.marvel.agent.exporter.Exporters; 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 org.elasticsearch.xpack.XPackFeatureSet;
import java.io.IOException; 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 boolean enabled;
private final MonitoringLicensee licensee; private final MonitoringLicensee licensee;
private final org.elasticsearch.marvel.agent.exporter.Exporters exporters; private final Exporters exporters;
@Inject @Inject
public MonitoringFeatureSet(Settings settings, @Nullable MonitoringLicensee licensee, @Nullable Exporters exporters, 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.enabled = MonitoringSettings.ENABLED.get(settings);
this.licensee = licensee; this.licensee = licensee;
this.exporters = exporters; this.exporters = exporters;
namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); namedWriteableRegistry.register(Usage.class, Usage.writeableName(Monitoring.NAME), Usage::new);
} }
@Override @Override
@ -61,123 +59,53 @@ public class MonitoringFeatureSet implements XPackFeatureSet {
} }
@Override @Override
public Usage usage() { public XPackFeatureSet.Usage usage() {
return new Usage(available(), enabled(), exportersUsage(exporters)); return new Usage(available(), enabled(), exportersUsage(exporters));
} }
static Usage.Exporters exportersUsage(Exporters exporters) { static Map<String, Object> exportersUsage(Exporters exporters) {
if (exporters == null) { if (exporters == null) {
return null; return null;
} }
int local = 0; Map<String, Object> usage = new HashMap<>();
int http = 0;
int unknown = 0;
for (Exporter exporter : exporters) { for (Exporter exporter : exporters) {
if (exporter.config().enabled()) { if (exporter.config().enabled()) {
switch (exporter.type()) { String type = exporter.type();
case LocalExporter.TYPE: int count = (Integer) usage.getOrDefault(type, 0);
local++; usage.put(type, count + 1);
break;
case HttpExporter.TYPE:
http++;
break;
default:
unknown++;
} }
} }
} return usage;
return new Usage.Exporters(local, http, unknown);
} }
static class Usage extends XPackFeatureSet.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 private @Nullable Map<String, Object> exporters;
Exporters exporters;
public Usage(StreamInput in) throws IOException { public Usage(StreamInput in) throws IOException {
super(in); super(in);
exporters = new Exporters(in); exporters = in.readMap();
} }
public Usage(boolean available, boolean enabled, Exporters exporters) { public Usage(boolean available, boolean enabled, Map<String, Object> exporters) {
super(Monitoring.NAME, available, enabled); super(Monitoring.NAME, available, enabled);
this.exporters = exporters; this.exporters = exporters;
} }
@Override
public boolean available() {
return available;
}
@Override
public boolean enabled() {
return enabled;
}
@Override
public String getWriteableName() {
return WRITEABLE_NAME;
}
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
out.writeOptionalWriteable(exporters); out.writeMap(exporters);
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); super.innerXContent(builder, params);
builder.field(Field.AVAILABLE, available);
builder.field(Field.ENABLED, enabled);
if (exporters != null) { 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";
} }
} }

View File

@ -7,10 +7,20 @@ package org.elasticsearch.marvel;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.settings.Settings; 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.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.test.ESTestCase;
import org.elasticsearch.xpack.XPackFeatureSet;
import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource;
import org.junit.Before; 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.hamcrest.core.Is.is;
import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
@ -42,21 +52,85 @@ public class MonitoringFeatureSetTests extends ESTestCase {
public void testAvailable() throws Exception { public void testAvailable() throws Exception {
MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licensee, exporters, namedWriteableRegistry); MonitoringFeatureSet featureSet = new MonitoringFeatureSet(Settings.EMPTY, licensee, exporters, namedWriteableRegistry);
boolean available = randomBoolean(); boolean available = randomBoolean();
when(licensee.available()).thenReturn(available); when(licensee.isAvailable()).thenReturn(available);
assertThat(featureSet.available(), is(available)); assertThat(featureSet.available(), is(available));
} }
public void testEnabled() throws Exception { public void testEnabledSetting() throws Exception {
boolean enabled = randomBoolean(); boolean enabled = randomBoolean();
Settings.Builder settings = Settings.builder(); Settings.Builder settings = Settings.builder();
if (enabled) {
if (randomBoolean()) {
settings.put("xpack.monitoring.enabled", enabled); settings.put("xpack.monitoring.enabled", enabled);
}
} else {
settings.put("xpack.monitoring.enabled", enabled);
}
MonitoringFeatureSet featureSet = new MonitoringFeatureSet(settings.build(), licensee, exporters, namedWriteableRegistry); MonitoringFeatureSet featureSet = new MonitoringFeatureSet(settings.build(), licensee, exporters, namedWriteableRegistry);
assertThat(featureSet.enabled(), is(enabled)); 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<Exporter> 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;
}
} }

View File

@ -40,7 +40,7 @@ public class SecurityFeatureSet implements XPackFeatureSet {
this.enabled = Security.enabled(settings); this.enabled = Security.enabled(settings);
this.licenseState = licenseState; this.licenseState = licenseState;
this.realms = realms; this.realms = realms;
namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); namedWriteableRegistry.register(Usage.class, Usage.writeableName(Security.NAME), Usage::new);
} }
@Override @Override
@ -64,7 +64,7 @@ public class SecurityFeatureSet implements XPackFeatureSet {
} }
@Override @Override
public Usage usage() { public XPackFeatureSet.Usage usage() {
List<Map<String, Object>> enabledRealms = buildEnabledRealms(realms); List<Map<String, Object>> enabledRealms = buildEnabledRealms(realms);
return new Usage(available(), enabled(), enabledRealms); return new Usage(available(), enabled(), enabledRealms);
} }
@ -86,7 +86,8 @@ public class SecurityFeatureSet implements XPackFeatureSet {
static class Usage extends XPackFeatureSet.Usage { 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<Map<String, Object>> enabledRealms; private List<Map<String, Object>> enabledRealms;
public Usage(StreamInput in) throws IOException { public Usage(StreamInput in) throws IOException {
@ -99,11 +100,6 @@ public class SecurityFeatureSet implements XPackFeatureSet {
this.enabledRealms = enabledRealms; this.enabledRealms = enabledRealms;
} }
@Override
public String getWriteableName() {
return WRITEABLE_NAME;
}
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out); super.writeTo(out);
@ -111,18 +107,11 @@ public class SecurityFeatureSet implements XPackFeatureSet {
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { protected void innerXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(); super.innerXContent(builder, params);
builder.field(Field.AVAILABLE, available);
builder.field(Field.ENABLED, enabled);
if (enabled) { 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";
} }
} }
} }

View File

@ -61,7 +61,7 @@ public class FileRealm extends CachingUsernamePasswordRealm {
public Map<String, Object> usageStats() { public Map<String, Object> usageStats() {
Map<String, Object> stats = super.usageStats(); Map<String, Object> stats = super.usageStats();
// here we can determine the size based on the in mem user store // 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; return stats;
} }

View File

@ -168,7 +168,7 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm
@Override @Override
public Map<String, Object> usageStats() { public Map<String, Object> usageStats() {
Map<String, Object> stats = super.usageStats(); Map<String, Object> stats = super.usageStats();
stats.put("size", UserbaseScale.resolve(cache.count()).toString()); stats.put("size", UserbaseSize.resolve(cache.count()).toString());
return stats; return stats;
} }

View File

@ -39,21 +39,25 @@ public abstract class UsernamePasswordRealm extends Realm<UsernamePasswordToken>
} }
} }
public enum UserbaseScale { public enum UserbaseSize {
TINY,
SMALL, SMALL,
MEDIUM, MEDIUM,
LARGE, LARGE,
XLARGE; XLARGE;
public static UserbaseScale resolve(int count) { public static UserbaseSize resolve(int count) {
if (count < 10) { if (count < 10) {
return TINY;
}
if (count < 100) {
return SMALL; return SMALL;
} }
if (count < 50) { if (count < 500) {
return MEDIUM; return MEDIUM;
} }
if (count < 250) { if (count < 1000) {
return LARGE; return LARGE;
} }
return XLARGE; return XLARGE;

View File

@ -10,6 +10,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.shield.authc.Realm; import org.elasticsearch.shield.authc.Realm;
import org.elasticsearch.shield.authc.Realms; import org.elasticsearch.shield.authc.Realms;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.XPackFeatureSet;
import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource; import org.elasticsearch.xpack.watcher.support.xcontent.XContentSource;
import org.junit.Before; import org.junit.Before;
@ -55,20 +56,20 @@ public class SecurityFeatureSetTests extends ESTestCase {
assertThat(featureSet.available(), is(available)); assertThat(featureSet.available(), is(available));
} }
public void testEnabled() throws Exception { public void testEnabledSetting() throws Exception {
boolean enabled = randomBoolean(); boolean enabled = randomBoolean();
Settings.Builder settings = Settings.builder(); Settings settings = Settings.builder()
if (enabled) { .put("xpack.security.enabled", enabled)
if (randomBoolean()) { .build();
settings.put("xpack.security.enabled", enabled); SecurityFeatureSet featureSet = new SecurityFeatureSet(settings, licenseState, realms, namedWriteableRegistry);
}
} else {
settings.put("xpack.security.enabled", enabled);
}
SecurityFeatureSet featureSet = new SecurityFeatureSet(settings.build(), licenseState, realms, namedWriteableRegistry);
assertThat(featureSet.enabled(), is(enabled)); 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 { public void testUsage() throws Exception {
boolean available = randomBoolean(); boolean available = randomBoolean();
@ -77,13 +78,7 @@ public class SecurityFeatureSetTests extends ESTestCase {
Settings.Builder settings = Settings.builder(); Settings.Builder settings = Settings.builder();
boolean enabled = randomBoolean(); boolean enabled = randomBoolean();
if (enabled) {
if (randomBoolean()) {
settings.put("xpack.security.enabled", enabled); settings.put("xpack.security.enabled", enabled);
}
} else {
settings.put("xpack.security.enabled", enabled);
}
List<Realm> realmsList= new ArrayList<>(); List<Realm> realmsList= new ArrayList<>();
@ -100,7 +95,7 @@ public class SecurityFeatureSetTests extends ESTestCase {
when(realms.iterator()).thenReturn(realmsList.iterator()); when(realms.iterator()).thenReturn(realmsList.iterator());
SecurityFeatureSet featureSet = new SecurityFeatureSet(settings.build(), licenseState, realms, namedWriteableRegistry); 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, is(notNullValue()));
assertThat(usage.name(), is(Security.NAME)); assertThat(usage.name(), is(Security.NAME));
assertThat(usage.enabled(), is(enabled)); assertThat(usage.enabled(), is(enabled));

View File

@ -37,7 +37,7 @@ public class ActiveDirectoryRealmUsageTests extends AbstractActiveDirectoryInteg
assertThat(stats, hasEntry("type", "active_directory")); assertThat(stats, hasEntry("type", "active_directory"));
assertThat(stats, hasEntry("name", "ad-test")); assertThat(stats, hasEntry("name", "ad-test"));
assertThat(stats, hasEntry("order", realm.order())); assertThat(stats, hasEntry("order", realm.order()));
assertThat(stats, hasEntry("size", "small")); assertThat(stats, hasEntry("size", "tiny"));
assertThat(stats, hasEntry("ssl", true)); assertThat(stats, hasEntry("ssl", true));
assertThat(stats, hasEntry("load_balance_type", loadBalanceType)); assertThat(stats, hasEntry("load_balance_type", loadBalanceType));
} }

View File

@ -120,7 +120,7 @@ public class ReservedRealmTests extends ESTestCase {
} }
public void testHelperMethods() { 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(); final String principal = expectedUser.principal();
assertThat(ReservedRealm.isReserved(principal), is(true)); assertThat(ReservedRealm.isReserved(principal), is(true));
assertThat(ReservedRealm.getUser(principal), sameInstance(expectedUser)); assertThat(ReservedRealm.getUser(principal), sameInstance(expectedUser));

View File

@ -180,7 +180,7 @@ public class FileRealmTests extends ESTestCase {
assertThat(usage, hasEntry("type", "file")); assertThat(usage, hasEntry("type", "file"));
assertThat(usage, hasEntry("name", "file-realm")); assertThat(usage, hasEntry("name", "file-realm"));
assertThat(usage, hasEntry("order", order)); assertThat(usage, hasEntry("order", order));
assertThat(usage, hasEntry("size", UsernamePasswordRealm.UserbaseScale.resolve(userCount))); assertThat(usage, hasEntry("size", UsernamePasswordRealm.UserbaseSize.resolve(userCount)));
} }

View File

@ -250,7 +250,7 @@ public class LdapRealmTests extends LdapTestCase {
assertThat(stats, hasEntry("type", "ldap")); assertThat(stats, hasEntry("type", "ldap"));
assertThat(stats, hasEntry("name", "ldap-realm")); assertThat(stats, hasEntry("name", "ldap-realm"));
assertThat(stats, hasEntry("order", realm.order())); assertThat(stats, hasEntry("order", realm.order()));
assertThat(stats, hasEntry("size", "small")); assertThat(stats, hasEntry("size", "tiny"));
assertThat(stats, hasEntry("ssl", false)); assertThat(stats, hasEntry("ssl", false));
assertThat(stats, hasEntry("user_search", userSearch)); assertThat(stats, hasEntry("user_search", userSearch));
} }

View File

@ -18,25 +18,27 @@ public class UsernamePasswordRealmTests extends ESTestCase {
public void testUserbaseScaelResolve() throws Exception { public void testUserbaseScaelResolve() throws Exception {
int count = randomIntBetween(0, 1000); int count = randomIntBetween(0, 1000);
UsernamePasswordRealm.UserbaseScale scale = UsernamePasswordRealm.UserbaseScale.resolve(count); UsernamePasswordRealm.UserbaseSize size = UsernamePasswordRealm.UserbaseSize.resolve(count);
if (count < 10) { if (count < 10) {
assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.SMALL)); assertThat(size, is(UsernamePasswordRealm.UserbaseSize.SMALL));
} else if (count < 50) { } else if (count < 100) {
assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.MEDIUM)); assertThat(size, is(UsernamePasswordRealm.UserbaseSize.SMALL));
} else if (count < 250) { } else if (count < 500) {
assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.LARGE)); assertThat(size, is(UsernamePasswordRealm.UserbaseSize.MEDIUM));
} else if (count < 1000) {
assertThat(size, is(UsernamePasswordRealm.UserbaseSize.LARGE));
} else { } else {
assertThat(scale, is(UsernamePasswordRealm.UserbaseScale.XLARGE)); assertThat(size, is(UsernamePasswordRealm.UserbaseSize.XLARGE));
} }
} }
public void testUserbaseScaleToString() throws Exception { public void testUserbaseScaleToString() throws Exception {
UsernamePasswordRealm.UserbaseScale scale = randomFrom(UsernamePasswordRealm.UserbaseScale.values()); UsernamePasswordRealm.UserbaseSize size = randomFrom(UsernamePasswordRealm.UserbaseSize.values());
String value = scale.toString(); String value = size.toString();
if (scale == UsernamePasswordRealm.UserbaseScale.XLARGE) { if (size == UsernamePasswordRealm.UserbaseSize.XLARGE) {
assertThat(value , is("x-large")); assertThat(value , is("x-large"));
} else { } else {
assertThat(value , is(scale.name().toLowerCase(Locale.ROOT))); assertThat(value , is(size.name().toLowerCase(Locale.ROOT)));
} }
} }
} }

View File

@ -9,6 +9,7 @@ import org.elasticsearch.common.io.stream.NamedWriteable;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
@ -29,6 +30,9 @@ public interface XPackFeatureSet {
abstract class Usage implements ToXContent, NamedWriteable { 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 String name;
protected final boolean available; protected final boolean available;
protected final boolean enabled; protected final boolean enabled;
@ -55,18 +59,30 @@ public interface XPackFeatureSet {
return enabled; return enabled;
} }
@Override
public String getWriteableName() {
return writeableName(name);
}
@Override @Override
public void writeTo(StreamOutput out) throws IOException { public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(available); out.writeBoolean(available);
out.writeBoolean(enabled); out.writeBoolean(enabled);
} }
protected interface Field { @Override
String AVAILABLE = "available"; public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
String ENABLED = "enabled"; 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; return "xpack.usage." + featureName;
} }
} }

View File

@ -10,7 +10,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.XPackFeatureSet; import org.elasticsearch.xpack.XPackFeatureSet;
import java.io.IOException; import java.io.IOException;
@ -27,7 +26,7 @@ public class WatcherFeatureSet implements XPackFeatureSet {
public WatcherFeatureSet(Settings settings, @Nullable WatcherLicensee licensee, NamedWriteableRegistry namedWriteableRegistry) { public WatcherFeatureSet(Settings settings, @Nullable WatcherLicensee licensee, NamedWriteableRegistry namedWriteableRegistry) {
this.enabled = Watcher.enabled(settings); this.enabled = Watcher.enabled(settings);
this.licensee = licensee; this.licensee = licensee;
namedWriteableRegistry.register(Usage.class, Usage.WRITEABLE_NAME, Usage::new); namedWriteableRegistry.register(Usage.class, Usage.writeableName(Watcher.NAME), Usage::new);
} }
@Override @Override
@ -51,14 +50,12 @@ public class WatcherFeatureSet implements XPackFeatureSet {
} }
@Override @Override
public Usage usage() { public XPackFeatureSet.Usage usage() {
return new Usage(available(), enabled()); return new Usage(available(), enabled());
} }
static class Usage extends XPackFeatureSet.Usage { static class Usage extends XPackFeatureSet.Usage {
private static final String WRITEABLE_NAME = writeableName(Watcher.NAME);
public Usage(StreamInput input) throws IOException { public Usage(StreamInput input) throws IOException {
super(input); super(input);
} }
@ -67,18 +64,5 @@ public class WatcherFeatureSet implements XPackFeatureSet {
super(Watcher.NAME, available, enabled); 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();
}
} }
} }

View File

@ -39,7 +39,7 @@ public class WatcherFeatureSetTests extends ESTestCase {
public void testAvailable() throws Exception { public void testAvailable() throws Exception {
WatcherFeatureSet featureSet = new WatcherFeatureSet(Settings.EMPTY, licensee, namedWriteableRegistry); WatcherFeatureSet featureSet = new WatcherFeatureSet(Settings.EMPTY, licensee, namedWriteableRegistry);
boolean available = randomBoolean(); boolean available = randomBoolean();
when(licensee.available()).thenReturn(available); when(licensee.isAvailable()).thenReturn(available);
assertThat(featureSet.available(), is(available)); assertThat(featureSet.available(), is(available));
} }