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

Commit

Permalink
#7 Can now update the REST API servers
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrudin committed Jun 3, 2015
1 parent 9e6a2c4 commit 95b5b24
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/marklogic/appdeployer/ConfigDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public File getRestApiFile() {
return new File(baseDir, restApiPath);
}

public File getRestApiServerFile() {
return new File(new File(baseDir, "servers"), "rest-api-server.json");
}

public void setDatabasesPath(String databasesPath) {
this.databasesPath = databasesPath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.marklogic.appdeployer.plugin.servers;

import java.io.File;

import com.marklogic.appdeployer.AppConfig;
import com.marklogic.appdeployer.AppPluginContext;
import com.marklogic.appdeployer.plugin.AbstractPlugin;
import com.marklogic.rest.mgmt.appservers.ServerManager;

public class UpdateRestApiServersPlugin extends AbstractPlugin {

@Override
public Integer getSortOrderOnDeploy() {
return 600;
}

@Override
public void onDeploy(AppPluginContext context) {
File f = context.getConfigDir().getRestApiServerFile();
if (f.exists()) {
ServerManager mgr = new ServerManager(context.getManageClient());

String payload = copyFileToString(f);
AppConfig appConfig = context.getAppConfig();

String json = tokenReplacer.replaceTokens(payload, appConfig, false);
mgr.updateServer(appConfig.getRestServerName(), appConfig.getGroupName(), json);

if (appConfig.isTestPortSet()) {
json = tokenReplacer.replaceTokens(payload, appConfig, true);
mgr.updateServer(appConfig.getTestRestServerName(), appConfig.getGroupName(), json);
}
} else {
logger.info(format("No REST API server file found at %s, so not updating the server", f.getAbsolutePath()));
}

}

@Override
public void onUndeploy(AppPluginContext context) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.marklogic.rest.mgmt.AbstractManager;
import com.marklogic.rest.mgmt.ManageClient;
import com.marklogic.rest.util.Fragment;

public class ServerManager extends AbstractManager {

Expand All @@ -16,6 +17,11 @@ public boolean serverExists(String name) {
String.format("/s:server-default-list/s:list-items/s:list-item[s:nameref = '%s']", name));
}

public Fragment getServerPropertiesAsXml(String serverIdOrName, String groupIdOrName) {
return manageClient
.getXml(format("/manage/v2/servers/%s/properties?group-id=%s", serverIdOrName, groupIdOrName));
}

public void updateServer(String serverIdOrName, String groupIdOrName, String json) {
String path = format("/manage/v2/servers/%s/properties?group-id=%s", serverIdOrName, groupIdOrName);
// TODO Log the JSON at debug level?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public void closeAppContext() {
/**
* Useful for when your test only needs a REST API and not full the sample app created.
*/
protected void createSampleAppRestApi() {
protected void deployRestApi() {
new RestApiPlugin().onDeploy(new AppPluginContext(appConfig, configDir, manageClient, adminManager));
}

protected void deleteSampleApp() {
protected void undeploySampleApp() {
try {
appDeployer.undeploy(appConfig, configDir);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void createAndDelete() {
assertTrue("A forest for the triggers database should have been created", forestMgr.forestExists(forestName));
assertTrue("The forest should be attached", forestMgr.isForestAttached(forestName));

deleteSampleApp();
undeploySampleApp();

assertFalse("The triggers database should have been deleted", dbMgr.dbExists(dbName));
assertFalse("The triggers forest should have been deleted", forestMgr.forestExists(forestName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public void updateDatabase() {

@After
public void teardown() {
deleteSampleApp();
undeploySampleApp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.marklogic.appdeployer.servers;

import org.junit.After;
import org.junit.Test;

import com.marklogic.appdeployer.AbstractAppDeployerTest;
import com.marklogic.appdeployer.plugin.RestApiPlugin;
import com.marklogic.appdeployer.plugin.servers.UpdateRestApiServersPlugin;
import com.marklogic.rest.mgmt.appservers.ServerManager;
import com.marklogic.rest.util.Fragment;

public class UpdateRestApiServersTest extends AbstractAppDeployerTest {

@After
public void teardown() {
undeploySampleApp();
}

@Test
public void updateMainAndRestRestApiServers() {
// Deploy a REST API server and a test one too
initializeAppDeployer();
appConfig.setTestRestPort(SAMPLE_APP_TEST_REST_PORT);
appDeployer.deploy(appConfig, configDir);

assertAuthentication("The REST API server auth should default to digest", appConfig.getRestServerName(),
"digest");
assertAuthentication("The test REST API server auth should default to digest",
appConfig.getTestRestServerName(), "digest");

// Now redeploy with the update plugin
initializeAppDeployer(new RestApiPlugin(), new UpdateRestApiServersPlugin());
appDeployer.deploy(appConfig, configDir);

assertAuthentication(
"The REST API server auth should now be set to basic because of what's in the rest-api-server.json file",
appConfig.getRestServerName(), "basic");
assertAuthentication(
"The test REST API server auth should now be set to basic because of what's in the rest-api-server.json file",
appConfig.getTestRestServerName(), "basic");
}

private void assertAuthentication(String message, String serverName, String auth) {
Fragment xml = new ServerManager(manageClient).getServerPropertiesAsXml(serverName, appConfig.getGroupName());
assertTrue(message,
xml.elementExists(String.format("/m:http-server-properties/m:authentication[. = '%s']", auth)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public void createAndDelete() {
ServiceManager mgr = new ServiceManager(manageClient);
ServerManager serverMgr = new ServerManager(manageClient);

createSampleAppRestApi();
deployRestApi();
assertTrue("The REST API server should exist", mgr.restApiServerExists(SAMPLE_APP_NAME));
assertTrue("The REST API app server should exist", serverMgr.serverExists(SAMPLE_APP_NAME));

deleteSampleApp();
undeploySampleApp();
assertFalse("The REST API server should have been deleted", mgr.restApiServerExists(SAMPLE_APP_NAME));
assertFalse("The REST API app server have been deleted", serverMgr.serverExists(SAMPLE_APP_NAME));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"authentication": "basic"
}

0 comments on commit 95b5b24

Please sign in to comment.