| 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.aws.ec2.compute.config; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import javax.inject.Named; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.aws.ec2.AWSEC2AsyncClient; |
| 28 | import org.jclouds.aws.ec2.AWSEC2Client; |
| 29 | import org.jclouds.aws.ec2.compute.AWSEC2ComputeService; |
| 30 | import org.jclouds.aws.ec2.compute.AWSEC2TemplateOptions; |
| 31 | import org.jclouds.aws.ec2.compute.suppliers.CallForImages; |
| 32 | import org.jclouds.aws.ec2.domain.PlacementGroup; |
| 33 | import org.jclouds.aws.ec2.domain.RegionNameAndPublicKeyMaterial; |
| 34 | import org.jclouds.aws.ec2.functions.CreatePlacementGroupIfNeeded; |
| 35 | import org.jclouds.aws.ec2.functions.ImportOrReturnExistingKeypair; |
| 36 | import org.jclouds.aws.ec2.predicates.PlacementGroupAvailable; |
| 37 | import org.jclouds.aws.ec2.predicates.PlacementGroupDeleted; |
| 38 | import org.jclouds.compute.ComputeService; |
| 39 | import org.jclouds.compute.ComputeServiceContext; |
| 40 | import org.jclouds.compute.domain.Image; |
| 41 | import org.jclouds.compute.domain.NodeMetadata; |
| 42 | import org.jclouds.compute.domain.TemplateBuilder; |
| 43 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
| 44 | import org.jclouds.compute.options.TemplateOptions; |
| 45 | import org.jclouds.domain.Credentials; |
| 46 | import org.jclouds.ec2.compute.config.EC2ComputeServiceDependenciesModule; |
| 47 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 48 | import org.jclouds.ec2.compute.functions.CreateSecurityGroupIfNeeded; |
| 49 | import org.jclouds.ec2.compute.functions.CreateUniqueKeyPair; |
| 50 | import org.jclouds.ec2.compute.functions.CredentialsForInstance; |
| 51 | import org.jclouds.ec2.compute.functions.RegionAndIdToImage; |
| 52 | import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata; |
| 53 | import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl; |
| 54 | import org.jclouds.ec2.domain.KeyPair; |
| 55 | import org.jclouds.ec2.domain.RunningInstance; |
| 56 | import org.jclouds.predicates.RetryablePredicate; |
| 57 | import org.jclouds.rest.RestContext; |
| 58 | import org.jclouds.rest.internal.RestContextImpl; |
| 59 | |
| 60 | import com.google.common.base.Function; |
| 61 | import com.google.common.base.Predicate; |
| 62 | import com.google.common.cache.Cache; |
| 63 | import com.google.common.cache.CacheBuilder; |
| 64 | import com.google.common.cache.CacheLoader; |
| 65 | import com.google.common.collect.Sets; |
| 66 | import com.google.inject.Provides; |
| 67 | import com.google.inject.Scopes; |
| 68 | import com.google.inject.TypeLiteral; |
| 69 | import com.google.inject.assistedinject.FactoryModuleBuilder; |
| 70 | |
| 71 | /** |
| 72 | * |
| 73 | * @author Adrian Cole |
| 74 | */ |
| 75 | public class AWSEC2ComputeServiceDependenciesModule extends EC2ComputeServiceDependenciesModule { |
| 76 | @Override |
| 77 | protected void configure() { |
| 78 | bind(TemplateBuilder.class).to(EC2TemplateBuilderImpl.class); |
| 79 | bind(TemplateOptions.class).to(AWSEC2TemplateOptions.class); |
| 80 | bind(ComputeService.class).to(AWSEC2ComputeService.class); |
| 81 | bind(new TypeLiteral<Function<RunningInstance, NodeMetadata>>() { |
| 82 | }).to(RunningInstanceToNodeMetadata.class); |
| 83 | bind(new TypeLiteral<CacheLoader<RunningInstance, Credentials>>() { |
| 84 | }).to(CredentialsForInstance.class); |
| 85 | bind(new TypeLiteral<CacheLoader<RegionAndName, String>>() { |
| 86 | }).to(CreateSecurityGroupIfNeeded.class); |
| 87 | bind(new TypeLiteral<Function<RegionAndName, KeyPair>>() { |
| 88 | }).to(CreateUniqueKeyPair.class); |
| 89 | bind(new TypeLiteral<Function<RegionNameAndPublicKeyMaterial, KeyPair>>() { |
| 90 | }).to(ImportOrReturnExistingKeypair.class); |
| 91 | bind(new TypeLiteral<CacheLoader<RegionAndName, Image>>() { |
| 92 | }).to(RegionAndIdToImage.class); |
| 93 | bind(new TypeLiteral<ComputeServiceContext>() { |
| 94 | }).to(new TypeLiteral<ComputeServiceContextImpl<AWSEC2Client, AWSEC2AsyncClient>>() { |
| 95 | }).in(Scopes.SINGLETON); |
| 96 | bind(new TypeLiteral<RestContext<AWSEC2Client, AWSEC2AsyncClient>>() { |
| 97 | }).to(new TypeLiteral<RestContextImpl<AWSEC2Client, AWSEC2AsyncClient>>() { |
| 98 | }).in(Scopes.SINGLETON); |
| 99 | install(new FactoryModuleBuilder().build(CallForImages.Factory.class)); |
| 100 | } |
| 101 | |
| 102 | @Provides |
| 103 | @Singleton |
| 104 | @Named("AVAILABLE") |
| 105 | protected Predicate<PlacementGroup> placementGroupAvailable(PlacementGroupAvailable available) { |
| 106 | return new RetryablePredicate<PlacementGroup>(available, 60, 1, TimeUnit.SECONDS); |
| 107 | } |
| 108 | |
| 109 | @Provides |
| 110 | @Singleton |
| 111 | @Named("DELETED") |
| 112 | protected Predicate<PlacementGroup> placementGroupDeleted(PlacementGroupDeleted deleted) { |
| 113 | return new RetryablePredicate<PlacementGroup>(deleted, 60, 1, TimeUnit.SECONDS); |
| 114 | } |
| 115 | |
| 116 | @Provides |
| 117 | @Singleton |
| 118 | @Named("PLACEMENT") |
| 119 | protected Cache<RegionAndName, String> placementGroupMap(CreatePlacementGroupIfNeeded in) { |
| 120 | return CacheBuilder.newBuilder().build(in); |
| 121 | } |
| 122 | |
| 123 | @Provides |
| 124 | @ClusterCompute |
| 125 | @Singleton |
| 126 | protected Set<String> provideClusterComputeIds() { |
| 127 | return Sets.newLinkedHashSet(); |
| 128 | } |
| 129 | |
| 130 | } |