Allow AddInternetService to disable monitors

This commit is contained in:
Mike Matczynski 2011-02-18 18:44:58 -05:00
parent e8a4c28fbc
commit 0b57d27829
9 changed files with 91 additions and 10 deletions

View File

@ -63,9 +63,20 @@ public class BindAddInternetServiceToXmlPayload implements MapBinder {
String payload = Strings2.replaceTokens(xmlTemplate,
ImmutableMap.of("name", name, "protocol", protocol, "port", port, "enabled", enabled, "ns", ns));
payload = Strings2.replaceAll(payload, Patterns.TOKEN_TO_PATTERN.get("description"), description == null ? ""
: String.format("\n <Description>%s</Description>", description));
: String.format("\n\t<Description>%s</Description>", description));
payload = Strings2.replaceAll(payload, Patterns.TOKEN_TO_PATTERN.get("monitor"), getMonitorString(postParams));
return stringBinder.bindToRequest(request, payload);
}
private String getMonitorString(Map<String, String> postParams)
{
// Sending no <Monitor> element to Terremark will result in default behavior, which is to create a monitor.
String monitor = postParams.get("monitor");
if (monitor == null || "true".equalsIgnoreCase(monitor)) {
return "";
}
return "\n\t<Monitor><MonitorType>Disabled</MonitorType></Monitor>";
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {

View File

@ -38,12 +38,17 @@ public class AddInternetServiceOptions extends BindAddInternetServiceToXmlPayloa
String description = null;
@VisibleForTesting
String enabled = "true";
@VisibleForTesting
Boolean monitorEnabled = null;
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
Map<String, String> copy = Maps.newHashMap();
copy.putAll(postParams);
copy.put("description", description);
copy.put("enabled", enabled);
if (monitorEnabled != null) {
copy.put("monitor", monitorEnabled.toString());
}
return super.bindToRequest(request, copy);
}
@ -51,12 +56,15 @@ public class AddInternetServiceOptions extends BindAddInternetServiceToXmlPayloa
this.enabled = "false";
return this;
}
public AddInternetServiceOptions monitorDisabled() {
this.monitorEnabled = false;
return this;
}
public AddInternetServiceOptions withDescription(String description) {
this.description = description;
return this;
}
public static class Builder {
/**
@ -67,6 +75,14 @@ public class AddInternetServiceOptions extends BindAddInternetServiceToXmlPayloa
return options.withDescription(description);
}
/**
* @see AddInternetServiceOptions#monitorDisabled()
*/
public static AddInternetServiceOptions monitorDisabled() {
AddInternetServiceOptions options = new AddInternetServiceOptions();
return options.monitorDisabled();
}
/**
* @see AddInternetServiceOptions#disabled()
*/

View File

@ -6,4 +6,5 @@
<Port>{port}</Port>
<Enabled>{enabled}</Enabled>
{description}
{monitor}
</CreateInternetServiceRequest>

View File

@ -33,8 +33,9 @@ import static org.testng.Assert.assertNotNull;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Properties;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@ -46,8 +47,8 @@ import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.RestContextFactory;
import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.SshException;
import org.jclouds.ssh.SshClient.Factory;
import org.jclouds.ssh.SshException;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.jclouds.vcloud.VCloudExpressClientLiveTest;
import org.jclouds.vcloud.VCloudExpressMediaType;
@ -65,9 +66,11 @@ import org.jclouds.vcloud.predicates.TaskSuccess;
import org.jclouds.vcloud.terremark.domain.CustomizationParameters;
import org.jclouds.vcloud.terremark.domain.InternetService;
import org.jclouds.vcloud.terremark.domain.Node;
import org.jclouds.vcloud.terremark.domain.Protocol;
import org.jclouds.vcloud.terremark.domain.PublicIpAddress;
import org.jclouds.vcloud.terremark.domain.TerremarkCatalogItem;
import org.jclouds.vcloud.terremark.domain.TerremarkVDC;
import org.jclouds.vcloud.terremark.options.AddInternetServiceOptions;
import org.jclouds.vcloud.terremark.options.TerremarkInstantiateVAppTemplateOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeGroups;
@ -103,6 +106,17 @@ public abstract class TerremarkClientLiveTest extends VCloudExpressClientLiveTes
assertNotNull(tmClient.getNodes(service.getId()));
}
}
@Test
public void testCreateInternetServiceMonitorDisabled() throws Exception {
VDC vdc = tmClient.findVDCInOrgNamed(null, null);
Set<PublicIpAddress> publicIpAddresses = tmClient.getPublicIpsAssociatedWithVDC(vdc.getHref());
PublicIpAddress publicIp = publicIpAddresses.iterator().next();
System.out.println("PublicIP: " + publicIp.getAddress());
tmClient.addInternetServiceToExistingIp(publicIp.getId(), PREFIX + "-no-monitoring", Protocol.TCP, 1234,
AddInternetServiceOptions.Builder.monitorDisabled());
}
@Test
public void testGetPublicIpsAssociatedWithVDC() throws Exception {

View File

@ -21,6 +21,7 @@ package org.jclouds.vcloud.terremark.binders;
import static org.jclouds.vcloud.terremark.reference.TerremarkConstants.PROPERTY_TERREMARK_EXTENSION_NS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
@ -81,6 +82,41 @@ public class BindAddInternetServiceToXmlPayloadTest {
map.put("description", "name TCP 22");
binder.bindToRequest(request, map);
assertEquals(request.getPayload().getRawContent(), expected);
}
public void testDisableMonitoringEnabled() throws IOException {
HttpRequest request = new HttpRequest("GET", URI.create("http://test"));
BindAddInternetServiceToXmlPayload binder = injector
.getInstance(BindAddInternetServiceToXmlPayload.class);
Map<String, String> map = Maps.newHashMap();
map.put("name", "name");
map.put("protocol", "TCP");
map.put("port", "22");
map.put("enabled", "true");
map.put("monitor", "true");
binder.bindToRequest(request, map);
String rawContent = (String) request.getPayload().getRawContent();
assertTrue(rawContent.indexOf("<MonitorType>Disabled</MonitorType>") == -1);
}
public void testDisableMonitoringDisabled() throws IOException {
HttpRequest request = new HttpRequest("GET", URI.create("http://test"));
BindAddInternetServiceToXmlPayload binder = injector
.getInstance(BindAddInternetServiceToXmlPayload.class);
Map<String, String> map = Maps.newHashMap();
map.put("name", "name");
map.put("protocol", "TCP");
map.put("port", "22");
map.put("enabled", "true");
map.put("monitor", "false");
binder.bindToRequest(request, map);
String rawContent = (String) request.getPayload().getRawContent();
assertTrue(rawContent.indexOf("<MonitorType>Disabled</MonitorType>") != -1);
}
}

