diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersApi.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersApi.java index 77c55b23b1..71924ba0a4 100644 --- a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersApi.java +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersApi.java @@ -30,6 +30,7 @@ import org.jclouds.rackspace.cloudloadbalancers.features.ConnectionApi; import org.jclouds.rackspace.cloudloadbalancers.features.HealthMonitorApi; import org.jclouds.rackspace.cloudloadbalancers.features.LoadBalancerApi; import org.jclouds.rackspace.cloudloadbalancers.features.NodeApi; +import org.jclouds.rackspace.cloudloadbalancers.features.SessionPersistenceApi; import org.jclouds.rackspace.cloudloadbalancers.features.VirtualIPApi; import org.jclouds.rest.annotations.Delegate; import org.jclouds.rest.annotations.EndpointParam; @@ -97,4 +98,12 @@ public interface CloudLoadBalancersApi { @Path("/loadbalancers/{lbId}") HealthMonitorApi getHealthMonitorApiForZoneAndLoadBalancer( @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); + + /** + * Provides synchronous access to Session Persistence features. + */ + @Delegate + @Path("/loadbalancers/{lbId}") + SessionPersistenceApi getSessionPersistenceApiForZoneAndLoadBalancer( + @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); } diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersAsyncApi.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersAsyncApi.java index 454dea7dfd..48bd7f9a44 100644 --- a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersAsyncApi.java +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/CloudLoadBalancersAsyncApi.java @@ -31,6 +31,7 @@ import org.jclouds.rackspace.cloudloadbalancers.features.ConnectionAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.features.HealthMonitorAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.features.LoadBalancerAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.features.NodeAsyncApi; +import org.jclouds.rackspace.cloudloadbalancers.features.SessionPersistenceAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.features.VirtualIPAsyncApi; import org.jclouds.rest.annotations.Delegate; import org.jclouds.rest.annotations.EndpointParam; @@ -68,7 +69,7 @@ public interface CloudLoadBalancersAsyncApi { @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); /** - * Provides synchronous access to Access Rule features. + * Provides asynchronous access to Access Rule features. */ @Delegate @Path("/loadbalancers/{lbId}") @@ -76,7 +77,7 @@ public interface CloudLoadBalancersAsyncApi { @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); /** - * Provides synchronous access to Virtual IP features. + * Provides asynchronous access to Virtual IP features. */ @Delegate @Path("/loadbalancers/{lbId}") @@ -84,7 +85,7 @@ public interface CloudLoadBalancersAsyncApi { @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); /** - * Provides synchronous access to Connection features. + * Provides asynchronous access to Connection features. */ @Delegate @Path("/loadbalancers/{lbId}") @@ -92,10 +93,18 @@ public interface CloudLoadBalancersAsyncApi { @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); /** - * Provides synchronous access to Health Monitor features. + * Provides asynchronous access to Health Monitor features. */ @Delegate @Path("/loadbalancers/{lbId}") HealthMonitorAsyncApi getHealthMonitorApiForZoneAndLoadBalancer( @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); + + /** + * Provides asynchronous access to Session Persistence features. + */ + @Delegate + @Path("/loadbalancers/{lbId}") + SessionPersistenceAsyncApi getSessionPersistenceApiForZoneAndLoadBalancer( + @EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone, @PathParam("lbId") int lbId); } diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/config/CloudLoadBalancersRestClientModule.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/config/CloudLoadBalancersRestClientModule.java index a1ed967403..e946c2e926 100644 --- a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/config/CloudLoadBalancersRestClientModule.java +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/config/CloudLoadBalancersRestClientModule.java @@ -38,6 +38,8 @@ import org.jclouds.rackspace.cloudloadbalancers.features.LoadBalancerAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.features.LoadBalancerApi; import org.jclouds.rackspace.cloudloadbalancers.features.NodeAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.features.NodeApi; +import org.jclouds.rackspace.cloudloadbalancers.features.SessionPersistenceApi; +import org.jclouds.rackspace.cloudloadbalancers.features.SessionPersistenceAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.features.VirtualIPApi; import org.jclouds.rackspace.cloudloadbalancers.features.VirtualIPAsyncApi; import org.jclouds.rackspace.cloudloadbalancers.functions.ConvertLB; @@ -64,6 +66,7 @@ public class CloudLoadBalancersRestClientModule extends .put(VirtualIPApi.class, VirtualIPAsyncApi.class) .put(ConnectionApi.class, ConnectionAsyncApi.class) .put(HealthMonitorApi.class, HealthMonitorAsyncApi.class) + .put(SessionPersistenceApi.class, SessionPersistenceAsyncApi.class) .build(); public CloudLoadBalancersRestClientModule() { diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancer.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancer.java index 34340a0945..914a0d3229 100644 --- a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancer.java +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancer.java @@ -54,7 +54,7 @@ public class LoadBalancer extends BaseLoadBalancer { public LoadBalancer(String region, int id, String name, String protocol, @Nullable Integer port, Set nodes, @Nullable Integer timeout, @Nullable Boolean halfClosed, @Nullable Algorithm algorithm, Status status, - Set virtualIPs, @Nullable Map sessionPersistenceType, + Set virtualIPs, @Nullable Map sessionPersistenceType, String clusterName, Date created, Date updated, @Nullable Map connectionLogging, @Nullable ConnectionThrottle connectionThrottle, boolean contentCaching, int nodeCount, @Nullable HealthMonitor healthMonitor, @Nullable SSLTermination sslTermination, @@ -417,7 +417,7 @@ public class LoadBalancer extends BaseLoadBalancer { * {@inheritDoc} */ @Override - public Builder sessionPersistenceType(@Nullable SessionPersistenceType sessionPersistenceType) { + public Builder sessionPersistenceType(@Nullable SessionPersistence sessionPersistenceType) { return Builder.class.cast(super.sessionPersistenceType(sessionPersistenceType)); } diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancerRequest.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancerRequest.java index 5f767d3700..3fb5506ca0 100644 --- a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancerRequest.java +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/LoadBalancerRequest.java @@ -45,7 +45,7 @@ public class LoadBalancerRequest extends BaseLoadBalancer nodes, @Nullable Algorithm algorithm, @Nullable Integer timeout, @Nullable Boolean halfClosed, - @Nullable Map sessionPersistenceType, + @Nullable Map sessionPersistenceType, @Nullable Map connectionLogging, @Nullable ConnectionThrottle connectionThrottle, @Nullable HealthMonitor healthMonitor, @Nullable Set accessRules, @Nullable Map metadata, VirtualIP.Type virtualIPType, Integer virtualIPId) { @@ -56,7 +56,7 @@ public class LoadBalancerRequest extends BaseLoadBalancer nodes, @Nullable Algorithm algorithm, @Nullable Integer timeout, @Nullable Boolean halfClosed, - @Nullable Map sessionPersistenceType, + @Nullable Map sessionPersistenceType, @Nullable Map connectionLogging, @Nullable ConnectionThrottle connectionThrottle, @Nullable HealthMonitor healthMonitor, @Nullable Set accessRules, @Nullable Map metadata, Set> virtualIPsFromOptions) { diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/Metadata.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/Metadata.java index 9061ee8945..f4aad3976c 100644 --- a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/Metadata.java +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/Metadata.java @@ -19,7 +19,6 @@ package org.jclouds.rackspace.cloudloadbalancers.domain; import java.util.Map; -import java.util.Set; import com.google.common.collect.ForwardingMap; import com.google.common.collect.Maps; diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/SessionPersistence.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/SessionPersistence.java new file mode 100644 index 0000000000..287d8bb460 --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/SessionPersistence.java @@ -0,0 +1,33 @@ +package org.jclouds.rackspace.cloudloadbalancers.domain; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Session persistence is a feature of the load balancing service that forces multiple requests from clients to be + * directed to the same node. This is common with many web applications that do not inherently share application + * state between back-end servers. + */ +public enum SessionPersistence { + /** + * A session persistence mechanism that inserts an HTTP cookie and is used to determine the destination back-end + * node. This is supported for HTTP load balancing only. + */ + HTTP_COOKIE, + /** + * A session persistence mechanism that will keep track of the source IP address that is mapped and is able to + * determine the destination back-end node. This is supported for HTTPS pass-through and non-HTTP load balancing + * only. + */ + SOURCE_IP, + + UNRECOGNIZED; + + public static SessionPersistence fromValue(String sessionPersistence) { + try { + return valueOf(checkNotNull(sessionPersistence, "sessionPersistence")); + } + catch (IllegalArgumentException e) { + return UNRECOGNIZED; + } + } +} \ No newline at end of file diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/internal/BaseLoadBalancer.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/internal/BaseLoadBalancer.java index 9681645648..8d642dde94 100644 --- a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/internal/BaseLoadBalancer.java +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/domain/internal/BaseLoadBalancer.java @@ -28,6 +28,7 @@ import org.jclouds.javax.annotation.Nullable; import org.jclouds.rackspace.cloudloadbalancers.domain.ConnectionThrottle; import org.jclouds.rackspace.cloudloadbalancers.domain.HealthMonitor; import org.jclouds.rackspace.cloudloadbalancers.domain.LoadBalancer; +import org.jclouds.rackspace.cloudloadbalancers.domain.SessionPersistence; import org.jclouds.rackspace.cloudloadbalancers.features.LoadBalancerApi; import com.google.common.base.Objects; @@ -56,7 +57,7 @@ public class BaseLoadBalancer, T extends BaseLoadBalancer< protected Algorithm algorithm; protected Integer timeout; protected Boolean halfClosed; - protected Map sessionPersistence; + protected Map sessionPersistence; protected Map connectionLogging; protected ConnectionThrottle connectionThrottle; protected HealthMonitor healthMonitor; @@ -67,7 +68,7 @@ public class BaseLoadBalancer, T extends BaseLoadBalancer< public BaseLoadBalancer(String name, @Nullable String protocol, @Nullable Integer port, Iterable nodes, @Nullable Algorithm algorithm, @Nullable Integer timeout, @Nullable Boolean halfClosed, - @Nullable Map sessionPersistence, + @Nullable Map sessionPersistence, @Nullable Map connectionLogging, @Nullable ConnectionThrottle connectionThrottle, @Nullable HealthMonitor healthMonitor) { this.name = checkNotNull(name, "name"); @@ -140,7 +141,7 @@ public class BaseLoadBalancer, T extends BaseLoadBalancer< * @return sessionPersistenceType, which may be null if sessionPersistenceType has not been set. */ @Nullable - public SessionPersistenceType getSessionPersistenceType() { + public SessionPersistence getSessionPersistenceType() { return sessionPersistence == null ? null : sessionPersistence.get(PERSISTENCE_TYPE); } @@ -235,36 +236,6 @@ public class BaseLoadBalancer, T extends BaseLoadBalancer< } } - /** - * Session persistence is a feature of the load balancing service that forces multiple requests from clients to be - * directed to the same node. This is common with many web applications that do not inherently share application - * state between back-end servers. - */ - public static enum SessionPersistenceType { - /** - * A session persistence mechanism that inserts an HTTP cookie and is used to determine the destination back-end - * node. This is supported for HTTP load balancing only. - */ - HTTP_COOKIE, - /** - * A session persistence mechanism that will keep track of the source IP address that is mapped and is able to - * determine the destination back-end node. This is supported for HTTPS pass-through and non-HTTP load balancing - * only. - */ - SOURCE_IP, - - UNRECOGNIZED; - - public static SessionPersistenceType fromValue(String sessionPersistenceType) { - try { - return valueOf(checkNotNull(sessionPersistenceType, "sessionPersistenceType")); - } - catch (IllegalArgumentException e) { - return UNRECOGNIZED; - } - } - } - public static class Builder, T extends BaseLoadBalancer> { protected String name; protected String protocol; @@ -273,7 +244,7 @@ public class BaseLoadBalancer, T extends BaseLoadBalancer< protected Algorithm algorithm; protected Integer timeout; protected Boolean halfClosed; - protected Map sessionPersistence; + protected Map sessionPersistence; protected Map connectionLogging; protected ConnectionThrottle connectionThrottle; protected HealthMonitor healthMonitor; @@ -352,9 +323,9 @@ public class BaseLoadBalancer, T extends BaseLoadBalancer< /** * Specifies whether multiple requests from clients are directed to the same node. * - * @see SessionPersistenceType + * @see SessionPersistence */ - public Builder sessionPersistenceType(@Nullable SessionPersistenceType sessionPersistenceType) { + public Builder sessionPersistenceType(@Nullable SessionPersistence sessionPersistenceType) { if (sessionPersistenceType != null) { this.sessionPersistence = Maps.newHashMap(); this.sessionPersistence.put(PERSISTENCE_TYPE, sessionPersistenceType); diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApi.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApi.java new file mode 100644 index 0000000000..554fc1db2d --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApi.java @@ -0,0 +1,53 @@ +/** + * 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.rackspace.cloudloadbalancers.features; + +import org.jclouds.rackspace.cloudloadbalancers.domain.SessionPersistence; + + +/** + * Session persistence is a feature of the load balancing service that forces multiple requests from clients to be + * directed to the same node. This is common with many web applications that do not inherently share application + * state between back-end servers. Two session persistence modes are available, HTTP Cookie and Source IP. + * + * @see SessionPersistenceAsyncApi + * @author Everett Toews + */ +public interface SessionPersistenceApi { + /** + * Get the current session persistence. + * + * @see SessionPersistence + */ + SessionPersistence get(); + + /** + * Create session persistence. + * + * @see SessionPersistence + */ + void create(SessionPersistence sessionPersistence); + + /** + * Remove session persistence. + * + * @see SessionPersistence + */ + void remove(); +} \ No newline at end of file diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceAsyncApi.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceAsyncApi.java new file mode 100644 index 0000000000..7c2e8ffdf1 --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceAsyncApi.java @@ -0,0 +1,85 @@ +/** + * 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.rackspace.cloudloadbalancers.features; + +import javax.inject.Named; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.jclouds.Fallbacks.NullOnNotFoundOr404; +import org.jclouds.Fallbacks.VoidOnNotFoundOr404; +import org.jclouds.openstack.keystone.v2_0.filters.AuthenticateRequest; +import org.jclouds.rackspace.cloudloadbalancers.domain.SessionPersistence; +import org.jclouds.rackspace.cloudloadbalancers.functions.ParseSessionPersistence; +import org.jclouds.rest.annotations.Fallback; +import org.jclouds.rest.annotations.Payload; +import org.jclouds.rest.annotations.PayloadParam; +import org.jclouds.rest.annotations.RequestFilters; +import org.jclouds.rest.annotations.ResponseParser; + +import com.google.common.util.concurrent.ListenableFuture; + +/** + * Provides asynchronous access to Rackspace Cloud Load Balancers via their REST API. + *

+ * + * @see SessionPersistenceApi + * @author Everett Toews + */ +@RequestFilters(AuthenticateRequest.class) +public interface SessionPersistenceAsyncApi { + + /** + * @see SessionPersistenceApi#get() + */ + @Named("sessionpersistence:state") + @GET + @Consumes(MediaType.APPLICATION_JSON) + @ResponseParser(ParseSessionPersistence.class) + @Fallback(NullOnNotFoundOr404.class) + @Path("/sessionpersistence") + ListenableFuture get(); + + /** + * @see SessionPersistenceApi#create(SessionPersistence) + */ + @Named("sessionpersistence:state") + @PUT + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @Fallback(VoidOnNotFoundOr404.class) + @Payload("%7B\"sessionPersistence\":%7B\"persistenceType\":\"{sessionPersistence}\"%7D%7D") + @Path("/sessionpersistence") + ListenableFuture create(@PayloadParam("sessionPersistence") SessionPersistence sessionPersistence); + + /** + * @see SessionPersistenceApi#remove() + */ + @Named("sessionpersistence:state") + @DELETE + @Consumes(MediaType.APPLICATION_JSON) + @Fallback(VoidOnNotFoundOr404.class) + @Path("/sessionpersistence") + ListenableFuture remove(); +} diff --git a/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/functions/ParseSessionPersistence.java b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/functions/ParseSessionPersistence.java new file mode 100644 index 0000000000..059b54104d --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/main/java/org/jclouds/rackspace/cloudloadbalancers/functions/ParseSessionPersistence.java @@ -0,0 +1,65 @@ +/** + * 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.rackspace.cloudloadbalancers.functions; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import javax.inject.Inject; + +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.HttpResponseException; +import org.jclouds.http.functions.ParseJson; +import org.jclouds.rackspace.cloudloadbalancers.domain.SessionPersistence; +import org.jclouds.rest.InvocationContext; + +import com.google.common.base.Function; +import com.google.common.collect.Iterables; + +/** + * @author Everett Toews + */ +public class ParseSessionPersistence implements Function, InvocationContext { + + private final ParseJson>> json; + + @Inject + ParseSessionPersistence(ParseJson>> json) { + this.json = checkNotNull(json, "json"); + } + + @Override + public SessionPersistence apply(HttpResponse response) { + Map> map = json.apply(response); + + if (map == null || map.size() == 0) + throw new HttpResponseException("Unexpected connection logging format returned.", null, response); + else if (Iterables.get(map.values(), 0).size() == 0) + return null; + else + return Iterables.get(Iterables.get(map.values(), 0).values(), 0); + } + + @Override + public ParseSessionPersistence setContext(HttpRequest request) { + return this; + } +} diff --git a/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApiExpectTest.java b/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApiExpectTest.java new file mode 100644 index 0000000000..896f9335d0 --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApiExpectTest.java @@ -0,0 +1,86 @@ +/** + * 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.rackspace.cloudloadbalancers.features; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; + +import java.net.URI; + +import org.jclouds.http.HttpResponse; +import org.jclouds.rackspace.cloudloadbalancers.CloudLoadBalancersApi; +import org.jclouds.rackspace.cloudloadbalancers.domain.SessionPersistence; +import org.jclouds.rackspace.cloudloadbalancers.internal.BaseCloudLoadBalancerApiExpectTest; +import org.testng.annotations.Test; + +/** + * @author Everett Toews + */ +@Test(groups = "unit") +public class SessionPersistenceApiExpectTest extends BaseCloudLoadBalancerApiExpectTest { + public void testGetSessionPersistence() { + URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/sessionpersistence"); + SessionPersistenceApi api = requestsSendResponses( + rackspaceAuthWithUsernameAndApiKey, + responseWithAccess, + authenticatedGET().endpoint(endpoint).build(), + HttpResponse.builder().statusCode(200).payload(payloadFromResource("/sessionpersistence-get.json")).build() + ).getSessionPersistenceApiForZoneAndLoadBalancer("DFW", 2000); + + SessionPersistence sessionPersistence = api.get(); + assertEquals(sessionPersistence, SessionPersistence.HTTP_COOKIE); + } + + public void testGetDeletedSessionPersistence() { + URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/sessionpersistence"); + SessionPersistenceApi api = requestsSendResponses( + rackspaceAuthWithUsernameAndApiKey, + responseWithAccess, + authenticatedGET().endpoint(endpoint).build(), + HttpResponse.builder().statusCode(200).payload(payloadFromResource("/sessionpersistence-get-deleted.json")).build() + ).getSessionPersistenceApiForZoneAndLoadBalancer("DFW", 2000); + + SessionPersistence sessionPersistence = api.get(); + assertNull(sessionPersistence); + } + + public void testCreateSessionPersistence() { + URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/sessionpersistence"); + SessionPersistenceApi api = requestsSendResponses( + rackspaceAuthWithUsernameAndApiKey, + responseWithAccess, + authenticatedGET().method("PUT").endpoint(endpoint).payload(payloadFromResource("/sessionpersistence-create.json")).build(), + HttpResponse.builder().statusCode(200).build() + ).getSessionPersistenceApiForZoneAndLoadBalancer("DFW", 2000); + + api.create(SessionPersistence.HTTP_COOKIE); + } + + public void testRemoveSessionPersistence() { + URI endpoint = URI.create("https://dfw.loadbalancers.api.rackspacecloud.com/v1.0/123123/loadbalancers/2000/sessionpersistence"); + SessionPersistenceApi api = requestsSendResponses( + rackspaceAuthWithUsernameAndApiKey, + responseWithAccess, + authenticatedGET().method("DELETE").endpoint(endpoint).build(), + HttpResponse.builder().statusCode(200).build() + ).getSessionPersistenceApiForZoneAndLoadBalancer("DFW", 2000); + + api.remove(); + } +} diff --git a/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApiLiveTest.java b/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApiLiveTest.java new file mode 100644 index 0000000000..4bcb3cfd2c --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/features/SessionPersistenceApiLiveTest.java @@ -0,0 +1,87 @@ +/** + * 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.rackspace.cloudloadbalancers.features; + +import static org.jclouds.rackspace.cloudloadbalancers.predicates.LoadBalancerPredicates.awaitAvailable; +import static org.jclouds.rackspace.cloudloadbalancers.predicates.LoadBalancerPredicates.awaitDeleted; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; + +import org.jclouds.rackspace.cloudloadbalancers.domain.LoadBalancer; +import org.jclouds.rackspace.cloudloadbalancers.domain.LoadBalancerRequest; +import org.jclouds.rackspace.cloudloadbalancers.domain.NodeRequest; +import org.jclouds.rackspace.cloudloadbalancers.domain.SessionPersistence; +import org.jclouds.rackspace.cloudloadbalancers.domain.VirtualIP.Type; +import org.jclouds.rackspace.cloudloadbalancers.internal.BaseCloudLoadBalancersApiLiveTest; +import org.testng.annotations.AfterGroups; +import org.testng.annotations.Test; + +import com.google.common.collect.Iterables; + +/** + * @author Everett Toews + */ +@Test(groups = "live", singleThreaded = true, testName = "SessionPersistenceApiLiveTest") +public class SessionPersistenceApiLiveTest extends BaseCloudLoadBalancersApiLiveTest { + private LoadBalancer lb; + private String zone; + + public void testCreateLoadBalancer() { + NodeRequest nodeRequest = NodeRequest.builder().address("192.168.1.1").port(8080).build(); + LoadBalancerRequest lbRequest = LoadBalancerRequest.builder() + .name(prefix+"-jclouds").protocol("HTTP").port(80).virtualIPType(Type.PUBLIC).node(nodeRequest).build(); + + zone = Iterables.getFirst(clbApi.getConfiguredZones(), null); + lb = clbApi.getLoadBalancerApiForZone(zone).create(lbRequest); + + assertTrue(awaitAvailable(clbApi.getLoadBalancerApiForZone(zone)).apply(lb)); + } + + @Test(dependsOnMethods = "testCreateLoadBalancer") + public void testCreateAndGetSessionPersistence() throws Exception { + clbApi.getSessionPersistenceApiForZoneAndLoadBalancer(zone, lb.getId()).create(SessionPersistence.HTTP_COOKIE); + assertTrue(awaitAvailable(clbApi.getLoadBalancerApiForZone(zone)).apply(lb)); + + SessionPersistence sessionPersistence = + clbApi.getSessionPersistenceApiForZoneAndLoadBalancer(zone, lb.getId()).get(); + + assertEquals(sessionPersistence, SessionPersistence.HTTP_COOKIE); + } + + @Test(dependsOnMethods = "testCreateAndGetSessionPersistence") + public void testRemoveAndGetSessionPersistence() throws Exception { + clbApi.getSessionPersistenceApiForZoneAndLoadBalancer(zone, lb.getId()).remove(); + assertTrue(awaitAvailable(clbApi.getLoadBalancerApiForZone(zone)).apply(lb)); + + SessionPersistence sessionPersistence = + clbApi.getSessionPersistenceApiForZoneAndLoadBalancer(zone, lb.getId()).get(); + + assertNull(sessionPersistence); + } + + @Override + @AfterGroups(groups = "live") + protected void tearDownContext() { + assertTrue(awaitAvailable(clbApi.getLoadBalancerApiForZone(zone)).apply(lb)); + clbApi.getLoadBalancerApiForZone(zone).remove(lb.getId()); + assertTrue(awaitDeleted(clbApi.getLoadBalancerApiForZone(zone)).apply(lb)); + super.tearDownContext(); + } +} diff --git a/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/functions/ParseLoadBalancerTest.java b/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/functions/ParseLoadBalancerTest.java index b76f601c01..125786510e 100644 --- a/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/functions/ParseLoadBalancerTest.java +++ b/apis/rackspace-cloudloadbalancers/src/test/java/org/jclouds/rackspace/cloudloadbalancers/functions/ParseLoadBalancerTest.java @@ -31,11 +31,11 @@ import org.jclouds.rackspace.cloudloadbalancers.domain.LoadBalancer.Status; import org.jclouds.rackspace.cloudloadbalancers.domain.Metadata; import org.jclouds.rackspace.cloudloadbalancers.domain.Node; import org.jclouds.rackspace.cloudloadbalancers.domain.SSLTermination; +import org.jclouds.rackspace.cloudloadbalancers.domain.SessionPersistence; import org.jclouds.rackspace.cloudloadbalancers.domain.SourceAddresses; import org.jclouds.rackspace.cloudloadbalancers.domain.VirtualIP; import org.jclouds.rackspace.cloudloadbalancers.domain.VirtualIPWithId; import org.jclouds.rackspace.cloudloadbalancers.domain.internal.BaseLoadBalancer.Algorithm; -import org.jclouds.rackspace.cloudloadbalancers.domain.internal.BaseLoadBalancer.SessionPersistenceType; import org.testng.annotations.Test; import com.google.common.base.Function; @@ -90,7 +90,7 @@ public class ParseLoadBalancerTest extends BaseItemParserTest { .nodes(ImmutableSet.of( Node.builder().id(1041).address("10.1.1.1").port(80).condition(Node.Condition.ENABLED).status(Node.Status.ONLINE).build(), Node.builder().id(1411).address("10.1.1.2").port(80).condition(Node.Condition.ENABLED).status(Node.Status.ONLINE).build())) - .sessionPersistenceType(SessionPersistenceType.HTTP_COOKIE) + .sessionPersistenceType(SessionPersistence.HTTP_COOKIE) .clusterName("c1.dfw1") .created(new SimpleDateFormatDateService().iso8601SecondsDateParse("2010-11-30T03:23:42Z")) .updated(new SimpleDateFormatDateService().iso8601SecondsDateParse("2010-11-30T03:23:44Z")) diff --git a/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-create.json b/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-create.json new file mode 100644 index 0000000000..70455cf35f --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-create.json @@ -0,0 +1 @@ +{"sessionPersistence":{"persistenceType":"HTTP_COOKIE"}} \ No newline at end of file diff --git a/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-get-deleted.json b/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-get-deleted.json new file mode 100644 index 0000000000..a5dd94b699 --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-get-deleted.json @@ -0,0 +1,4 @@ +{ + "sessionPersistence": { + } +} \ No newline at end of file diff --git a/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-get.json b/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-get.json new file mode 100644 index 0000000000..b7b151c80e --- /dev/null +++ b/apis/rackspace-cloudloadbalancers/src/test/resources/sessionpersistence-get.json @@ -0,0 +1,5 @@ +{ + "sessionPersistence": { + "persistenceType": "HTTP_COOKIE" + } +} \ No newline at end of file