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 java.util.Map;
22  import java.util.Set;
23  
24  import javax.inject.Inject;
25  
26  import org.jclouds.cloudloadbalancers.domain.LoadBalancer;
27  import org.jclouds.http.HttpRequest;
28  import org.jclouds.http.HttpResponse;
29  import org.jclouds.http.functions.ParseJson;
30  import org.jclouds.rest.InvocationContext;
31  
32  import com.google.common.base.Function;
33  import com.google.common.collect.ImmutableSet;
34  import com.google.common.collect.Iterables;
35  
36  /**
37   * @author Adrian Cole
38   */
39  public class UnwrapLoadBalancers implements Function<HttpResponse, Set<LoadBalancer>>,
40           InvocationContext<UnwrapLoadBalancers> {
41  
42     private final ParseJson<Map<String, Set<LB>>> json;
43     private ConvertLB convertLB;
44  
45     @Inject
46     UnwrapLoadBalancers(ParseJson<Map<String, Set<LB>>> json) {
47        this.json = json;
48     }
49  
50     @Override
51     public Set<LoadBalancer> apply(HttpResponse arg0) {
52        Map<String, Set<LB>> map = json.apply(arg0);
53        if (map.size() == 0)
54           return ImmutableSet.<LoadBalancer> of();
55        ;
56        return ImmutableSet.copyOf(Iterables.transform(Iterables.get(map.values(), 0), convertLB));
57     }
58  
59     @Override
60     public UnwrapLoadBalancers setContext(HttpRequest request) {
61        return setRegion(request.getEndpoint().getHost().substring(0, request.getEndpoint().getHost().indexOf('.')));
62     }
63  
64     UnwrapLoadBalancers setRegion(String region) {
65        this.convertLB = new ConvertLB(region);
66        return this;
67     }
68  
69  }