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

Commit 1e28c13

Browse files
committed
#355 Not deploying amps when they haven't changed
1 parent f439019 commit 1e28c13

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

src/main/java/com/marklogic/appdeployer/command/security/DeployAmpsCommand.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
import com.marklogic.mgmt.api.security.Amp;
1010
import com.marklogic.mgmt.resource.ResourceManager;
1111
import com.marklogic.mgmt.resource.security.AmpManager;
12+
import com.marklogic.rest.util.ResourcesFragment;
1213

1314
import java.io.File;
1415

1516
public class DeployAmpsCommand extends AbstractResourceCommand implements SupportsCmaCommand {
1617

18+
private AmpManager ampManager;
19+
private ResourcesFragment existingAmpResources;
20+
1721
public DeployAmpsCommand() {
1822
setExecuteSortOrder(SortOrderConstants.DEPLOY_AMPS);
1923
setUndoSortOrder(SortOrderConstants.DELETE_AMPS);
@@ -22,6 +26,13 @@ public DeployAmpsCommand() {
2226
setResourceClassType(Amp.class);
2327
}
2428

29+
@Override
30+
public void execute(CommandContext context) {
31+
ampManager = new AmpManager(context.getManageClient());
32+
existingAmpResources = ampManager.getAsXml();
33+
super.execute(context);
34+
}
35+
2536
@Override
2637
protected File[] getResourceDirs(CommandContext context) {
2738
return findResourceDirs(context, configDir -> configDir.getAmpsDir());
@@ -37,9 +48,32 @@ public boolean cmaShouldBeUsed(CommandContext context) {
3748
return context.getAppConfig().getCmaConfig().isDeployAmps();
3849
}
3950

51+
/**
52+
* Because amps are static and CMA doesn't allow for roles to be changed, we can do some optimization here and
53+
* not deploy the amp if it exists - which means its local name, namespace, document URI, and modules database are
54+
* all the same.
55+
*
56+
* @param amp
57+
* @param configuration the CMA Configuration object that the payload should be added to
58+
*/
4059
@Override
41-
public void addResourceToConfiguration(ObjectNode payload, Configuration configuration) {
42-
configuration.addAmp(payload);
60+
public void addResourceToConfiguration(ObjectNode amp, Configuration configuration) {
61+
if (ampIsUnchanged(amp)) {
62+
logger.info("Amp is unchanged, so not deploying: " + amp.get("local-name"));
63+
} else {
64+
configuration.addAmp(amp);
65+
}
66+
}
67+
68+
protected boolean ampIsUnchanged(ObjectNode amp) {
69+
if (existingAmpResources != null) {
70+
String localName = amp.get("local-name").asText();
71+
String namespace = amp.has("namespace") ? amp.get("namespace").asText() : null;
72+
String documentUri = amp.get("document-uri").asText();
73+
String modulesDatabase = amp.has("modules-database") ? amp.get("modules-database").asText() : null;
74+
return ampManager.ampExists(existingAmpResources, localName, documentUri, namespace, modulesDatabase);
75+
}
76+
return false;
4377
}
4478

4579
@Override

src/main/java/com/marklogic/mgmt/resource/security/AmpManager.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.marklogic.mgmt.resource.security;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4-
import com.marklogic.mgmt.resource.AbstractResourceManager;
54
import com.marklogic.mgmt.ManageClient;
65
import com.marklogic.mgmt.SaveReceipt;
6+
import com.marklogic.mgmt.resource.AbstractResourceManager;
77
import com.marklogic.rest.util.Fragment;
88
import com.marklogic.rest.util.ResourcesFragment;
99
import org.springframework.http.ResponseEntity;
@@ -74,17 +74,19 @@ public SaveReceipt save(String payload) {
7474
public boolean ampExists(String payload) {
7575
String resourceId = getResourceId(payload);
7676
AmpParams params = getAmpParams(payload);
77-
ResourcesFragment resources = getAsXml();
77+
return ampExists(getAsXml(), resourceId, params.documentUri, params.namespace, params.modulesDatabase);
78+
}
7879

80+
public boolean ampExists(ResourcesFragment resources, String localName, String documentUri, String namespace, String modulesDatabase) {
7981
String xpath = "/node()/*[local-name(.) = 'list-items']/node()[" +
8082
"(*[local-name(.) = 'nameref'] = '%s' or *[local-name(.) = 'idref'] = '%s')" +
8183
" and *[local-name(.) = 'document-uri'] = '%s'";
82-
xpath = format(xpath, resourceId, resourceId, params.documentUri);
83-
if (params.namespace != null) {
84-
xpath += format(" and *[local-name(.) = 'namespace'] = '%s'", params.namespace);
84+
xpath = format(xpath, localName, localName, documentUri);
85+
if (namespace != null) {
86+
xpath += format(" and *[local-name(.) = 'namespace'] = '%s'", namespace);
8587
}
86-
if (params.modulesDatabase != null) {
87-
xpath += format(" and *[local-name(.) = 'modules-database'] = '%s'", params.modulesDatabase);
88+
if (modulesDatabase != null) {
89+
xpath += format(" and *[local-name(.) = 'modules-database'] = '%s'", modulesDatabase);
8890
} else {
8991
xpath += format(" and *[local-name(.) = 'modules-database'] = 'filesystem'");
9092
}

src/test/java/com/marklogic/appdeployer/command/cma/DeployAmpsWithCmaTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.marklogic.appdeployer.ConfigDir;
55
import com.marklogic.appdeployer.command.CommandContext;
66
import com.marklogic.appdeployer.command.security.DeployAmpsCommand;
7+
import com.marklogic.mgmt.api.configuration.Configuration;
78
import com.marklogic.mgmt.resource.ResourceManager;
89
import com.marklogic.mgmt.resource.security.AmpManager;
910
import org.junit.Test;
@@ -31,7 +32,10 @@ public void test() throws Exception {
3132
assertTrue(mgr.ampExists(amp1));
3233
assertTrue(mgr.ampExists(amp2));
3334

35+
// On the second invocation, nothing should be sent.
36+
initializeAppDeployer(new SecondDeploymentDeployAmpsCommand());
3437
deploySampleApp();
38+
3539
assertTrue(mgr.ampExists(amp1));
3640
assertTrue(mgr.ampExists(amp2));
3741
} finally {
@@ -56,3 +60,14 @@ protected ResourceManager getResourceManager(CommandContext context) {
5660
}
5761

5862
}
63+
64+
class SecondDeploymentDeployAmpsCommand extends TestDeployAmpsCommand {
65+
66+
@Override
67+
protected void deployConfiguration(CommandContext context, Configuration config) {
68+
if (config.getAmps() != null && !config.getAmps().isEmpty()) {
69+
throw new RuntimeException("No amps should have been added during the second deployment as none of them changed");
70+
}
71+
super.deployConfiguration(context, config);
72+
}
73+
}

0 commit comments

Comments
 (0)