![JAR search and dependency download from the Maven repository](/logo.png)
org.dasein.cloud.rackspace.network.CloudLoadBalancers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dasein-cloud-rackspace Show documentation
Show all versions of dasein-cloud-rackspace Show documentation
Native API implementation for the Rackspace API
The newest version!
/**
* Copyright (C) 2009-2012 enStratus Networks Inc
*
* ====================================================================
* Licensed 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.dasein.cloud.rackspace.network;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TreeSet;
import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.dasein.cloud.CloudErrorType;
import org.dasein.cloud.CloudException;
import org.dasein.cloud.InternalException;
import org.dasein.cloud.OperationNotSupportedException;
import org.dasein.cloud.ResourceStatus;
import org.dasein.cloud.compute.VirtualMachine;
import org.dasein.cloud.identity.ServiceAction;
import org.dasein.cloud.network.IPVersion;
import org.dasein.cloud.network.LbAlgorithm;
import org.dasein.cloud.network.LbListener;
import org.dasein.cloud.network.LbProtocol;
import org.dasein.cloud.network.LoadBalancer;
import org.dasein.cloud.network.LoadBalancerAddressType;
import org.dasein.cloud.network.LoadBalancerState;
import org.dasein.cloud.network.LoadBalancerSupport;
import org.dasein.cloud.rackspace.RackspaceCloud;
import org.dasein.cloud.rackspace.RackspaceException;
import org.dasein.cloud.rackspace.RackspaceMethod;
import org.dasein.util.CalendarWrapper;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class CloudLoadBalancers implements LoadBalancerSupport {
private RackspaceCloud provider;
CloudLoadBalancers(RackspaceCloud provider) { this.provider = provider; }
@Override
public void addDataCenters(String toLoadBalancerId, String... dataCenterIdsToAdd) throws CloudException, InternalException {
throw new OperationNotSupportedException("No support for data-center constrained load balancers");
}
@Override
public void addServers(String toLoadBalancerId, String... serverIdsToAdd) throws CloudException, InternalException {
Logger logger = RackspaceCloud.getLogger(CloudLoadBalancers.class, "std");
if( logger.isTraceEnabled() ) {
logger.trace("enter - " + CloudLoadBalancers.class.getName() + ".addServers(" + toLoadBalancerId + "," + serverIdsToAdd + ")");
}
try {
ArrayList> nodes = new ArrayList>();
LoadBalancer lb = getLoadBalancer(toLoadBalancerId);
int port = -1;
if( lb == null ) {
logger.error("addServers(): No such load balancer: " + toLoadBalancerId);
throw new CloudException("No such load balancer: " + toLoadBalancerId);
}
LbListener[] listeners = lb.getListeners();
if( listeners != null ) {
for( LbListener listener : listeners ) {
port = listener.getPrivatePort();
break;
}
if( port == -1 ) {
for( LbListener listener : listeners ) {
port = listener.getPublicPort();
break;
}
}
}
if( port == -1 ) {
if( lb.getPublicPorts() != null && lb.getPublicPorts().length > 0 ) {
port = lb.getPublicPorts()[0];
}
if( port == -1 ) {
logger.error("addServers(): Could not determine a proper private port for mapping");
throw new CloudException("No port understanding exists for this load balancer");
}
}
for( String id : serverIdsToAdd ) {
if( logger.isTraceEnabled() ) {
logger.trace("addServers(): Adding " + id + "...");
}
VirtualMachine vm = provider.getComputeServices().getVirtualMachineSupport().getVirtualMachine(id);
if( vm == null ) {
logger.error("addServers(): Failed to add " + id + " because it does not exist");
throw new CloudException("No such server: " + id);
}
String address = null;
if( vm.getProviderRegionId().equals(provider.getContext().getRegionId()) ) {
for( String addr : vm.getPrivateIpAddresses() ) {
address = addr;
break;
}
}
if( address == null ) {
for( String addr : vm.getPublicIpAddresses() ) {
address = addr;
break;
}
}
if( address == null ) {
logger.error("addServers(): No address exists for mapping the load balancer to this server");
throw new CloudException("The virtual machine " + id + " has no mappable addresses");
}
if( logger.isDebugEnabled() ) {
logger.debug("addServers(): Mapping IP is: " + address);
}
HashMap node = new HashMap();
node.put("address", address);
node.put("condition", "ENABLED");
node.put("port", port);
nodes.add(node);
}
if( !nodes.isEmpty() ) {
HashMap json = new HashMap();
json.put("nodes", nodes);
RackspaceMethod method = new RackspaceMethod(provider);
if( logger.isTraceEnabled() ) {
logger.debug("addServers(): Calling cloud...");
}
try {
method.postLoadBalancers("/loadbalancers", toLoadBalancerId + "/nodes", new JSONObject(json));
}
catch( RackspaceException e ) {
if( e.getHttpCode() == 422 && nodes.size() == 1 ) {
nodes.clear();
for( String id : serverIdsToAdd ) {
if( logger.isTraceEnabled() ) {
logger.trace("addServers(): Adding " + id + "...");
}
VirtualMachine vm = provider.getComputeServices().getVirtualMachineSupport().getVirtualMachine(id);
if( vm == null ) {
logger.error("addServers(): Failed to add " + id + " because it does not exist");
throw new CloudException("No such server:" + id);
}
String address = null;
for( String addr : vm.getPublicIpAddresses() ) {
address = addr;
break;
}
if( address == null ) {
logger.error("addServers(): No public address exists for mapping the load balancer to this server");
throw new CloudException("The virtual machine " + id + " has no publicly mappable addresses");
}
if( logger.isDebugEnabled() ) {
logger.debug("addServers(): Mapping IP is: " + address);
}
HashMap node = new HashMap();
node.put("address", address);
node.put("condition", "ENABLED");
node.put("port", port);
nodes.add(node);
}
json.clear();
json.put("nodes", nodes);
if( logger.isTraceEnabled() ) {
logger.debug("addServers(): Attemptign with public IP...");
}
method.postLoadBalancers("/loadbalancers", toLoadBalancerId + "/nodes", new JSONObject(json));
}
}
if( logger.isTraceEnabled() ) {
logger.debug("addServers(): Done.");
}
}
}
finally {
if( logger.isTraceEnabled() ) {
logger.trace("exit - " + CloudLoadBalancers.class.getName() + ".addServers()");
}
}
}
@Override
public String create(String name, String description, String addressId, String[] dataCenterIds, LbListener[] listeners, String[] serverIds) throws CloudException, InternalException {
Logger logger = RackspaceCloud.getLogger(CloudLoadBalancers.class, "std");
if( logger.isTraceEnabled() ) {
logger.trace("enter - " + CloudLoadBalancers.class.getName() + ".create(" + name + "," + description + "," + addressId + "," + dataCenterIds + "," + listeners + "," + serverIds + ")");
}
try {
if( listeners == null || listeners.length < 1 ) {
logger.error("create(): Call failed to specify any listeners");
throw new CloudException("Rackspace requires exactly one listener");
}
HashMap lb = new HashMap();
lb.put("name", name);
lb.put("port", listeners[0].getPublicPort());
if( listeners[0].getNetworkProtocol().equals(LbProtocol.HTTP) ) {
lb.put("protocol", "HTTP");
}
else if( listeners[0].getNetworkProtocol().equals(LbProtocol.HTTPS) ) {
lb.put("protocol", "HTTPS");
}
else if( listeners[0].getNetworkProtocol().equals(LbProtocol.RAW_TCP) ) {
lb.put("protocol", matchProtocol(listeners[0].getPublicPort()));
}
else {
logger.error("create(): Invalid protocol: " + listeners[0].getNetworkProtocol());
throw new CloudException("Unsupported protocol: " + listeners[0].getNetworkProtocol());
}
if( listeners[0].getAlgorithm().equals(LbAlgorithm.LEAST_CONN) ) {
lb.put("algorithm", "LEAST_CONNECTIONS");
}
else if( listeners[0].getAlgorithm().equals(LbAlgorithm.ROUND_ROBIN) ) {
lb.put("algorithm", "ROUND_ROBIN");
}
else {
logger.error("create(): Invalid algorithm: " + listeners[0].getAlgorithm());
throw new CloudException("Unsupported algorithm: " + listeners[0].getAlgorithm());
}
ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy