| 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.compute.config; |
| 20 | |
| 21 | import static com.google.common.collect.Maps.newLinkedHashMap; |
| 22 | |
| 23 | import java.security.SecureRandom; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Named; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.compute.ComputeService; |
| 30 | import org.jclouds.compute.ComputeServiceContext; |
| 31 | import org.jclouds.compute.domain.Image; |
| 32 | import org.jclouds.compute.domain.NodeMetadata; |
| 33 | import org.jclouds.compute.domain.NodeState; |
| 34 | import org.jclouds.compute.domain.TemplateBuilder; |
| 35 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
| 36 | import org.jclouds.compute.options.TemplateOptions; |
| 37 | import org.jclouds.domain.Credentials; |
| 38 | import org.jclouds.ec2.EC2AsyncClient; |
| 39 | import org.jclouds.ec2.EC2Client; |
| 40 | import org.jclouds.ec2.compute.EC2ComputeService; |
| 41 | import org.jclouds.ec2.compute.domain.RegionAndName; |
| 42 | import org.jclouds.ec2.compute.domain.RegionNameAndIngressRules; |
| 43 | import org.jclouds.ec2.compute.functions.CreateSecurityGroupIfNeeded; |
| 44 | import org.jclouds.ec2.compute.functions.CreateUniqueKeyPair; |
| 45 | import org.jclouds.ec2.compute.functions.CredentialsForInstance; |
| 46 | import org.jclouds.ec2.compute.functions.RegionAndIdToImage; |
| 47 | import org.jclouds.ec2.compute.functions.RunningInstanceToNodeMetadata; |
| 48 | import org.jclouds.ec2.compute.internal.EC2TemplateBuilderImpl; |
| 49 | import org.jclouds.ec2.compute.options.EC2TemplateOptions; |
| 50 | import org.jclouds.ec2.domain.InstanceState; |
| 51 | import org.jclouds.ec2.domain.KeyPair; |
| 52 | import org.jclouds.ec2.domain.RunningInstance; |
| 53 | import org.jclouds.rest.RestContext; |
| 54 | import org.jclouds.rest.internal.RestContextImpl; |
| 55 | |
| 56 | import com.google.common.base.Function; |
| 57 | import com.google.common.base.Supplier; |
| 58 | import com.google.common.collect.ImmutableMap; |
| 59 | import com.google.common.collect.MapMaker; |
| 60 | import com.google.inject.AbstractModule; |
| 61 | import com.google.inject.Provides; |
| 62 | import com.google.inject.Scopes; |
| 63 | import com.google.inject.TypeLiteral; |
| 64 | |
| 65 | /** |
| 66 | * |
| 67 | * @author Adrian Cole |
| 68 | */ |
| 69 | public class EC2ComputeServiceDependenciesModule extends AbstractModule { |
| 70 | |
| 71 | public static final Map<InstanceState, NodeState> instanceToNodeState = ImmutableMap |
| 72 | .<InstanceState, NodeState> builder().put(InstanceState.PENDING, NodeState.PENDING).put( |
| 73 | InstanceState.RUNNING, NodeState.RUNNING).put(InstanceState.SHUTTING_DOWN, NodeState.PENDING).put( |
| 74 | InstanceState.TERMINATED, NodeState.TERMINATED).put(InstanceState.STOPPING, NodeState.PENDING) |
| 75 | .put(InstanceState.STOPPED, NodeState.SUSPENDED).put(InstanceState.UNRECOGNIZED, NodeState.UNRECOGNIZED) |
| 76 | .build(); |
| 77 | |
| 78 | @Singleton |
| 79 | @Provides |
| 80 | Map<InstanceState, NodeState> provideServerToNodeState() { |
| 81 | return instanceToNodeState; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void configure() { |
| 86 | bind(TemplateBuilder.class).to(EC2TemplateBuilderImpl.class); |
| 87 | bind(TemplateOptions.class).to(EC2TemplateOptions.class); |
| 88 | bind(ComputeService.class).to(EC2ComputeService.class); |
| 89 | bind(new TypeLiteral<Function<RunningInstance, NodeMetadata>>() { |
| 90 | }).to(RunningInstanceToNodeMetadata.class); |
| 91 | bind(new TypeLiteral<Function<RunningInstance, Credentials>>() { |
| 92 | }).to(CredentialsForInstance.class); |
| 93 | bind(new TypeLiteral<Function<RegionNameAndIngressRules, String>>() { |
| 94 | }).to(CreateSecurityGroupIfNeeded.class); |
| 95 | bind(new TypeLiteral<Function<RegionAndName, KeyPair>>() { |
| 96 | }).to(CreateUniqueKeyPair.class); |
| 97 | bind(new TypeLiteral<Function<RegionAndName, Image>>() { |
| 98 | }).to(RegionAndIdToImage.class); |
| 99 | bind(new TypeLiteral<ComputeServiceContext>() { |
| 100 | }).to(new TypeLiteral<ComputeServiceContextImpl<EC2Client, EC2AsyncClient>>() { |
| 101 | }).in(Scopes.SINGLETON); |
| 102 | bind(new TypeLiteral<RestContext<EC2Client, EC2AsyncClient>>() { |
| 103 | }).to(new TypeLiteral<RestContextImpl<EC2Client, EC2AsyncClient>>() { |
| 104 | }).in(Scopes.SINGLETON); |
| 105 | } |
| 106 | |
| 107 | @Provides |
| 108 | @Singleton |
| 109 | Supplier<String> provideSuffix() { |
| 110 | return new Supplier<String>() { |
| 111 | final SecureRandom random = new SecureRandom(); |
| 112 | |
| 113 | @Override |
| 114 | public String get() { |
| 115 | return random.nextInt(100) + ""; |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | } |
| 120 | |
| 121 | @Provides |
| 122 | @Singleton |
| 123 | protected final Map<RegionAndName, KeyPair> credentialsMap(Function<RegionAndName, KeyPair> in) { |
| 124 | // doesn't seem to clear when someone issues remove(key) |
| 125 | // return new MapMaker().makeComputingMap(in); |
| 126 | return newLinkedHashMap(); |
| 127 | } |
| 128 | |
| 129 | @Provides |
| 130 | @Singleton |
| 131 | @Named("SECURITY") |
| 132 | protected final Map<RegionAndName, String> securityGroupMap(Function<RegionNameAndIngressRules, String> in) { |
| 133 | // doesn't seem to clear when someone issues remove(key) |
| 134 | // return new MapMaker().makeComputingMap(in); |
| 135 | return newLinkedHashMap(); |
| 136 | } |
| 137 | |
| 138 | @Provides |
| 139 | @Singleton |
| 140 | protected Map<RegionAndName, Image> provideImageMap(Function<RegionAndName, Image> regionAndIdToImage) { |
| 141 | return new MapMaker().makeComputingMap(regionAndIdToImage); |
| 142 | } |
| 143 | |
| 144 | } |