mirror of https://github.com/apache/jclouds.git
Issue 695: Converted CreateSSHKey to use a XMLBuilder
This commit is contained in:
parent
7525fc7a15
commit
e71728d82c
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -23,6 +23,7 @@ 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.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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue