| 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.vcloud.terremark.compute.config; |
| 20 | |
| 21 | import static org.jclouds.compute.domain.OsFamily.UBUNTU; |
| 22 | |
| 23 | import java.security.SecureRandom; |
| 24 | import java.util.concurrent.ConcurrentHashMap; |
| 25 | import java.util.concurrent.ConcurrentMap; |
| 26 | |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.compute.ComputeService; |
| 31 | import org.jclouds.compute.config.BindComputeStrategiesByClass; |
| 32 | import org.jclouds.compute.config.BindComputeSuppliersByClass; |
| 33 | import org.jclouds.compute.domain.NodeMetadata; |
| 34 | import org.jclouds.compute.domain.TemplateBuilder; |
| 35 | import org.jclouds.compute.options.TemplateOptions; |
| 36 | import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy; |
| 37 | import org.jclouds.vcloud.compute.VCloudExpressComputeClient; |
| 38 | import org.jclouds.vcloud.compute.config.VCloudExpressComputeServiceContextModule; |
| 39 | import org.jclouds.vcloud.domain.VCloudExpressVApp; |
| 40 | import org.jclouds.vcloud.terremark.compute.TerremarkVCloudComputeClient; |
| 41 | import org.jclouds.vcloud.terremark.compute.TerremarkVCloudComputeService; |
| 42 | import org.jclouds.vcloud.terremark.compute.domain.KeyPairCredentials; |
| 43 | import org.jclouds.vcloud.terremark.compute.domain.OrgAndName; |
| 44 | import org.jclouds.vcloud.terremark.compute.functions.NodeMetadataToOrgAndName; |
| 45 | import org.jclouds.vcloud.terremark.compute.functions.TerremarkVCloudExpressVAppToNodeMetadata; |
| 46 | import org.jclouds.vcloud.terremark.compute.options.TerremarkVCloudTemplateOptions; |
| 47 | import org.jclouds.vcloud.terremark.compute.strategy.ParseVAppTemplateDescriptionToGetDefaultLoginCredentials; |
| 48 | |
| 49 | import com.google.common.base.Function; |
| 50 | import com.google.common.base.Supplier; |
| 51 | import com.google.inject.Injector; |
| 52 | import com.google.inject.Provides; |
| 53 | import com.google.inject.TypeLiteral; |
| 54 | |
| 55 | /** |
| 56 | * Configures the {@link TerremarkVCloudComputeServiceContext}; requires |
| 57 | * {@link TerremarkVCloudComputeClientImpl} bound. |
| 58 | * |
| 59 | * @author Adrian Cole |
| 60 | */ |
| 61 | public class TerremarkVCloudComputeServiceContextModule extends VCloudExpressComputeServiceContextModule { |
| 62 | |
| 63 | @Provides |
| 64 | @Singleton |
| 65 | Supplier<String> provideSuffix(final SecureRandom random) { |
| 66 | return new Supplier<String>() { |
| 67 | @Override |
| 68 | public String get() { |
| 69 | return random.nextInt(4096) + ""; |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | // prefer jeos as the copy time is much shorter |
| 76 | @Override |
| 77 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 78 | return template.osFamily(UBUNTU).osDescriptionMatches(".*JeOS.*").os64Bit(true); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void configure() { |
| 83 | super.configure(); |
| 84 | bind(new TypeLiteral<Function<NodeMetadata, OrgAndName>>() { |
| 85 | }).to(new TypeLiteral<NodeMetadataToOrgAndName>() { |
| 86 | }); |
| 87 | bind(TemplateOptions.class).to(TerremarkVCloudTemplateOptions.class); |
| 88 | bind(ComputeService.class).to(TerremarkVCloudComputeService.class); |
| 89 | bind(VCloudExpressComputeClient.class).to(TerremarkVCloudComputeClient.class); |
| 90 | bind(PopulateDefaultLoginCredentialsForImageStrategy.class).to( |
| 91 | ParseVAppTemplateDescriptionToGetDefaultLoginCredentials.class); |
| 92 | bind(SecureRandom.class).toInstance(new SecureRandom()); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | protected void bindVAppConverter() { |
| 97 | bind(new TypeLiteral<Function<VCloudExpressVApp, NodeMetadata>>() { |
| 98 | }).to(TerremarkVCloudExpressVAppToNodeMetadata.class); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public BindComputeStrategiesByClass defineComputeStrategyModule() { |
| 103 | return new TerremarkBindComputeStrategiesByClass(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public BindComputeSuppliersByClass defineComputeSupplierModule() { |
| 108 | return new TerremarkBindComputeSuppliersByClass(); |
| 109 | } |
| 110 | |
| 111 | @Provides |
| 112 | @Singleton |
| 113 | ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap() { |
| 114 | return new ConcurrentHashMap<OrgAndName, KeyPairCredentials>(); |
| 115 | } |
| 116 | |
| 117 | @Named("PASSWORD") |
| 118 | @Provides |
| 119 | String providePassword(SecureRandom random) { |
| 120 | return random.nextLong() + ""; |
| 121 | } |
| 122 | |
| 123 | } |