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

Commit 70cf912

Browse files
committed
Merge branch 'dev'
2 parents 96c6ee4 + acd3642 commit 70cf912

File tree

6 files changed

+174
-50
lines changed

6 files changed

+174
-50
lines changed

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

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,49 +25,83 @@ public class DeployRestApiServersCommand extends AbstractCommand implements Undo
2525
private boolean deleteModulesDatabase = true;
2626
private boolean deleteContentDatabase = false;
2727

28+
private String restApiFilename;
29+
2830
public DeployRestApiServersCommand() {
2931
setExecuteSortOrder(SortOrderConstants.DEPLOY_REST_API_SERVERS);
3032
}
3133

34+
public DeployRestApiServersCommand(String restApiFilename) {
35+
this();
36+
this.restApiFilename = restApiFilename;
37+
}
38+
3239
public DeployRestApiServersCommand(boolean deleteContentDatabase) {
3340
this();
3441
this.deleteContentDatabase = deleteContentDatabase;
3542
}
3643

44+
public DeployRestApiServersCommand(String restApiFilename, boolean deleteContentDatabase) {
45+
this();
46+
this.restApiFilename = restApiFilename;
47+
this.deleteContentDatabase = deleteContentDatabase;
48+
}
49+
3750
@Override
3851
public Integer getUndoSortOrder() {
3952
return SortOrderConstants.DELETE_REST_API_SERVERS;
4053
}
4154

4255
@Override
4356
public void execute(CommandContext context) {
44-
File f = context.getAppConfig().getConfigDir().getRestApiFile();
45-
String payload = null;
57+
String payload = getRestApiPayload(context);
58+
if (payload != null) {
59+
RestApiManager mgr = new RestApiManager(context.getManageClient());
60+
AppConfig appConfig = context.getAppConfig();
61+
62+
mgr.createRestApi(tokenReplacer.replaceTokens(payload, appConfig, false));
63+
64+
if (appConfig.isTestPortSet()) {
65+
mgr.createRestApi(tokenReplacer.replaceTokens(payload, appConfig, true));
66+
}
67+
}
68+
}
69+
70+
protected String getRestApiPayload(CommandContext context) {
71+
File f = findRestApiConfigFile(context);
4672
if (f.exists()) {
47-
payload = copyFileToString(f);
73+
return copyFileToString(f);
4874
} else {
4975
logger.info(format("Could not find REST API file at %s, will use default payload", f.getAbsolutePath()));
50-
payload = getDefaultRestApiPayload();
76+
return getDefaultRestApiPayload();
5177
}
78+
}
5279

