Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sidecar services to use new MongoDB API #21016

Merged
merged 10 commits into from
Nov 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog2.database.BuildableMongoEntity;
import org.mongojack.Id;
import org.mongojack.ObjectId;

Expand All @@ -29,7 +30,7 @@
@AutoValue
@JsonAutoDetect
@JsonIgnoreProperties({"default_template_crc"})
public abstract class Collector {
public abstract class Collector implements BuildableMongoEntity<Collector, Collector.Builder> {
public static final String FIELD_ID = "id";
public static final String FIELD_NAME = "name";
public static final String FIELD_SERVICE_TYPE = "service_type";
Expand Down Expand Up @@ -77,15 +78,23 @@ public static Builder builder() {
public abstract Builder toBuilder();

@AutoValue.Builder
public abstract static class Builder {
public abstract static class Builder implements BuildableMongoEntity.Builder<Collector, Builder> {
public abstract Builder id(String id);

public abstract Builder name(String value);

public abstract Builder serviceType(String serviceType);

public abstract Builder nodeOperatingSystem(String nodeOperatingSystem);

public abstract Builder executablePath(String executablePath);

public abstract Builder executeParameters(String executeParameters);

public abstract Builder validationParameters(String validationParameters);

public abstract Builder defaultTemplate(String defaultTemplate);

public abstract Collector build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog2.database.MongoEntity;
import org.joda.time.DateTime;
import org.mongojack.Id;
import org.mongojack.ObjectId;
Expand All @@ -29,7 +30,7 @@

@AutoValue
@JsonAutoDetect
public abstract class CollectorActions {
public abstract class CollectorActions implements MongoEntity {
@JsonProperty("id")
@Nullable
@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog.autovalue.WithBeanGetter;
import org.graylog2.database.MongoEntity;
import org.mongojack.Id;
import org.mongojack.ObjectId;

Expand All @@ -30,7 +31,7 @@
@AutoValue
@WithBeanGetter
@JsonAutoDetect
public abstract class Configuration {
public abstract class Configuration implements MongoEntity {
public static final String FIELD_ID = "id";
public static final String FIELD_COLLECTOR_ID = "collector_id";
public static final String FIELD_NAME = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog2.database.MongoEntity;
import org.mongojack.Id;
import org.mongojack.ObjectId;

import javax.annotation.Nullable;
import java.util.Locale;

@AutoValue
public abstract class ConfigurationVariable {
public abstract class ConfigurationVariable implements MongoEntity {
public static final String FIELD_ID = "id";
public static final String FIELD_NAME = "name";
public static final String FIELD_DESCRIPTION = "description";
Expand Down Expand Up @@ -68,6 +69,6 @@ public static ConfigurationVariable create(String name, String description, Stri

@JsonIgnore
public String fullName() {
return String.format(Locale.ENGLISH, "${%s.%s}", VARIABLE_PREFIX, name());
return String.format(Locale.ENGLISH, "${%s.%s}", VARIABLE_PREFIX, name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog.plugins.sidecar.rest.requests.ConfigurationAssignment;
import org.graylog2.database.MongoEntity;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.mongojack.Id;
Expand All @@ -36,7 +37,7 @@

@AutoValue
@JsonAutoDetect
public abstract class Sidecar {
public abstract class Sidecar implements MongoEntity {

public static final String FIELD_ID = "id";
public static final String FIELD_NODE_ID = "node_id";
Expand Down Expand Up @@ -64,10 +65,14 @@ public int getStatusCode() {

public static Status fromStatusCode(int statusCode) {
switch (statusCode) {
case 0: return RUNNING;
case 2: return FAILING;
case 3: return STOPPED;
default: return UNKNOWN;
case 0:
return RUNNING;
case 2:
return FAILING;
case 3:
return STOPPED;
default:
return UNKNOWN;
}
}

Expand Down Expand Up @@ -109,12 +114,19 @@ public static Builder builder() {
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder id(String id);

public abstract Builder nodeId(String title);

public abstract Builder nodeName(String title);

public abstract Builder nodeDetails(NodeDetails nodeDetails);

public abstract Builder assignments(List<ConfigurationAssignment> assignments);

public abstract Builder sidecarVersion(String sidecarVersion);

public abstract Builder lastSeen(DateTime lastSeen);

public abstract Sidecar build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.graylog2.database.PaginatedList;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.rest.PluginRestResource;
import org.graylog2.rest.models.SortOrder;
import org.graylog2.search.SearchQuery;
import org.graylog2.search.SearchQueryParser;
import org.graylog2.shared.rest.resources.RestResource;
Expand Down Expand Up @@ -121,7 +122,7 @@ public AdministrationResource(SidecarService sidecarService,
public SidecarListResponse administration(@ApiParam(name = "JSON body", required = true)
@Valid @NotNull AdministrationRequest request) {
final String sort = Sidecar.FIELD_NODE_NAME;
final String order = "asc";
final SortOrder order = SortOrder.ASCENDING;
final String mappedQuery = sidecarStatusMapper.replaceStringStatusSearchQuery(request.query());
SearchQuery searchQuery;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.graylog2.database.PaginatedList;
import org.graylog2.plugin.rest.PluginRestResource;
import org.graylog2.plugin.rest.ValidationResult;
import org.graylog2.rest.models.SortOrder;
import org.graylog2.search.SearchQuery;
import org.graylog2.search.SearchQueryField;
import org.graylog2.search.SearchQueryParser;
Expand Down Expand Up @@ -189,7 +190,7 @@ public CollectorSummaryResponse listSummary(@ApiParam(name = "page") @QueryParam
allowableValues = "name,id,collector_id")
@DefaultValue(Collector.FIELD_NAME) @QueryParam("sort") String sort,
@ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc")
@DefaultValue("asc") @QueryParam("order") String order) {
@DefaultValue("asc") @QueryParam("order") SortOrder order) {
final SearchQuery searchQuery = searchQueryParser.parse(query);
final PaginatedList<Collector> collectors = this.collectorService.findPaginated(searchQuery, page, perPage, sort, order);
final long total = this.collectorService.count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.graylog2.database.PaginatedList;
import org.graylog2.plugin.rest.PluginRestResource;
import org.graylog2.plugin.rest.ValidationResult;
import org.graylog2.rest.models.SortOrder;
import org.graylog2.search.SearchQuery;
import org.graylog2.search.SearchQueryField;
import org.graylog2.search.SearchQueryParser;
Expand Down Expand Up @@ -134,7 +135,7 @@ public ConfigurationListResponse listConfigurations(@ApiParam(name = "page") @Qu
allowableValues = "name,id,collector_id")
@DefaultValue(Configuration.FIELD_NAME) @QueryParam("sort") String sort,
@ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc")
@DefaultValue("asc") @QueryParam("order") String order) {
@DefaultValue("asc") @QueryParam("order") SortOrder order) {
final SearchQuery searchQuery = searchQueryParser.parse(query);
final PaginatedList<Configuration> configurations = this.configurationService.findPaginated(searchQuery, page, perPage, sort, order);
final long total = this.configurationService.count();
Expand Down Expand Up @@ -274,9 +275,9 @@ public Response createConfiguration(@ApiParam(name = "JSON body", required = tru
if (!config.tags().isEmpty()) {
final String os = Optional.ofNullable(collectorService.find(request.collectorId()))
.map(Collector::nodeOperatingSystem).orElse("");
sidecarService.findByTagsAndOS(config.tags(), os)
.map(Sidecar::nodeId)
.forEach(etagService::invalidateRegistration);
try (final var stream = sidecarService.findByTagsAndOS(config.tags(), os)) {
stream.map(Sidecar::nodeId).forEach(etagService::invalidateRegistration);
}
}

return Response.ok().entity(config).build();
Expand Down Expand Up @@ -332,9 +333,9 @@ public Response updateConfiguration(@ApiParam(name = "id", required = true)
final Set<String> tags = Sets.symmetricDifference(previousConfiguration.tags(), updatedConfiguration.tags());
final String os = Optional.ofNullable(collectorService.find(request.collectorId()))
.map(Collector::nodeOperatingSystem).orElse("");
sidecarService.findByTagsAndOS(tags, os)
.map(Sidecar::nodeId)
.forEach(etagService::invalidateRegistration);
try (final var stream = sidecarService.findByTagsAndOS(tags, os)) {
stream.map(Sidecar::nodeId).forEach(etagService::invalidateRegistration);
}
}

return Response.ok().entity(configurationService.save(updatedConfiguration)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.graylog2.database.PaginatedList;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.rest.PluginRestResource;
import org.graylog2.rest.models.SortOrder;
import org.graylog2.search.SearchQuery;
import org.graylog2.search.SearchQueryField;
import org.graylog2.search.SearchQueryParser;
Expand Down Expand Up @@ -159,7 +160,7 @@ public SidecarListResponse sidecars(@ApiParam(name = "page") @QueryParam("page")
allowableValues = "title,description,name,id")
@DefaultValue(Sidecar.FIELD_NODE_NAME) @QueryParam("sort") String sort,
@ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc")
@DefaultValue("asc") @QueryParam("order") String order,
@DefaultValue("asc") @QueryParam("order") SortOrder order,
@ApiParam(name = "only_active") @QueryParam("only_active") @DefaultValue("false") boolean onlyActive) {
final String mappedQuery = sidecarStatusMapper.replaceStringStatusSearchQuery(query);
SearchQuery searchQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.auto.value.AutoValue;
import org.graylog.plugins.sidecar.rest.models.CollectorSummary;
import org.graylog2.database.PaginatedList;
import org.graylog2.rest.models.SortOrder;

import javax.annotation.Nullable;
import java.util.Collection;
Expand All @@ -43,7 +44,7 @@ public abstract class CollectorSummaryResponse {

@Nullable
@JsonProperty
public abstract String order();
public abstract SortOrder order();

@JsonProperty
public abstract Collection<CollectorSummary> collectors();
Expand All @@ -53,7 +54,7 @@ public static CollectorSummaryResponse create(@JsonProperty("query") @Nullable S
@JsonProperty("pagination") PaginatedList.PaginationInfo paginationInfo,
@JsonProperty("total") long total,
@JsonProperty("sort") String sort,
@JsonProperty("order") String order,
@JsonProperty("order") SortOrder order,
@JsonProperty("collectors") Collection<CollectorSummary> collectors) {
return new AutoValue_CollectorSummaryResponse(query, paginationInfo, total, sort, order, collectors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.auto.value.AutoValue;
import org.graylog.plugins.sidecar.rest.models.ConfigurationSummary;
import org.graylog2.database.PaginatedList;
import org.graylog2.rest.models.SortOrder;

import javax.annotation.Nullable;
import java.util.Collection;
Expand All @@ -43,7 +44,7 @@ public abstract class ConfigurationListResponse {

@Nullable
@JsonProperty
public abstract String order();
public abstract SortOrder order();

@JsonProperty
public abstract Collection<ConfigurationSummary> configurations();
Expand All @@ -53,7 +54,7 @@ public static ConfigurationListResponse create(@JsonProperty("query") String que
@JsonProperty("pagination") PaginatedList.PaginationInfo paginationInfo,
@JsonProperty("total") long total,
@JsonProperty("sort") String sort,
@JsonProperty("order") String order,
@JsonProperty("order") SortOrder order,
@JsonProperty("configurations") Collection<ConfigurationSummary> configurations) {
return new AutoValue_ConfigurationListResponse(query, paginationInfo, total, sort, order, configurations);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.auto.value.AutoValue;
import org.graylog.plugins.sidecar.rest.models.SidecarSummary;
import org.graylog2.database.PaginatedList;
import org.graylog2.rest.models.SortOrder;

import javax.annotation.Nullable;
import java.util.Collection;
Expand All @@ -47,7 +48,7 @@ public abstract class SidecarListResponse {

@Nullable
@JsonProperty
public abstract String order();
public abstract SortOrder order();

@JsonProperty
public abstract Collection<SidecarSummary> sidecars();
Expand All @@ -62,7 +63,7 @@ public static SidecarListResponse create(@JsonProperty("query") @Nullable String
@JsonProperty("total") long total,
@JsonProperty("only_active") Boolean onlyActive,
@JsonProperty("sort") @Nullable String sort,
@JsonProperty("order") @Nullable String order,
@JsonProperty("order") @Nullable SortOrder order,
@JsonProperty("sidecars") Collection<SidecarSummary> sidecars,
@JsonProperty("filters") @Nullable Map<String, String> filters) {
return new AutoValue_SidecarListResponse(query, paginationInfo, total, onlyActive, sort, order, sidecars, filters);
Expand All @@ -73,7 +74,7 @@ public static SidecarListResponse create(@JsonProperty("query") @Nullable String
@JsonProperty("total") long total,
@JsonProperty("only_active") Boolean onlyActive,
@JsonProperty("sort") @Nullable String sort,
@JsonProperty("order") @Nullable String order,
@JsonProperty("order") @Nullable SortOrder order,
@JsonProperty("sidecars") Collection<SidecarSummary> sidecars) {
return create(query, paginationInfo, total, onlyActive, sort, order, sidecars, null);
}
Expand Down
Loading
Loading