Skip to content

Commit

Permalink
Aligning Radio and Server input handling.
Browse files Browse the repository at this point in the history
Using the same value object for requests in rest-client and server code.
Changing ApiClient o accept arbitraty objects as request body.
  • Loading branch information
dennisoelkers committed Jan 2, 2015
1 parent a8a2d39 commit a1060e9
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 103 deletions.
Expand Up @@ -18,15 +18,13 @@

import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.graylog2.plugin.Tools;
import org.graylog2.plugin.configuration.Configuration;
import org.graylog2.plugin.configuration.ConfigurationException;
import org.graylog2.plugin.IOState;
import org.graylog2.plugin.inputs.MessageInput;
import org.graylog2.radio.cluster.InputService;
import org.graylog2.radio.rest.resources.RestResource;
import org.graylog2.rest.models.system.inputs.responses.InputStateSummary;
import org.graylog2.rest.models.system.inputs.responses.InputSummary;
Expand All @@ -37,7 +35,7 @@
import org.graylog2.shared.inputs.MessageInputFactory;
import org.graylog2.shared.inputs.NoSuchInputTypeException;
import org.graylog2.shared.inputs.PersistedInputs;
import org.graylog2.shared.rest.resources.system.inputs.requests.InputLaunchRequest;
import org.graylog2.rest.models.system.inputs.requests.InputLaunchRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -55,7 +53,6 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -147,7 +144,7 @@ public Response launch(String body) {
try {
input = messageInputFactory.create(lr.type(), inputConfig);
input.setTitle(lr.title());
input.setCreatorUserId(lr.creatorUserId());
//input.setCreatorUserId(lr.creatorUserId());
input.setCreatedAt(Tools.iso8601());
input.setGlobal(lr.global());

Expand Down
6 changes: 5 additions & 1 deletion graylog2-rest-client/pom.xml
Expand Up @@ -43,8 +43,12 @@
<dependency>
<groupId>org.graylog2</groupId>
<artifactId>graylog2-rest-routes</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.graylog2</groupId>
<artifactId>graylog2-rest-models</artifactId>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package org.graylog2.restclient.lib;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
Expand Down Expand Up @@ -218,7 +219,7 @@ public class ApiRequestBuilder<T> implements org.graylog2.restclient.lib.ApiRequ
private Radio radio;
private Collection<Node> nodes;
private final Method method;
private ApiRequest body;
private Object body;
private final Class<T> responseClass;
private final ArrayList<Object> pathParams = Lists.newArrayList();
private final ListMultimap<String, String> queryParams = ArrayListMultimap.create();
Expand Down Expand Up @@ -355,7 +356,7 @@ public org.graylog2.restclient.lib.ApiRequestBuilder<T> unauthenticated() {
}

@Override
public org.graylog2.restclient.lib.ApiRequestBuilder<T> body(ApiRequest body) {
public org.graylog2.restclient.lib.ApiRequestBuilder<T> body(Object body) {
this.body = body;
return this;
}
Expand Down Expand Up @@ -573,7 +574,7 @@ public Response onCompleted(Response response) throws Exception {
return results;
}

private AsyncHttpClient.BoundRequestBuilder requestBuilderForUrl(URL url) {
private AsyncHttpClient.BoundRequestBuilder requestBuilderForUrl(URL url) throws JsonProcessingException {
// *sigh* the generic requestBuilder methods are protected/private making this verbose :(
final AsyncHttpClient.BoundRequestBuilder requestBuilder;
final String userInfo = url.getUserInfo();
Expand Down Expand Up @@ -610,7 +611,7 @@ private AsyncHttpClient.BoundRequestBuilder requestBuilderForUrl(URL url) {
}
requestBuilder.addHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
requestBuilder.setBodyEncoding("UTF-8");
requestBuilder.setBody(body.toJson());
requestBuilder.setBody(objectMapper.writeValueAsString(body));
} else if (method == Method.POST) {
LOG.warn("POST without body, this doesn't make sense,", new IllegalStateException());
}
Expand Down
Expand Up @@ -64,7 +64,7 @@ public interface ApiRequestBuilder<T> {

ApiRequestBuilder<T> unauthenticated();

ApiRequestBuilder<T> body(ApiRequest body);
ApiRequestBuilder<T> body(Object body);

ApiRequestBuilder<T> expect(int... httpStatusCodes);

Expand Down
Expand Up @@ -57,14 +57,21 @@ protected URI normalizeUriPath(URI uri) {
return uri;
}

public InputLaunchResponse launchInput(String title, String type, Boolean global, Map<String, Object> configuration, boolean isExclusive) throws ExclusiveInputException {
if (global)
return launchInput(title, type, global, configuration, isExclusive, null);
else
return launchInput(title, type, global, configuration, isExclusive, this.getNodeId());
}

public abstract String getShortNodeId();
public abstract String getHostname();
public abstract String getTransportAddress();
public abstract void touch();
public abstract void markFailure();
public abstract boolean terminateInput(String inputId);
public abstract String getNodeId();
public abstract InputLaunchResponse launchInput(String title, String type, Boolean global, Map<String, Object> configuration, boolean isExclusive) throws ExclusiveInputException;
public abstract InputLaunchResponse launchInput(String title, String type, Boolean global, Map<String, Object> configuration, boolean isExclusive, String nodeId) throws ExclusiveInputException; public abstract boolean launchExistingInput(String inputId);
public abstract InputTypeSummaryResponse getInputTypeInformation(String type) throws IOException, APIException;
public abstract void stopInput(String inputId) throws IOException, APIException;
public abstract void startInput(String inputId) throws IOException, APIException;
Expand Down
Expand Up @@ -23,13 +23,15 @@
import org.graylog2.restclient.lib.ApiClient;
import org.graylog2.restclient.lib.ExclusiveInputException;
import org.graylog2.restclient.lib.ServerNodes;
import org.graylog2.restclient.models.api.requests.InputLaunchRequest;
import org.graylog2.restclient.models.api.responses.system.*;
import org.graylog2.restclient.models.api.responses.system.InputLaunchResponse;
import org.graylog2.restclient.models.api.responses.system.InputStateSummaryResponse;
import org.graylog2.restclient.models.api.responses.system.InputTypeSummaryResponse;
import org.graylog2.restclient.models.api.responses.system.InputTypesResponse;
import org.graylog2.restclient.models.api.responses.system.InputsResponse;
import org.graylog2.restroutes.generated.InputsResource;
import org.graylog2.restroutes.generated.routes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.mvc.Http;

import java.io.IOException;
import java.util.AbstractMap;
Expand Down Expand Up @@ -194,19 +196,17 @@ public String launchGlobal(String title, String type, Map<String, Object> config

for (Node serverNode: serverNodes.all()) {
if (!serverNode.isMaster())
serverNode.launchExistingInput(ilr.persistId);
serverNode.launchExistingInput(ilr.id);
}
try {
for (Radio radio : nodeService.radios().values()) {
radio.launchExistingInput(ilr.persistId);
radio.launchExistingInput(ilr.id);
}
} catch (APIException e) {
log.error("Unable to fetch list of radios: " + e);
} catch (IOException e) {
} catch (APIException | IOException e) {
log.error("Unable to fetch list of radios: " + e);
}

return ilr.persistId;
return ilr.id;
}

public Map<ClusterEntity, Boolean> terminateGlobal(String inputId) {
Expand Down
Expand Up @@ -21,12 +21,12 @@
import com.google.common.net.MediaType;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.graylog2.rest.models.system.inputs.requests.InputLaunchRequest;
import org.graylog2.restclient.lib.APIException;
import org.graylog2.restclient.lib.ApiClient;
import org.graylog2.restclient.lib.DateTools;
import org.graylog2.restclient.lib.ExclusiveInputException;
import org.graylog2.restclient.lib.metrics.Metric;
import org.graylog2.restclient.models.api.requests.InputLaunchRequest;
import org.graylog2.restclient.models.api.responses.BufferClassesResponse;
import org.graylog2.restclient.models.api.responses.BuffersResponse;
import org.graylog2.restclient.models.api.responses.JournalInfo;
Expand Down Expand Up @@ -235,12 +235,8 @@ public int numberOfInputs() {
return inputs().total;
}

public InputLaunchResponse updateInput(String inputId, String title, String type, boolean global, Map<String, Object> configuration) {
final InputLaunchRequest request = new InputLaunchRequest();
request.title = title;
request.type = type;
request.global = global;
request.configuration = configuration;
public InputLaunchResponse updateInput(String inputId, String title, String type, boolean global, Map<String, Object> configuration, String node) {
final InputLaunchRequest request = InputLaunchRequest.create(title, type, global, configuration, node);

try {
return api.path(routes.InputsResource().update(inputId), InputLaunchResponse.class)
Expand All @@ -255,7 +251,7 @@ public InputLaunchResponse updateInput(String inputId, String title, String type
}

@Override
public InputLaunchResponse launchInput(String title, String type, Boolean global, Map<String, Object> configuration, boolean isExclusive) throws ExclusiveInputException {
public InputLaunchResponse launchInput(String title, String type, Boolean global, Map<String, Object> configuration, boolean isExclusive, String nodeId) throws ExclusiveInputException {
if (isExclusive) {
for (Input input : getInputs()) {
if (input.getType().equals(type)) {
Expand All @@ -264,11 +260,7 @@ public InputLaunchResponse launchInput(String title, String type, Boolean global
}
}

final InputLaunchRequest request = new InputLaunchRequest();
request.title = title;
request.type = type;
request.global = global;
request.configuration = configuration;
final InputLaunchRequest request = InputLaunchRequest.create(title, type, global, configuration, nodeId);

try {
return api.path(routes.InputsResource().create(), InputLaunchResponse.class)
Expand All @@ -282,6 +274,7 @@ public InputLaunchResponse launchInput(String title, String type, Boolean global
}
}

@Override
public boolean launchExistingInput(String inputId) {
try {
api.path(routes.InputsResource().launchExisting(inputId))
Expand Down
Expand Up @@ -21,11 +21,11 @@
import com.google.common.net.MediaType;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.graylog2.rest.models.system.inputs.requests.InputLaunchRequest;
import org.graylog2.restclient.lib.APIException;
import org.graylog2.restclient.lib.ApiClient;
import org.graylog2.restclient.lib.ExclusiveInputException;
import org.graylog2.restclient.lib.metrics.Metric;
import org.graylog2.restclient.models.api.requests.InputLaunchRequest;
import org.graylog2.restclient.models.api.responses.BuffersResponse;
import org.graylog2.restclient.models.api.responses.SystemOverviewResponse;
import org.graylog2.restclient.models.api.responses.cluster.RadioSummaryResponse;
Expand Down Expand Up @@ -153,6 +153,7 @@ public void overrideLbStatus(String override) throws APIException, IOException {
.execute();
}

@Override
public boolean launchExistingInput(String inputId) {
try {
api.path(routes.radio().InputsResource().launchExisting(inputId), InputLaunchResponse.class)
Expand Down Expand Up @@ -280,7 +281,7 @@ private InputsResponse inputs() {
}

@Override
public InputLaunchResponse launchInput(String title, String type, Boolean global, Map<String, Object> configuration, boolean isExclusive) throws ExclusiveInputException {
public InputLaunchResponse launchInput(String title, String type, Boolean global, Map<String, Object> configuration, boolean isExclusive, String nodeId) throws ExclusiveInputException {
if (isExclusive) {
for (Input input : getInputs()) {
if (input.getType().equals(type)) {
Expand All @@ -289,11 +290,7 @@ public InputLaunchResponse launchInput(String title, String type, Boolean global
}
}

final InputLaunchRequest request = new InputLaunchRequest();
request.title = title;
request.type = type;
request.global = global;
request.configuration = configuration;
final InputLaunchRequest request = InputLaunchRequest.create(title, type, global, configuration, nodeId);

try {
return api.path(routes.radio().InputsResource().launch(), InputLaunchResponse.class)
Expand Down

This file was deleted.

Expand Up @@ -22,9 +22,6 @@
* @author Dennis Oelkers <dennis@torch.sh>
*/
public class InputLaunchResponse {
@JsonProperty("input_id")
public String inputId;

@JsonProperty("persist_id")
public String persistId;
@JsonProperty
public String id;
}
Expand Up @@ -16,16 +16,20 @@
*/
package org.graylog2.restclient.models.api.requests;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.graylog2.rest.models.system.inputs.requests.InputLaunchRequest;
import org.testng.annotations.Test;

import java.util.HashMap;

import static org.testng.AssertJUnit.assertNotNull;

public class InputLaunchRequestTest {
@Test
public void testToJson() {
final InputLaunchRequest request = new InputLaunchRequest();
request.title = "title";
final String json = request.toJson();
public void testToJson() throws JsonProcessingException {
final InputLaunchRequest request = InputLaunchRequest.create("title", "type", true, new HashMap<String, Object>(), "foobar");
final String json = new ObjectMapper().writeValueAsString(request);

assertNotNull(json);
}
Expand Down
Expand Up @@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with Graylog2. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.shared.rest.resources.system.inputs.requests;
package org.graylog2.rest.models.system.inputs.requests;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
Expand All @@ -33,22 +33,22 @@ public abstract class InputLaunchRequest {
@JsonProperty
public abstract String type();

@JsonProperty("creator_user_id")
@Nullable
public abstract String creatorUserId();

@JsonProperty
public abstract boolean global();

@JsonProperty
public abstract Map<String, Object> configuration();

@JsonProperty
@Nullable
public abstract String node();

@JsonCreator
public static InputLaunchRequest create(@JsonProperty("title") String title,
@JsonProperty("type") String type,
@JsonProperty("creator_user_id") String creatorUserId,
@JsonProperty("global") boolean global,
@JsonProperty("configuration") Map<String, Object> configuration) {
return new AutoValue_InputLaunchRequest(title, type, creatorUserId, global, configuration);
@JsonProperty("configuration") Map<String, Object> configuration,
@JsonProperty("node") String node) {
return new AutoValue_InputLaunchRequest(title, type, global, configuration, node);
}
}

0 comments on commit a1060e9

Please sign in to comment.