| 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.vcloud.compute.config; |
| 20 | |
| 21 | |
| 22 | import static org.jclouds.compute.domain.OsFamily.UBUNTU; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.compute.ComputeServiceContext; |
| 29 | import org.jclouds.compute.config.BaseComputeServiceContextModule; |
| 30 | import org.jclouds.compute.config.BindComputeStrategiesByClass; |
| 31 | import org.jclouds.compute.config.BindComputeSuppliersByClass; |
| 32 | import org.jclouds.compute.domain.Hardware; |
| 33 | import org.jclouds.compute.domain.Image; |
| 34 | import org.jclouds.compute.domain.NodeMetadata; |
| 35 | import org.jclouds.compute.domain.NodeState; |
| 36 | import org.jclouds.compute.domain.TemplateBuilder; |
| 37 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
| 38 | import org.jclouds.compute.options.TemplateOptions; |
| 39 | import org.jclouds.compute.strategy.PopulateDefaultLoginCredentialsForImageStrategy; |
| 40 | import org.jclouds.rest.RestContext; |
| 41 | import org.jclouds.rest.internal.RestContextImpl; |
| 42 | import org.jclouds.vcloud.VCloudClient; |
| 43 | import org.jclouds.vcloud.compute.functions.HardwareForVApp; |
| 44 | import org.jclouds.vcloud.compute.functions.HardwareInOrg; |
| 45 | import org.jclouds.vcloud.compute.functions.ImagesInOrg; |
| 46 | import org.jclouds.vcloud.compute.functions.VAppToNodeMetadata; |
| 47 | import org.jclouds.vcloud.compute.internal.VCloudTemplateBuilderImpl; |
| 48 | import org.jclouds.vcloud.compute.options.VCloudTemplateOptions; |
| 49 | import org.jclouds.vcloud.compute.strategy.GetLoginCredentialsFromGuestConfiguration; |
| 50 | import org.jclouds.vcloud.domain.Org; |
| 51 | import org.jclouds.vcloud.domain.Status; |
| 52 | import org.jclouds.vcloud.domain.VApp; |
| 53 | |
| 54 | import com.google.common.annotations.VisibleForTesting; |
| 55 | import com.google.common.base.Function; |
| 56 | import com.google.common.collect.ImmutableMap; |
| 57 | import com.google.inject.Injector; |
| 58 | import com.google.inject.Provides; |
| 59 | import com.google.inject.Scopes; |
| 60 | import com.google.inject.TypeLiteral; |
| 61 | |
| 62 | /** |
| 63 | * Configures the {@link VCloudComputeServiceContext}; requires {@link VCloudComputeClientImpl} |
| 64 | * bound. |
| 65 | * |
| 66 | * @author Adrian Cole |
| 67 | */ |
| 68 | public class VCloudComputeServiceContextModule extends BaseComputeServiceContextModule { |
| 69 | |
| 70 | @VisibleForTesting |
| 71 | public static final Map<Status, NodeState> VAPPSTATUS_TO_NODESTATE = ImmutableMap.<Status, NodeState> builder() |
| 72 | .put(Status.OFF, NodeState.SUSPENDED).put(Status.ON, NodeState.RUNNING) |
| 73 | .put(Status.RESOLVED, NodeState.PENDING).put(Status.ERROR, NodeState.ERROR) |
| 74 | .put(Status.UNRECOGNIZED, NodeState.UNRECOGNIZED).put(Status.DEPLOYED, NodeState.PENDING) |
| 75 | .put(Status.INCONSISTENT, NodeState.PENDING).put(Status.UNKNOWN, NodeState.UNRECOGNIZED) |
| 76 | .put(Status.MIXED, NodeState.PENDING).put(Status.WAITING_FOR_INPUT, NodeState.PENDING) |
| 77 | .put(Status.SUSPENDED, NodeState.SUSPENDED).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 | install(defineComputeStrategyModule()); |
| 89 | install(defineComputeSupplierModule()); |
| 90 | bind(new TypeLiteral<Function<VApp, NodeMetadata>>() { |
| 91 | }).to(VAppToNodeMetadata.class); |
| 92 | bind(TemplateOptions.class).to(VCloudTemplateOptions.class); |
| 93 | bind(TemplateBuilder.class).to(VCloudTemplateBuilderImpl.class); |
| 94 | bind(new TypeLiteral<Function<VApp, Hardware>>() { |
| 95 | }).to(new TypeLiteral<HardwareForVApp>() { |
| 96 | }); |
| 97 | bind(new TypeLiteral<ComputeServiceContext>() { |
| 98 | }).to(new TypeLiteral<ComputeServiceContextImpl<VCloudClient, VCloudClient>>() { |
| 99 | }).in(Scopes.SINGLETON); |
| 100 | bind(new TypeLiteral<RestContext<VCloudClient, VCloudClient>>() { |
| 101 | }).to(new TypeLiteral<RestContextImpl<VCloudClient, VCloudClient>>() { |
| 102 | }).in(Scopes.SINGLETON); |
| 103 | bind(new TypeLiteral<Function<Org, Iterable<? extends Image>>>() { |
| 104 | }).to(new TypeLiteral<ImagesInOrg>() { |
| 105 | }); |
| 106 | bind(new TypeLiteral<Function<Org, Iterable<? extends Hardware>>>() { |
| 107 | }).to(new TypeLiteral<HardwareInOrg>() { |
| 108 | }); |
| 109 | bind(PopulateDefaultLoginCredentialsForImageStrategy.class).to(GetLoginCredentialsFromGuestConfiguration.class); |
| 110 | } |
| 111 | |
| 112 | //CIM ostype does not include version info |
| 113 | @Override |
| 114 | protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) { |
| 115 | return template.osFamily(UBUNTU).os64Bit(true); |
| 116 | } |
| 117 | |
| 118 | public BindComputeStrategiesByClass defineComputeStrategyModule() { |
| 119 | return new VCloudBindComputeStrategiesByClass(); |
| 120 | } |
| 121 | |
| 122 | public BindComputeSuppliersByClass defineComputeSupplierModule() { |
| 123 | return new VCloudBindComputeSuppliersByClass(); |
| 124 | } |
| 125 | } |