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

Commit 7b12603

Browse files
committed
#277 Can now deploy roles via CMA
1 parent 63ee7f3 commit 7b12603

File tree

7 files changed

+59
-2
lines changed

7 files changed

+59
-2
lines changed

src/main/java/com/marklogic/appdeployer/AppConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class AppConfig {
6969

7070
private boolean deployAmpsWithCma = false;
7171
private boolean deployForestsWithCma = false;
72+
private boolean deployRolesWithCma = false;
7273

7374
// Used to construct DatabaseClient instances based on inputs defined in this class
7475
private ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory();
@@ -1260,4 +1261,12 @@ public boolean isCreateTriggersDatabase() {
12601261
public void setCreateTriggersDatabase(boolean createTriggerDatabase) {
12611262
this.createTriggersDatabase = createTriggerDatabase;
12621263
}
1264+
1265+
public boolean isDeployRolesWithCma() {
1266+
return deployRolesWithCma;
1267+
}
1268+
1269+
public void setDeployRolesWithCma(boolean deployRolesWithCma) {
1270+
this.deployRolesWithCma = deployRolesWithCma;
1271+
}
12631272
}

src/main/java/com/marklogic/appdeployer/DefaultAppConfigFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public void initialize() {
5858
config.setDeployForestsWithCma(Boolean.parseBoolean(prop));
5959
});
6060

61+
propertyConsumerMap.put("mlDeployRolesWithCma", (config, prop) -> {
62+
logger.info("Deploy roles" + cmaMessage + prop);
63+
config.setDeployRolesWithCma(Boolean.parseBoolean(prop));
64+
});
65+
6166
/**
6267
* The application name is used as a prefix for default names for a variety of resources, such as REST API servers
6368
* and databases.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ protected void deployResourcesViaCma(CommandContext context, File resourceDir) {
132132
logger.info("Processing file: " + f.getAbsolutePath());
133133
}
134134
String payload = readResourceFromFile(null, context, f);
135-
((SupportsCmaCommand) this).addResourceToConfiguration(payload, resourceMapper, config);
135+
if (payload != null && payload.trim().length() > 0) {
136+
((SupportsCmaCommand) this).addResourceToConfiguration(payload, resourceMapper, config);
137+
}
136138
}
137139
new Configurations(config).submit(context.getManageClient());
138140
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
import com.marklogic.appdeployer.command.AbstractResourceCommand;
44
import com.marklogic.appdeployer.command.CommandContext;
55
import com.marklogic.appdeployer.command.SortOrderConstants;
6+
import com.marklogic.appdeployer.command.SupportsCmaCommand;
67
import com.marklogic.mgmt.SaveReceipt;
78
import com.marklogic.mgmt.api.API;
9+
import com.marklogic.mgmt.api.configuration.Configuration;
810
import com.marklogic.mgmt.api.security.Role;
911
import com.marklogic.mgmt.mapper.DefaultResourceMapper;
1012
import com.marklogic.mgmt.mapper.ResourceMapper;
1113
import com.marklogic.mgmt.resource.ResourceManager;
1214
import com.marklogic.mgmt.resource.security.RoleManager;
15+
import org.springframework.http.ResponseEntity;
1316

1417
import java.io.File;
1518
import java.util.HashSet;
1619
import java.util.Set;
1720

18-
public class DeployRolesCommand extends AbstractResourceCommand {
21+
public class DeployRolesCommand extends AbstractResourceCommand implements SupportsCmaCommand {
1922

2023
// Used internally
2124
private boolean removeRolesAndPermissionsDuringDeployment = false;
@@ -29,6 +32,16 @@ public DeployRolesCommand() {
2932
setUndoSortOrder(SortOrderConstants.DELETE_ROLES);
3033
}
3134

35+
@Override
36+
public boolean cmaShouldBeUsed(CommandContext context) {
37+
return context.getAppConfig().isDeployRolesWithCma();
38+
}
39+
40+
@Override
41+
public void addResourceToConfiguration(String payload, ResourceMapper resourceMapper, Configuration configuration) {
42+
configuration.addRole(resourceMapper.readResource(payload, Role.class));
43+
}
44+
3245
/**
3346
* The set of roles is processed twice. The first time, the roles are saved without any default permissions or references to other roles.
3447
* This is to avoid issues where the roles refer to each other or to themselves (via default permissions). The second time, the roles are

src/main/java/com/marklogic/mgmt/api/security/Role.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ public boolean hasPermissionsOrRoles() {
5050
public void clearPermissionsAndRoles() {
5151
if (role != null) {
5252
role.clear();
53+
role = null;
5354
}
5455
if (permission != null) {
5556
permission.clear();
57+
permission = null;
5658
}
5759
}
5860

src/test/java/com/marklogic/appdeployer/DefaultAppConfigFactoryTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void allProperties() {
110110

111111
p.setProperty("mlDeployAmpsWithCma", "true");
112112
p.setProperty("mlDeployForestsWithCma", "true");
113+
p.setProperty("mlDeployRolesWithCma", "true");
113114

114115
p.setProperty("mlHost", "prophost");
115116
p.setProperty("mlAppName", "propname");
@@ -210,6 +211,7 @@ public void allProperties() {
210211

211212
assertTrue(config.isDeployAmpsWithCma());
212213
assertTrue(config.isDeployForestsWithCma());
214+
assertTrue(config.isDeployRolesWithCma());
213215

214216
assertEquals("prophost", config.getHost());
215217
assertEquals("propname", config.getName());

src/test/java/com/marklogic/appdeployer/command/security/DeployRolesWithDependenciesTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.marklogic.appdeployer.command.security;
22

33
import com.marklogic.appdeployer.AbstractAppDeployerTest;
4+
import com.marklogic.appdeployer.command.CommandContext;
5+
import com.marklogic.mgmt.resource.ResourceManager;
46
import com.marklogic.mgmt.resource.security.RoleManager;
57
import org.junit.Test;
68

@@ -55,6 +57,20 @@ public void testEvenMoreRoles() {
5557
}
5658
}
5759

60+
@Test
61+
public void testEvenMoreRolesWithCma() {
62+
appConfig.getFirstConfigDir().setBaseDir(new File("src/test/resources/sample-app/even-more-roles-with-dependencies"));
63+
appConfig.setDeployRolesWithCma(true);
64+
65+
initializeAppDeployer(new TestDeployRolesCommand());
66+
try {
67+
deploySampleApp();
68+
} finally {
69+
initializeAppDeployer(new DeployRolesCommand());
70+
undeploySampleApp();
71+
}
72+
}
73+
5874
/**
5975
* This scenario tests that when two roles are next to each other with different dependencies - role1 and role2 -
6076
* that we figure out that role1 has a dependency on a role closest to the end of the current list of files.
@@ -108,3 +124,11 @@ public void twoPhasesDisabled() {
108124
}
109125
}
110126
}
127+
128+
class TestDeployRolesCommand extends DeployRolesCommand {
129+
130+
@Override
131+
protected ResourceManager getResourceManager(CommandContext context) {
132+
throw new RuntimeException("This should not be called when deploying with CMA");
133+
}
134+
}

0 commit comments

Comments
 (0)