| 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.vcloud.compute.internal; |
| 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.Set; |
| 26 | |
| 27 | import javax.annotation.Nullable; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.compute.domain.NodeState; |
| 31 | import org.jclouds.vcloud.VCloudExpressClient; |
| 32 | import org.jclouds.vcloud.compute.VCloudExpressComputeClient; |
| 33 | import org.jclouds.vcloud.domain.Status; |
| 34 | import org.jclouds.vcloud.domain.Task; |
| 35 | import org.jclouds.vcloud.domain.VCloudExpressVApp; |
| 36 | import org.jclouds.vcloud.domain.VCloudExpressVAppTemplate; |
| 37 | import org.jclouds.vcloud.options.InstantiateVAppTemplateOptions; |
| 38 | |
| 39 | import com.google.common.base.Predicate; |
| 40 | import com.google.common.collect.ImmutableSet; |
| 41 | import com.google.common.collect.Sets; |
| 42 | import com.google.inject.Inject; |
| 43 | |
| 44 | /** |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | @Singleton |
| 48 | public class VCloudExpressComputeClientImpl extends |
| 49 | CommonVCloudComputeClientImpl<VCloudExpressVAppTemplate, VCloudExpressVApp> implements VCloudExpressComputeClient { |
| 50 | |
| 51 | protected final Map<Status, NodeState> vAppStatusToNodeState; |
| 52 | |
| 53 | @Inject |
| 54 | public VCloudExpressComputeClientImpl(VCloudExpressClient client, Predicate<URI> successTester, |
| 55 | Map<Status, NodeState> vAppStatusToNodeState) { |
| 56 | super(client, successTester); |
| 57 | this.vAppStatusToNodeState = vAppStatusToNodeState; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void deleteVApp(VCloudExpressVApp vApp) { |
| 62 | logger.debug(">> deleting vApp(%s)", vApp.getName()); |
| 63 | Task task = VCloudExpressClient.class.cast(client).deleteVApp(vApp.getHref()); |
| 64 | if (task != null) |
| 65 | if (!taskTester.apply(task.getHref())) |
| 66 | throw new RuntimeException(String.format("failed to %s %s: %s", "delete", vApp.getName(), task)); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public VCloudExpressVApp start(@Nullable URI VDC, URI templateId, String name, |
| 71 | InstantiateVAppTemplateOptions options, int... portsToOpen) { |
| 72 | checkNotNull(options, "options"); |
| 73 | logger.debug(">> instantiating vApp vDC(%s) template(%s) name(%s) options(%s) ", VDC, templateId, name, options); |
| 74 | |
| 75 | VCloudExpressVApp vAppResponse = VCloudExpressClient.class.cast(client).instantiateVAppTemplateInVDC(VDC, |
| 76 | templateId, name, options); |
| 77 | logger.debug("<< instantiated VApp(%s)", vAppResponse.getName()); |
| 78 | if (options.shouldDeploy()) { |
| 79 | logger.debug(">> deploying vApp(%s)", vAppResponse.getName()); |
| 80 | |
| 81 | Task task = VCloudExpressClient.class.cast(client).deployVApp(vAppResponse.getHref()); |
| 82 | if (options.shouldBlock()) { |
| 83 | if (!taskTester.apply(task.getHref())) { |
| 84 | throw new RuntimeException(String.format("failed to %s %s: %s", "deploy", vAppResponse.getName(), task)); |
| 85 | } |
| 86 | logger.debug("<< deployed vApp(%s)", vAppResponse.getName()); |
| 87 | if (options.shouldPowerOn()) { |
| 88 | logger.debug(">> powering vApp(%s)", vAppResponse.getName()); |
| 89 | task = VCloudExpressClient.class.cast(client).powerOnVApp(vAppResponse.getHref()); |
| 90 | if (!taskTester.apply(task.getHref())) { |
| 91 | throw new RuntimeException(String.format("failed to %s %s: %s", "powerOn", vAppResponse.getName(), |
| 92 | task)); |
| 93 | } |
| 94 | logger.debug("<< on vApp(%s)", vAppResponse.getName()); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return vAppResponse; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Set<String> getPrivateAddresses(URI id) { |
| 103 | return ImmutableSet.of(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public Set<String> getPublicAddresses(URI id) { |
| 108 | VCloudExpressVApp vApp = refreshVApp(id); |
| 109 | return Sets.newHashSet(vApp.getNetworkToAddresses().values()); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected Status getStatus(VCloudExpressVApp vApp) { |
| 114 | return vApp.getStatus(); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected VCloudExpressVApp refreshVApp(URI id) { |
| 119 | return VCloudExpressClient.class.cast(client).getVApp(id); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | protected Task powerOff(VCloudExpressVApp vApp) { |
| 124 | return VCloudExpressClient.class.cast(client).powerOffVApp(vApp.getHref()); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected Task reset(VCloudExpressVApp vApp) { |
| 129 | return VCloudExpressClient.class.cast(client).resetVApp(vApp.getHref()); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected Task undeploy(VCloudExpressVApp vApp) { |
| 134 | return VCloudExpressClient.class.cast(client).undeployVApp(vApp.getHref()); |
| 135 | } |
| 136 | } |