View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.cloudloadbalancers.functions;
20  
21  import org.jclouds.cloudloadbalancers.domain.LoadBalancer;
22  import org.jclouds.cloudloadbalancers.domain.LoadBalancer.Builder;
23  
24  import com.google.common.base.Function;
25  import com.google.common.collect.Iterables;
26  
27  /**
28   * @author Adrian Cole
29   */
30  public class ConvertLB implements Function<LB, LoadBalancer> {
31  
32     private final String region;
33  
34     ConvertLB(String region) {
35        this.region = region.toUpperCase();
36     }
37  
38     @Override
39     public LoadBalancer apply(LB lb) {
40        Builder builder = LoadBalancer.builder().region(region).name(lb.getName()).port(lb.getPort()).protocol(
41                 lb.getProtocol()).algorithm(lb.getAlgorithm()).nodes(lb.getNodes()).id(lb.id).status(lb.status)
42                 .virtualIPs(lb.virtualIps);
43        if (lb.cluster.size() == 1)
44           builder.clusterName(Iterables.get(lb.cluster.values(), 0));
45        if (lb.sessionPersistence.size() == 1)
46           builder.sessionPersistenceType(Iterables.get(lb.sessionPersistence.values(), 0));
47        if (lb.created.size() == 1)
48           builder.created(Iterables.get(lb.created.values(), 0));
49        if (lb.updated.size() == 1)
50           builder.updated(Iterables.get(lb.updated.values(), 0));
51        if (lb.connectionLogging.size() == 1)
52           builder.connectionLoggingEnabled(Iterables.get(lb.connectionLogging.values(), 0));
53        return builder.build();
54     }
55  
56  }