| 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.rimuhosting.miro.compute.config; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import javax.inject.Singleton; |
| 24 | |
| 25 | import org.jclouds.compute.ComputeServiceContext; |
| 26 | import org.jclouds.compute.domain.NodeMetadata; |
| 27 | import org.jclouds.compute.domain.NodeState; |
| 28 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
| 29 | import org.jclouds.rest.RestContext; |
| 30 | import org.jclouds.rest.internal.RestContextImpl; |
| 31 | import org.jclouds.rimuhosting.miro.RimuHostingAsyncClient; |
| 32 | import org.jclouds.rimuhosting.miro.RimuHostingClient; |
| 33 | import org.jclouds.rimuhosting.miro.compute.functions.ServerToNodeMetadata; |
| 34 | import org.jclouds.rimuhosting.miro.domain.Server; |
| 35 | import org.jclouds.rimuhosting.miro.domain.internal.RunningState; |
| 36 | |
| 37 | import com.google.common.annotations.VisibleForTesting; |
| 38 | import com.google.common.base.Function; |
| 39 | import com.google.common.collect.ImmutableList; |
| 40 | import com.google.common.collect.ImmutableMap; |
| 41 | import com.google.common.collect.ImmutableSet; |
| 42 | import com.google.common.collect.Iterables; |
| 43 | import com.google.inject.AbstractModule; |
| 44 | import com.google.inject.Provides; |
| 45 | import com.google.inject.Scopes; |
| 46 | import com.google.inject.TypeLiteral; |
| 47 | |
| 48 | /** |
| 49 | * Configures the {@link RimuHostingComputeServiceContext}; requires |
| 50 | * {@link RimuHostingComputeService} bound. |
| 51 | * |
| 52 | * @author Adrian Cole |
| 53 | */ |
| 54 | public class RimuHostingComputeServiceDependenciesModule extends AbstractModule { |
| 55 | |
| 56 | @Override |
| 57 | protected void configure() { |
| 58 | bind(new TypeLiteral<Function<Server, NodeMetadata>>() { |
| 59 | }).to(ServerToNodeMetadata.class); |
| 60 | bind(new TypeLiteral<ComputeServiceContext>() { |
| 61 | }).to(new TypeLiteral<ComputeServiceContextImpl<RimuHostingClient, RimuHostingAsyncClient>>() { |
| 62 | }).in(Scopes.SINGLETON); |
| 63 | bind(new TypeLiteral<RestContext<RimuHostingClient, RimuHostingAsyncClient>>() { |
| 64 | }).to(new TypeLiteral<RestContextImpl<RimuHostingClient, RimuHostingAsyncClient>>() { |
| 65 | }).in(Scopes.SINGLETON); |
| 66 | bind(new TypeLiteral<Function<Server, Iterable<String>>>() { |
| 67 | }).to(ServerToPublicAddresses.class); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | @VisibleForTesting |
| 72 | static final Map<RunningState, NodeState> runningStateToNodeState = ImmutableMap.<RunningState, NodeState> builder() |
| 73 | .put(RunningState.RUNNING, NodeState.RUNNING)// |
| 74 | .put(RunningState.NOTRUNNING, NodeState.SUSPENDED)// |
| 75 | .put(RunningState.POWERCYCLING, NodeState.PENDING)// |
| 76 | .put(RunningState.RESTARTING, NodeState.PENDING)// |
| 77 | .put(RunningState.UNRECOGNIZED, NodeState.UNRECOGNIZED)// |
| 78 | .build(); |
| 79 | |
| 80 | @Singleton |
| 81 | @Provides |
| 82 | Map<RunningState, NodeState> provideServerToNodeState() { |
| 83 | return runningStateToNodeState; |
| 84 | } |
| 85 | |
| 86 | @Singleton |
| 87 | private static class ServerToPublicAddresses implements Function<Server, Iterable<String>> { |
| 88 | @Override |
| 89 | public Iterable<String> apply(Server server) { |
| 90 | return server.getIpAddresses() == null ? ImmutableSet.<String> of() : Iterables.concat(ImmutableList.of(server |
| 91 | .getIpAddresses().getPrimaryIp()), server.getIpAddresses().getSecondaryIps()); |
| 92 | } |
| 93 | } |
| 94 | } |