| 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 | |
| 20 | package org.jclouds.savvis.vpdc.compute.strategy; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static org.jclouds.savvis.vpdc.options.GetVMOptions.Builder.withPowerState; |
| 24 | import static org.jclouds.savvis.vpdc.reference.VPDCConstants.PROPERTY_VPDC_VDC_EMAIL; |
| 25 | |
| 26 | import java.net.URI; |
| 27 | import java.util.Map; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 32 | |
| 33 | import org.jclouds.compute.ComputeService; |
| 34 | import org.jclouds.compute.ComputeServiceAdapter; |
| 35 | import org.jclouds.compute.domain.CIMOperatingSystem; |
| 36 | import org.jclouds.compute.domain.Template; |
| 37 | import org.jclouds.compute.domain.Volume; |
| 38 | import org.jclouds.domain.Credentials; |
| 39 | import org.jclouds.predicates.RetryablePredicate; |
| 40 | import org.jclouds.savvis.vpdc.VPDCClient; |
| 41 | import org.jclouds.savvis.vpdc.domain.Network; |
| 42 | import org.jclouds.savvis.vpdc.domain.Org; |
| 43 | import org.jclouds.savvis.vpdc.domain.Resource; |
| 44 | import org.jclouds.savvis.vpdc.domain.Task; |
| 45 | import org.jclouds.savvis.vpdc.domain.VDC; |
| 46 | import org.jclouds.savvis.vpdc.domain.VM; |
| 47 | import org.jclouds.savvis.vpdc.domain.VMSpec; |
| 48 | import org.jclouds.savvis.vpdc.predicates.TaskSuccess; |
| 49 | import org.jclouds.savvis.vpdc.reference.VCloudMediaType; |
| 50 | |
| 51 | import com.google.common.base.Predicate; |
| 52 | import com.google.common.collect.ImmutableSet; |
| 53 | import com.google.common.collect.Iterables; |
| 54 | import com.google.common.collect.ImmutableSet.Builder; |
| 55 | import com.google.inject.Inject; |
| 56 | |
| 57 | ; |
| 58 | |
| 59 | /** |
| 60 | * defines the connection between the {@link VPDCClient} implementation and the jclouds |
| 61 | * {@link ComputeService} |
| 62 | * |
| 63 | */ |
| 64 | @Singleton |
| 65 | public class VPDCComputeServiceAdapter implements ComputeServiceAdapter<VM, VMSpec, CIMOperatingSystem, Network> { |
| 66 | private final VPDCClient client; |
| 67 | private final RetryablePredicate<String> taskTester; |
| 68 | @Inject(optional = true) |
| 69 | @Named(PROPERTY_VPDC_VDC_EMAIL) |
| 70 | String email; |
| 71 | |
| 72 | @Inject |
| 73 | public VPDCComputeServiceAdapter(VPDCClient client, TaskSuccess taskSuccess) { |
| 74 | this.client = checkNotNull(client, "client"); |
| 75 | // TODO: parameterize |
| 76 | this.taskTester = new RetryablePredicate<String>(checkNotNull(taskSuccess, "taskSuccess"), 650, 10, |
| 77 | TimeUnit.SECONDS); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public VM createNodeWithGroupEncodedIntoNameThenStoreCredentials(String tag, String name, Template template, |
| 82 | Map<String, Credentials> credentialStore) { |
| 83 | String networkTierName = template.getLocation().getId(); |
| 84 | String vpdcId = template.getLocation().getParent().getId(); |
| 85 | String billingSiteId = template.getLocation().getParent().getParent().getId(); |
| 86 | |
| 87 | VMSpec.Builder specBuilder = VMSpec.builder(); |
| 88 | specBuilder.name(name); |
| 89 | specBuilder.networkTierName(networkTierName); |
| 90 | specBuilder.operatingSystem(CIMOperatingSystem.class.cast(template.getImage().getOperatingSystem())); |
| 91 | specBuilder.processorCount(template.getHardware().getProcessors().size()); |
| 92 | specBuilder.memoryInGig(template.getHardware().getRam() / 1024); |
| 93 | |
| 94 | for (Volume volume : template.getHardware().getVolumes()) { |
| 95 | if (volume.isBootDevice()) |
| 96 | specBuilder.bootDeviceName(volume.getDevice()).bootDiskSize(volume.getSize().intValue()); |
| 97 | else |
| 98 | specBuilder.addDataDrive(volume.getDevice(), volume.getSize().intValue()); |
| 99 | } |
| 100 | |
| 101 | Task task = client.getVMClient().addVMIntoVDC(billingSiteId, vpdcId, specBuilder.build()); |
| 102 | // make sure there's no error |
| 103 | if (task.getError() != null) |
| 104 | throw new RuntimeException("cloud not add vm: " + task.getError().toString()); |
| 105 | |
| 106 | if (taskTester.apply(task.getId())) { |
| 107 | try { |
| 108 | return this.getNode(task.getResult().getHref().toASCIIString()); |
| 109 | } finally { |
| 110 | // TODO: get the credentials relevant to the billingSiteId/Org |
| 111 | // credentialStore.put(id, new Credentials(orgId, orgUser)); |
| 112 | } |
| 113 | } else { |
| 114 | throw new RuntimeException("task timed out: " + task); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Iterable<VMSpec> listHardwareProfiles() { |
| 120 | // TODO don't depend on OS |
| 121 | return ImmutableSet.of(VMSpec.builder().operatingSystem(Iterables.get(listImages(), 0)).memoryInGig(2) |
| 122 | .addDataDrive("/data01", 25).build()); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Iterable<CIMOperatingSystem> listImages() { |
| 127 | return client.listPredefinedOperatingSystems(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Iterable<VM> listNodes() { |
| 132 | Builder<VM> builder = ImmutableSet.<VM> builder(); |
| 133 | for (Resource org1 : client.listOrgs()) { |
| 134 | Org org = client.getBrowsingClient().getOrg(org1.getId()); |
| 135 | for (Resource vdc : org.getVDCs()) { |
| 136 | VDC VDC = client.getBrowsingClient().getVDCInOrg(org.getId(), vdc.getId()); |
| 137 | for (Resource vApp : Iterables.filter(VDC.getResourceEntities(), new Predicate<Resource>() { |
| 138 | |
| 139 | @Override |
| 140 | public boolean apply(Resource arg0) { |
| 141 | return VCloudMediaType.VAPP_XML.equals(arg0.getType()); |
| 142 | } |
| 143 | |
| 144 | })) { |
| 145 | builder.add(client.getBrowsingClient().getVMInVDC(org.getId(), vdc.getId(), vApp.getId(), |
| 146 | withPowerState())); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | return builder.build(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Iterable<Network> listLocations() { |
| 155 | Builder<Network> builder = ImmutableSet.<Network> builder(); |
| 156 | for (Resource org1 : client.listOrgs()) { |
| 157 | Org org = client.getBrowsingClient().getOrg(org1.getId()); |
| 158 | for (Resource vdc : org.getVDCs()) { |
| 159 | VDC VDC = client.getBrowsingClient().getVDCInOrg(org.getId(), vdc.getId()); |
| 160 | // optionally constrain locations |
| 161 | if (email != null && VDC.getDescription().indexOf(email) != -1) |
| 162 | continue; |
| 163 | for (Resource network : VDC.getAvailableNetworks()) { |
| 164 | builder.add(client.getBrowsingClient().getNetworkInVDC(org.getId(), vdc.getId(), network.getId())); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | return builder.build(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public VM getNode(String id) { |
| 173 | return client.getBrowsingClient().getVM(URI.create(checkNotNull(id, "id")), withPowerState()); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void destroyNode(String id) { |
| 178 | taskTester.apply(client.getVMClient().removeVM(URI.create(checkNotNull(id, "id"))).getId()); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void rebootNode(String id) { |
| 183 | // there is no support for restart in savvis yet |
| 184 | suspendNode(id); |
| 185 | resumeNode(id); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void resumeNode(String id) { |
| 190 | taskTester.apply(client.getServiceManagementClient().powerOnVM(URI.create(checkNotNull(id, "id"))).getId()); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void suspendNode(String id) { |
| 195 | taskTester.apply(client.getServiceManagementClient().powerOffVM(URI.create(checkNotNull(id, "id"))).getId()); |
| 196 | } |
| 197 | } |