* 'master' of https://github.com/mikem2005/jclouds:
  Allow AddInternetService to disable monitors
  Implemented getIpAddresses for Terremark ECloud. 
This commit is contained in:
Adrian Cole 2011-02-18 17:01:38 -08:00
commit c83eb2e395
12 changed files with 118 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

@ -23,6 +23,7 @@ import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static org.jclouds.vcloud.VCloudMediaType.NETWORK_XML;
import static org.jclouds.vcloud.terremark.TerremarkECloudMediaType.INTERNETSERVICESLIST_XML;
import static org.jclouds.vcloud.terremark.TerremarkECloudMediaType.INTERNETSERVICE_XML;
import static org.jclouds.vcloud.terremark.TerremarkECloudMediaType.IPADDRESS_LIST_XML;
import static org.jclouds.vcloud.terremark.TerremarkECloudMediaType.KEYSLIST_XML;
import static org.jclouds.vcloud.terremark.TerremarkECloudMediaType.PUBLICIP_XML;
@ -50,6 +51,7 @@ import org.jclouds.vcloud.filters.SetVCloudTokenCookie;
import org.jclouds.vcloud.functions.OrgNameVDCNameResourceEntityNameToEndpoint;
import org.jclouds.vcloud.terremark.binders.BindCreateKeyToXmlPayload;
import org.jclouds.vcloud.terremark.domain.InternetService;
import org.jclouds.vcloud.terremark.domain.IpAddress;
import org.jclouds.vcloud.terremark.domain.KeyPair;
import org.jclouds.vcloud.terremark.domain.Protocol;
import org.jclouds.vcloud.terremark.domain.PublicIpAddress;
@ -61,6 +63,7 @@ import org.jclouds.vcloud.terremark.functions.VDCURIToPublicIPsEndpoint;
import org.jclouds.vcloud.terremark.options.AddInternetServiceOptions;
import org.jclouds.vcloud.terremark.xml.InternetServiceHandler;
import org.jclouds.vcloud.terremark.xml.InternetServicesHandler;
import org.jclouds.vcloud.terremark.xml.IpAddressesHandler;
import org.jclouds.vcloud.terremark.xml.KeyPairByNameHandler;
import org.jclouds.vcloud.terremark.xml.KeyPairHandler;
import org.jclouds.vcloud.terremark.xml.KeyPairsHandler;
@ -239,4 +242,13 @@ public interface TerremarkECloudAsyncClient extends TerremarkVCloudAsyncClient {
@Consumes(APPLICATION_XML)
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
ListenableFuture<? extends TerremarkNetwork> getTerremarkNetwork(@EndpointParam URI network);
/**
* @see TerremarkECloudClient#getIpAddresses
*/
@GET
@Consumes(IPADDRESS_LIST_XML)
@XMLResponseParser(IpAddressesHandler.class)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<? extends Set<IpAddress>> getIpAddresses(@EndpointParam URI network);
}

View File

@ -20,11 +20,13 @@
package org.jclouds.vcloud.terremark;
import java.net.URI;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.jclouds.concurrent.Timeout;
import org.jclouds.vcloud.terremark.domain.IpAddress;
import org.jclouds.vcloud.terremark.domain.PublicIpAddress;
import org.jclouds.vcloud.terremark.domain.TerremarkNetwork;
import org.jclouds.vcloud.terremark.domain.TerremarkOrgNetwork;
@ -54,5 +56,7 @@ public interface TerremarkECloudClient extends TerremarkVCloudClient {
TerremarkOrgNetwork getNetwork(URI network);
TerremarkNetwork getTerremarkNetwork(URI network);
Set<IpAddress> getIpAddresses(URI network);
}

View File

@ -67,4 +67,15 @@ public interface TerremarkECloudMediaType extends TerremarkVCloudMediaType {
* "application/vnd.tmrk.ecloud.keysList+xml"
*/
public final static MediaType KEYSLIST_XML_TYPE = new MediaType("application", "vnd.tmrk.ecloud.keysList+xml");
/**
* "application/vnd.tmrk.ecloud.ipAddressList+xml"
*/
public final static String IPADDRESS_LIST_XML = "application/vnd.tmrk.ecloud.ipAddressList+xml";
/**
* "application/vnd.tmrk.ecloud.ipAddressList+xml"
*/
public final static MediaType IPADDRESSES_LIST_XML_TYPE = new MediaType("application", "vnd.tmrk.ecloud.ipAddressList+xml");
}

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");