Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 7fdf620

Browse files
committed
#332 Default REST API payload now uses contentForestsPerHost
1 parent d546775 commit 7fdf620

File tree

3 files changed

+70
-25
lines changed

3 files changed

+70
-25
lines changed

src/main/java/com/marklogic/appdeployer/command/restapis/DeployRestApiServersCommand.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected String getRestApiPayload(CommandContext context) {
8080
return null;
8181
} else {
8282
logger.info(format("Could not find REST API file at %s, will use default payload", f.getAbsolutePath()));
83-
return getDefaultRestApiPayload();
83+
return getDefaultRestApiPayload(context);
8484
}
8585
}
8686

@@ -104,8 +104,7 @@ protected File findRestApiConfigFile(CommandContext context) {
104104
restApiFile = new File(context.getAppConfig().getFirstConfigDir().getBaseDir(), restApiFilename);
105105
}
106106
return restApiFile;
107-
}
108-
else {
107+
} else {
109108
File restApiFile = null;
110109
// Check each ConfigDir for the file, with the last one winning
111110
for (ConfigDir configDir : context.getAppConfig().getConfigDirs()) {
@@ -173,8 +172,12 @@ public boolean execute() {
173172
}
174173
}
175174

176-
protected String getDefaultRestApiPayload() {
177-
return RestApiUtil.buildDefaultRestApiJson();
175+
protected String getDefaultRestApiPayload(CommandContext context) {
176+
// Use contentForestsPerHost in case the user does not have a database file for the content database that would
177+
// otherwise control the number of forests created (as it would be created before the REST API instance is
178+
// created)
179+
Integer count = context.getAppConfig().getContentForestsPerHost();
180+
return count != null ? RestApiUtil.buildDefaultRestApiJson(count) : RestApiUtil.buildDefaultRestApiJson();
178181
}
179182

180183
/**
@@ -188,7 +191,7 @@ protected String getDefaultRestApiPayload() {
188191
* @return
189192
*/
190193
protected boolean deleteRestApi(String serverName, String groupName, ManageClient manageClient,
191-
boolean includeModules, boolean includeContent) {
194+
boolean includeModules, boolean includeContent) {
192195
RestApiDeletionRequest request = new RestApiDeletionRequest(serverName, groupName);
193196
request.setIncludeContent(includeContent);
194197
request.setIncludeModules(includeModules);

src/main/java/com/marklogic/appdeployer/util/RestApiUtil.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,35 @@
99
public abstract class RestApiUtil {
1010

1111
public static String buildDefaultRestApiJson() {
12-
ObjectMapper m = ObjectMapperFactory.getObjectMapper();
13-
ObjectNode node = m.createObjectNode();
14-
ObjectNode n = node.putObject("rest-api");
15-
n.put("name", "%%NAME%%");
16-
n.put("group", "%%GROUP%%");
17-
n.put("database", "%%DATABASE%%");
18-
n.put("modules-database", "%%MODULES_DATABASE%%");
19-
n.put("port", "%%PORT%%");
20-
n.put("xdbc-enabled", true);
21-
// n.put("forests-per-host", 3);
22-
n.put("error-format", "json");
12+
return buildDefaultRestApiJson(0);
13+
}
2314

24-
try {
25-
String json = m.writer(new DefaultPrettyPrinter()).writeValueAsString(node);
26-
json = json.replace("\"%%PORT%%\"", "%%PORT%%");
27-
return json;
28-
} catch (JsonProcessingException ex) {
29-
throw new RuntimeException(ex);
30-
}
31-
}
15+
/**
16+
* @param forestsPerHost if greater than zero, than will be used to set the "forests-per-host" property in the
17+
* payload
18+
* @return
19+
*/
20+
public static String buildDefaultRestApiJson(int forestsPerHost) {
21+
ObjectMapper m = ObjectMapperFactory.getObjectMapper();
22+
ObjectNode node = m.createObjectNode();
23+
ObjectNode n = node.putObject("rest-api");
24+
n.put("name", "%%NAME%%");
25+
n.put("group", "%%GROUP%%");
26+
n.put("database", "%%DATABASE%%");
27+
n.put("modules-database", "%%MODULES_DATABASE%%");
28+
n.put("port", "%%PORT%%");
29+
n.put("xdbc-enabled", true);
30+
if (forestsPerHost > 0) {
31+
n.put("forests-per-host", forestsPerHost);
32+
}
33+
n.put("error-format", "json");
34+
35+
try {
36+
String json = m.writer(new DefaultPrettyPrinter()).writeValueAsString(node);
37+
json = json.replace("\"%%PORT%%\"", "%%PORT%%");
38+
return json;
39+
} catch (JsonProcessingException ex) {
40+
throw new RuntimeException(ex);
41+
}
42+
}
3243
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.marklogic.appdeployer.command.restapis;
2+
3+
import com.fasterxml.jackson.databind.node.ObjectNode;
4+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
5+
import com.marklogic.appdeployer.command.CommandContext;
6+
import com.marklogic.appdeployer.command.DefaultPayloadTokenReplacer;
7+
import com.marklogic.mgmt.util.ObjectMapperFactory;
8+
import org.junit.Test;
9+
10+
public class DeployRestApiTest extends AbstractAppDeployerTest {
11+
12+
@Test
13+
public void noRestApiFileAndNoContentDatabaseFile() throws Exception {
14+
final CommandContext context = new CommandContext(appConfig, null, null);
15+
16+
DeployRestApiServersCommand command = new DeployRestApiServersCommand();
17+
String payload = command.getDefaultRestApiPayload(context);
18+
payload = new DefaultPayloadTokenReplacer().replaceTokens(payload, appConfig, false);
19+
ObjectNode node = (ObjectNode) ObjectMapperFactory.getObjectMapper().readTree(payload);
20+
assertFalse("forests-per-host shouldn't be set unless contentForestsPerHost is greater than zero",
21+
node.get("rest-api").has("forests-per-host"));
22+
23+
appConfig.setContentForestsPerHost(2);
24+
25+
payload = command.getDefaultRestApiPayload(context);
26+
payload = new DefaultPayloadTokenReplacer().replaceTokens(payload, appConfig, false);
27+
node = (ObjectNode) ObjectMapperFactory.getObjectMapper().readTree(payload);
28+
assertEquals("When contentForestsPerHost is set, its value should be included in the REST API payload",
29+
2, node.get("rest-api").get("forests-per-host").asInt());
30+
}
31+
}

0 commit comments

Comments
 (0)