| 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.compute.strategy.impl; | 
| 20 |   | 
| 21 | import static com.google.common.base.Preconditions.checkNotNull; | 
| 22 | import static com.google.common.base.Preconditions.checkState; | 
| 23 |   | 
| 24 | import java.util.Map; | 
| 25 |   | 
| 26 | import javax.annotation.Resource; | 
| 27 | import javax.inject.Inject; | 
| 28 | import javax.inject.Named; | 
| 29 | import javax.inject.Singleton; | 
| 30 |   | 
| 31 | import org.jclouds.compute.ComputeServiceAdapter; | 
| 32 | import org.jclouds.compute.domain.ComputeMetadata; | 
| 33 | import org.jclouds.compute.domain.NodeMetadata; | 
| 34 | import org.jclouds.compute.domain.NodeState; | 
| 35 | import org.jclouds.compute.domain.Template; | 
| 36 | import org.jclouds.compute.predicates.NodePredicates; | 
| 37 | import org.jclouds.compute.reference.ComputeServiceConstants; | 
| 38 | import org.jclouds.compute.strategy.CreateNodeWithGroupEncodedIntoName; | 
| 39 | import org.jclouds.compute.strategy.DestroyNodeStrategy; | 
| 40 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; | 
| 41 | import org.jclouds.compute.strategy.ListNodesStrategy; | 
| 42 | import org.jclouds.compute.strategy.RebootNodeStrategy; | 
| 43 | import org.jclouds.compute.strategy.ResumeNodeStrategy; | 
| 44 | import org.jclouds.compute.strategy.SuspendNodeStrategy; | 
| 45 | import org.jclouds.domain.Credentials; | 
| 46 | import org.jclouds.logging.Logger; | 
| 47 |   | 
| 48 | import com.google.common.base.Function; | 
| 49 | import com.google.common.base.Predicate; | 
| 50 | import com.google.common.collect.Iterables; | 
| 51 |   | 
| 52 | /** | 
| 53 |  * @author Adrian Cole | 
| 54 |  *  | 
| 55 |  */ | 
| 56 | @Singleton | 
| 57 | public class AdaptingComputeServiceStrategies<N, H, I, L> implements CreateNodeWithGroupEncodedIntoName, DestroyNodeStrategy, | 
| 58 |          GetNodeMetadataStrategy, ListNodesStrategy, RebootNodeStrategy, ResumeNodeStrategy, SuspendNodeStrategy { | 
| 59 |    @Resource | 
| 60 |    @Named(ComputeServiceConstants.COMPUTE_LOGGER) | 
| 61 |    protected Logger logger = Logger.NULL; | 
| 62 |   | 
| 63 |    private final Map<String, Credentials> credentialStore; | 
| 64 |    private final ComputeServiceAdapter<N, H, I, L> client; | 
| 65 |    private final Function<N, NodeMetadata> nodeMetadataAdapter; | 
| 66 |   | 
| 67 |    @Inject | 
| 68 |    public AdaptingComputeServiceStrategies(Map<String, Credentials> credentialStore, | 
| 69 |             ComputeServiceAdapter<N, H, I, L> client, Function<N, NodeMetadata> nodeMetadataAdapter) { | 
| 70 |       this.credentialStore = checkNotNull(credentialStore, "credentialStore"); | 
| 71 |       this.client = checkNotNull(client, "client"); | 
| 72 |       this.nodeMetadataAdapter = checkNotNull(nodeMetadataAdapter, "nodeMetadataAdapter"); | 
| 73 |    } | 
| 74 |   | 
| 75 |    @Override | 
| 76 |    public Iterable<? extends ComputeMetadata> listNodes() { | 
| 77 |       return listDetailsOnNodesMatching(NodePredicates.all()); | 
| 78 |    } | 
| 79 |   | 
| 80 |    @Override | 
| 81 |    public Iterable<? extends NodeMetadata> listDetailsOnNodesMatching(Predicate<ComputeMetadata> filter) { | 
| 82 |       return Iterables.filter(Iterables.transform(client.listNodes(), nodeMetadataAdapter), filter); | 
| 83 |    } | 
| 84 |   | 
| 85 |    @Override | 
| 86 |    public NodeMetadata getNode(String id) { | 
| 87 |       N node = client.getNode(checkNotNull(id, "id")); | 
| 88 |       return node == null ? null : nodeMetadataAdapter.apply(node); | 
| 89 |    } | 
| 90 |   | 
| 91 |    @Override | 
| 92 |    public NodeMetadata rebootNode(String id) { | 
| 93 |       NodeMetadata node = getNode(checkNotNull(id, "id")); | 
| 94 |       if (node == null || node.getState() == NodeState.TERMINATED) | 
| 95 |          return node; | 
| 96 |       client.rebootNode(id); | 
| 97 |       return node; | 
| 98 |    } | 
| 99 |   | 
| 100 |    @Override | 
| 101 |    public NodeMetadata resumeNode(String id) { | 
| 102 |       NodeMetadata node = getNode(checkNotNull(id, "id")); | 
| 103 |       if (node == null || node.getState() == NodeState.TERMINATED || node.getState() == NodeState.RUNNING) | 
| 104 |          return node; | 
| 105 |       client.resumeNode(id); | 
| 106 |       return node; | 
| 107 |    } | 
| 108 |   | 
| 109 |    @Override | 
| 110 |    public NodeMetadata suspendNode(String id) { | 
| 111 |       NodeMetadata node = getNode(checkNotNull(id, "id")); | 
| 112 |       if (node == null || node.getState() == NodeState.TERMINATED || node.getState() == NodeState.SUSPENDED) | 
| 113 |          return node; | 
| 114 |       client.suspendNode(id); | 
| 115 |       return node; | 
| 116 |    } | 
| 117 |   | 
| 118 |    @Override | 
| 119 |    public NodeMetadata destroyNode(String id) { | 
| 120 |       NodeMetadata node = getNode(checkNotNull(id, "id")); | 
| 121 |       if (node == null) | 
| 122 |          return node; | 
| 123 |       client.destroyNode(id); | 
| 124 |       return node; | 
| 125 |    } | 
| 126 |   | 
| 127 |    /** | 
| 128 |     * {@inheritDoc} | 
| 129 |     */ | 
| 130 |    @Override | 
| 131 |    public NodeMetadata createNodeWithGroupEncodedIntoName(String group, String name, Template template) { | 
| 132 |       checkState(group != null, "group (that which groups identical nodes together) must be specified"); | 
| 133 |       checkState(name != null && name.indexOf(group) != -1, "name should have %s encoded into it", group); | 
| 134 |       checkState(template != null, "template must be specified"); | 
| 135 |   | 
| 136 |       N from = client.createNodeWithGroupEncodedIntoNameThenStoreCredentials(group, name, template, credentialStore); | 
| 137 |       NodeMetadata node = nodeMetadataAdapter.apply(from); | 
| 138 |       return node; | 
| 139 |    } | 
| 140 |   | 
| 141 | } |