Skip to content

Commit 5c6157d

Browse files
authored
Merge pull request #758 from marklogic/feature/22812-print-forest-plan
MLE-22812 Refactor: Created ForestPlanner
2 parents c15fd70 + 353ee3f commit 5c6157d

File tree

3 files changed

+81
-27
lines changed

3 files changed

+81
-27
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright © 2025 MarkLogic Corporation. All Rights Reserved.
3+
*/
4+
package com.marklogic.appdeployer.command.forests;
5+
6+
import com.marklogic.appdeployer.AppConfig;
7+
import com.marklogic.appdeployer.command.CommandContext;
8+
import com.marklogic.appdeployer.command.databases.DatabasePlan;
9+
import com.marklogic.appdeployer.command.databases.DeployDatabaseCommand;
10+
import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand;
11+
import com.marklogic.mgmt.ManageClient;
12+
import com.marklogic.mgmt.api.forest.Forest;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
/**
18+
* Intended to be the central interface for "Give me a database name and a set of inputs, and I'll give you back a list
19+
* of forests with replicas that should be created for that database". We'll sort out the naming of this and the
20+
* related classes later.
21+
*/
22+
public class ForestPlanner {
23+
24+
private final ManageClient manageClient;
25+
26+
public ForestPlanner(ManageClient manageClient) {
27+
this.manageClient = manageClient;
28+
}
29+
30+
public List<Forest> previewForestPlan(String database, AppConfig appConfig) {
31+
// We unfortunately still need a CommandContext here, even though this is just for previewing forests. The
32+
// classes that this depends on would need to be modified first to only require an AppConfig and/or ManageClient.
33+
final CommandContext context = new CommandContext(appConfig, this.manageClient, null);
34+
35+
List<DatabasePlan> plans = new DeployOtherDatabasesCommand().buildDatabasePlans(context);
36+
DeployDatabaseCommand dbCommand = null;
37+
for (DatabasePlan plan : plans) {
38+
if (plan.getDatabaseName().equals(database)) {
39+
dbCommand = plan.getDeployDatabaseCommand();
40+
break;
41+
}
42+
}
43+
if (dbCommand == null) {
44+
throw new IllegalArgumentException("Did not find any database plan with a database name of: " + database);
45+
}
46+
47+
DeployForestsCommand deployForestsCommand = dbCommand.buildDeployForestsCommand(database, context);
48+
return deployForestsCommand != null ?
49+
deployForestsCommand.buildForests(context, true) :
50+
new ArrayList<>();
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright © 2025 MarkLogic Corporation. All Rights Reserved.
3+
*/
4+
package com.marklogic.appdeployer.command.forests;
5+
6+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
7+
import com.marklogic.mgmt.api.forest.Forest;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.util.List;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
class ForestPlannerTest extends AbstractAppDeployerTest {
15+
16+
private static final String DB_NAME = "sample-app-content";
17+
18+
@Test
19+
void test() {
20+
List<Forest> forests = new ForestPlanner(manageClient).previewForestPlan(DB_NAME, appConfig);
21+
assertEquals(3, forests.size());
22+
assertEquals("sample-app-content-1", forests.get(0).getForestName());
23+
assertEquals("sample-app-content-2", forests.get(1).getForestName());
24+
assertEquals("sample-app-content-3", forests.get(2).getForestName());
25+
}
26+
}

ml-gradle/src/main/groovy/com/marklogic/gradle/task/forests/PrintForestPlanTask.groovy

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
*/
1616
package com.marklogic.gradle.task.forests
1717

18-
import com.marklogic.appdeployer.command.databases.DatabasePlan
19-
import com.marklogic.appdeployer.command.databases.DeployDatabaseCommand
20-
import com.marklogic.appdeployer.command.databases.DeployOtherDatabasesCommand
21-
import com.marklogic.appdeployer.command.forests.DeployForestsCommand
22-
import com.marklogic.appdeployer.impl.SimpleAppDeployer
18+
19+
import com.marklogic.appdeployer.command.forests.ForestPlanner
2320
import com.marklogic.gradle.task.MarkLogicTask
2421
import com.marklogic.mgmt.api.forest.Forest
25-
import org.gradle.api.GradleException
2622
import org.gradle.api.tasks.TaskAction
2723

2824
/**
@@ -41,27 +37,7 @@ class PrintForestPlanTask extends MarkLogicTask {
4137
}
4238

4339
String database = project.property("database")
44-
45-
SimpleAppDeployer appDeployer = getAppDeployer()
46-
DeployOtherDatabasesCommand command = appDeployer.getCommandOfType(DeployOtherDatabasesCommand.class)
47-
List<DatabasePlan> plans = command.buildDatabasePlans(getCommandContext())
48-
49-
DeployDatabaseCommand deployDatabaseCommand
50-
for (DatabasePlan plan : plans) {
51-
if (database.equals(plan.getDatabaseName())) {
52-
deployDatabaseCommand = plan.getDeployDatabaseCommand()
53-
break
54-
}
55-
}
56-
57-
if (deployDatabaseCommand == null) {
58-
throw new GradleException("Did not find any database plan with a database name of: " + database)
59-
}
60-
61-
DeployForestsCommand deployForestsCommand = deployDatabaseCommand.buildDeployForestsCommand(database, getCommandContext())
62-
List<Forest> forests = deployForestsCommand != null ?
63-
deployForestsCommand.buildForests(getCommandContext(), true) :
64-
new ArrayList<>()
40+
List<Forest> forests = new ForestPlanner(getManageClient()).previewForestPlan(database, getAppConfig())
6541

6642
if (forests.isEmpty()) {
6743
println "\nNo primary forests will be created the next time the database '" + database + "' is deployed. This is " +

0 commit comments

Comments
 (0)