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