FloatingIP and KeyPair additions

This commit is contained in:
Jeremy Daggett 2012-02-28 09:24:29 -08:00
parent 699fa417c8
commit 8296d47e96
9 changed files with 360 additions and 34 deletions

View File

@ -31,7 +31,7 @@ import com.google.gson.annotations.SerializedName;
* @author Jeremy Daggett
* @author chamerling
*/
public class FloatingIP {
public class FloatingIP implements Comparable<FloatingIP> {
public static Builder builder() {
return new Builder();
}
@ -108,6 +108,11 @@ public class FloatingIP {
return this.fixedIp;
}
@Override
public int compareTo(FloatingIP o) {
return this.id.compareTo(o.getId());
}
@Override
public int hashCode() {
final int prime = 31;

View File

@ -0,0 +1,125 @@
/**
* 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.openstack.nova.v1_1.domain;
import static com.google.common.base.Objects.toStringHelper;
import org.jclouds.javax.annotation.Nullable;
import com.google.gson.annotations.SerializedName;
public class KeyPair {
public static Builder builder() {
return new Builder();
}
public Builder toBuilder() {
return builder().fromKeyPair(this);
}
public static class Builder {
private String publicKey;
private String privateKey;
private String userId;
private String name;
private String fingerprint;
public Builder publicKey(String publicKey) {
this.publicKey = publicKey;
return this;
}
public Builder privateKey(String privateKey) {
this.privateKey = privateKey;
return this;
}
public Builder userId(String userId) {
this.userId = userId;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder fingerprint(String fingerprint) {
this.fingerprint = fingerprint;
return this;
}
public KeyPair build() {
return new KeyPair(publicKey, privateKey, userId, name, fingerprint);
}
public Builder fromKeyPair(KeyPair in) {
return publicKey(in.getPublicKey()).privateKey(in.getPrivateKey())
.userId(in.getUserId()).name(in.getName())
.fingerprint(in.getFingerprint());
}
}
@SerializedName("public_key")
String publicKey;
@SerializedName("private_key")
String privateKey;
@SerializedName("user_id")
String userId;
String name;
String fingerprint;
protected KeyPair(String publicKey, String privateKey,
@Nullable String userId, String name, String fingerprint) {
this.publicKey = publicKey;
this.privateKey = privateKey;
this.userId = userId;
this.name = name;
this.fingerprint = fingerprint;
}
public String getPublicKey() {
return this.publicKey;
}
public String getPrivateKey() {
return this.privateKey;
}
public String getUserId() {
return this.privateKey;
}
public String getName() {
return this.name;
}
public String getFingerprint() {
return this.fingerprint;
}
@Override
public String toString() {
return toStringHelper("").add("publicKey", publicKey)
.add("privateKey", privateKey).add("userId", userId)
.add("name", name).add("fingerprint", fingerprint).toString();
}
}

View File

@ -94,4 +94,28 @@ public interface FloatingIPAsyncClient {
@Path("/os-floating-ips/{id}")
ListenableFuture<Void> deallocate(@PathParam("id") String id);
/**
* @see FloatingIPClient#addFloatingIP
*/
@POST
@Path("/servers/{server}/action")
@Consumes
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"addFloatingIp\":%7B\"server\":\"{server}\",\"address\":\"{address}\"%7D%7D")
ListenableFuture<Void> addFloatingIP(
@PayloadParam("server") String serverId,
@PayloadParam("address") String address);
/**
* @see FloatingIPClient#removeFloatingIP
*/
@POST
@Path("/servers/{server}/action")
@Consumes
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"removeFloatingIp\":%7B\"server\":\"{server}\",\"address\":\"{address}\"%7D%7D")
ListenableFuture<Void> removeFloatingIP(
@PayloadParam("server") String serverId,
@PayloadParam("address") String address);
}

View File

@ -63,4 +63,27 @@ public interface FloatingIPClient {
*/
void deallocate(String id);
/**
* Add a Floating IP address to a Server
*
* @param serverId
* the serverId
* @param address
* the IP address to add
*
* NOTE: Possibly move this to ServerClient?
*/
void addFloatingIP(String serverId, String address);
/**
* Remove a Floating IP address from a Server
*
* @param serverId
* the serverId
* @param address
* the IP address to remove
*
* NOTE: Possibly move this to ServerClient?
*/
void removeFloatingIP(String serverId, String address);
}

View File

@ -0,0 +1,88 @@
/**
* 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.openstack.nova.v1_1.features;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jclouds.openstack.filters.AuthenticateRequest;
import org.jclouds.openstack.nova.v1_1.domain.KeyPair;
import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.Payload;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.SelectJson;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404;
import org.jclouds.rest.functions.ReturnFalseOnNotFoundOr404;
import com.google.common.util.concurrent.ListenableFuture;
/**
* Provides asynchronous access to Key Pairs via the REST API.
* <p/>
*
* @see KeyPairClient
* @author Jeremy Daggett
*/
@SkipEncoding({ '/', '=' })
@RequestFilters(AuthenticateRequest.class)
public interface KeyPairAsyncClient {
@GET
@Path("/os-keypairs")
@SelectJson("keypairs")
@Consumes(MediaType.APPLICATION_JSON)
@ExceptionParser(ReturnEmptySetOnNotFoundOr404.class)
ListenableFuture<Set<Map<String, KeyPair>>> listKeyPairs();
@POST
@Path("/os-keypairs")
@SelectJson("keypair")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"keypair\":%7B\"name\":\"{name}\"%7D%7D")
ListenableFuture<KeyPair> createKeyPair(@PayloadParam("name") String name);
@POST
@Path("/os-keypairs")
@SelectJson("keypair")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Payload("%7B\"keypair\":%7B\"name\":\"{name}\",\"public_key\":\"{publicKey}\"%7D%7D")
ListenableFuture<KeyPair> createKeyPairWithPublicKey(@PayloadParam("name") String name,
@PayloadParam("publicKey") String publicKey);
@DELETE
@Path("/os-keypairs/{name}")
@ExceptionParser(ReturnFalseOnNotFoundOr404.class)
@Consumes
ListenableFuture<Boolean> deleteKeyPair(@PathParam("name") String name);
}

View File

@ -0,0 +1,66 @@
/**
* 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.openstack.nova.v1_1.features;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout;
import org.jclouds.openstack.nova.v1_1.domain.KeyPair;
/**
* Provides synchronous access to Security Groups.
* <p/>
*
* @see KeyPairAsyncClient
* @author Jeremy Daggett
*/
@Timeout(duration = 30, timeUnit = TimeUnit.SECONDS)
public interface KeyPairClient {
/**
* List all Key Pairs.
*
* @return all Key Pairs
*/
Set<Map<String,KeyPair>> listKeyPairs();
/**
* Create a Key Pair.
*
* @return a Key Pair
*/
KeyPair createKeyPair(String name);
/**
* Create a Key Pair with a public key.
*
* @return a Key Pair with a public key.
*/
KeyPair createKeyPairWithPublicKey(String name, String publicKey);
/**
* Delete a Key Pairs.
*
* @return
*/
Boolean deleteKeyPair(String name);
}

View File

@ -18,13 +18,11 @@
*/
package org.jclouds.openstack.nova.v1_1.features;
import static java.lang.System.out;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.util.Set;
import org.jclouds.openstack.domain.Resource;
import org.jclouds.openstack.nova.v1_1.domain.FloatingIP;
import org.jclouds.openstack.nova.v1_1.domain.Server;
import org.jclouds.openstack.nova.v1_1.internal.BaseNovaClientLiveTest;
@ -55,37 +53,7 @@ public class FloatingIPClientLiveTest extends BaseNovaClientLiveTest {
assertEquals(newDetails.getFixedIp(), ip.getFixedIp());
assertEquals(newDetails.getInstanceId(), ip.getInstanceId());
//checkServer(newDetails);
}
}
}
/*
out.println("Allocating a new floating ip address");
FloatingIP newIP = ipClient.allocate();
out.println(newIP);
out.println("List of floating ips after allocate");
floatingIPs = ipClient.listFloatingIPs();
for (FloatingIP ip : floatingIPs) {
System.out.println("Floating IP: " + ip);
}
out.println("Get floating ip address 3815");
FloatingIP getIP = ipClient.getFloatingIP("3815");
out.println(getIP);
out.println("Deallocating the floating ip address");
ipClient.deallocate(newIP.getId());
out.println("List of floating ips after deallocate");
floatingIPs = ipClient.listFloatingIPs();
for (FloatingIP ip : floatingIPs) {
System.out.println("Floating IP: " + ip);
}
*/
private void checkServer(Server server) {
assert server.getAddresses().size() > 0 : server;
}
}

