addressed review comments
Original commit: elastic/x-pack-elasticsearch@01552f5e82
This commit is contained in:
parent
084179f457
commit
27db7c40b1
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, Object> exportersUsage(Exporters exporters) {
|
||||
if (exporters == null) {
|
||||
return null;
|
||||
}
|
||||
int local = 0;
|
||||
int http = 0;
|
||||
int unknown = 0;
|
||||
Map<String, Object> 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<String, Object> 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<String, Object> 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";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
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<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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Map<String, Object>> 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<Map<String, Object>> 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,7 +61,7 @@ public class FileRealm extends CachingUsernamePasswordRealm {
|
|||
public Map<String, Object> usageStats() {
|
||||
Map<String, Object> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -168,7 +168,7 @@ public abstract class CachingUsernamePasswordRealm extends UsernamePasswordRealm
|
|||
@Override
|
||||
public Map<String, Object> 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,21 +39,25 @@ public abstract class UsernamePasswordRealm extends Realm<UsernamePasswordToken>
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
List<Realm> 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));
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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)));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue