| 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.vcloud.compute.strategy; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.compute.domain.NodeMetadata; |
| 31 | import org.jclouds.compute.reference.ComputeServiceConstants; |
| 32 | import org.jclouds.compute.strategy.DestroyNodeStrategy; |
| 33 | import org.jclouds.compute.strategy.GetNodeMetadataStrategy; |
| 34 | import org.jclouds.logging.Logger; |
| 35 | import org.jclouds.rest.AuthorizationException; |
| 36 | import org.jclouds.vcloud.VCloudClient; |
| 37 | import org.jclouds.vcloud.domain.Status; |
| 38 | import org.jclouds.vcloud.domain.Task; |
| 39 | import org.jclouds.vcloud.domain.VApp; |
| 40 | |
| 41 | import com.google.common.base.Predicate; |
| 42 | |
| 43 | /** |
| 44 | * @author Adrian Cole |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class VCloudDestroyNodeStrategy implements DestroyNodeStrategy { |
| 48 | @Resource |
| 49 | @Named(ComputeServiceConstants.COMPUTE_LOGGER) |
| 50 | protected Logger logger = Logger.NULL; |
| 51 | |
| 52 | protected final Predicate<URI> successTester; |
| 53 | protected final VCloudClient client; |
| 54 | protected final GetNodeMetadataStrategy getNode; |
| 55 | |
| 56 | @Inject |
| 57 | protected VCloudDestroyNodeStrategy(Predicate<URI> successTester, VCloudClient client, |
| 58 | GetNodeMetadataStrategy getNode) { |
| 59 | this.successTester = successTester; |
| 60 | this.client = client; |
| 61 | this.getNode = getNode; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public NodeMetadata destroyNode(String id) { |
| 66 | URI vappId = URI.create(checkNotNull(id, "node.id")); |
| 67 | VApp vApp = client.getVAppClient().getVApp(vappId); |
| 68 | if (vApp == null) |
| 69 | return null; |
| 70 | |
| 71 | waitForPendingTasksToComplete(vApp); |
| 72 | |
| 73 | vApp = undeployVAppIfDeployed(vApp); |
| 74 | deleteVApp(vApp); |
| 75 | try { |
| 76 | return getNode.getNode(id); |
| 77 | } catch (AuthorizationException e) { |
| 78 | // vcloud bug will sometimes throw an exception getting the vapp right after deleting it. |
| 79 | logger.trace("authorization error getting %s after deletion: %s", id, e.getMessage()); |
| 80 | return null; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void waitForPendingTasksToComplete(VApp vApp) { |
| 85 | for (Task task : vApp.getTasks()) |
| 86 | waitForTask(task, vApp); |
| 87 | } |
| 88 | |
| 89 | public void waitForTask(Task task, VApp vAppResponse) { |
| 90 | if (!successTester.apply(task.getHref())) { |
| 91 | throw new RuntimeException(String.format("failed to %s %s: %s", task.getName(), vAppResponse.getName(), task)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void deleteVApp(VApp vApp) { |
| 96 | logger.debug(">> deleting vApp(%s)", vApp.getHref()); |
| 97 | waitForTask(client.getVAppClient().deleteVApp(vApp.getHref()), vApp); |
| 98 | logger.debug("<< deleted vApp(%s)", vApp.getHref()); |
| 99 | } |
| 100 | |
| 101 | VApp undeployVAppIfDeployed(VApp vApp) { |
| 102 | if (vApp.getStatus() != Status.OFF) { |
| 103 | logger.debug(">> undeploying vApp(%s), current status: %s", vApp.getName(), vApp.getStatus()); |
| 104 | try { |
| 105 | waitForTask(client.getVAppClient().undeployVApp(vApp.getHref()), vApp); |
| 106 | vApp = client.getVAppClient().getVApp(vApp.getHref()); |
| 107 | logger.debug("<< %s vApp(%s)", vApp.getStatus(), vApp.getName()); |
| 108 | } catch (IllegalStateException e) { |
| 109 | logger.warn(e, "<< %s vApp(%s)", vApp.getStatus(), vApp.getName()); |
| 110 | } |
| 111 | } |
| 112 | return vApp; |
| 113 | } |
| 114 | } |