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.cloudservers.compute.config; |
20 | |
21 | import java.util.Map; |
22 | |
23 | import javax.inject.Singleton; |
24 | |
25 | import org.jclouds.cloudservers.CloudServersAsyncClient; |
26 | import org.jclouds.cloudservers.CloudServersClient; |
27 | import org.jclouds.cloudservers.compute.functions.CloudServersImageToImage; |
28 | import org.jclouds.cloudservers.compute.functions.CloudServersImageToOperatingSystem; |
29 | import org.jclouds.cloudservers.compute.functions.FlavorToHardware; |
30 | import org.jclouds.cloudservers.compute.functions.ServerToNodeMetadata; |
31 | import org.jclouds.cloudservers.domain.Flavor; |
32 | import org.jclouds.cloudservers.domain.Server; |
33 | import org.jclouds.cloudservers.domain.ServerStatus; |
34 | import org.jclouds.compute.ComputeServiceContext; |
35 | import org.jclouds.compute.domain.Hardware; |
36 | import org.jclouds.compute.domain.Image; |
37 | import org.jclouds.compute.domain.NodeMetadata; |
38 | import org.jclouds.compute.domain.NodeState; |
39 | import org.jclouds.compute.domain.OperatingSystem; |
40 | import org.jclouds.compute.internal.BaseComputeService; |
41 | import org.jclouds.compute.internal.ComputeServiceContextImpl; |
42 | import org.jclouds.rest.RestContext; |
43 | import org.jclouds.rest.internal.RestContextImpl; |
44 | |
45 | import com.google.common.annotations.VisibleForTesting; |
46 | import com.google.common.base.Function; |
47 | import com.google.common.collect.ImmutableMap; |
48 | import com.google.inject.AbstractModule; |
49 | import com.google.inject.Provides; |
50 | import com.google.inject.Scopes; |
51 | import com.google.inject.TypeLiteral; |
52 | |
53 | /** |
54 | * Configures the {@link CloudServersComputeServiceContext}; requires {@link BaseComputeService} |
55 | * bound. |
56 | * |
57 | * @author Adrian Cole |
58 | */ |
59 | public class CloudServersComputeServiceDependenciesModule extends AbstractModule { |
60 | |
61 | @Override |
62 | protected void configure() { |
63 | bind(new TypeLiteral<Function<Server, NodeMetadata>>() { |
64 | }).to(ServerToNodeMetadata.class); |
65 | |
66 | bind(new TypeLiteral<Function<org.jclouds.cloudservers.domain.Image, Image>>() { |
67 | }).to(CloudServersImageToImage.class); |
68 | |
69 | bind(new TypeLiteral<Function<org.jclouds.cloudservers.domain.Image, OperatingSystem>>() { |
70 | }).to(CloudServersImageToOperatingSystem.class); |
71 | |
72 | bind(new TypeLiteral<Function<Flavor, Hardware>>() { |
73 | }).to(FlavorToHardware.class); |
74 | |
75 | bind(new TypeLiteral<ComputeServiceContext>() { |
76 | }).to(new TypeLiteral<ComputeServiceContextImpl<CloudServersClient, CloudServersAsyncClient>>() { |
77 | }).in(Scopes.SINGLETON); |
78 | bind(new TypeLiteral<RestContext<CloudServersClient, CloudServersAsyncClient>>() { |
79 | }).to(new TypeLiteral<RestContextImpl<CloudServersClient, CloudServersAsyncClient>>() { |
80 | }).in(Scopes.SINGLETON); |
81 | } |
82 | |
83 | @VisibleForTesting |
84 | public static final Map<ServerStatus, NodeState> serverToNodeState = ImmutableMap |
85 | .<ServerStatus, NodeState> builder().put(ServerStatus.ACTIVE, NodeState.RUNNING)// |
86 | .put(ServerStatus.SUSPENDED, NodeState.SUSPENDED)// |
87 | .put(ServerStatus.DELETED, NodeState.TERMINATED)// |
88 | .put(ServerStatus.QUEUE_RESIZE, NodeState.PENDING)// |
89 | .put(ServerStatus.PREP_RESIZE, NodeState.PENDING)// |
90 | .put(ServerStatus.RESIZE, NodeState.PENDING)// |
91 | .put(ServerStatus.VERIFY_RESIZE, NodeState.PENDING)// |
92 | .put(ServerStatus.QUEUE_MOVE, NodeState.PENDING)// |
93 | .put(ServerStatus.PREP_MOVE, NodeState.PENDING)// |
94 | .put(ServerStatus.MOVE, NodeState.PENDING)// |
95 | .put(ServerStatus.VERIFY_MOVE, NodeState.PENDING)// |
96 | .put(ServerStatus.RESCUE, NodeState.PENDING)// |
97 | .put(ServerStatus.ERROR, NodeState.ERROR)// |
98 | .put(ServerStatus.BUILD, NodeState.PENDING)// |
99 | .put(ServerStatus.RESTORING, NodeState.PENDING)// |
100 | .put(ServerStatus.PASSWORD, NodeState.PENDING)// |
101 | .put(ServerStatus.REBUILD, NodeState.PENDING)// |
102 | .put(ServerStatus.DELETE_IP, NodeState.PENDING)// |
103 | .put(ServerStatus.SHARE_IP_NO_CONFIG, NodeState.PENDING)// |
104 | .put(ServerStatus.SHARE_IP, NodeState.PENDING)// |
105 | .put(ServerStatus.REBOOT, NodeState.PENDING)// |
106 | .put(ServerStatus.HARD_REBOOT, NodeState.PENDING)// |
107 | .put(ServerStatus.UNKNOWN, NodeState.UNRECOGNIZED)// |
108 | .put(ServerStatus.UNRECOGNIZED, NodeState.UNRECOGNIZED).build(); |
109 | |
110 | @Singleton |
111 | @Provides |
112 | Map<ServerStatus, NodeState> provideServerToNodeState() { |
113 | return serverToNodeState; |
114 | } |
115 | |
116 | } |