From cd6ef318b8edf3a5e3e76c8983e975b145e785ec Mon Sep 17 00:00:00 2001 From: danikov Date: Mon, 14 Nov 2011 15:50:11 +0000 Subject: [PATCH] new classes for the node rest client --- .../domain/NodeAttributes.java | 93 ++++++++++++ .../features/NodeAsyncClient.java | 125 +++++++++++++++++ .../features/NodeClient.java | 132 ++++++++++++++++++ .../functions/UnwrapNode.java | 59 ++++++++ .../functions/UnwrapNodes.java | 64 +++++++++ 5 files changed, 473 insertions(+) create mode 100644 apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/NodeAttributes.java create mode 100644 apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java create mode 100644 apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeClient.java create mode 100644 apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java create mode 100644 apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/NodeAttributes.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/NodeAttributes.java new file mode 100644 index 0000000000..df8dc50e39 --- /dev/null +++ b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/domain/NodeAttributes.java @@ -0,0 +1,93 @@ +/** + * 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.cloudloadbalancers.domain; + +import org.jclouds.cloudloadbalancers.domain.internal.BaseNode; + +/** + * + * @author Dan Lo Bianco + * @see + */ +public class NodeAttributes { + protected String condition; + protected Integer weight; + + public NodeAttributes condition(String condition) { + this.condition = condition; + return this; + } + + public NodeAttributes weight(int weight) { + this.weight = weight; + return this; + } + + public static > NodeAttributes fromNode(T n) { + return Builder.condition(n.getCondition().name()).weight(n.getWeight()); + } + + public static class Builder { + public static NodeAttributes condition(String condition) { + return new NodeAttributes().condition(condition); + } + + public static NodeAttributes weight(int weight) { + return new NodeAttributes().weight(weight); + } + } + + @Override + public String toString() { + return String.format("[condition=%s, weight=%s]", condition, weight); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((condition == null) ? 0 : condition.hashCode()); + result = prime * result + ((weight == null) ? 0 : weight.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + NodeAttributes other = (NodeAttributes) obj; + if (condition == null) { + if (other.condition != null) + return false; + } else if (!condition.equals(other.condition)) + return false; + if (weight == null) { + if (other.weight != null) + return false; + } else if (!weight.equals(other.weight)) + return false; + return true; + } +} diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java new file mode 100644 index 0000000000..6546b34414 --- /dev/null +++ b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeAsyncClient.java @@ -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.cloudloadbalancers.features; + +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.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import org.jclouds.cloudloadbalancers.domain.Node; +import org.jclouds.cloudloadbalancers.domain.NodeAttributes; +import org.jclouds.cloudloadbalancers.domain.NodeRequest; +import org.jclouds.openstack.filters.AuthenticateRequest; +import org.jclouds.rest.annotations.ExceptionParser; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.ResponseParser; +import org.jclouds.rest.annotations.SkipEncoding; +import org.jclouds.rest.annotations.WrapWith; +import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; + +import com.google.common.util.concurrent.ListenableFuture; + +/** + * Provides asynchronous access to CloudLoadBalancers Node features. + *

+ * + * @see NodeAsyncClient + * @see + * @author Dan Lo Bianco + */ +@SkipEncoding('/') +@RequestFilters(AuthenticateRequest.class) +public interface NodeAsyncClient { + + /** + * @see NodeClient#createNode + */ + @POST + @ResponseParser(UnwrapNode.class) + @Consumes(MediaType.APPLICATION_JSON) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + @Path("/loadbalancers/{lbid}/nodes") + ListenableFuture createNode(@PathParam("lbid") int lbid, + @WrapWith("node") NodeRequest n); + + /** + * @see NodeClient#modifyNode + */ + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @Path("/loadbalancers/{lbid}/nodes/{nid}") + ListenableFuture modifyNode(@PathParam("lbid") int lbid, + @PathParam("nid") int nid, + @WrapWith("node") NodeAttributes attrs); + + /** + * @see NodeClient#listNodes + */ + @GET + @ResponseParser(UnwrapNodes.class) + @Consumes(MediaType.APPLICATION_JSON) + @Path("/loadbalancers/{lbid}/nodes") + @ExceptionParser(ReturnEmptySetOnNotFoundOr404.class) + ListenableFuture> listNodes(@PathParam("lbid") int lbid); + + /** + * @see NodeClient#getNode + */ + @GET + @ResponseParser(UnwrapNode.class) + @Consumes(MediaType.APPLICATION_JSON) + @Path("/loadbalancers/{lbid}/nodes/{nid}") + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getNode(@PathParam("lbid") int lbid, + @PathParam("nid") int nid); + + /** + * @see NodeClient#removeNode + */ + @DELETE + @Path("/loadbalancers/{lbid}/nodes/{nid}") + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + @Consumes("*/*") + ListenableFuture removeNode(@PathParam("lbid") int lbid, + @PathParam("nid") int nid); + + /** + * @see NodeClient#removeNode + */ + @DELETE + @Path("/loadbalancers/{lbid}/nodes") + @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + @Consumes("*/*") + ListenableFuture removeNode(@PathParam("lbid") int lbid, + @QueryParam("id") Set nids); + + +} diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeClient.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeClient.java new file mode 100644 index 0000000000..9996fd3404 --- /dev/null +++ b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/features/NodeClient.java @@ -0,0 +1,132 @@ +/** + * 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.cloudloadbalancers.features; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import org.jclouds.cloudloadbalancers.domain.LoadBalancerAttributes; +import org.jclouds.cloudloadbalancers.domain.Node; +import org.jclouds.cloudloadbalancers.domain.NodeAttributes; +import org.jclouds.cloudloadbalancers.domain.NodeRequest; +import org.jclouds.concurrent.Timeout; +import org.jclouds.http.HttpResponseException; + +/** + * Provides synchronous access to CloudLoadBalancers Node features. + *

+ * + * @see NodeAsyncClient + * @see + * @author Dan Lo Bianco + */ +@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS) +public interface NodeClient { + /** + * Create a new node with the configuration defined by the request. + * + *

+ * When a node is added, it is assigned a unique identifier that can be used for mutating operations + * such as changing the condition or removing it. Every load balancer is dual-homed on both the public + * Internet and ServiceNet; as a result, nodes can either be internal ServiceNet addresses or addresses + * on the public Internet. + * + * + * @param n + * configuration to create + * @return + * @throws HttpResponseException + * If the corresponding request cannot be fulfilled due to insufficient or invalid + * data + * + */ + Node createNode(NodeRequest n); + + /** + * + * Update the properties of a node. + * + *

+ * This operation asynchronously updates the attributes of the specified node. Upon + * successful validation of the request, the service will return a 202 (Accepted) response code. + * A caller can poll the load balancer with its ID to wait for the changes to be applied and the + * load balancer to return to an ACTIVE status. + * + * @param lbid + * loadbalancer from which to get the node + * @param nid + * node to get + * @param attrs + * what to change + * + * @see LoadBalancerAttributes#fromLoadBalancer + */ + void modifyNode(int lbid, int nid, NodeAttributes attrs); + + /** + * + * @return all nodes for a given loadbalancer, or empty set if none available + * + * @param lbid + * id of the loadbalancer to get the nodes for + */ + Set listNodes(int lbid); + + /** + * + * + * @param lbid + * loadbalancer from which to get the node + * @param nid + * node to get + * @return details of the specified node, or null if not found + */ + Node getNode(int lbid, int nid); + + /** + * Remove a node from the account. + *

+ * The remove load balancer function removes the specified load balancer and its associated + * configuration from the account. Any and all configuration data is immediately purged and is + * not recoverable. + * + * @param lbid + * loadbalancer from which to remove the node + * @param nid + * node to remove + */ + void removeNode(int lbid, int nid); + + /** + * Batch-remove nodes from the account. + *

+ * The current default limit is ten ids per request. Any and all configuration data is + * immediately purged and is not recoverable. By chance one of the items in the list + * cannot be removed due to its current status a 400:BadRequest is returned along with the ids + * of the ones the system identified as potential failures for this request + * + * @param lbid + * loadbalancer from which to remove the node + * @param nid + * node to remove + */ + void removeNodes(int lbid, Set nids); +} diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java new file mode 100644 index 0000000000..310fb126a8 --- /dev/null +++ b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNode.java @@ -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.cloudloadbalancers.functions; + +import java.util.Map; + +import javax.inject.Inject; + +import org.jclouds.cloudloadbalancers.domain.Node; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.ParseJson; +import org.jclouds.rest.InvocationContext; + +import com.google.common.base.Function; +import com.google.common.collect.Iterables; + +/** + * @author Dan Lo Bianco + */ +public class UnwrapNode implements Function, InvocationContext { + + private final ParseJson> json; + + @Inject + UnwrapNode(ParseJson> json) { + this.json = json; + } + + @Override + public Node apply(HttpResponse arg0) { + Map map = json.apply(arg0); + if (map == null || map.size() == 0) + return null; + Node n = Iterables.get(map.values(), 0); + return n; + } + + public UnwrapNode setContext(HttpRequest request) { + return this; + } + +} \ No newline at end of file diff --git a/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java new file mode 100644 index 0000000000..62b3a98198 --- /dev/null +++ b/apis/cloudloadbalancers/src/main/java/org/jclouds/cloudloadbalancers/functions/UnwrapNodes.java @@ -0,0 +1,64 @@ +/** + * 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.cloudloadbalancers.functions; + +import java.util.Map; +import java.util.Set; + +import javax.inject.Inject; + +import org.jclouds.cloudloadbalancers.domain.Node; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.ParseJson; +import org.jclouds.rest.InvocationContext; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; + +/** + * @author Dan Lo Bianco + */ +public class UnwrapNodes implements Function>, + InvocationContext { + + private final ParseJson>> json; + private ConvertLB convertLB; + + @Inject + UnwrapNodes(ParseJson>> json) { + this.json = json; + } + + @Override + public Set apply(HttpResponse arg0) { + Map> map = json.apply(arg0); + if (map.size() == 0) + return ImmutableSet. of(); + ; + return ImmutableSet.copyOf(Iterables.get(map.values(), 0)); + } + + @Override + public UnwrapNodes setContext(HttpRequest request) { + return this; + } + +} \ No newline at end of file