Issue 695: Added createInternetService and fixed Consumes type on editInternetService

This commit is contained in:
Jason King 2011-12-21 14:11:22 +00:00
parent 4e5b1e96cd
commit a352dc89c8
7 changed files with 242 additions and 13 deletions

View File

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

View File

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

View File

@ -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();
}
}

View File

@ -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,19 @@ 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<Task> 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<Task> editInternetService(@EndpointParam(parser = URISource.GetURI.class) InternetService internetService);
InternetService createInternetService(@EndpointParam URI uri, @BinderParam(BindCreateInternetServiceToXmlPayload.class)InternetService data);
}

View File

@ -75,4 +75,27 @@ 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);
}

View File

@ -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 =
"<CreateInternetService name='testName'>" +
"<Protocol>HTTP</Protocol>" +
"<Port>2020</Port>" +
"<Enabled>true</Enabled>" +
"<Persistence>" +
"<Type>None</Type>" +
"</Persistence>" +
"</CreateInternetService>";
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("'","\""));
}
}

View File

@ -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<InternetServiceAsyncClient> {
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,16 +77,36 @@ 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 = "<InternetService name='testName'><Enabled>true</Enabled><Persistence><Type>None</Type></Persistence></InternetService>".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 = "<CreateInternetService name='testName'><Protocol>HTTP</Protocol><Port>2020</Port><Enabled>true</Enabled><Persistence><Type>None</Type></Persistence></CreateInternetService>".replaceAll("'","\"");
assertRequestLineEquals(httpRequest, requestLine);
assertNonPayloadHeadersEqual(httpRequest,
"Accept: application/vnd.tmrk.cloud.internetService\nx-tmrk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false);
assertPayloadEquals(httpRequest, payload, MediaType.APPLICATION_XML, false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);