| 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.trmk.vcloud_0_8.compute.config; |
| 20 | |
| 21 | import java.security.SecureRandom; |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.ConcurrentMap; |
| 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.config.BaseComputeServiceContextModule; |
| 32 | import org.jclouds.compute.domain.Image; |
| 33 | import org.jclouds.compute.domain.NodeMetadata; |
| 34 | import org.jclouds.compute.domain.NodeState; |
| 35 | import org.jclouds.compute.domain.OperatingSystem; |
| 36 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
| 37 | import org.jclouds.compute.options.TemplateOptions; |
| 38 | import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy; |
| 39 | import org.jclouds.rest.RestContext; |
| 40 | import org.jclouds.rest.internal.RestContextImpl; |
| 41 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudAsyncClient; |
| 42 | import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudClient; |
| 43 | import org.jclouds.trmk.vcloud_0_8.compute.TerremarkVCloudComputeService; |
| 44 | import org.jclouds.trmk.vcloud_0_8.compute.domain.KeyPairCredentials; |
| 45 | import org.jclouds.trmk.vcloud_0_8.compute.domain.OrgAndName; |
| 46 | import org.jclouds.trmk.vcloud_0_8.compute.functions.ImagesInVCloudExpressOrg; |
| 47 | import org.jclouds.trmk.vcloud_0_8.compute.functions.NodeMetadataToOrgAndName; |
| 48 | import org.jclouds.trmk.vcloud_0_8.compute.functions.ParseOsFromVAppTemplateName; |
| 49 | import org.jclouds.trmk.vcloud_0_8.compute.functions.VAppToNodeMetadata; |
| 50 | import org.jclouds.trmk.vcloud_0_8.compute.options.TerremarkVCloudTemplateOptions; |
| 51 | import org.jclouds.trmk.vcloud_0_8.compute.strategy.ParseVAppTemplateDescriptionToGetDefaultLoginCredentials; |
| 52 | import org.jclouds.trmk.vcloud_0_8.domain.Org; |
| 53 | import org.jclouds.trmk.vcloud_0_8.domain.Status; |
| 54 | import org.jclouds.trmk.vcloud_0_8.domain.VApp; |
| 55 | |
| 56 | import com.google.common.annotations.VisibleForTesting; |
| 57 | import com.google.common.base.Function; |
| 58 | import com.google.common.base.Supplier; |
| 59 | import com.google.common.collect.ImmutableMap; |
| 60 | import com.google.inject.Provides; |
| 61 | import com.google.inject.Scopes; |
| 62 | import com.google.inject.TypeLiteral; |
| 63 | |
| 64 | /** |
| 65 | * Configures the {@link TerremarkVCloudComputeServiceContext}; requires |
| 66 | * {@link TerremarkVCloudComputeClientImpl} bound. |
| 67 | * |
| 68 | * @author Adrian Cole |
| 69 | */ |
| 70 | public class TerremarkVCloudComputeServiceContextModule extends BaseComputeServiceContextModule { |
| 71 | |
| 72 | @VisibleForTesting |
| 73 | public static final Map<Status, NodeState> VAPPSTATUS_TO_NODESTATE = ImmutableMap.<Status, NodeState> builder() |
| 74 | .put(Status.OFF, NodeState.SUSPENDED).put(Status.ON, NodeState.RUNNING) |
| 75 | .put(Status.RESOLVED, NodeState.PENDING).put(Status.UNRECOGNIZED, NodeState.UNRECOGNIZED) |
| 76 | .put(Status.DEPLOYED, NodeState.PENDING).put(Status.SUSPENDED, NodeState.SUSPENDED) |
| 77 | .put(Status.UNRESOLVED, NodeState.PENDING).build(); |
| 78 | |
| 79 | @Singleton |
| 80 | @Provides |
| 81 | protected Map<Status, NodeState> provideVAppStatusToNodeState() { |
| 82 | return VAPPSTATUS_TO_NODESTATE; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | protected void configure() { |
| 87 | super.configure(); |
| 88 | bind(new TypeLiteral<Function<NodeMetadata, OrgAndName>>() { |
| 89 | }).to(new TypeLiteral<NodeMetadataToOrgAndName>() { |
| 90 | }); |
| 91 | bind(TemplateOptions.class).to(TerremarkVCloudTemplateOptions.class); |
| 92 | bind(ComputeService.class).to(TerremarkVCloudComputeService.class); |
| 93 | bind(PopulateDefaultLoginCredentialsForImageStrategy.class).to( |
| 94 | ParseVAppTemplateDescriptionToGetDefaultLoginCredentials.class); |
| 95 | bind(SecureRandom.class).toInstance(new SecureRandom()); |
| 96 | install(new TerremarkBindComputeStrategiesByClass()); |
| 97 | install(new TerremarkBindComputeSuppliersByClass()); |
| 98 | bindVAppConverter(); |
| 99 | bind(new TypeLiteral<ComputeServiceContext>() { |
| 100 | }).to(new TypeLiteral<ComputeServiceContextImpl<TerremarkVCloudClient, TerremarkVCloudAsyncClient>>() { |
| 101 | }).in(Scopes.SINGLETON); |
| 102 | bind(new TypeLiteral<RestContext<TerremarkVCloudClient, TerremarkVCloudAsyncClient>>() { |
| 103 | }).to(new TypeLiteral<RestContextImpl<TerremarkVCloudClient, TerremarkVCloudAsyncClient>>() { |
| 104 | }).in(Scopes.SINGLETON); |
| 105 | bind(new TypeLiteral<Function<Org, Iterable<? extends Image>>>() { |
| 106 | }).to(new TypeLiteral<ImagesInVCloudExpressOrg>() { |
| 107 | }); |
| 108 | bind(new TypeLiteral<Function<String, OperatingSystem>>() { |
| 109 | }).to(ParseOsFromVAppTemplateName.class); |
| 110 | } |
| 111 | |
| 112 | protected void bindVAppConverter() { |
| 113 | bind(new TypeLiteral<Function<VApp, NodeMetadata>>() { |
| 114 | }).to(VAppToNodeMetadata.class); |
| 115 | } |
| 116 | |
| 117 | @Provides |
| 118 | @Singleton |
| 119 | Supplier<String> provideSuffix(final SecureRandom random) { |
| 120 | return new Supplier<String>() { |
| 121 | @Override |
| 122 | public String get() { |
| 123 | return random.nextInt(4096) + ""; |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | } |
| 128 | |
| 129 | @Provides |
| 130 | @Singleton |
| 131 | ConcurrentMap<OrgAndName, KeyPairCredentials> credentialsMap() { |
| 132 | return new ConcurrentHashMap<OrgAndName, KeyPairCredentials>(); |
| 133 | } |
| 134 | |
| 135 | @Named("PASSWORD") |
| 136 | @Provides |
| 137 | String providePassword(SecureRandom random) { |
| 138 | return random.nextLong() + ""; |
| 139 | } |
| 140 | |
| 141 | } |