| 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.deltacloud.compute.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Named; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.compute.ComputeService; |
| 33 | import org.jclouds.compute.ComputeServiceAdapter; |
| 34 | import org.jclouds.compute.domain.Template; |
| 35 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 36 | import org.jclouds.deltacloud.DeltacloudClient; |
| 37 | import org.jclouds.deltacloud.domain.HardwareProfile; |
| 38 | import org.jclouds.deltacloud.domain.Instance; |
| 39 | import org.jclouds.deltacloud.domain.PasswordAuthentication; |
| 40 | import org.jclouds.deltacloud.domain.Realm; |
| 41 | import org.jclouds.deltacloud.domain.Transition; |
| 42 | import org.jclouds.deltacloud.domain.TransitionOnAction; |
| 43 | import org.jclouds.deltacloud.domain.Instance.State; |
| 44 | import org.jclouds.deltacloud.options.CreateInstanceOptions; |
| 45 | import org.jclouds.deltacloud.predicates.InstanceFinished; |
| 46 | import org.jclouds.deltacloud.predicates.InstanceRunning; |
| 47 | import org.jclouds.domain.Credentials; |
| 48 | import org.jclouds.http.HttpRequest; |
| 49 | import org.jclouds.logging.Logger; |
| 50 | import org.jclouds.predicates.RetryablePredicate; |
| 51 | |
| 52 | import com.google.common.base.Predicate; |
| 53 | import com.google.common.collect.ImmutableMap; |
| 54 | import com.google.common.collect.ImmutableSet; |
| 55 | import com.google.common.collect.Iterables; |
| 56 | import com.google.common.collect.Multimap; |
| 57 | |
| 58 | /** |
| 59 | * defines the connection between the {@link DeltacloudClient} implementation and the jclouds |
| 60 | * {@link ComputeService} |
| 61 | * |
| 62 | */ |
| 63 | @Singleton |
| 64 | public class DeltacloudComputeServiceAdapter implements |
| 65 | ComputeServiceAdapter<Instance, HardwareProfile, org.jclouds.deltacloud.domain.Image, Realm> { |
| 66 | @Resource |
| 67 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 68 | protected Logger logger = Logger.NULL; |
| 69 | |
| 70 | private final org.jclouds.deltacloud.DeltacloudClient client; |
| 71 | private final ImmutableMap<State, Predicate<Instance>> stateChanges; |
| 72 | |
| 73 | @Inject |
| 74 | public DeltacloudComputeServiceAdapter(DeltacloudClient client) { |
| 75 | this.client = checkNotNull(client, "client"); |
| 76 | // TODO: parameterize |
| 77 | stateChanges = ImmutableMap.<Instance.State, Predicate<Instance>> of(// |
| 78 | Instance.State.RUNNING, new RetryablePredicate<Instance>(new InstanceRunning(client), 600, 1, |
| 79 | TimeUnit.SECONDS),// |
| 80 | Instance.State.FINISH, new RetryablePredicate<Instance>(new InstanceFinished(client), 30, 1, |
| 81 | TimeUnit.SECONDS)// |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Instance createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template, |
| 87 | Map<String, Credentials> credentialStore) { |
| 88 | Instance instance = client.createInstance(template.getImage().getProviderId(), CreateInstanceOptions.Builder |
| 89 | .named(name).hardwareProfile(template.getHardware().getId()).realm(template.getLocation().getId())); |
| 90 | if (instance.getAuthentication() != null && instance.getAuthentication() instanceof PasswordAuthentication) { |
| 91 | Credentials creds = PasswordAuthentication.class.cast(instance.getAuthentication()).getLoginCredentials(); |
| 92 | // store the credentials so that later functions can use them |
| 93 | credentialStore.put(instance.getHref().toASCIIString(), creds); |
| 94 | } |
| 95 | return instance; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Iterable<HardwareProfile> listHardwareProfiles() { |
| 100 | return client.listHardwareProfiles(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Iterable<org.jclouds.deltacloud.domain.Image> listImages() { |
| 105 | return client.listImages(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Iterable<Instance> listNodes() { |
| 110 | return client.listInstances(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Iterable<Realm> listLocations() { |
| 115 | return client.listRealms(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public org.jclouds.deltacloud.domain.Instance getNode(String id) { |
| 120 | return client.getInstance(URI.create(checkNotNull(id, "id"))); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void destroyNode(String id) { |
| 125 | Instance instance = getNode(id); |
| 126 | for (Transition transition : findChainTo(Instance.State.FINISH, instance.getState(), client.getInstanceStates())) { |
| 127 | instance = getNode(id); |
| 128 | if (instance == null) |
| 129 | break; |
| 130 | if (transition instanceof TransitionOnAction) { |
| 131 | client.performAction(instance.getActions().get(TransitionOnAction.class.cast(transition).getAction())); |
| 132 | } |
| 133 | Predicate<Instance> stateTester = stateChanges.get(transition.getTo()); |
| 134 | if (stateTester != null) |
| 135 | stateTester.apply(instance); |
| 136 | else |
| 137 | logger.debug(String.format("no state tester for: %s", transition)); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | Iterable<Transition> findChainTo(Instance.State desired, Instance.State currentState, |
| 142 | Multimap<Instance.State, ? extends Transition> states) { |
| 143 | for (Transition transition : states.get(currentState)) { |
| 144 | if (currentState.ordinal() >= transition.getTo().ordinal()) |
| 145 | continue; |
| 146 | if (transition.getTo() == desired) |
| 147 | return ImmutableSet.<Transition> of(transition); |
| 148 | Iterable<Transition> transitions = findChainTo(desired, transition.getTo(), states); |
| 149 | if (Iterables.size(transitions) > 0) |
| 150 | return Iterables.concat(ImmutableSet.of(transition), transitions); |
| 151 | } |
| 152 | return ImmutableSet.<Transition> of(); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void rebootNode(String id) { |
| 157 | HttpRequest rebootUri = getNode(id).getActions().get(Instance.Action.REBOOT); |
| 158 | if (rebootUri != null) { |
| 159 | client.performAction(rebootUri); |
| 160 | } else { |
| 161 | throw new UnsupportedOperationException(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void resumeNode(String id) { |
| 167 | throw new UnsupportedOperationException(); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void suspendNode(String id) { |
| 172 | throw new UnsupportedOperationException(); |
| 173 | } |
| 174 | } |