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

Commit 09bb43a

Browse files
committed
#201 Deploying servers now accounts for group-name in payload
1 parent caf3544 commit 09bb43a

File tree

7 files changed

+110
-8
lines changed

7 files changed

+110
-8
lines changed

src/main/java/com/marklogic/appdeployer/command/AbstractCommand.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,25 @@ protected String copyFileToString(File f, CommandContext context) {
132132
*/
133133
protected SaveReceipt saveResource(ResourceManager mgr, CommandContext context, File f) {
134134
String payload = copyFileToString(f, context);
135+
mgr = adjustResourceManagerForPayload(mgr, context, payload);
135136
SaveReceipt receipt = mgr.save(payload);
136137
if (storeResourceIdsAsCustomTokens) {
137138
storeTokenForResourceId(receipt, context);
138139
}
139140
return receipt;
140141
}
141142

143+
/**
144+
* A subclass can override this when the ResourceManager needs to be adjusted based on data in the payload.
145+
*
146+
* @param mgr
147+
* @param payload
148+
* @return
149+
*/
150+
protected ResourceManager adjustResourceManagerForPayload(ResourceManager mgr, CommandContext context, String payload) {
151+
return mgr;
152+
}
153+
142154
/**
143155
* Any resource that may be referenced by its ID by another resource will most likely need its ID stored as a custom
144156
* token so that it can be referenced by the other resource. To enable this, the subclass should set

src/main/java/com/marklogic/appdeployer/command/AbstractResourceCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,19 @@ protected void processUndoOnResourceDir(CommandContext context, File resourceDir
125125
* @param context
126126
* @param f
127127
*/
128-
protected void deleteResource(final ResourceManager mgr, CommandContext context, File f) {
128+
protected void deleteResource(ResourceManager mgr, CommandContext context, File f) {
129129
final String payload = copyFileToString(f, context);
130+
final ResourceManager resourceManager = adjustResourceManagerForPayload(mgr, context, payload);
130131
try {
131132
if (restartAfterDelete) {
132133
context.getAdminManager().invokeActionRequiringRestart(new ActionRequiringRestart() {
133134
@Override
134135
public boolean execute() {
135-
return mgr.delete(payload).isDeleted();
136+
return resourceManager.delete(payload).isDeleted();
136137
}
137138
});
138139
} else {
139-
mgr.delete(payload);
140+
resourceManager.delete(payload);
140141
}
141142
} catch (RuntimeException e) {
142143
if (catchExceptionOnDeleteFailure) {

src/main/java/com/marklogic/appdeployer/command/appservers/DeployOtherServersCommand.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import com.marklogic.appdeployer.command.CommandContext;
55
import com.marklogic.appdeployer.command.ResourceFilenameFilter;
66
import com.marklogic.appdeployer.command.SortOrderConstants;
7+
import com.marklogic.mgmt.PayloadParser;
8+
import com.marklogic.mgmt.SaveReceipt;
79
import com.marklogic.mgmt.resource.ResourceManager;
810
import com.marklogic.mgmt.resource.appservers.ServerManager;
11+
import com.marklogic.mgmt.util.ObjectMapperFactory;
912

1013
import java.io.File;
1114

@@ -38,4 +41,21 @@ public Integer getUndoSortOrder() {
3841
return 0;
3942
}
4043

44+
/**
45+
* If the payload has a group-name that differs from the group name in the AppConfig, then this returns a new
46+
* ServerManager using the group-name in the payload.
47+
*
48+
* @param mgr
49+
* @param context
50+
* @param payload
51+
* @return
52+
*/
53+
@Override
54+
protected ResourceManager adjustResourceManagerForPayload(ResourceManager mgr, CommandContext context, String payload) {
55+
String groupName = new PayloadParser().getPayloadFieldValue(payload, "group-name", false);
56+
if (groupName != null && !groupName.equals(context.getAppConfig().getGroupName())) {
57+
return new ServerManager(context.getManageClient(), groupName);
58+
}
59+
return mgr;
60+
}
4161
}

src/main/java/com/marklogic/mgmt/PayloadParser.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,31 @@ public String getPayloadName(String payload, String idFieldName) {
2828
return getPayloadFieldValue(payload, idFieldName);
2929
}
3030

31-
public String getPayloadFieldValue(String payload, String fieldName) {
31+
public String getPayloadFieldValue(String payload, String fieldName) {
32+
return getPayloadFieldValue(payload, fieldName, true);
33+
}
34+
35+
public String getPayloadFieldValue(String payload, String fieldName, boolean throwErrorIfNotFound) {
3236
if (isJsonPayload(payload)) {
3337
JsonNode node = parseJson(payload);
3438
if (!node.has(fieldName)) {
35-
throw new RuntimeException("Cannot get field value from JSON; field name: " + fieldName + "; JSON: "
36-
+ payload);
39+
if (throwErrorIfNotFound) {
40+
throw new RuntimeException("Cannot get field value from JSON; field name: " + fieldName + "; JSON: "
41+
+ payload);
42+
} else {
43+
return null;
44+
}
3745
}
3846
return node.get(fieldName).isTextual() ? node.get(fieldName).asText() : node.get(fieldName).toString();
39-
4047
} else {
4148
Fragment f = new Fragment(payload);
4249
String xpath = String.format("/node()/*[local-name(.) = '%s']", fieldName);
4350
if (!f.elementExists(xpath)) {
44-
throw new RuntimeException("Cannot get field value from XML at path: " + xpath + "; XML: " + payload);
51+
if (throwErrorIfNotFound) {
52+
throw new RuntimeException("Cannot get field value from XML at path: " + xpath + "; XML: " + payload);
53+
} else {
54+
return null;
55+
}
4556
}
4657
return f.getElementValues(xpath).get(0);
4758
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.marklogic.appdeployer.command.servers;
2+
3+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.ConfigDir;
5+
import com.marklogic.appdeployer.command.appservers.DeployOtherServersCommand;
6+
import com.marklogic.appdeployer.command.groups.DeployGroupsCommand;
7+
import com.marklogic.mgmt.resource.appservers.ServerManager;
8+
import com.marklogic.mgmt.resource.groups.GroupManager;
9+
import org.junit.Test;
10+
11+
import java.io.File;
12+
13+
public class UpdateServerInOtherGroupTest extends AbstractAppDeployerTest {
14+
15+
@Test
16+
public void test() {
17+
appConfig.setConfigDir(new ConfigDir(new File("src/test/resources/sample-app/other-group")));
18+
19+
appConfig.setGroupName("Default");
20+
21+
final String otherGroup = "sample-app-other-group";
22+
final String otherServer = "sample-app-other-server";
23+
24+
GroupManager groupManager = new GroupManager(manageClient);
25+
ServerManager serverManager = new ServerManager(manageClient);
26+
27+
initializeAppDeployer(new DeployGroupsCommand(), new DeployOtherServersCommand());
28+
29+
try {
30+
deploySampleApp();
31+
32+
assertTrue(groupManager.exists(otherGroup));
33+
assertTrue(serverManager.exists(otherServer));
34+
35+
// Now deploy it again, verifying no errors - i.e. that the other group name is included correctly in the call to update the server
36+
deploySampleApp();
37+
38+
assertTrue(groupManager.exists(otherGroup));
39+
assertTrue(serverManager.exists(otherServer));
40+
41+
} finally {
42+
undeploySampleApp();
43+
}
44+
}
45+
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"group-name": "sample-app-other-group"
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"server-name": "sample-app-other-server",
3+
"server-type": "http",
4+
"root": "/",
5+
"group-name": "sample-app-other-group",
6+
"port": 8048,
7+
"modules-database": "Modules",
8+
"content-database": "Documents"
9+
}

0 commit comments

Comments
 (0)