| 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.ec2.config; |
| 20 | |
| 21 | import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | import java.util.Map.Entry; |
| 27 | |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.aws.config.WithZonesFormSigningRestClientModule; |
| 32 | import org.jclouds.ec2.EC2AsyncClient; |
| 33 | import org.jclouds.ec2.EC2Client; |
| 34 | import org.jclouds.ec2.domain.AvailabilityZoneInfo; |
| 35 | import org.jclouds.ec2.services.AMIAsyncClient; |
| 36 | import org.jclouds.ec2.services.AMIClient; |
| 37 | import org.jclouds.ec2.services.AvailabilityZoneAndRegionAsyncClient; |
| 38 | import org.jclouds.ec2.services.AvailabilityZoneAndRegionClient; |
| 39 | import org.jclouds.ec2.services.ElasticBlockStoreAsyncClient; |
| 40 | import org.jclouds.ec2.services.ElasticBlockStoreClient; |
| 41 | import org.jclouds.ec2.services.ElasticIPAddressAsyncClient; |
| 42 | import org.jclouds.ec2.services.ElasticIPAddressClient; |
| 43 | import org.jclouds.ec2.services.InstanceAsyncClient; |
| 44 | import org.jclouds.ec2.services.InstanceClient; |
| 45 | import org.jclouds.ec2.services.KeyPairAsyncClient; |
| 46 | import org.jclouds.ec2.services.KeyPairClient; |
| 47 | import org.jclouds.ec2.services.SecurityGroupAsyncClient; |
| 48 | import org.jclouds.ec2.services.SecurityGroupClient; |
| 49 | import org.jclouds.ec2.services.WindowsAsyncClient; |
| 50 | import org.jclouds.ec2.services.WindowsClient; |
| 51 | import org.jclouds.http.RequiresHttp; |
| 52 | import org.jclouds.location.Region; |
| 53 | import org.jclouds.location.Zone; |
| 54 | import org.jclouds.rest.ConfiguresRestClient; |
| 55 | |
| 56 | import com.google.common.base.Predicates; |
| 57 | import com.google.common.base.Splitter; |
| 58 | import com.google.common.collect.ImmutableMap; |
| 59 | import com.google.common.collect.ImmutableSet; |
| 60 | import com.google.common.collect.Maps; |
| 61 | import com.google.common.collect.ImmutableMap.Builder; |
| 62 | import com.google.inject.ConfigurationException; |
| 63 | import com.google.inject.Injector; |
| 64 | import com.google.inject.Key; |
| 65 | import com.google.inject.name.Names; |
| 66 | |
| 67 | /** |
| 68 | * Configures the EC2 connection. |
| 69 | * |
| 70 | * @author Adrian Cole |
| 71 | */ |
| 72 | @RequiresHttp |
| 73 | @ConfiguresRestClient |
| 74 | public class EC2RestClientModule<S extends EC2Client, A extends EC2AsyncClient> extends |
| 75 | WithZonesFormSigningRestClientModule<S, A> { |
| 76 | |
| 77 | public static final Map<Class<?>, Class<?>> DELEGATE_MAP = ImmutableMap.<Class<?>, Class<?>> builder()// |
| 78 | .put(AMIClient.class, AMIAsyncClient.class)// |
| 79 | .put(ElasticIPAddressClient.class, ElasticIPAddressAsyncClient.class)// |
| 80 | .put(InstanceClient.class, InstanceAsyncClient.class)// |
| 81 | .put(KeyPairClient.class, KeyPairAsyncClient.class)// |
| 82 | .put(SecurityGroupClient.class, SecurityGroupAsyncClient.class)// |
| 83 | .put(WindowsClient.class, WindowsAsyncClient.class)// |
| 84 | .put(AvailabilityZoneAndRegionClient.class, AvailabilityZoneAndRegionAsyncClient.class)// |
| 85 | .put(ElasticBlockStoreClient.class, ElasticBlockStoreAsyncClient.class)// |
| 86 | .build(); |
| 87 | |
| 88 | public static EC2RestClientModule<EC2Client, EC2AsyncClient> create() { |
| 89 | return new EC2RestClientModule<EC2Client, EC2AsyncClient>(EC2Client.class, EC2AsyncClient.class, DELEGATE_MAP); |
| 90 | } |
| 91 | |
| 92 | public EC2RestClientModule(Class<S> sync, Class<A> async, Map<Class<?>, Class<?>> delegateMap) { |
| 93 | super(sync, async, delegateMap); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | protected void bindRegionsToProvider() { |
| 98 | bindRegionsToProvider(RegionIdsToURI.class); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | protected void bindZonesToProvider() { |
| 103 | bindZonesToProvider(RegionIdToZoneId.class); |
| 104 | } |
| 105 | |
| 106 | @Singleton |
| 107 | public static class RegionIdsToURI implements javax.inject.Provider<Map<String, URI>> { |
| 108 | private final AvailabilityZoneAndRegionClient client; |
| 109 | private final Injector injector; |
| 110 | |
| 111 | @Inject |
| 112 | public RegionIdsToURI(EC2Client client, Injector injector) { |
| 113 | this.client = client.getAvailabilityZoneAndRegionServices(); |
| 114 | this.injector = injector; |
| 115 | } |
| 116 | |
| 117 | @Singleton |
| 118 | @Region |
| 119 | @Override |
| 120 | public Map<String, URI> get() { |
| 121 | try { |
| 122 | String regionString = injector.getInstance(Key.get(String.class, Names.named(PROPERTY_REGIONS))); |
| 123 | Set<String> regions = ImmutableSet.copyOf(Splitter.on(',').split(regionString)); |
| 124 | if (regions.size() > 0) |
| 125 | return Maps.filterKeys(client.describeRegions(), Predicates.in(regions)); |
| 126 | } catch (ConfigurationException e) { |
| 127 | // this happens if regions property isn't set |
| 128 | // services not run by AWS may not have regions, so this is ok. |
| 129 | } |
| 130 | return client.describeRegions(); |
| 131 | |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @Singleton |
| 136 | public static class RegionIdToZoneId implements javax.inject.Provider<Map<String, String>> { |
| 137 | private final AvailabilityZoneAndRegionClient client; |
| 138 | private final Map<String, URI> regions; |
| 139 | |
| 140 | @Inject |
| 141 | public RegionIdToZoneId(EC2Client client, @Region Map<String, URI> regions) { |
| 142 | this.client = client.getAvailabilityZoneAndRegionServices(); |
| 143 | this.regions = regions; |
| 144 | } |
| 145 | |
| 146 | @Singleton |
| 147 | @Zone |
| 148 | @Override |
| 149 | public Map<String, String> get() { |
| 150 | Builder<String, String> map = ImmutableMap.<String, String> builder(); |
| 151 | for (Entry<String, URI> region : regions.entrySet()) { |
| 152 | for (AvailabilityZoneInfo zoneInfo : client.describeAvailabilityZonesInRegion(region.getKey())) { |
| 153 | map.put(zoneInfo.getZone(), region.getKey()); |
| 154 | } |
| 155 | } |
| 156 | return map.build(); |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | } |