diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateInternetServiceToXmlPayload.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateInternetServiceToXmlPayload.java new file mode 100644 index 0000000000..229277a1fe --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateInternetServiceToXmlPayload.java @@ -0,0 +1,37 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.tmrk.enterprisecloud.binders; + +import org.jclouds.rest.binders.BindToStringPayload; + +import javax.inject.Inject; +import javax.inject.Singleton; + +/** + * For use with {@see org.jclouds.tmrk.enterprisecloud.features.InternetServiceClient#createInternetService} + * @author Jason King + */ +@Singleton +public class BindCreateInternetServiceToXmlPayload extends BindInternetServiceToXmlPayload { + + @Inject + BindCreateInternetServiceToXmlPayload(BindToStringPayload stringBinder) { + super(stringBinder, "CreateInternetService"); + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayload.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayload.java index 700cc1de37..3ed7c374ad 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayload.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayload.java @@ -24,9 +24,7 @@ import org.jclouds.rest.Binder; import org.jclouds.rest.binders.BindToStringPayload; import org.jclouds.tmrk.enterprisecloud.domain.NamedResource; import org.jclouds.tmrk.enterprisecloud.domain.layout.LayoutRequest; -import org.jclouds.tmrk.enterprisecloud.domain.network.LinuxCustomization; -import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkAdapterSetting; -import org.jclouds.tmrk.enterprisecloud.domain.network.WindowsCustomization; +import org.jclouds.tmrk.enterprisecloud.domain.network.*; import org.jclouds.tmrk.enterprisecloud.domain.vm.CreateVirtualMachine; import javax.inject.Inject; @@ -135,14 +133,17 @@ public class BindCreateVirtualMachineKeyToXmlPayload implements Binder { in = in.e("LinuxCustomization") .e("NetworkSettings") .e("NetworkAdapterSettings"); + for(NetworkAdapterSetting setting:linuxCustomization.getNetworkSettings().getNetworkAdapterSettings().getNetworkAdapterSettings()) { in = networkAdapterSetting(in,setting); } - //TODO DNS Settings + in = in.up(); + in = dnsSettings(in, linuxCustomization.getNetworkSettings().getDnsSettings()); + String href = linuxCustomization.getSshKey().getHref().toString(); String type = linuxCustomization.getSshKey().getType(); - return in.up().up().e("SshKey").a("href",href).a("type",type).up().up(); + return in.up().e("SshKey").a("href",href).a("type",type).up().up(); } private XMLBuilder networkAdapterSetting(XMLBuilder builder, NetworkAdapterSetting setting) { @@ -155,6 +156,18 @@ public class BindCreateVirtualMachineKeyToXmlPayload implements Binder { return builder; } + private XMLBuilder dnsSettings(XMLBuilder in, DnsSettings dnsSettings) { + if(dnsSettings==null)return in; + final String primary = dnsSettings.getPrimaryDns(); + final String secondary = dnsSettings.getSecondaryDns(); + + in = in.e("DnsSettings").e("PrimaryDns").t(primary).up(); + if(secondary!=null && !secondary.isEmpty()) { + in = in.e("SecondaryDns").t(secondary).up(); + } + return in.up(); + } + private XMLBuilder windowsCustomization(XMLBuilder builder, CreateVirtualMachine vmData) { WindowsCustomization windowsCustomization = vmData.getWindowsCustomization(); if(windowsCustomization==null) return builder; diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindInternetServiceToXmlPayload.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindInternetServiceToXmlPayload.java index 8063324757..eaa3be8131 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindInternetServiceToXmlPayload.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindInternetServiceToXmlPayload.java @@ -23,6 +23,7 @@ import org.jclouds.http.HttpRequest; import org.jclouds.rest.Binder; import org.jclouds.rest.binders.BindToStringPayload; import org.jclouds.tmrk.enterprisecloud.domain.NamedResource; +import org.jclouds.tmrk.enterprisecloud.domain.service.Protocol; import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetService; import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetServicePersistenceType; @@ -36,17 +37,23 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; /** - * For use with {@see VirtualMachineClient#createVirtualMachineFromTemplate} + * For use with {@see org.jclouds.tmrk.enterprisecloud.features.InternetServiceClient#editInternetService} * @author Jason King */ @Singleton public class BindInternetServiceToXmlPayload implements Binder { private final BindToStringPayload stringBinder; - + private final String rootElement; + @Inject BindInternetServiceToXmlPayload(BindToStringPayload stringBinder) { + this(stringBinder,"InternetService"); + } + + protected BindInternetServiceToXmlPayload(BindToStringPayload stringBinder, String rootElement) { this.stringBinder = stringBinder; + this.rootElement = rootElement; } @Override @@ -65,6 +72,8 @@ public class BindInternetServiceToXmlPayload implements Binder { outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); final String name = checkNotNull(data.getName(), "name"); + final Protocol protocol = data.getProtocol(); + final int port = data.getPort(); final String enabled = Boolean.toString(data.isEnabled()); final String description = data.getDescription(); final InternetServicePersistenceType persistence = data.getPersistence(); @@ -72,18 +81,29 @@ public class BindInternetServiceToXmlPayload implements Binder { final NamedResource trustedNetworkGroup = data.getTrustedNetworkGroup(); final NamedResource backupInternetService = data.getBackupInternetService(); + XMLBuilder builder = XMLBuilder.create(rootElement).a("name", name); + + if(protocol!=null) { + builder = builder.e("Protocol").t(protocol.value()).up(); + } + + if(port>0) { + builder = builder.e("Port").t(Integer.toString(port)).up(); + } + + builder = builder.e("Enabled").t(enabled).up(); - XMLBuilder builder = XMLBuilder.create("InternetService").a("name", name) - .e("Enabled").t(enabled).up(); if(description!=null) { builder = builder.e("Description").t(description).up(); } - + //TODO: Public IP builder = persistence(builder,persistence); if(redirectUrl!=null) { builder = builder.e("RedirectUrl").t(redirectUrl); } + + //TODO: Monitor if(trustedNetworkGroup!=null) { final String href = trustedNetworkGroup.getHref().toString(); @@ -99,6 +119,8 @@ public class BindInternetServiceToXmlPayload implements Binder { builder = builder.e("BackupInternetService").a("href",href).a("name",groupName).a("type",type).up(); } + //TODO: NodeServices + return builder.asString(outputProperties); } catch (ParserConfigurationException e) { throw new RuntimeException(e); diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/service/Protocol.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/service/Protocol.java index a6b629a153..8c6e52a0a8 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/service/Protocol.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/service/Protocol.java @@ -6,21 +6,39 @@ import javax.xml.bind.annotation.XmlEnumValue; @XmlEnum public enum Protocol { + /** + * HyperText Transfer Protocol + */ @XmlEnumValue("HTTP") HTTP, + /** + * HyperText Transfer Protocol Secure + */ @XmlEnumValue("HTTPS") HTTPS, + /** + * Transmission Control Protocol + */ @XmlEnumValue("TCP") TCP, + /** + * User Datagram Protocol + */ @XmlEnumValue("UDP") UDP, + /** + * Internet Protocol security + */ @XmlEnumValue("IPSEC") IPSEC, + /** + * File Transfer Protocol + */ @XmlEnumValue("FTP") FTP, @@ -31,4 +49,8 @@ public enum Protocol { public String toString() { return name(); } + + public String value() { + return name(); + } } \ No newline at end of file diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClient.java index be49178d05..a5f0b8a6c3 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClient.java @@ -22,13 +22,14 @@ import com.google.common.util.concurrent.ListenableFuture; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.*; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.tmrk.enterprisecloud.binders.BindCreateInternetServiceToXmlPayload; +import org.jclouds.tmrk.enterprisecloud.binders.BindInternetServiceToXmlPayload; import org.jclouds.tmrk.enterprisecloud.domain.Task; import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetService; import org.jclouds.tmrk.enterprisecloud.functions.URISource; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.PUT; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; import java.net.URI; /** @@ -58,8 +59,29 @@ public interface InternetServiceAsyncClient { * @see org.jclouds.tmrk.enterprisecloud.features.InternetServiceClient#editInternetService */ @PUT + @Produces(MediaType.APPLICATION_XML) + @Consumes("application/vnd.tmrk.cloud.task") + @JAXBResponseParser + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture editInternetService(@EndpointParam(parser = URISource.GetURI.class) @BinderParam(BindInternetServiceToXmlPayload.class) InternetService internetService); + + /** + * @see org.jclouds.tmrk.enterprisecloud.features.InternetServiceClient#editInternetService + */ + @POST + @Produces(MediaType.APPLICATION_XML) @Consumes("application/vnd.tmrk.cloud.internetService") @JAXBResponseParser @ExceptionParser(ReturnNullOnNotFoundOr404.class) - ListenableFuture editInternetService(@EndpointParam(parser = URISource.GetURI.class) InternetService internetService); + ListenableFuture createInternetService(@EndpointParam URI uri, @BinderParam(BindCreateInternetServiceToXmlPayload.class)InternetService data); + + /** + * @see org.jclouds.tmrk.enterprisecloud.features.InternetServiceClient#removeInternetService + */ + @DELETE + @Consumes("application/vnd.tmrk.cloud.task") + @JAXBResponseParser + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture removeInternetService(@EndpointParam URI uri); + } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClient.java index 2daf3a5376..9b04cce4fc 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClient.java @@ -40,7 +40,7 @@ public interface InternetServiceClient { /** - * getInternetService call returns information regarding a specified Internet service defined in an environment. + * The getInternetService call returns information regarding a specified Internet service defined in an environment. * @param uri the uri of the internet service * e.g. /cloudapi/ecloud/internetservices/{internet service id} * @return the internet service @@ -75,4 +75,37 @@ public interface InternetServiceClient { * @return the Task representing the create action */ Task editInternetService(InternetService service); + + /** + * The createInternetService call creates an Internet service on a public IP address in an environment. + * If successful, the call returns information regarding the Internet service that was created. + * Input InternetService object Properties: + * The name is required and may not be that of another Internet service. + * Protocol is required. + * Port is required and must be in the range of 1 to 65535. + * Enabled is required. + * Type refers to the method for persisting a connection session. + * If Timeout is absent with a type of SourceIp then Timeout defaults to 2 minutes. + * Omit Timeout if type is None. + * + * Note: The href of the InternetService data parameter is not used. + * When creating the input object you may use any value or an empty URI + * e.g. java.net.URI.create("") + * + * @param uri The uri of the call based on the public IP identifier + * e.g. /cloudapi/ecloud/internetServices/publicIps/{public IP identifier}/action/createInternetService + * @param data The input internet service data used to create the service + * @return + */ + InternetService createInternetService(URI uri, InternetService data); + + /** + * The removeInternetService call removes an Internet service from an environment. + * If successful, the call returns the task that removed the Internet service. + * Note: The Internet service must have no node services associated to remove. + * @param uri the uri of the call based on the internet service + * e.g. /cloudapi/ecloud/internetServices/{internet service identifier} + * @return + */ + Task removeInternetService(URI uri); } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicates.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicates.java new file mode 100644 index 0000000000..51070c5296 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicates.java @@ -0,0 +1,60 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.tmrk.enterprisecloud.predicates; + +import com.google.common.base.Predicate; +import org.jclouds.tmrk.enterprisecloud.domain.Task; +import org.jclouds.tmrk.enterprisecloud.features.TaskClient; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Predicates relating to a task + * @author Jason King + */ +public class TaskPredicates { + + private TaskClient client; + + public static Predicate completeOrSuccess(final TaskClient client) { + return new Predicate() { + @Override + public boolean apply(Task task) { + checkNotNull(task,"task cannot be null"); + if(client!=null) { + task = client.getTask(task.getURI()); + } + switch(task.getStatus()) { + case QUEUED: + case RUNNING: + return false; + case COMPLETE: + case SUCCESS: + return true; + default: + throw new RuntimeException("Task Failed:"+task.getHref()+", Status:"+task.getStatus()); + } + } + @Override + public String toString() { + return "completeOrSuccess"; + } + }; + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateInternetServiceToXmlPayloadTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateInternetServiceToXmlPayloadTest.java new file mode 100644 index 0000000000..c053fcf113 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateInternetServiceToXmlPayloadTest.java @@ -0,0 +1,74 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.tmrk.enterprisecloud.binders; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import org.jclouds.http.HttpRequest; +import org.jclouds.tmrk.enterprisecloud.domain.service.Protocol; +import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetService; +import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetServicePersistenceType; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.net.URI; + +import static org.testng.Assert.assertEquals; + +/** + * Tests behavior of {@code BindCreateInternetServiceToXmlPayload} + * @author Jason King + */ +@Test(groups = "unit", testName = "BindCreateInternetServiceToXmlPayloadTest") +public class BindCreateInternetServiceToXmlPayloadTest { + Injector injector = Guice.createInjector(new AbstractModule() { + + @Override + protected void configure() { + } + }); + + public void testPayloadMinimalXmlContent() throws IOException { + String expected = + "" + + "HTTP" + + "2020" + + "true" + + "" + + "None" + + "" + + ""; + + HttpRequest request = new HttpRequest("GET", URI.create("http://test")); + BindInternetServiceToXmlPayload binder = injector + .getInstance(BindCreateInternetServiceToXmlPayload.class); + + InternetService.Builder builder = InternetService.builder() + .href(URI.create("")) + .name("testName") + .protocol(Protocol.HTTP) + .port(2020) + .enabled(true) + .persistence(InternetServicePersistenceType.builder().persistenceType(InternetServicePersistenceType.PersistenceType.NONE).build()); + + binder.bindToRequest(request, builder.build()); + assertEquals(request.getPayload().getRawContent(), expected.replaceAll("'","\"")); + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayloadTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayloadTest.java index 7771c60112..b94e71e54d 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayloadTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/binders/BindCreateVirtualMachineKeyToXmlPayloadTest.java @@ -26,10 +26,7 @@ import org.jclouds.http.HttpRequest; import org.jclouds.tmrk.enterprisecloud.domain.NamedResource; import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity; import org.jclouds.tmrk.enterprisecloud.domain.layout.LayoutRequest; -import org.jclouds.tmrk.enterprisecloud.domain.network.LinuxCustomization; -import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkAdapterSetting; -import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkAdapterSettings; -import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkSettings; +import org.jclouds.tmrk.enterprisecloud.domain.network.*; import org.jclouds.tmrk.enterprisecloud.domain.vm.CreateVirtualMachine; import org.testng.annotations.Test; @@ -67,6 +64,10 @@ public class BindCreateVirtualMachineKeyToXmlPayloadTest { "10.146.204.68" + "" + "" + + "" + + "1.2.3.4" + + "5.6.7.8" + + "" + "" + "" + "" + @@ -102,7 +103,9 @@ public class BindCreateVirtualMachineKeyToXmlPayloadTest { NetworkAdapterSettings adapterSettings = NetworkAdapterSettings.builder() .addNetworkAdapterSetting(adapterSetting).build(); - NetworkSettings networkSettings = NetworkSettings.builder().networkAdapterSettings(adapterSettings).build(); + NetworkSettings networkSettings = NetworkSettings.builder().networkAdapterSettings(adapterSettings) + .dnsSettings(DnsSettings.builder().primaryDns("1.2.3.4").secondaryDns("5.6.7.8").build()) + .build(); LinuxCustomization linuxCustomization = LinuxCustomization.builder() .sshKey(sshKey) diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClientTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClientTest.java index 15245a0126..92703ab909 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClientTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceAsyncClientTest.java @@ -23,9 +23,13 @@ import org.jclouds.http.HttpRequest; import org.jclouds.http.functions.ParseXMLWithJAXB; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.jclouds.tmrk.enterprisecloud.domain.service.Protocol; import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetService; +import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetServicePersistenceType; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import javax.ws.rs.core.MediaType; import java.io.IOException; import java.lang.reflect.Method; import java.net.URI; @@ -39,6 +43,21 @@ import java.net.URISyntaxException; @Test(groups = "unit", testName = "LayoutAsyncClientTest") public class InternetServiceAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClientTest { + private URI uri = URI.create("/cloudapi/ecloud/internetservices/797"); + private InternetServicePersistenceType persistenceType; + private InternetService service; + + @BeforeMethod + public void setUp() { + + persistenceType = InternetServicePersistenceType.builder().persistenceType(InternetServicePersistenceType.PersistenceType.NONE).build(); + + service = InternetService.builder().href(uri) + .name("testName") + .enabled(true) + .persistence(persistenceType).build(); + } + public void testGetInternetService() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { Method method = InternetServiceAsyncClient.class.getMethod("getInternetService", URI.class); HttpRequest httpRequest = processor.createRequest(method, URI.create("/cloudapi/ecloud/internetservices/797")); @@ -58,15 +77,54 @@ public class InternetServiceAsyncClientTest extends BaseTerremarkEnterpriseCloud public void testEditInternetService() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { Method method = InternetServiceAsyncClient.class.getMethod("editInternetService", InternetService.class); - URI uri = URI.create("/cloudapi/ecloud/internetservices/797"); - InternetService service = InternetService.builder().href(uri).build(); - HttpRequest httpRequest = processor.createRequest(method, service); String requestLine = "PUT https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/internetservices/797 HTTP/1.1"; + String payload = "trueNone".replaceAll("'","\""); + assertRequestLineEquals(httpRequest, requestLine); + assertNonPayloadHeadersEqual(httpRequest, + "Accept: application/vnd.tmrk.cloud.task\nx-tmrk-version: 2011-07-01\n"); + assertPayloadEquals(httpRequest, payload, MediaType.APPLICATION_XML, false); + + assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + + + public void testCreateInternetService() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + Method method = InternetServiceAsyncClient.class.getMethod("createInternetService", URI.class, InternetService.class); + + URI uri = URI.create("/cloudapi/ecloud/internetServices/publicIps/123/action/createInternetService"); + InternetService createData = service.toBuilder().href(URI.create("")).protocol(Protocol.HTTP).port(2020).build(); + + HttpRequest httpRequest = processor.createRequest(method, uri, createData); + + String requestLine = "POST https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/internetServices/publicIps/123/action/createInternetService HTTP/1.1"; + String payload = "HTTP2020trueNone".replaceAll("'","\""); assertRequestLineEquals(httpRequest, requestLine); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.internetService\nx-tmrk-version: 2011-07-01\n"); + assertPayloadEquals(httpRequest, payload, MediaType.APPLICATION_XML, false); + + assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + + public void testRemoveInternetService() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + Method method = InternetServiceAsyncClient.class.getMethod("removeInternetService", URI.class); + + URI uri = URI.create("/cloudapi/ecloud/internetServices/123"); + + HttpRequest httpRequest = processor.createRequest(method, uri); + + String requestLine = "DELETE https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/internetServices/123 HTTP/1.1"; + assertRequestLineEquals(httpRequest, requestLine); + assertNonPayloadHeadersEqual(httpRequest, + "Accept: application/vnd.tmrk.cloud.task\nx-tmrk-version: 2011-07-01\n"); assertPayloadEquals(httpRequest, null, null, false); assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClientLiveTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClientLiveTest.java index b3f093937a..aa716e84f3 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClientLiveTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/InternetServiceClientLiveTest.java @@ -18,15 +18,18 @@ */ package org.jclouds.tmrk.enterprisecloud.features; +import org.jclouds.predicates.RetryablePredicate; import org.jclouds.tmrk.enterprisecloud.domain.Task; +import org.jclouds.tmrk.enterprisecloud.domain.service.Protocol; import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetService; +import org.jclouds.tmrk.enterprisecloud.domain.service.internet.InternetServicePersistenceType; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; import java.net.URI; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; +import static org.jclouds.tmrk.enterprisecloud.predicates.TaskPredicates.completeOrSuccess; +import static org.testng.Assert.*; /** * Tests behavior of {@code InternetServiceClient} @@ -39,43 +42,48 @@ public class InternetServiceClientLiveTest extends BaseTerremarkEnterpriseCloudC public void setupClient() { super.setupClient(); client = context.getApi().getInternetServiceClient(); + taskClient = context.getApi().getTaskClient(); } private InternetServiceClient client; + private TaskClient taskClient; - public void testGetInternetService() throws Exception { - //TODO: The URI should come from the environment - //TODO: Should create a new service edit it then delete it. - //TODO: Need a retryable predicate to wait until the task is done. - URI uri = URI.create("/cloudapi/ecloud/internetservices/797"); - InternetService internetService = client.getInternetService(uri); - assertNotNull(internetService); - /* - final String originalName = internetService.getName(); - final String newName = originalName+"edited"; - boolean enable = !internetService.isEnabled(); + public void testInternetServiceCalls() { + InternetService service = InternetService.builder() + .name("live test") + .href(URI.create("")) + .protocol(Protocol.TCP) + .port(2020) + .enabled(true) + .persistence(InternetServicePersistenceType.builder().persistenceType(InternetServicePersistenceType.PersistenceType.NONE).build()) + .build(); - // Change the name and enabled flag - testEditInternetService(internetService.getHref(),newName,enable); - internetService = client.getInternetService(uri); - assertEquals(internetService.getName(),newName); - assertEquals(internetService.isEnabled(),enable); + // TODO: Fetch a public ip from the environment + // This has a method not allowed error - needs debugging. + URI uri = URI.create("/cloudapi/ecloud/publicips/3929"); + InternetService internetService = client.createInternetService(uri, service); + System.out.println("service:"+internetService); - // Change it back again - enable = !internetService.isEnabled(); - testEditInternetService(internetService.getHref(),originalName,enable); - assertEquals(internetService.getName(),originalName); - assertEquals(internetService.isEnabled(),enable); - */ + InternetService editServiceData = InternetService.builder().href(uri).name("testName").enabled(false).build(); + + Task editTask = client.editInternetService(editServiceData); + System.out.println("Task:"+editTask); + RetryablePredicate retryablePredicate = new RetryablePredicate(completeOrSuccess(taskClient), 1000*60); + if (!retryablePredicate.apply(editTask)) { + fail("Did not manage to edit service:"+editTask); + } + + InternetService editedService = client.getInternetService(internetService.getHref()); + assertEquals(editedService.getName(),"testName"); + assertFalse(editedService.isEnabled()); + + Task removeTask = client.removeInternetService(internetService.getHref()); + if (!retryablePredicate.apply(removeTask)) { + fail("Did not manage to remove service:"+removeTask); + } } public void testGetMissingInternetService() { assertNull(client.getInternetService(URI.create("/cloudapi/ecloud/internetservices/-1"))); } - - private void testEditInternetService(URI uri, String name, boolean enable) { - InternetService service = InternetService.builder().href(uri).name(name).enabled(enable).build(); - Task task = client.editInternetService(service); - //TODO: Wait for task to complete. - } } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicatesTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicatesTest.java new file mode 100644 index 0000000000..96ce3385e0 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/predicates/TaskPredicatesTest.java @@ -0,0 +1,73 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.tmrk.enterprisecloud.predicates; + +import org.jclouds.tmrk.enterprisecloud.domain.NamedResource; +import org.jclouds.tmrk.enterprisecloud.domain.Task; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.net.URI; +import java.util.Date; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import static org.jclouds.tmrk.enterprisecloud.predicates.TaskPredicates.completeOrSuccess; + +/** + * @author Jason King + */ +@Test(groups = "unit", testName = "TaskPredicatesTest") +public class TaskPredicatesTest { + + private Task task; + + @BeforeMethod + public void setUp() { + task = Task.builder().href(URI.create("")).name("test") + .operation("no-op") + .impactedItem(NamedResource.builder().href(URI.create("")).build()) + .startTime(new Date()) + .initiatedBy(NamedResource.builder().href(URI.create("")).build()) + .status(Task.Status.UNRECOGNIZED) + .build(); + } + + public void testCompleteOrSuccess() { + assertTrue(completeOrSuccess(null).apply(task.toBuilder().status(Task.Status.COMPLETE).build())); + assertTrue(completeOrSuccess(null).apply(task.toBuilder().status(Task.Status.SUCCESS).build())); + assertFalse(completeOrSuccess(null).apply(task.toBuilder().status(Task.Status.RUNNING).build())); + assertFalse(completeOrSuccess(null).apply(task.toBuilder().status(Task.Status.QUEUED).build())); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testCompleteOrSuccessWhenNull() { + TaskPredicates.completeOrSuccess(null).apply(null); + } + + @Test(expectedExceptions = RuntimeException.class) + public void testCompleteOrSuccessWhenFailure() { + TaskPredicates.completeOrSuccess(null).apply(task.toBuilder().status(Task.Status.FAILED).build()); + } + + @Test(expectedExceptions = RuntimeException.class) + public void testCompleteOrSuccessWhenError() { + TaskPredicates.completeOrSuccess(null).apply(task.toBuilder().status(Task.Status.ERROR).build()); + } +}