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.ec2.suppliers; |
20 | |
21 | import java.util.Map; |
22 | import java.util.Set; |
23 | |
24 | import javax.annotation.Resource; |
25 | import javax.inject.Inject; |
26 | import javax.inject.Singleton; |
27 | |
28 | import org.jclouds.ec2.EC2Client; |
29 | import org.jclouds.ec2.domain.AvailabilityZoneInfo; |
30 | import org.jclouds.ec2.services.AvailabilityZoneAndRegionClient; |
31 | import org.jclouds.http.HttpResponseException; |
32 | import org.jclouds.location.Region; |
33 | import org.jclouds.location.suppliers.RegionIdToZoneIdsSupplier; |
34 | import org.jclouds.logging.Logger; |
35 | import org.jclouds.util.Suppliers2; |
36 | |
37 | import com.google.common.base.Function; |
38 | import com.google.common.base.Supplier; |
39 | import com.google.common.collect.ImmutableMap; |
40 | import com.google.common.collect.ImmutableMap.Builder; |
41 | import com.google.common.collect.ImmutableSet; |
42 | import com.google.common.collect.Iterables; |
43 | import com.google.common.collect.Maps; |
44 | |
45 | @Singleton |
46 | public class DescribeAvailabilityZonesInRegion implements RegionIdToZoneIdsSupplier { |
47 | @Resource |
48 | protected Logger logger = Logger.NULL; |
49 | |
50 | private final AvailabilityZoneAndRegionClient client; |
51 | private final Supplier<Set<String>> regions; |
52 | |
53 | @Inject |
54 | public DescribeAvailabilityZonesInRegion(EC2Client client, @Region Supplier<Set<String>> regions) { |
55 | this.client = client.getAvailabilityZoneAndRegionServices(); |
56 | this.regions = regions; |
57 | } |
58 | |
59 | @Override |
60 | public Map<String, Supplier<Set<String>>> get() { |
61 | Builder<String, Set<String>> map = ImmutableMap.builder(); |
62 | HttpResponseException exception = null; |
63 | // TODO: this should be parallel |
64 | for (String region : regions.get()) { |
65 | try { |
66 | ImmutableSet<String> zones = ImmutableSet.copyOf(Iterables.transform(client |
67 | .describeAvailabilityZonesInRegion(region), new Function<AvailabilityZoneInfo, String>() { |
68 | |
69 | @Override |
70 | public String apply(AvailabilityZoneInfo arg0) { |
71 | return arg0.getZone(); |
72 | } |
73 | |
74 | })); |
75 | if (zones.size() > 0) |
76 | map.put(region, zones); |
77 | } catch (HttpResponseException e) { |
78 | // TODO: this should be in retry handler, not here. |
79 | if (e.getMessage().contains("Unable to tunnel through proxy")) { |
80 | exception = e; |
81 | logger.error(e, "Could not describe availability zones in Region: %s", region); |
82 | } else { |
83 | throw e; |
84 | } |
85 | } |
86 | } |
87 | ImmutableMap<String, Set<String>> result = map.build(); |
88 | if (result.isEmpty() && exception != null) { |
89 | throw exception; |
90 | } |
91 | return Maps.transformValues(result, Suppliers2.<Set<String>> ofInstanceFunction()); |
92 | } |
93 | |
94 | } |