Merge pull request #244 from jsonking/695-Terremark

Issue 695: fixes to previous pull request
This commit is contained in:
Adrian Cole 2011-12-14 14:06:04 -08:00
commit 596923445c
11 changed files with 191 additions and 54 deletions

View File

@ -60,6 +60,11 @@
<artifactId>jclouds-compute</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.jamesmurty.utils</groupId>
<artifactId>java-xmlbuilder</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.jclouds</groupId>
<artifactId>jclouds-core</artifactId>

View File

@ -0,0 +1,78 @@
/**
* 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.jamesmurty.utils.XMLBuilder;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToStringPayload;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.util.Map;
import java.util.Properties;
import static com.google.common.base.Preconditions.checkNotNull;
/**
*
* @author Jason King
*/
@Singleton
public class BindCreateSSHKeyToXmlPayload implements MapBinder {
private final BindToStringPayload stringBinder;
@Inject
BindCreateSSHKeyToXmlPayload(BindToStringPayload stringBinder) {
this.stringBinder = stringBinder;
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> params) {
checkNotNull(request, "request");
checkNotNull(params, "params");
String name = checkNotNull(params.get("name"), "name");
String isDefault = checkNotNull(params.get("isDefault"), "isDefault");
String payload = createXMLPayload(name,isDefault);
return stringBinder.bindToRequest(request, payload);
}
private String createXMLPayload(String name, String isDefault) {
try {
Properties outputProperties = new Properties();
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
return XMLBuilder.create("CreateSshKey").a("name",name)
.e("Default").t(isDefault)
.asString(outputProperties);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (TransformerException t) {
throw new RuntimeException(t);
}
}
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
throw new IllegalStateException("BindCreateKey needs parameters");
}
}

View File

@ -18,16 +18,17 @@
*/
package org.jclouds.tmrk.enterprisecloud.binders;
import com.google.common.collect.ImmutableMap;
import com.jamesmurty.utils.XMLBuilder;
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 javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.util.Properties;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
@ -39,13 +40,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
@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;
BindSSHKeyToXmlPayload(BindToStringPayload stringBinder) {
this.stringBinder = stringBinder;
}
@ -59,9 +57,22 @@ public class BindSSHKeyToXmlPayload implements Binder {
String isDefault = Boolean.toString(sshKey.isDefaultKey());
String fingerPrint = sshKey.getFingerPrint();
String payload = Strings2.replaceTokens(xmlTemplate,
ImmutableMap.of("name", name, "isDefault", isDefault, "fingerPrint", fingerPrint));
String payload = createXMLPayload(name,isDefault,fingerPrint);
return stringBinder.bindToRequest(request, payload);
}
private String createXMLPayload(String name, String isDefault, String fingerPrint) {
try {
Properties outputProperties = new Properties();
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
return XMLBuilder.create("SshKey").a("name",name)
.e("Default").t(isDefault).up()
.e("FingerPrint").t(fingerPrint)
.asString(outputProperties);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (TransformerException t) {
throw new RuntimeException(t);
}
}
}

View File

@ -82,11 +82,4 @@ public class TerremarkEnterpriseCloudRestClientModule extends
protected void bindRetryHandlers() {
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(BackoffLimitedRetryHandler.class);
}
@Singleton
@Provides
@Named("EditSSHKey")
String provideEditSSHKey() throws IOException {
return Strings2.toStringAndClose(getClass().getResourceAsStream("/EditSSHKey.xml"));
}
}

View File

@ -21,8 +21,9 @@ package org.jclouds.tmrk.enterprisecloud.features;
import com.google.common.util.concurrent.ListenableFuture;
import org.jclouds.http.filters.BasicAuthentication;
import org.jclouds.rest.annotations.*;
import org.jclouds.rest.functions.ReturnFalseOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404;
import org.jclouds.tmrk.enterprisecloud.binders.BindCreateSSHKeyToXmlPayload;
import org.jclouds.tmrk.enterprisecloud.binders.BindSSHKeyToXmlPayload;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKeys;
@ -70,10 +71,9 @@ 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);
@MapBinder(BindCreateSSHKeyToXmlPayload.class)
public ListenableFuture<SSHKey> createSSHKey(@EndpointParam URI uri, @PayloadParam("name")String name, @PayloadParam("isDefault")boolean defaultKey);
/**
* @see SSHKeyClient#editSSHKey
@ -87,11 +87,10 @@ public interface SSHKeyAsyncClient {
/**
* @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);
@ExceptionParser(ReturnFalseOnNotFoundOr404.class)
public ListenableFuture<Boolean> deleteSSHKey(@EndpointParam URI uri);
}

View File

@ -94,6 +94,6 @@ public interface SSHKeyClient {
* @param uri the uri of the ssk key to delete
* e.g. /cloudapi/ecloud/admin/sshkeys/77
*/
public void deleteSSHKey(URI uri);
public boolean deleteSSHKey(URI uri);
}

View File

@ -1,4 +0,0 @@
<SshKey name="{name}">
<Default>{isDefault}</Default>
<FingerPrint>{fingerPrint}</FingerPrint>
</SshKey>

View File

@ -0,0 +1,59 @@
/**
* 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 com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.jclouds.http.HttpRequest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.net.URI;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code BindCreateSSHKeyToXmlPayloadTest}
* @author Jason King
*/
@Test(groups = "unit", testName = "BindCreateSSHKeyToXmlPayloadTest")
public class BindCreateSSHKeyToXmlPayloadTest {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
}
});
public void testPayloadXmlContent() throws IOException {
final String name = "newName";
final boolean isDefault = false;
final String expected = String.format("<CreateSshKey name=\"%s\"><Default>%b</Default></CreateSshKey>",
name,isDefault);
HttpRequest request = new HttpRequest("GET", URI.create("http://test"));
BindCreateSSHKeyToXmlPayload binder = injector
.getInstance(BindCreateSSHKeyToXmlPayload.class);
binder.bindToRequest(request, ImmutableMap.of("name", name, "isDefault", Boolean.toString(isDefault)));
assertEquals(request.getPayload().getRawContent(), expected);
}
}

View File

@ -46,28 +46,23 @@ public class BindSSHKeyToXmlPayloadTest {
@Override
protected void configure() {
}
@SuppressWarnings("unused")
@Singleton
@Provides
@Named("EditSSHKey")
String provideInstantiateVAppTemplateParams() throws IOException {
InputStream is = getClass().getResourceAsStream("/EditSSHKey.xml");
return Strings2.toStringAndClose(is);
}
});
public void testApplyInputStream() throws IOException {
String expected = Strings2.toStringAndClose(getClass().getResourceAsStream(
"/EditSSHKey-test.xml"));
public void testPayloadXmlContent() throws IOException {
final String name = "newName";
final boolean isDefault = false;
final String fingerPrint = "123";
final String expected = String.format("<SshKey name=\"%s\"><Default>%b</Default><FingerPrint>%s</FingerPrint></SshKey>",
name,isDefault,fingerPrint);
HttpRequest request = new HttpRequest("GET", URI.create("http://test"));
BindSSHKeyToXmlPayload binder = injector
.getInstance(BindSSHKeyToXmlPayload.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();
.name(name)
.defaultKey(isDefault).fingerPrint(fingerPrint).build();
binder.bindToRequest(request, key);
assertEquals(request.getPayload().getRawContent(), expected);

View File

@ -21,9 +21,9 @@ 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.http.functions.ReturnTrueIf2xx;
import org.jclouds.rest.functions.ReturnFalseOnNotFoundOr404;
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;
@ -78,7 +78,7 @@ public class SSHKeyAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClie
assertRequestLineEquals(httpRequest, "POST https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/admin/sshkeys/organizations/17/action/createsshkey HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest,
"Accept: application/vnd.tmrk.cloud.admin.sshKey\nx-tmrk-version: 2011-07-01\n");
String xml = "<CreateSshKey name='myKey'><Default>true</Default></CreateSshKey>";
String xml = "<CreateSshKey name=\"myKey\"><Default>true</Default></CreateSshKey>";
assertPayloadEquals(httpRequest, xml, "application/xml", false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
@ -99,10 +99,7 @@ public class SSHKeyAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClie
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>";
String xml = "<SshKey name=\"newName\"><Default>false</Default><FingerPrint>123</FingerPrint></SshKey>";
assertPayloadEquals(httpRequest, xml, "application/xml", false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
@ -119,8 +116,8 @@ public class SSHKeyAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClie
assertNonPayloadHeadersEqual(httpRequest,"x-tmrk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
assertExceptionParserClassEquals(method, ReturnVoidOnNotFoundOr404.class);
assertResponseParserClassEquals(method, httpRequest, ReturnTrueIf2xx.class);
assertExceptionParserClassEquals(method, ReturnFalseOnNotFoundOr404.class);
checkFilters(httpRequest);
}

View File

@ -63,7 +63,7 @@ public class SSHKeyClientLiveTest extends BaseTerremarkEnterpriseCloudClientLive
assertFalse(sshKey.isDefaultKey());
assertFalse(sshKey.getFingerPrint().isEmpty());
assertFalse(sshKey.getPrivateKey().isEmpty());
client.deleteSSHKey(sshKey.getHref());
assertTrue(client.deleteSSHKey(sshKey.getHref()));
assertNull(client.getSSHKey(sshKey.getHref()));
}
@ -76,7 +76,11 @@ public class SSHKeyClientLiveTest extends BaseTerremarkEnterpriseCloudClientLive
SSHKey result = client.getSSHKey(sshKey.getHref());
assertEquals(result.getName(),"editedname");
client.deleteSSHKey(sshKey.getHref());
assertTrue(client.deleteSSHKey(sshKey.getHref()));
assertNull(client.getSSHKey(sshKey.getHref()));
}
public void testDeleteWhenNoKey() {
assertFalse(client.deleteSSHKey(URI.create("/cloudapi/ecloud/admin/sshkeys/-1")));
}
}