View File

@ -5,6 +5,7 @@
<Protocol>TCP</Protocol>
<Port>22</Port>
<Enabled>false</Enabled>
<Description>yahoo</Description>
<Description>yahoo</Description>
<Monitor><MonitorType>Disabled</MonitorType></Monitor>
</CreateInternetServiceRequest>

View File

@ -5,6 +5,7 @@
<Protocol>TCP</Protocol>
<Port>22</Port>
<Enabled>true</Enabled>
<Description>name TCP 22</Description>
<Description>name TCP 22</Description>
</CreateInternetServiceRequest>

View File

@ -6,4 +6,5 @@
<Port>22</Port>
<Enabled>true</Enabled>
</CreateInternetServiceRequest>

View File

@ -299,7 +299,7 @@ public class TerremarkECloudAsyncClientTest extends RestClientTest<TerremarkEClo
Method method = TerremarkECloudAsyncClient.class.getMethod("addInternetServiceToExistingIp", URI.class,
String.class, Protocol.class, int.class, AddInternetServiceOptions[].class);
HttpRequest request = processor.createRequest(method, URI.create("https://vcloud/extensions/publicIp/12"),
"name", Protocol.TCP, 22, disabled().withDescription("yahoo"));
"name", Protocol.TCP, 22, disabled().withDescription("yahoo"), AddInternetServiceOptions.Builder.monitorDisabled());
assertRequestLineEquals(request, "POST https://vcloud/extensions/publicIp/12/internetServices HTTP/1.1");
assertNonPayloadHeadersEqual(request, "Accept: application/vnd.tmrk.ecloud.internetService+xml\n");