View File

@ -0,0 +1,9 @@
{
"keypair": {
"public_key": "ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQDFNyGjgs6c9akgmZ2ou/fJf7Pdrc23hC95/gM/33OrG4GZABACE4DTioa/PGN+7rHv9YUavUCtXrWayhGniKq/wCuI5fo5TO4AmDNv7/sCGHIHFumADSIoLx0vFhGJIetXEWxL9r0lfFC7//6yZM2W3KcGjbMtlPXqBT9K9PzdyQ== nova@nv-aw2az1-api0001\n",
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIICXQIAAAKBgQDFNyGjgs6c9akgmZ2ou/fJf7Pdrc23hC95/gM/33OrG4GZABAC\nE4DTioa/PGN+7rHv9YUavUCtXrWayhGniKq/wCuI5fo5TO4AmDNv7/sCGHIHFumA\nDSIoLx0vFhGJIetXEWxL9r0lfFC7//6yZM2W3KcGjbMtlPXqBT9K9PzdyQIDAQAB\nAoGAW8Ww+KbpQK8smcgCTr/RqcmsSI8VeL2hXjJvDq0L5WbyYuFdkanDvCztUVZn\nsmyfDtwAqZXB4Ct/dN1tY7m8QpdyRaKRW4Q+hghGCAQpsG7rYDdvwdEyvMaW5RA4\ntucQyajMNyQ/tozU3wMx/v8A7RvGcE9tqoG0WK1C3kBu95UCQQDrOd+joYDkvccz\nFIVu5gNPMXEh3fGGzDxk225UlvESquYLzfz4TfmuUjH4Z1BL3wRiwfJsrrjFkm33\njIidDE8PAkEA1qHjxuaIS1yz/rfzErmcOVNlbFHMP4ihjGTTvh1ZctXlNeLwzENQ\nEDaQV3IpUY1KQR6rxcWb5AXgfF9D9PYFpwJBANucAqGAbRgh3lJgPFtXP4u2O0tF\nLPOOxmvbOdybt6KYD4LB5AXmts77SlACFMNhCXUyYaT6UuOSXDyb5gfJsB0CQQC3\nFaGXKU9Z+doQjhlq/6mjvN/nZl80Uvh7Kgb1RVPoAU1kihGeLE0/h0vZTCiyyDNv\nGRqtucMg32J+tUTi0HpBAkAwHiCZMHMeJWHUwIwlRQY/dnR86FWobRl98ViF2rCL\nDHkDVOeIser3Q6zSqU5/m99lX6an5g8pAh/R5LqnOQZC\n-----END RSA PRIVATE KEY-----\n",
"user_id": "65649731189278",
"name": "testkeypair",
"fingerprint": "d2:1f:c9:2b:d8:90:77:5f:15:64:27:e3:9f:77:1d:e4"
}
}

View File

@ -0,0 +1,18 @@
{
"keypairs": [
{
"keypair": {
"public_key": "ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQCy9EC3O7Ff80vPEfAHDQob61PGwcpYc5KE7tEZnZhrB9n0NyHPRm0E0M+ls3fcTa04HDi+R0DzmRwoyhHQJyI658v8kWZZcuvFjKCcsgsSh/dzdX0xTreLIzSOzt5U7RnZYfshP5cmxtF99yrEY3M/swdin0L+fXsTSkR1B42STQ== nova@nv-aw2az1-api0001\n",
"name": "default",
"fingerprint": "ab:0c:f4:f3:54:c0:5d:3f:ed:62:ad:d3:94:7c:79:7c"
}
},
{
"keypair": {
"public_key": "ssh-rsa AAAXB3NzaC1yc2EAAAADAQABAAAAgQDFNyGjgs6c9akgmZ2ou/fJf7Pdrc23hC95/gM/33OrG4GZABACE4DTioa/PGN+7rHv9YUavUCtXrWayhGniKq/wCuI5fo5TO4AmDNv7/sCGHIHFumADSIoLx0vFhGJIetXEWxL9r0lfFC7//6yZM2W3KcGjbMtlPXqBT9K9PzdyQ== nova@nv-aw2az1-api0001\n",
"name": "testkeypair",
"fingerprint": "d2:1f:c9:2b:d8:90:77:5f:15:64:27:e3:9f:77:1d:e4"
}
}
]
}