Skip to content

Commit

Permalink
#415 Ensuring that schema loading is disabled during a preview
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Jan 4, 2019
1 parent fa355f9 commit 92c9d07
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions src/main/groovy/com/marklogic/gradle/task/PreviewDeployTask.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.marklogic.gradle.task


import com.marklogic.appdeployer.AppDeployer
import com.marklogic.appdeployer.command.Command
import com.marklogic.appdeployer.command.modules.LoadModulesCommand
import com.marklogic.appdeployer.command.schemas.LoadSchemasCommand
import com.marklogic.appdeployer.command.security.DeployRolesCommand
import com.marklogic.appdeployer.impl.SimpleAppDeployer
import com.marklogic.mgmt.util.ObjectMapperFactory
Expand All @@ -16,32 +19,58 @@ class PreviewDeployTask extends DeployAppTask {

@TaskAction
void deployApp() {
modifyAppConfigBeforePreview()
modifyAppDeployerBeforePreview()

PreviewInterceptor interceptor = configurePreviewInterceptor()

super.deployApp()

println "\nPREVIEW OF DEPLOYMENT:\n"
println ObjectMapperFactory.getObjectMapper().writeValueAsString(interceptor.getResults())
}

void modifyAppConfigBeforePreview() {
// Disable loading of any modules
getAppConfig().setModulePaths(new ArrayList<String>())

// Disable loading of any schemas
// Disable loading of schemas from the default path
// Database-specific schema paths are handled by removing instances of LoadSchemasCommand
getAppConfig().setSchemasPath(null)
}

void modifyAppDeployerBeforePreview() {
AppDeployer deployer = getAppDeployer()

SimpleAppDeployer deployer = getAppDeployer()
if (deployer instanceof SimpleAppDeployer) {
SimpleAppDeployer simpleAppDeployer = (SimpleAppDeployer) deployer

// Loading roles in two phases breaks the preview feature, so it's disabled
DeployRolesCommand deployRolesCommand = deployer.getCommandOfType(DeployRolesCommand.class)
if (deployRolesCommand != null) {
deployRolesCommand.setDeployRolesInTwoPhases(false)
List<Command> newCommands = new ArrayList<>()
for (Command c : simpleAppDeployer.getCommands()) {
if (c instanceof LoadSchemasCommand || c instanceof LoadModulesCommand) {
// Don't include these; no need to load schemas or modules during a preview
}
// Loading roles in two phases breaks the preview feature, so it's disabled
else if (c instanceof DeployRolesCommand) {
DeployRolesCommand deployRolesCommand = (DeployRolesCommand) c
deployRolesCommand.setDeployRolesInTwoPhases(false)
newCommands.add(c)
} else {
newCommands.add(c)
}
}
simpleAppDeployer.setCommands(newCommands)
}
}

PreviewInterceptor configurePreviewInterceptor() {
PreviewInterceptor interceptor = new PreviewInterceptor(getManageClient())
getManageClient().getRestTemplate().getInterceptors().add(interceptor)
getManageClient().getRestTemplate().setErrorHandler(interceptor)
if (getManageClient().getRestTemplate() != getManageClient().getSecurityUserRestTemplate()) {
getManageClient().getSecurityUserRestTemplate().getInterceptors().add(interceptor)
getManageClient().getSecurityUserRestTemplate().setErrorHandler(interceptor)
}

super.deployApp()

println "\nPREVIEW OF DEPLOYMENT:\n"
println ObjectMapperFactory.getObjectMapper().writeValueAsString(interceptor.getResults())
return interceptor
}

}

0 comments on commit 92c9d07

Please sign in to comment.