mirror of https://github.com/apache/jclouds.git
Issue 695: Implemented edit and delete ssh key
This commit is contained in:
parent
ab01343a42
commit
ccaac6da5f
|
@ -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 extends HttpRequest> 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);
|
||||
}
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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("<CreateSshKey name='{name}'><Default>{defaultKey}</Default></CreateSshKey>")
|
||||
@Produces(MediaType.APPLICATION_XML)
|
||||
public ListenableFuture<SSHKey> 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<SSHKey> 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<Void> deleteSSHKey(@EndpointParam URI uri);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
<SshKey name="{name}">
|
||||
<Default>{isDefault}</Default>
|
||||
<FingerPrint>{fingerPrint}</FingerPrint>
|
||||
</SshKey>
|
|
@ -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 = "<SshKey name=\"newName\">\n" +
|
||||
" <Default>false</Default>\n" +
|
||||
" <FingerPrint>123</FingerPrint>\n" +
|
||||
"</SshKey>";
|
||||
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<RestAnnotationProcessor<SSHKeyAsyncClient>> createTypeLiteral() {
|
||||
return new TypeLiteral<RestAnnotationProcessor<SSHKeyAsyncClient>>() {
|
||||
|
|
|
@ -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()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue