| 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.gogrid.compute.config; |
| 20 | |
| 21 | import static org.jclouds.compute.util.ComputeServiceUtils.*; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.compute.ComputeService; |
| 28 | import org.jclouds.compute.ComputeServiceContext; |
| 29 | import org.jclouds.compute.domain.Hardware; |
| 30 | import org.jclouds.compute.domain.NodeMetadata; |
| 31 | import org.jclouds.compute.domain.NodeState; |
| 32 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
| 33 | import org.jclouds.compute.options.TemplateOptions; |
| 34 | import org.jclouds.gogrid.GoGridAsyncClient; |
| 35 | import org.jclouds.gogrid.GoGridClient; |
| 36 | import org.jclouds.gogrid.compute.GoGridComputeService; |
| 37 | import org.jclouds.gogrid.compute.functions.ServerToNodeMetadata; |
| 38 | import org.jclouds.gogrid.compute.options.GoGridTemplateOptions; |
| 39 | import org.jclouds.gogrid.domain.Server; |
| 40 | import org.jclouds.gogrid.domain.ServerState; |
| 41 | import org.jclouds.rest.RestContext; |
| 42 | import org.jclouds.rest.internal.RestContextImpl; |
| 43 | |
| 44 | import com.google.common.annotations.VisibleForTesting; |
| 45 | import com.google.common.base.Function; |
| 46 | import com.google.common.collect.ImmutableMap; |
| 47 | import com.google.inject.AbstractModule; |
| 48 | import com.google.inject.Provides; |
| 49 | import com.google.inject.Scopes; |
| 50 | import com.google.inject.TypeLiteral; |
| 51 | |
| 52 | /** |
| 53 | * @author Oleksiy Yarmula |
| 54 | * @author Adrian Cole |
| 55 | * @author Andrew Kennedy |
| 56 | */ |
| 57 | public class GoGridComputeServiceDependenciesModule extends AbstractModule { |
| 58 | protected void configure() { |
| 59 | bind(TemplateOptions.class).to(GoGridTemplateOptions.class); |
| 60 | bind(ComputeService.class).to(GoGridComputeService.class); |
| 61 | bind(new TypeLiteral<Function<Server, NodeMetadata>>() { |
| 62 | }).to(ServerToNodeMetadata.class); |
| 63 | bind(new TypeLiteral<ComputeServiceContext>() { |
| 64 | }).to(new TypeLiteral<ComputeServiceContextImpl<GoGridClient, GoGridAsyncClient>>() { |
| 65 | }).in(Scopes.SINGLETON); |
| 66 | bind(new TypeLiteral<RestContext<GoGridClient, GoGridAsyncClient>>() { |
| 67 | }).to(new TypeLiteral<RestContextImpl<GoGridClient, GoGridAsyncClient>>() { |
| 68 | }).in(Scopes.SINGLETON); |
| 69 | } |
| 70 | |
| 71 | @VisibleForTesting |
| 72 | static final Map<ServerState, NodeState> serverStateToNodeState = ImmutableMap.<ServerState, NodeState> builder() |
| 73 | .put(ServerState.ON, NodeState.RUNNING)// |
| 74 | .put(ServerState.STARTING, NodeState.PENDING)// |
| 75 | .put(ServerState.OFF, NodeState.SUSPENDED)// |
| 76 | .put(ServerState.STOPPING, NodeState.PENDING)// |
| 77 | .put(ServerState.RESTARTING, NodeState.PENDING)// |
| 78 | .put(ServerState.SAVING, NodeState.PENDING)// |
| 79 | .put(ServerState.UNRECOGNIZED, NodeState.UNRECOGNIZED)// |
| 80 | .put(ServerState.RESTORING, NodeState.PENDING)// |
| 81 | .put(ServerState.UPDATING, NodeState.PENDING).build(); |
| 82 | |
| 83 | @Singleton |
| 84 | @Provides |
| 85 | Map<ServerState, NodeState> provideServerToNodeState() { |
| 86 | return serverStateToNodeState; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Finds matches to required configurations. GoGrid's documentation only specifies how much RAM |
| 91 | * one can get with different instance types. The # of cores and disk sizes are purely empyrical |
| 92 | * and aren't guaranteed. However, these are the matches found: Ram: 512MB, CPU: 1 core, HDD: 28 |
| 93 | * GB Ram: 1GB, CPU: 1 core, HDD: 57 GB Ram: 2GB, CPU: 1 core, HDD: 113 GB Ram: 4GB, CPU: 3 |
| 94 | * cores, HDD: 233 GB Ram: 8GB, CPU: 6 cores, HDD: 462 GB (as of March 2010) |
| 95 | * |
| 96 | * @return matched size |
| 97 | */ |
| 98 | @Singleton |
| 99 | @Provides |
| 100 | Function<Hardware, String> provideSizeToRam() { |
| 101 | return new Function<Hardware, String>() { |
| 102 | @Override |
| 103 | public String apply(Hardware hardware) { |
| 104 | if (hardware.getRam() >= 8 * 1024 || getCores(hardware) >= 6 || getSpace(hardware) >= 450) |
| 105 | return "8GB"; |
| 106 | if (hardware.getRam() >= 4 * 1024 || getCores(hardware) >= 3 || getSpace(hardware) >= 230) |
| 107 | return "4GB"; |
| 108 | if (hardware.getRam() >= 2 * 1024 || getSpace(hardware) >= 110) |
| 109 | return "2GB"; |
| 110 | if (hardware.getRam() >= 1024 || getSpace(hardware) >= 55) |
| 111 | return "1GB"; |
| 112 | return "512MB"; /* smallest */ |
| 113 | } |
| 114 | }; |
| 115 | } |
| 116 | } |