Issue 695: Implemented ssh key GET requests. Added Entity to prevent duplication of tasks (similar to Resource).

This commit is contained in:
Jason King 2011-12-13 13:01:06 +00:00
parent 97ef140776
commit 9b0ea8f16f
13 changed files with 1066 additions and 0 deletions

View File

@ -57,6 +57,12 @@ public interface TerremarkEnterpriseCloudAsyncClient {
@Delegate
TaskAsyncClient getTaskClient();
/**
* Provides asynchronous access to SSH Key features.
*/
@Delegate
SSHKeyAsyncClient getSSHKeyClient();
/**
* Provides asynchronous access to VirtualMachine features.
*/

View File

@ -55,6 +55,12 @@ public interface TerremarkEnterpriseCloudClient {
@Delegate
NetworkClient getNetworkClient();
/**
* Provides synchronous access to SSH Key features.
*/
@Delegate
SSHKeyClient getSSHKeyClient();
/**
* Provides synchronous access to Task features.
*/

View File

@ -51,6 +51,7 @@ public class TerremarkEnterpriseCloudRestClientModule extends
.put(NetworkClient.class, NetworkAsyncClient.class)
.put(ResourceClient.class, ResourceAsyncClient.class)
.put(TaskClient.class, TaskAsyncClient.class)
.put(SSHKeyClient.class, SSHKeyAsyncClient.class)
.put(VirtualMachineClient.class, VirtualMachineAsyncClient.class)
.put(TemplateClient.class, TemplateAsyncClient.class)
.build();

View File

@ -0,0 +1,158 @@
/**
* 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.domain.internal;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.tmrk.enterprisecloud.domain.Action;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.Task;
import org.jclouds.tmrk.enterprisecloud.domain.Tasks;
import javax.xml.bind.annotation.XmlElement;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Base Entity class. Extends Resource with Tasks
* <xs:complexType name="EntityType">
* @author Jason King
*
*/
public class Entity<T extends Entity<T>> extends Resource<T> {
public static <T extends Entity<T>> Builder<T> builder() {
return new Builder<T>();
}
public Builder<T> toBuilder() {
return new Builder<T>().fromResource(this);
}
public static class Builder<T extends Entity<T>> extends Resource.Builder<T> {
protected Set<Task> tasks = Sets.newLinkedHashSet();
/**
* @see org.jclouds.tmrk.enterprisecloud.domain.internal.Entity#getTasks
*/
public Builder<T> tasks(Set<Task> tasks) {
this.tasks = ImmutableSet.<Task> copyOf(checkNotNull(tasks, "tasks"));
return this;
}
/**
* @see Resource#getLinks
*/
public Builder<T> links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* @see Resource#getActions
*/
public Builder<T> actions(Set<Action> actions) {
return Builder.class.cast(super.actions(actions));
}
/**
* @see Resource#getActions
*/
public Builder<T> name(String name) {
return Builder.class.cast(super.name(name));
}
public Entity<T> build() {
return new Entity<T>(href, type, name, links, actions, tasks);
}
/**
* {@inheritDoc}
*/
public Builder<T> fromBaseResource(BaseResource<T> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
/**
* {@inheritDoc}
*/
public Builder<T> fromResource(Resource<T> in) {
return Builder.class.cast(super.fromResource(in));
}
public Builder<T> fromEntity(Entity<T> in) {
return fromResource(in).links(in.getLinks()).actions(in.getActions()).name(in.getName())
.tasks(in.getTasks());
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public Builder<T> fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
@XmlElement(name = "Tasks", required = false)
private Tasks tasks = Tasks.builder().build();
protected Entity(URI href, String type, @Nullable String name, Set<Link> links, Set<Action> actions, Set<Task> tasks) {
super(href, type, name, links, actions);
this.tasks = Tasks.builder().tasks(checkNotNull(tasks,"tasks")).build();
}
protected Entity() {
//For JAXB
}
public Set<Task> getTasks() {
return Collections.unmodifiableSet(tasks.getTasks());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Entity entity = (Entity) o;
if (!tasks.equals(entity.tasks)) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + tasks.hashCode();
return result;
}
@Override
public String string() {
return super.string()+", name="+name+", tasks="+tasks;
}
}

View File

@ -0,0 +1,239 @@
/**
* 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.domain.keys;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.tmrk.enterprisecloud.domain.Action;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.Task;
import org.jclouds.tmrk.enterprisecloud.domain.internal.BaseResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.Entity;
import org.jclouds.tmrk.enterprisecloud.domain.internal.Resource;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.net.URI;
import java.util.Map;
import java.util.Set;
/**
* <xs:complexType name="SshKeyType">
* @author Jason King
*/
@XmlRootElement(name = "SshKey")
public class SSHKey extends Entity<SSHKey> {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return new Builder().fromSshKey(this);
}
public static class Builder extends Entity.Builder<SSHKey> {
private boolean defaultKey;
private String fingerPrint;
private String privateKey;
/**
* @see SSHKey#isDefaultKey
*/
public Builder defaultKey(boolean defaultKey) {
this.defaultKey = defaultKey;
return this;
}
/**
* @see SSHKey#getFingerPrint
*/
public Builder fingerPrint(String fingerPrint) {
this.fingerPrint = fingerPrint;
return this;
}
/**
* @see SSHKey#getPrivateKey
*/
public Builder privateKey(String privateKey) {
this.privateKey = privateKey;
return this;
}
@Override
public SSHKey build() {
return new SSHKey(href, type, name, links, actions, tasks, defaultKey, fingerPrint, privateKey);
}
public Builder fromSshKey(SSHKey in) {
return fromEntity(in).defaultKey(in.isDefaultKey()).fingerPrint(in.getFingerPrint()).privateKey(in.getPrivateKey());
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromBaseResource(BaseResource<SSHKey> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(Resource<SSHKey> in) {
return Builder.class.cast(super.fromResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromEntity(Entity<SSHKey> in) {
return Builder.class.cast(super.fromEntity(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* {@inheritDoc}
*/
@Override
public Builder actions(Set<Action> actions) {
return Builder.class.cast(super.actions(actions));
}
/**
* {@inheritDoc}
*/
@Override
public Builder tasks(Set<Task> tasks) {
return Builder.class.cast(super.tasks(tasks));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
@XmlElement(name = "Default", required = false)
private boolean defaultKey;
@XmlElement(name = "FingerPrint", required = false)
private String fingerPrint;
@XmlElement(name = "PrivateKey", required = false)
private String privateKey;
private SSHKey(URI href, String type, String name, Set<Link> links, Set<Action> actions, Set<Task> tasks,
boolean defaultKey, @Nullable String fingerPrint, @Nullable String privateKey) {
super(href, type, name, links, actions, tasks);
this.defaultKey = defaultKey;
this.fingerPrint = fingerPrint;
this.privateKey = privateKey;
}
private SSHKey() {
//For JAXB
}
public boolean isDefaultKey() {
return defaultKey;
}
public String getFingerPrint() {
return fingerPrint;
}
public String getPrivateKey() {
return privateKey;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
SSHKey sshKey = (SSHKey) o;
if (defaultKey != sshKey.defaultKey) return false;
if (fingerPrint != null ? !fingerPrint.equals(sshKey.fingerPrint) : sshKey.fingerPrint != null)
return false;
if (privateKey != null ? !privateKey.equals(sshKey.privateKey) : sshKey.privateKey != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (defaultKey ? 1 : 0);
result = 31 * result + (fingerPrint != null ? fingerPrint.hashCode() : 0);
result = 31 * result + (privateKey != null ? privateKey.hashCode() : 0);
return result;
}
@Override
public String string() {
return super.string()+", defaultKey="+defaultKey+", fingerPrint="+fingerPrint+", privateKey="+privateKey;
}
}

View File

@ -0,0 +1,182 @@
/**
* 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.domain.keys;
import com.google.common.collect.Sets;
import org.jclouds.tmrk.enterprisecloud.domain.Action;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.internal.BaseResource;
import org.jclouds.tmrk.enterprisecloud.domain.internal.Resource;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.net.URI;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* SshKeys is more than a simple wrapper as it extends Resource.
* @author Jason King
*
*/
@XmlRootElement(name = "SshKeys")
public class SSHKeys extends Resource<SSHKeys> {
@SuppressWarnings("unchecked")
public static Builder builder() {
return new Builder();
}
/**
* {@inheritDoc}
*/
@Override
public Builder toBuilder() {
return new Builder().fromTemplates(this);
}
public static class Builder extends Resource.Builder<SSHKeys> {
private Set<SSHKey> sshKeys = Sets.newLinkedHashSet();
/**
* @see SSHKeys#getSSHKeys
*/
public Builder sshKeys(Set<SSHKey> sshKeys) {
this.sshKeys =(checkNotNull(sshKeys,"sshKeys"));
return this;
}
@Override
public SSHKeys build() {
return new SSHKeys(href, type, name, links, actions, sshKeys);
}
public Builder fromTemplates(SSHKeys in) {
return fromResource(in).sshKeys(in.getSSHKeys());
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromBaseResource(BaseResource<SSHKeys> in) {
return Builder.class.cast(super.fromBaseResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromResource(Resource<SSHKeys> in) {
return Builder.class.cast(super.fromResource(in));
}
/**
* {@inheritDoc}
*/
@Override
public Builder type(String type) {
return Builder.class.cast(super.type(type));
}
/**
* {@inheritDoc}
*/
@Override
public Builder href(URI href) {
return Builder.class.cast(super.href(href));
}
/**
* {@inheritDoc}
*/
@Override
public Builder name(String name) {
return Builder.class.cast(super.name(name));
}
/**
* {@inheritDoc}
*/
@Override
public Builder links(Set<Link> links) {
return Builder.class.cast(super.links(links));
}
/**
* {@inheritDoc}
*/
@Override
public Builder actions(Set<Action> actions) {
return Builder.class.cast(super.actions(actions));
}
/**
* {@inheritDoc}
*/
@Override
public Builder fromAttributes(Map<String, String> attributes) {
return Builder.class.cast(super.fromAttributes(attributes));
}
}
@XmlElement(name = "SshKey", required = false)
private Set<SSHKey> sshKeys = Sets.newLinkedHashSet();
private SSHKeys(URI href, String type, String name, Set<Link> links, Set<Action> actions, Set<SSHKey> sshKeys) {
super(href, type, name, links, actions);
this.sshKeys = checkNotNull(sshKeys,"sshKeys");
}
private SSHKeys() {
//For JAXB
}
public Set<SSHKey> getSSHKeys() {
return sshKeys;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
SSHKeys networks1 = (SSHKeys) o;
if (!sshKeys.equals(networks1.sshKeys)) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + sshKeys.hashCode();
return result;
}
@Override
public String string() {
return super.string()+", sshKeys="+sshKeys;
}
}

View File

@ -0,0 +1,63 @@
/**
* 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.features;
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.domain.keys.SSHKey;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKeys;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import java.net.URI;
/**
* Provides asynchronous access to ssh keys via their REST API.
* <p/>
*
* @see SSHKeyClient
* @see <a href=
* "http://support.theenterprisecloud.com/kb/default.asp?id=984&Lang=1&SID="
* />
* @author Jason King
*/
@RequestFilters(BasicAuthentication.class)
@Headers(keys = "x-tmrk-version", values = "{jclouds.api-version}")
public interface SSHKeyAsyncClient {
/**
* @see SSHKeyClient#getSSHKeys
*/
@GET
@Consumes("application/vnd.tmrk.cloud.admin.sshKey; type=collection")
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
public ListenableFuture<SSHKeys> getSSHKeys(@EndpointParam URI uri);
/**
* @see SSHKeyClient#getSSHKey
*/
@GET
@Consumes("application/vnd.tmrk.cloud.admin.sshKey")
@JAXBResponseParser
@ExceptionParser(ReturnNullOnNotFoundOr404.class)
public ListenableFuture<SSHKey> getSSHKey(@EndpointParam URI uri);
}

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.features;
import org.jclouds.concurrent.Timeout;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKeys;
import java.net.URI;
import java.util.concurrent.TimeUnit;
/**
* Provides synchronous access to Ssh Key Functions.
* <p/>
*
* @see SSHKeyAsyncClient
* @see <a href=
* "http://support.theenterprisecloud.com/kb/default.asp?id=984&Lang=1&SID="
* />
* @author Jason King
*/
@Timeout(duration = 180, timeUnit = TimeUnit.SECONDS)
public interface SSHKeyClient {
/**
* The Get SSH Keys call returns information regarding the SSH keys for an organization
*
* @param uri the uri of the call based upon the organization
* e.g. /cloudapi/ecloud/admin/sshkeys/organizations/{id}
* @return the SSHKeys
*/
public SSHKeys getSSHKeys(URI uri);
/**
* The Get SSH Keys by ID call returns information regarding a specified SSH key for an organization.
*
* @param uri the uri of the call based upon the key
* e.g. /cloudapi/ecloud/admin/sshkeys/{id}
* @return the SSHKey
*/
public SSHKey getSSHKey(URI uri);
}

View File

@ -0,0 +1,76 @@
/**
* 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.features;
import com.google.inject.TypeLiteral;
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.testng.annotations.Test;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Tests annotation parsing of {@code SSHKeyAsyncClientTest}
*
* @author Jason King
*/
@Test(groups = "unit", testName = "SSHKeyAsyncClientTest")
public class SSHKeyAsyncClientTest extends BaseTerremarkEnterpriseCloudAsyncClientTest<SSHKeyAsyncClient> {
public void testGetSSHKeys() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException {
Method method = SSHKeyAsyncClient.class.getMethod("getSSHKeys", URI.class);
HttpRequest httpRequest = processor.createRequest(method, new URI("/cloudapi/ecloud/admin/sshkeys/organizations/17"));
assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/admin/sshkeys/organizations/17 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest,
"Accept: application/vnd.tmrk.cloud.admin.sshKey; type=collection\nx-tmrk-version: 2011-07-01\n");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
public void testGetSSHKey() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException {
Method method = SSHKeyAsyncClient.class.getMethod("getSSHKey", URI.class);
HttpRequest httpRequest = processor.createRequest(method, new URI("/cloudapi/ecloud/admin/sshkeys/77"));
assertRequestLineEquals(httpRequest, "GET 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");
assertPayloadEquals(httpRequest, null, null, false);
assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class);
assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class);
checkFilters(httpRequest);
}
@Override
protected TypeLiteral<RestAnnotationProcessor<SSHKeyAsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<SSHKeyAsyncClient>>() {
};
}
}

View File

@ -0,0 +1,58 @@
/**
* 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.features;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKeys;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertNotNull;
/**
* Tests behavior of {@code SSHKeyClient}
*
* @author Jason King
*/
@Test(groups = "live", testName = "SSHKeyClientLiveTest")
public class SSHKeyClientLiveTest extends BaseTerremarkEnterpriseCloudClientLiveTest {
@BeforeGroups(groups = { "live" })
public void setupClient() {
super.setupClient();
client = context.getApi().getSSHKeyClient();
}
private SSHKeyClient client;
public void testGetSSHKeys() throws Exception {
//TODO: Remove the hardcoded uri once have access to organization
SSHKeys sshKeys = client.getSSHKeys(URI.create("/cloudapi/ecloud/admin/sshkeys/organizations/17"));
for(SSHKey key: sshKeys.getSSHKeys()) {
testGetSSHKey(key.getHref());
}
}
private void testGetSSHKey(URI uri) {
SSHKey sshKey = client.getSSHKey(uri);
assertNotNull(sshKey);
assertNotNull(sshKey.getFingerPrint());
}
}

View File

@ -0,0 +1,153 @@
/**
* 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.xml;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Provides;
import org.jclouds.crypto.Crypto;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.http.functions.ParseXMLWithJAXB;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.AuthorizationException;
import org.jclouds.rest.BaseRestClientTest;
import org.jclouds.rest.RestContextSpec;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.tmrk.enterprisecloud.domain.Action;
import org.jclouds.tmrk.enterprisecloud.domain.Link;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKey;
import org.jclouds.tmrk.enterprisecloud.domain.keys.SSHKeys;
import org.jclouds.tmrk.enterprisecloud.features.SSHKeyAsyncClient;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import javax.inject.Named;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Set;
import static org.jclouds.io.Payloads.newInputStreamPayload;
import static org.jclouds.rest.RestContextFactory.contextSpec;
import static org.jclouds.rest.RestContextFactory.createContextBuilder;
import static org.testng.Assert.*;
/**
* Tests behavior of JAXB parsing for SSHKey(s)
*
* @author Jason King
*/
@Test(groups = "unit", testName = "SSHKeysJAXBParsingTest")
public class SSHKeysJAXBParsingTest extends BaseRestClientTest {
@BeforeClass
void setupFactory() {
RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo",
"credentialFoo", String.class, Integer.class,
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new AbstractModule() {
@Override
protected void configure() {}
@SuppressWarnings("unused")
@Provides
@Named("exception")
Set<String> exception() {
throw new AuthorizationException();
}
}));
injector = createContextBuilder(contextSpec).buildInjector();
parserFactory = injector.getInstance(ParseSax.Factory.class);
crypto = injector.getInstance(Crypto.class);
}
@Test
public void testParseSSHKeysWithJAXB() throws Exception {
Method method = SSHKeyAsyncClient.class.getMethod("getSSHKeys",URI.class);
HttpRequest request = factory(SSHKeyAsyncClient.class).createRequest(method, new URI("/1"));
assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class);
Function<HttpResponse, SSHKeys> parser = (Function<HttpResponse, SSHKeys>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request);
InputStream is = getClass().getResourceAsStream("/sshKeys.xml");
SSHKeys sshKeys = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is)));
assertNotNull(sshKeys);
Link link = Iterables.getOnlyElement(sshKeys.getLinks());
assertEquals(link.getName(),"Cloudsoft Corporation [Beta]");
assertEquals(link.getRelationship(), Link.Relationship.UP);
Action action = Iterables.getOnlyElement(sshKeys.getActions());
assertEquals(action.getName(),"createSshKey");
assertEquals(sshKeys.getSSHKeys().size(), 2);
SSHKey key1 = Iterables.get(sshKeys.getSSHKeys(),0);
assertTrue(key1.isDefaultKey());
assertEquals(key1.getFingerPrint(),"c8:3f:41:d6:28:e2:86:37:a6:a3:e6:df:62:d9:31:e5");
SSHKey key2= Iterables.get(sshKeys.getSSHKeys(),1);
assertFalse(key2.isDefaultKey());
assertEquals(key2.getFingerPrint(),"a1:3f:41:d6:28:e2:86:37:a6:a3:e6:df:62:d9:31:e5");
}
@Test
public void testParseSSHKeyWithJAXB() throws Exception {
Method method = SSHKeyAsyncClient.class.getMethod("getSSHKey",URI.class);
HttpRequest request = factory(SSHKeyAsyncClient.class).createRequest(method, new URI("/1"));
assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class);
Function<HttpResponse, SSHKey> parser = (Function<HttpResponse, SSHKey>) RestAnnotationProcessor
.createResponseParser(parserFactory, injector, method, request);
InputStream is = getClass().getResourceAsStream("/sshKey.xml");
SSHKey sshKey = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is)));
assertEquals(sshKey.getHref(),URI.create("/cloudapi/ecloud/admin/sshkeys/77"));
assertEquals(sshKey.getType(),"application/vnd.tmrk.cloud.admin.sshKey");
assertEquals(sshKey.getName(),"test");
assertEquals(sshKey.getLinks().size(),1);
Link link = Iterables.getOnlyElement(sshKey.getLinks());
assertEquals(link.getHref(),URI.create("/cloudapi/ecloud/admin/organizations/17"));
assertEquals(link.getType(),"application/vnd.tmrk.cloud.admin.organization");
assertEquals(link.getName(),"Cloudsoft Corporation [Beta]");
assertEquals(link.getRelationship(), Link.Relationship.UP);
assertEquals(sshKey.getActions().size(), 2);
Action action1 = Iterables.get(sshKey.getActions(), 0);
assertEquals(action1.getHref(),URI.create("/cloudapi/ecloud/admin/sshkeys/77"));
assertEquals(action1.getType(),"application/vnd.tmrk.cloud.admin.sshKey");
assertEquals(action1.getName(),"edit");
Action action2 = Iterables.get(sshKey.getActions(), 1);
assertEquals(action2.getHref(),URI.create("/cloudapi/ecloud/admin/sshkeys/77"));
assertNull(action2.getType());
assertEquals(action2.getName(),"remove");
assertEquals(action2.getActionDisabled(), Action.ActionDisabled.DISABLED);
assertEquals(sshKey.isDefaultKey(),true);
assertEquals(sshKey.getFingerPrint(),"c8:3f:41:d6:28:e2:86:37:a6:a3:e6:df:62:d9:31:e5");
}
}

View File

@ -0,0 +1,17 @@
<SshKey href="/cloudapi/ecloud/admin/sshkeys/77" name="test"
type="application/vnd.tmrk.cloud.admin.sshKey"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Links>
<Link href="/cloudapi/ecloud/admin/organizations/17"
name="Cloudsoft Corporation [Beta]"
type="application/vnd.tmrk.cloud.admin.organization" rel="up"/>
</Links>
<Actions>
<Action href="/cloudapi/ecloud/admin/sshkeys/77" name="edit"
type="application/vnd.tmrk.cloud.admin.sshKey"/>
<Action href="/cloudapi/ecloud/admin/sshkeys/77" name="remove"
actionDisabled="disabled"/>
</Actions>
<Default>true</Default>
<FingerPrint>c8:3f:41:d6:28:e2:86:37:a6:a3:e6:df:62:d9:31:e5</FingerPrint>
</SshKey>

View File

@ -0,0 +1,48 @@
<SshKeys href="/cloudapi/ecloud/admin/sshkeys/organizations/17"
type="application/vnd.tmrk.cloud.admin.sshKey; type=collection"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Links>
<Link href="/cloudapi/ecloud/admin/organizations/17"
name="Cloudsoft Corporation [Beta]"
type="application/vnd.tmrk.cloud.admin.organization" rel="up"/>
</Links>
<Actions>
<Action href="/cloudapi/ecloud/admin/sshkeys/organizations/17/action/createsshkey"
name="createSshKey"
type="application/vnd.tmrk.cloud.admin.sshKey"/>
</Actions>
<SshKey href="/cloudapi/ecloud/admin/sshkeys/77" name="test"
type="application/vnd.tmrk.cloud.admin.sshKey">
<Links>
<Link href="/cloudapi/ecloud/admin/organizations/17"
name="Cloudsoft Corporation [Beta]"
type="application/vnd.tmrk.cloud.admin.organization"
rel="up"/>
</Links>
<Actions>
<Action href="/cloudapi/ecloud/admin/sshkeys/77" name="edit"
type="application/vnd.tmrk.cloud.admin.sshKey"/>
<Action href="/cloudapi/ecloud/admin/sshkeys/77" name="remove"
actionDisabled="disabled"/>
</Actions>
<Default>true</Default>
<FingerPrint>c8:3f:41:d6:28:e2:86:37:a6:a3:e6:df:62:d9:31:e5</FingerPrint>
</SshKey>
<SshKey href="/cloudapi/ecloud/admin/sshkeys/78" name="added_to_xml"
type="application/vnd.tmrk.cloud.admin.sshKey">
<Links>
<Link href="/cloudapi/ecloud/admin/organizations/17"
name="Cloudsoft Corporation [Beta]"
type="application/vnd.tmrk.cloud.admin.organization"
rel="up"/>
</Links>
<Actions>
<Action href="/cloudapi/ecloud/admin/sshkeys/78" name="edit"
type="application/vnd.tmrk.cloud.admin.sshKey"/>
<Action href="/cloudapi/ecloud/admin/sshkeys/78" name="remove"
actionDisabled="disabled"/>
</Actions>
<Default>false</Default>
<FingerPrint>a1:3f:41:d6:28:e2:86:37:a6:a3:e6:df:62:d9:31:e5</FingerPrint>
</SshKey>
</SshKeys>