Skip to content

Commit

Permalink
IGNITE-16487 Use PATCH HTTP verb instead of PUT for modifying cluster…
Browse files Browse the repository at this point in the history
…/node configuration (#638)
  • Loading branch information
rpuch committed Feb 8, 2022
1 parent c06f4c4 commit 33b449a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
Expand Up @@ -43,8 +43,8 @@ public class ConfigurationClient {
/** Url for getting configuration from REST endpoint of the node. */
private static final String GET_URL = "/management/v1/configuration/";

/** Url for setting configuration with REST endpoint of the node. */
private static final String SET_URL = "/management/v1/configuration/";
/** Url for patching configuration with REST endpoint of the node. */
private static final String PATCH_URL = "/management/v1/configuration/";

/** Http client. */
private final HttpClient httpClient;
Expand Down Expand Up @@ -119,9 +119,9 @@ public String get(
public void set(String host, int port, String rawHoconData, PrintWriter out, ColorScheme cs, String type) {
var req = HttpRequest
.newBuilder()
.PUT(HttpRequest.BodyPublishers.ofString(renderJsonFromHocon(rawHoconData)))
.method("PATCH", HttpRequest.BodyPublishers.ofString(renderJsonFromHocon(rawHoconData)))
.header("Content-Type", "application/json")
.uri(URI.create("http://" + host + ":" + port + SET_URL + type + "/"))
.uri(URI.create("http://" + host + ":" + port + PATCH_URL + type + "/"))
.build();

try {
Expand Down
Expand Up @@ -553,7 +553,7 @@ void setHocon() throws IOException, InterruptedException {
Assertions.assertEquals(0, exitCode);
verify(httpClient).send(
argThat(r -> "http://localhost:8081/management/v1/configuration/node/".equals(r.uri().toString())
&& "PUT".equals(r.method())
&& "PATCH".equals(r.method())
&& r.bodyPublisher().get().contentLength() == expSentContent.getBytes().length
&& "application/json".equals(r.headers().firstValue("Content-Type").get())),
any());
Expand Down Expand Up @@ -583,7 +583,7 @@ void setJson() throws IOException, InterruptedException {
Assertions.assertEquals(0, exitCode);
verify(httpClient).send(
argThat(r -> "http://localhost:8081/management/v1/configuration/node/".equals(r.uri().toString())
&& "PUT".equals(r.method())
&& "PATCH".equals(r.method())
&& r.bodyPublisher().get().contentLength() == expSentContent.getBytes().length
&& "application/json".equals(r.headers().firstValue("Content-Type").get())),
any());
Expand Down
Expand Up @@ -126,12 +126,12 @@ public void start() {
CLUSTER_CFG_URL + ":" + PATH_PARAM,
(req, resp) -> handleRepresentByPath(req, resp, clusterCfgPresentation)
)
.put(
.patch(
NODE_CFG_URL,
APPLICATION_JSON,
(req, resp) -> handleUpdate(req, resp, nodeCfgPresentation)
)
.put(
.patch(
CLUSTER_CFG_URL,
APPLICATION_JSON,
(req, resp) -> handleUpdate(req, resp, clusterCfgPresentation)
Expand Down
Expand Up @@ -106,6 +106,23 @@ public Router put(
return this;
}

/**
* Defines a PATCH route.
*
* @param route Route.
* @param acceptType Accept type.
* @param hnd Actual handler of the request.
* @return Router
*/
public Router patch(
String route,
AsciiString acceptType,
BiConsumer<RestApiHttpRequest, RestApiHttpResponse> hnd
) {
addRoute(new Route(route, HttpMethod.PATCH, acceptType.toString(), hnd));
return this;
}

/**
* Adds the route to router chain.
*
Expand Down
Expand Up @@ -18,6 +18,7 @@
package org.apache.ignite.internal.rest.routes;

import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpMethod.PATCH;
import static io.netty.handler.codec.http.HttpMethod.PUT;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -82,4 +83,13 @@ void testMatchByUriWithParams() {
var req = new DefaultHttpRequest(HTTP_1_1, GET, "/user/John");
assertTrue(route.match(req));
}

@Test
void testPatchVerbMatch() {
var route = new Route("/user", PATCH, null, (request, response) -> {
});
var req = new DefaultHttpRequest(HTTP_1_1, PATCH, "/user");

assertTrue(route.match(req));
}
}

0 comments on commit 33b449a

Please sign in to comment.