new classes for the node rest client

This commit is contained in:
danikov 2011-11-14 15:50:11 +00:00
parent 64d5d067bd
commit cd6ef318b8
5 changed files with 473 additions and 0 deletions

View File

@ -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 <a href=
* "http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/Modify_Nodes-d1e2503.html"
* />
*/
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 <T extends BaseNode<T>> 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;
}
}

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.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.
* <p/>
*
* @see NodeAsyncClient
* @see <a
* href="http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/Nodes-d1e2173.html"
* />
* @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<Node> createNode(@PathParam("lbid") int lbid,
@WrapWith("node") NodeRequest n);
/**
* @see NodeClient#modifyNode
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Path("/loadbalancers/{lbid}/nodes/{nid}")
ListenableFuture<Void> 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<Set<Node>> 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<Node> getNode(@PathParam("lbid") int lbid,
@PathParam("nid") int nid);
/**
* @see NodeClient#removeNode
*/
@DELETE
@Path("/loadbalancers/{lbid}/nodes/{nid}")
@ExceptionParser(ReturnVoidOnNotFoundOr404.class)
@Consumes("*/*")
ListenableFuture<Void> removeNode(@PathParam("lbid") int lbid,
@PathParam("nid") int nid);
/**
* @see NodeClient#removeNode
*/
@DELETE
@Path("/loadbalancers/{lbid}/nodes")
@ExceptionParser(ReturnVoidOnNotFoundOr404.class)
@Consumes("*/*")
ListenableFuture<Void> removeNode(@PathParam("lbid") int lbid,
@QueryParam("id") Set<Integer> nids);
}

View File

@ -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.
* <p/>
*
* @see NodeAsyncClient
* @see <a
* href="http://docs.rackspace.com/loadbalancers/api/v1.0/clb-devguide/content/Nodes-d1e2173.html"
* />
* @author Dan Lo Bianco
*/
@Timeout(duration = 60, timeUnit = TimeUnit.SECONDS)
public interface NodeClient {
/**
* Create a new node with the configuration defined by the request.
*
* <p/>
* 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.
*
* <p/>
* 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<Node> 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.
* <p/>
* 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.
* <p/>
* 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<Integer> nids);
}

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.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<HttpResponse, Node>, InvocationContext<UnwrapNode> {
private final ParseJson<Map<String, Node>> json;
@Inject
UnwrapNode(ParseJson<Map<String, Node>> json) {
this.json = json;
}
@Override
public Node apply(HttpResponse arg0) {
Map<String, Node> 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;
}
}

View File

@ -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<HttpResponse, Set<Node>>,
InvocationContext<UnwrapNodes> {
private final ParseJson<Map<String, Set<Node>>> json;
private ConvertLB convertLB;
@Inject
UnwrapNodes(ParseJson<Map<String, Set<Node>>> json) {
this.json = json;
}
@Override
public Set<Node> apply(HttpResponse arg0) {
Map<String, Set<Node>> map = json.apply(arg0);
if (map.size() == 0)
return ImmutableSet.<Node> of();
;
return ImmutableSet.copyOf(Iterables.get(map.values(), 0));
}
@Override
public UnwrapNodes setContext(HttpRequest request) {
return this;
}
}