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.strategy; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.Map; |
24 | |
25 | import javax.inject.Inject; |
26 | import javax.inject.Singleton; |
27 | |
28 | import org.jclouds.compute.domain.NodeMetadata; |
29 | import org.jclouds.compute.domain.Template; |
30 | import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; |
31 | import org.jclouds.domain.Credentials; |
32 | import org.jclouds.cloudservers.CloudServersClient; |
33 | import org.jclouds.cloudservers.domain.Server; |
34 | |
35 | import com.google.common.base.Function; |
36 | |
37 | /** |
38 | * @author Adrian Cole |
39 | */ |
40 | @Singleton |
41 | public class CloudServersCreateNodeWithGroupEncodedIntoName implements CreateNodeWithGroupEncodedIntoName { |
42 | protected final CloudServersClient client; |
43 | protected final Map<String, Credentials> credentialStore; |
44 | protected final Function<Server, NodeMetadata> serverToNodeMetadata; |
45 | |
46 | @Inject |
47 | protected CloudServersCreateNodeWithGroupEncodedIntoName(CloudServersClient client, Map<String, Credentials> credentialStore, |
48 | Function<Server, NodeMetadata> serverToNodeMetadata) { |
49 | this.client = checkNotNull(client, "client"); |
50 | this.credentialStore = checkNotNull(credentialStore, "credentialStore"); |
51 | this.serverToNodeMetadata = checkNotNull(serverToNodeMetadata, "serverToNodeMetadata"); |
52 | } |
53 | |
54 | @Override |
55 | public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) { |
56 | Server from = client.createServer(name, Integer.parseInt(template.getImage().getProviderId()), Integer |
57 | .parseInt(template.getHardware().getProviderId())); |
58 | credentialStore.put("node#" + from.getId(), new Credentials("root", from.getAdminPass())); |
59 | return serverToNodeMetadata.apply(from); |
60 | } |
61 | |
62 | } |