1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
61
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
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
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
111
112 }
113 } else {
114 throw new RuntimeException("task timed out: " + task);
115 }
116 }
117
118 @Override
119 public Iterable<VMSpec> listHardwareProfiles() {
120
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
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
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 }