| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.cloudloadbalancers.loadbalancer.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.collect.Iterables.concat; |
| 23 | import static com.google.common.collect.Iterables.transform; |
| 24 | import static org.jclouds.concurrent.FutureIterables.transformParallel; |
| 25 | |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Future; |
| 29 | |
| 30 | import org.jclouds.javax.annotation.Nullable; |
| 31 | import javax.annotation.Resource; |
| 32 | import javax.inject.Inject; |
| 33 | import javax.inject.Named; |
| 34 | import javax.inject.Singleton; |
| 35 | |
| 36 | import org.jclouds.Constants; |
| 37 | import org.jclouds.cloudloadbalancers.CloudLoadBalancersAsyncClient; |
| 38 | import org.jclouds.cloudloadbalancers.domain.LoadBalancer; |
| 39 | import org.jclouds.loadbalancer.domain.LoadBalancerMetadata; |
| 40 | import org.jclouds.loadbalancer.reference.LoadBalancerConstants; |
| 41 | import org.jclouds.loadbalancer.strategy.ListLoadBalancersStrategy; |
| 42 | import org.jclouds.location.Region; |
| 43 | import org.jclouds.logging.Logger; |
| 44 | |
| 45 | import com.google.common.base.Function; |
| 46 | import com.google.common.util.concurrent.ListenableFuture; |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @author Adrian Cole |
| 51 | */ |
| 52 | @Singleton |
| 53 | public class CloudLoadBalancersListLoadBalancersStrategy implements ListLoadBalancersStrategy { |
| 54 | @Resource |
| 55 | @Named(LoadBalancerConstants.LOADBALANCER_LOGGER) |
| 56 | protected Logger logger = Logger.NULL; |
| 57 | |
| 58 | private final CloudLoadBalancersAsyncClient aclient; |
| 59 | private final Function<LoadBalancer, LoadBalancerMetadata> converter; |
| 60 | private final ExecutorService executor; |
| 61 | private final Set<String> regions; |
| 62 | |
| 63 | @Inject |
| 64 | protected CloudLoadBalancersListLoadBalancersStrategy(CloudLoadBalancersAsyncClient aclient, |
| 65 | Function<LoadBalancer, LoadBalancerMetadata> converter, |
| 66 | @Named(Constants.PROPERTY_USER_THREADS) ExecutorService executor, @Nullable @Region Set<String> regions) { |
| 67 | this.aclient = checkNotNull(aclient, "aclient"); |
| 68 | this.regions = checkNotNull(regions, "regions"); |
| 69 | this.converter = checkNotNull(converter, "converter"); |
| 70 | this.executor = checkNotNull(executor, "executor"); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Iterable<? extends LoadBalancerMetadata> listLoadBalancers() { |
| 75 | return transform(concat(transformParallel(regions, new Function<String, Future<Set<LoadBalancer>>>() { |
| 76 | |
| 77 | @Override |
| 78 | public ListenableFuture<Set<LoadBalancer>> apply(String from) { |
| 79 | return aclient.getLoadBalancerClient(from).listLoadBalancers(); |
| 80 | } |
| 81 | |
| 82 | }, executor, null, logger, "loadbalancers")), converter); |
| 83 | } |
| 84 | } |