diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayload.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayload.java new file mode 100644 index 0000000000..48578fd8d9 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/binders/BindSSHKeyToXmlPayload.java @@ -0,0 +1,68 @@ +/** + * 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.common.collect.ImmutableMap; +import org.jclouds.http.HttpRequest; +import org.jclouds.rest.Binder; +import org.jclouds.rest.binders.BindToStringPayload; +import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey; +import org.jclouds.util.Strings2; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * TODO Tests + * @author Jason King + * + */ +@Singleton +public class BindSSHKeyToXmlPayload implements Binder { + + private final String xmlTemplate; + private final BindToStringPayload stringBinder; + + @Inject + BindSSHKeyToXmlPayload(@Named("EditSSHKey") String xmlTemplate, + BindToStringPayload stringBinder) { + this.xmlTemplate = xmlTemplate; + this.stringBinder = stringBinder; + } + + @Override + public R bindToRequest(R request, Object key) { + checkArgument(checkNotNull(key, "key") instanceof SSHKey, "this binder is only valid for SSHKey instances!"); + checkNotNull(request, "request"); + SSHKey sshKey = SSHKey.class.cast(key); + + String name = sshKey.getName(); + String isDefault = Boolean.toString(sshKey.isDefaultKey()); + String fingerPrint = sshKey.getFingerPrint(); + + String payload = Strings2.replaceTokens(xmlTemplate, + ImmutableMap.of("name", name, "isDefault", isDefault, "fingerPrint", fingerPrint)); + + return stringBinder.bindToRequest(request, payload); + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java index bedc975b83..5cff6ef7aa 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/config/TerremarkEnterpriseCloudRestClientModule.java @@ -18,8 +18,10 @@ */ package org.jclouds.tmrk.enterprisecloud.config; +import java.io.IOException; import java.util.Map; +import com.google.inject.Provides; import org.jclouds.http.HttpErrorHandler; import org.jclouds.http.HttpRetryHandler; import org.jclouds.http.RequiresHttp; @@ -35,6 +37,10 @@ import org.jclouds.tmrk.enterprisecloud.features.*; import org.jclouds.tmrk.enterprisecloud.handlers.TerremarkEnterpriseCloudErrorHandler; import com.google.common.collect.ImmutableMap; +import org.jclouds.util.Strings2; + +import javax.inject.Named; +import javax.inject.Singleton; /** * Configures the TerremarkEnterpriseCloud connection. @@ -77,4 +83,10 @@ public class TerremarkEnterpriseCloudRestClientModule extends bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(BackoffLimitedRetryHandler.class); } + @Singleton + @Provides + @Named("EditSSHKey") + String provideEditSSHKey() throws IOException { + return Strings2.toStringAndClose(getClass().getResourceAsStream("/EditSSHKey.xml")); + } } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyAsyncClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyAsyncClient.java index 6fcb5dc17e..9ad83e597f 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyAsyncClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyAsyncClient.java @@ -22,13 +22,12 @@ 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.rest.functions.ReturnVoidOnNotFoundOr404; +import org.jclouds.tmrk.enterprisecloud.binders.BindSSHKeyToXmlPayload; import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey; import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKeys; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Produces; +import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import java.net.URI; @@ -71,7 +70,28 @@ public interface SSHKeyAsyncClient { @Consumes("application/vnd.tmrk.cloud.admin.sshKey") @JAXBResponseParser @ExceptionParser(ReturnNullOnNotFoundOr404.class) + //TODO This would be done better with a template like editSSHKey @Payload("{defaultKey}") @Produces(MediaType.APPLICATION_XML) public ListenableFuture createSSHKey(@EndpointParam URI uri, @PayloadParam("name")String name, @PayloadParam("defaultKey")boolean defaultKey); + + /** + * @see SSHKeyClient#editSSHKey + */ + @PUT + @Consumes("application/vnd.tmrk.cloud.admin.sshKey") + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + @JAXBResponseParser + @Produces(MediaType.APPLICATION_XML) + public ListenableFuture editSSHKey(@EndpointParam URI uri, @BinderParam(BindSSHKeyToXmlPayload.class)SSHKey key); + + /** + * @see SSHKeyClient#createSSHKey + * TODO Should map the 204 header to a boolean to indicate that it was sucessful + */ + @DELETE + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + public ListenableFuture deleteSSHKey(@EndpointParam URI uri); + + } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyClient.java index a75147159a..3ea252cb9c 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/SshKeyClient.java @@ -74,5 +74,26 @@ public interface SSHKeyClient { * @return the ssh key */ public SSHKey createSSHKey(URI uri, String name, boolean defaultKey); - + + + /** + * The editSSHKey call edits the name of the SSH key. + * If successful, the call returns information regarding the SSH key that was modified. + * The name attribute on the sshKey may be changed. + * Note: The name may not be changed to that of another SSH key and may not exceed fifty characters. + * FingerPrint is optional and ignored if sent. + * Note: The default SSH key may not be modified to false. + * Instead, modify the SSH key desired as the default to true and the existing + * default SSH key will be automatically modified to false. + */ + public void editSSHKey(URI uri, SSHKey key); + + /** + * The deleteSSHKey call removes a specified SSH key from an organization. + * //TODO Make this a boolean if sucessful + * @param uri the uri of the ssk key to delete + * e.g. /cloudapi/ecloud/admin/sshkeys/77 + */ + public void deleteSSHKey(URI uri); + } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/resources/editSSHKey.xml b/sandbox-providers/tmrk-enterprisecloud/src/main/resources/editSSHKey.xml new file mode 100644 index 0000000000..bf23606594 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/resources/editSSHKey.xml @@ -0,0 +1,4 @@ + + {isDefault} + {fingerPrint} + \ No newline at end of file diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java index 8bbbe90cd5..25a600b390 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyAsyncClientTest.java @@ -21,8 +21,11 @@ package org.jclouds.tmrk.enterprisecloud.features; import com.google.inject.TypeLiteral; import org.jclouds.http.HttpRequest; import org.jclouds.http.functions.ParseXMLWithJAXB; +import org.jclouds.http.functions.ReleasePayloadAndReturn; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey; import org.testng.annotations.Test; import java.io.IOException; @@ -84,6 +87,44 @@ public class SSHKeyAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClie checkFilters(httpRequest); } + public void testEditSSHKey() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + Method method = SSHKeyAsyncClient.class.getMethod("editSSHKey", URI.class,SSHKey.class); + + SSHKey key = SSHKey.builder().type("application/vnd.tmrk.cloud.admin.sshKey") + .href(URI.create("/cloudapi/ecloud/admin/sshkeys/77")) + .name("newName").defaultKey(false).fingerPrint("123").build(); + + HttpRequest httpRequest = processor.createRequest(method, new URI("/cloudapi/ecloud/admin/sshkeys/77"),key); + + assertRequestLineEquals(httpRequest, "PUT https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/admin/sshkeys/77 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, + "Accept: application/vnd.tmrk.cloud.admin.sshKey\nx-tmrk-version: 2011-07-01\n"); + String xml = "\n" + + " false\n" + + " 123\n" + + ""; + assertPayloadEquals(httpRequest, xml, "application/xml", false); + + assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + + public void testDeleteSSHKey() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + Method method = SSHKeyAsyncClient.class.getMethod("deleteSSHKey", URI.class); + HttpRequest httpRequest = processor.createRequest(method, new URI("/cloudapi/ecloud/admin/sshkeys/77")); + + assertRequestLineEquals(httpRequest, "DELETE https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/admin/sshkeys/77 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest,"x-tmrk-version: 2011-07-01\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class); + assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + @Override protected TypeLiteral> createTypeLiteral() { return new TypeLiteral>() { diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyClientLiveTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyClientLiveTest.java index fc06df6c28..d379519918 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyClientLiveTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/SSHKeyClientLiveTest.java @@ -25,9 +25,7 @@ import org.testng.annotations.Test; import java.net.URI; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.*; /** * Tests behavior of {@code SSHKeyClient} @@ -59,11 +57,26 @@ public class SSHKeyClientLiveTest extends BaseTerremarkEnterpriseCloudClientLive } public void testCreateSSHKey() { - SSHKey sshKey = client.createSSHKey(URI.create("/cloudapi/ecloud/admin/sshkeys/organizations/17/action/createsshkey"),"mylivetestkey",false); + SSHKey sshKey = client.createSSHKey(URI.create("/cloudapi/ecloud/admin/sshkeys/organizations/17/action/createsshkey"),"mynewtestkey1",false); assertNotNull(sshKey); - assertEquals(sshKey.getName(),"mylivetestkey"); + assertEquals(sshKey.getName(),"mynewtestkey1"); assertFalse(sshKey.isDefaultKey()); assertFalse(sshKey.getFingerPrint().isEmpty()); assertFalse(sshKey.getPrivateKey().isEmpty()); + client.deleteSSHKey(sshKey.getHref()); + assertNull(client.getSSHKey(sshKey.getHref())); + } + + public void testEditSSHKey() { + SSHKey sshKey = client.createSSHKey(URI.create("/cloudapi/ecloud/admin/sshkeys/organizations/17/action/createsshkey"),"mykeytoedit",false); + assertNotNull(sshKey); + SSHKey newKey = sshKey.toBuilder().name("editedname").defaultKey(false).build(); + client.editSSHKey(sshKey.getHref(),newKey); + + SSHKey result = client.getSSHKey(sshKey.getHref()); + assertEquals(result.getName(),"editedname"); + + client.deleteSSHKey(sshKey.getHref()); + assertNull(client.getSSHKey(sshKey.getHref())); } } \ No newline at end of file