53-
RestApiManager mgr = new RestApiManager(context.getManageClient());
54-
AppConfig appConfig = context.getAppConfig();
55-
56-
mgr.createRestApi(appConfig.getRestServerName(), tokenReplacer.replaceTokens(payload, appConfig, false));
57-
58-
if (appConfig.isTestPortSet()) {
59-
mgr.createRestApi(appConfig.getTestRestServerName(), tokenReplacer.replaceTokens(payload, appConfig, true));
80+
protected File findRestApiConfigFile(CommandContext context) {
81+
if (restApiFilename != null) {
82+
return new File(context.getAppConfig().getConfigDir().getBaseDir(), restApiFilename);
83+
} else {
84+
return context.getAppConfig().getConfigDir().getRestApiFile();
6085
}
6186
}
6287

6388
@Override
6489
public void undo(CommandContext context) {
90+
deleteTestRestServer(context);
91+
deleteMainRestServer(context);
92+
}
93+
94+
/**
95+
* If we have a test REST API, we first modify it to point at Documents for the modules database so we can safely
96+
* delete each REST API
97+
*
98+
* @param context
99+
*/
100+
protected void deleteTestRestServer(CommandContext context) {
65101
final AppConfig appConfig = context.getAppConfig();
66102
final ManageClient manageClient = context.getManageClient();
67103

68104
ServerManager mgr = new ServerManager(manageClient, appConfig.getGroupName());
69-
// If we have a test REST API, first modify it to point at Documents for the modules database so we can safely
70-
// delete each REST API
71105
if (appConfig.isTestPortSet() && mgr.exists(appConfig.getTestRestServerName())) {
72106
mgr.setModulesDatabaseToDocuments(appConfig.getTestRestServerName());
73107
context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() {
@@ -78,13 +112,24 @@ public boolean execute() {
78112
}
79113
});
80114
}
115+
}
116+
117+
protected void deleteMainRestServer(CommandContext context) {
118+
final AppConfig appConfig = context.getAppConfig();
119+
final ManageClient manageClient = context.getManageClient();
81120

82-
if (mgr.exists(appConfig.getRestServerName())) {
121+
ServerManager mgr = new ServerManager(manageClient, appConfig.getGroupName());
122+
123+
String payload = getRestApiPayload(context);
124+
payload = tokenReplacer.replaceTokens(payload, appConfig, false);
125+
final String serverName = new RestApiManager(manageClient).extractNameFromJson(payload);
126+
127+
if (mgr.exists(serverName)) {
83128
context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() {
84129
@Override
85130
public boolean execute() {
86-
return deleteRestApi(appConfig.getRestServerName(), appConfig.getGroupName(), manageClient,
87-
deleteModulesDatabase, deleteContentDatabase);
131+
return deleteRestApi(serverName, appConfig.getGroupName(), manageClient, deleteModulesDatabase,
132+
deleteContentDatabase);
88133
}
89134
});
90135
}
@@ -94,6 +139,11 @@ protected String getDefaultRestApiPayload() {
94139
return RestApiUtil.buildDefaultRestApiJson();
95140
}
96141

142+
/**
143+
* TODO Move this to RestApiManager.
144+
*
145+
* @return
146+
*/
97147
protected boolean deleteRestApi(String serverName, String groupName, ManageClient manageClient,
98148
boolean includeModules, boolean includeContent) {
99149
if (new ServerManager(manageClient, groupName).exists(serverName)) {
@@ -130,4 +180,12 @@ public void setDeleteContentDatabase(boolean includeContent) {
130180
this.deleteContentDatabase = includeContent;
131181
}
132182

183+
public String getRestApiFilename() {
184+
return restApiFilename;
185+
}
186+
187+
public void setRestApiFilename(String restApiFilename) {
188+
this.restApiFilename = restApiFilename;
189+
}
190+
133191
}

src/main/java/com/marklogic/mgmt/restapis/RestApiManager.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22

33
import org.springframework.http.ResponseEntity;
44

5+
import com.fasterxml.jackson.databind.JsonNode;
56
import com.marklogic.client.helper.LoggingObject;
67
import com.marklogic.mgmt.ManageClient;
8+
import com.marklogic.mgmt.PayloadParser;
79
import com.marklogic.rest.util.Fragment;
810

911
/**
10-
* For /v1/rest-apis.
12+
* For /v1/rest-apis. Currently only supports JSON files.
1113
*/
1214
public class RestApiManager extends LoggingObject {
1315

16+
private PayloadParser payloadParser = new PayloadParser();
1417
private ManageClient client;
1518

1619
public RestApiManager(ManageClient client) {
1720
this.client = client;
1821
}
1922

23+
public ResponseEntity<String> createRestApi(String json) {
24+
return createRestApi(extractNameFromJson(json), json);
25+
}
26+
2027
public ResponseEntity<String> createRestApi(String name, String json) {
2128
logger.info("Checking for existence of REST API with name: " + name);
2229
if (restApiServerExists(name)) {
@@ -30,6 +37,11 @@ public ResponseEntity<String> createRestApi(String name, String json) {
3037
}
3138
}
3239

40+
public String extractNameFromJson(String json) {
41+
JsonNode node = payloadParser.parseJson(json);
42+
return node.get("rest-api").get("name").textValue();
43+
}
44+
3345
public boolean restApiServerExists(String name) {
3446
Fragment f = client.getXml("/v1/rest-apis?format=xml", "rapi", "http://marklogic.com/rest-api");
3547
return f.elementExists(String.format("/rapi:rest-apis/rapi:rest-api[rapi:name = '%s']", name));

src/test/java/com/marklogic/appdeployer/command/restapis/CreateRestApiAsNonAdminUserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void test() {
5454
appConfig.setRestAdminUsername("sample-app-rest-admin");
5555
appConfig.setRestAdminPassword("sample-app-rest-admin");
5656

57-
initializeAppDeployer(new DeployRestApiServersCommand(), new DeployRolesCommand(), new DeployUsersCommand(),
57+
initializeAppDeployer(new DeployRestApiServersCommand(true), new DeployRolesCommand(), new DeployUsersCommand(),
5858
new LoadModulesCommand());
5959
appDeployer.deploy(appConfig);
6060

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.marklogic.appdeployer.command.restapis;
2+
3+
import java.io.File;
4+
5+
import org.junit.Test;
6+
7+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
8+
import com.marklogic.appdeployer.ConfigDir;
9+
import com.marklogic.mgmt.appservers.ServerManager;
10+
import com.marklogic.mgmt.databases.DatabaseManager;
11+
import com.marklogic.mgmt.forests.ForestManager;
12+
13+
public class CreateRestApiWithCustomFilenameTest extends AbstractAppDeployerTest {
14+
15+
@Test
16+
public void customFilename() {
17+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/rest-api-custom-filename")));
18+
19+
initializeAppDeployer(new DeployRestApiServersCommand("my-custom-rest-api.json", true));
20+
21+
ServerManager serverMgr = new ServerManager(manageClient);
22+
DatabaseManager dbMgr = new DatabaseManager(manageClient);
23+
ForestManager forestMgr = new ForestManager(manageClient);
24+
25+
try {
26+
appDeployer.deploy(appConfig);
27+
28+
assertTrue(serverMgr.exists("my-custom-rest-api"));
29+
assertTrue(dbMgr.exists("my-custom-content"));
30+
assertTrue(dbMgr.exists("my-custom-modules"));
31+
assertTrue(forestMgr.exists("my-custom-content-1"));
32+
assertFalse("The custom REST API file only asks for 1 forest", forestMgr.exists("my-custom-content-2"));
33+
} finally {
34+
undeploySampleApp();
35+
36+
assertFalse(serverMgr.exists("my-custom-rest-api"));
37+
assertFalse(dbMgr.exists("my-custom-content"));
38+
assertFalse(dbMgr.exists("my-custom-modules"));
39+
assertFalse(forestMgr.exists("my-custom-content-1"));
40+
}
41+
}
42+
}

src/test/java/com/marklogic/appdeployer/command/schemas/LoadSchemasTest.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,53 @@
1111
import com.marklogic.appdeployer.command.restapis.DeployRestApiServersCommand;
1212
import com.marklogic.client.DatabaseClient;
1313
import com.marklogic.client.document.GenericDocumentManager;
14+
import com.marklogic.client.io.DocumentMetadataHandle;
1415

15-
public class LoadSchemasTest extends AbstractAppDeployerTest {
16+
public class LoadSchemasTest extends AbstractAppDeployerTest {
1617

17-
@Test
18-
public void testSchemaLoading() {
19-
initializeAppDeployer(new DeploySchemasDatabaseCommand(),
20-
new DeployTriggersDatabaseCommand(),
21-
new DeployContentDatabasesCommand(1),
22-
new DeployRestApiServersCommand(),
23-
newCommand());
18+
@Test
19+
public void testSchemaLoading() {
20+
initializeAppDeployer(new DeploySchemasDatabaseCommand(), new DeployTriggersDatabaseCommand(),
21+
new DeployContentDatabasesCommand(1), new DeployRestApiServersCommand(), newCommand());
2422
appDeployer.deploy(appConfig);
25-
23+
2624
DatabaseClient client = appConfig.newSchemasDatabaseClient();
27-
25+
2826
GenericDocumentManager docMgr = client.newDocumentManager();
29-
27+
3028
assertNull("Rules document loaded", docMgr.exists("notExists"));
3129
assertNotNull("Rules document loaded", docMgr.exists("my.rules").getUri());
3230
assertNotNull("XSD document loaded", docMgr.exists("x.xsd").getUri());
33-
}
34-
35-
@Test
36-
public void testSchemaCustomSchemasPath() {
37-
initializeAppDeployer(new DeploySchemasDatabaseCommand(),
38-
new DeployTriggersDatabaseCommand(),
39-
new DeployContentDatabasesCommand(1),
40-
new DeployRestApiServersCommand(),
41-
newCommand());
31+
}
32+
33+
@Test
34+
public void testSchemaCustomSchemasPath() {
35+
initializeAppDeployer(new DeploySchemasDatabaseCommand(), new DeployTriggersDatabaseCommand(),
36+
new DeployContentDatabasesCommand(1), new DeployRestApiServersCommand(), newCommand());
4237
appConfig.setSchemasPath("src/test/resources/schemas-marklogic9");
4338
appDeployer.deploy(appConfig);
44-
45-
39+
4640
DatabaseClient client = appConfig.newSchemasDatabaseClient();
47-
41+
4842
GenericDocumentManager docMgr = client.newDocumentManager();
49-
43+
5044
assertNotNull("TDEXML document loaded", docMgr.exists("x.tdex").getUri());
5145
assertNotNull("TDEJSON document loaded", docMgr.exists("x.tdej").getUri());
52-
}
53-
54-
@After
55-
public void cleanup() {
46+
47+
for (String uri : new String[] { "x.tdex", "x.tdej" }) {
48+
DocumentMetadataHandle h = docMgr.readMetadata(uri, new DocumentMetadataHandle());
49+
assertEquals("Files ending in tdex and tdej go into a special collection", "http://marklogic.com/xdmp/tde",
50+
h.getCollections().iterator().next());
51+
}
52+
}
53+
54+
@After
55+
public void cleanup() {
5656
undeploySampleApp();
5757
}
5858

59-
private Command newCommand() {
60-
return new LoadSchemasCommand();
61-
}
62-
59+
private Command newCommand() {
60+
return new LoadSchemasCommand();
61+
}
62+
6363
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"rest-api": {
3+
"name": "my-custom-rest-api",
4+
"group": "%%GROUP%%",
5+
"database": "my-custom-content",
6+
"modules-database": "my-custom-modules",
7+
"port": "%%PORT%%",
8+
"xdbc-enabled": true,
9+
"forests-per-host": 1,
10+
"error-format": "xml"
11+
}
12+
}

0 commit comments

Comments
 (0)