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.location.suppliers; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | import static com.google.common.collect.Maps.uniqueIndex; |
23 | |
24 | import java.net.URI; |
25 | import java.util.Map; |
26 | import java.util.Set; |
27 | |
28 | import javax.inject.Inject; |
29 | import javax.inject.Singleton; |
30 | |
31 | import org.jclouds.domain.Location; |
32 | import org.jclouds.domain.LocationBuilder; |
33 | import org.jclouds.domain.LocationScope; |
34 | import org.jclouds.location.Iso3166; |
35 | import org.jclouds.location.Provider; |
36 | import org.jclouds.location.Zone; |
37 | |
38 | import com.google.common.base.Function; |
39 | import com.google.common.collect.ImmutableMap; |
40 | import com.google.common.collect.ImmutableSet; |
41 | import com.google.common.collect.ImmutableSet.Builder; |
42 | |
43 | /** |
44 | * |
45 | * @author Adrian Cole |
46 | */ |
47 | @Singleton |
48 | public class ZoneToRegionToProviderOrJustProvider extends RegionToProviderOrJustProvider { |
49 | private final Map<String, String> zoneToRegion; |
50 | private Map<String, Set<String>> isoCodesById; |
51 | |
52 | @Inject |
53 | ZoneToRegionToProviderOrJustProvider(@Iso3166 Set<String> isoCodes, @Provider String providerName, |
54 | @Provider URI endpoint, @Iso3166 Map<String, Set<String>> isoCodesById, |
55 | @Zone Map<String, String> zoneToRegion) { |
56 | super(isoCodes, providerName, endpoint, ImmutableSet.copyOf(checkNotNull(zoneToRegion, "zoneToRegion").values()), |
57 | isoCodesById); |
58 | this.zoneToRegion = zoneToRegion; |
59 | this.isoCodesById = checkNotNull(isoCodesById, "isoCodesById"); |
60 | } |
61 | |
62 | @Override |
63 | public Set<? extends Location> get() { |
64 | Builder<Location> locations = buildJustProviderOrRegions(); |
65 | ImmutableMap<String, Location> idToLocation = uniqueIndex(locations.build(), new Function<Location, String>() { |
66 | @Override |
67 | public String apply(Location from) { |
68 | return from.getId(); |
69 | } |
70 | }); |
71 | if (zoneToRegion.size() == 1) |
72 | return locations.build(); |
73 | for (String zone : zoneToRegion.keySet()) { |
74 | Location parent = idToLocation.get(zoneToRegion.get(zone)); |
75 | LocationBuilder builder = new LocationBuilder().scope(LocationScope.ZONE).id(zone).description(zone).parent( |
76 | parent); |
77 | if (isoCodesById.containsKey(zone)) |
78 | builder.iso3166Codes(isoCodesById.get(zone)); |
79 | // be cautious.. only inherit iso codes if the parent is a region |
80 | // regions may be added dynamically, and we prefer to inherit an |
81 | // empty set of codes from a region, then a provider, whose code |
82 | // are likely hard-coded. |
83 | else if (parent.getScope() == LocationScope.REGION) |
84 | builder.iso3166Codes(parent.getIso3166Codes()); |
85 | locations.add(builder.build()); |
86 | } |
87 | return locations.build(); |
88 | } |
89 | } |