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.ec2.compute.domain; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | import static com.google.common.base.Predicates.not; |
23 | import static org.jclouds.compute.predicates.ImagePredicates.any; |
24 | import static org.jclouds.compute.predicates.ImagePredicates.idIn; |
25 | |
26 | import java.net.URI; |
27 | import java.util.List; |
28 | import java.util.Map; |
29 | |
30 | import org.jclouds.compute.domain.Hardware; |
31 | import org.jclouds.compute.domain.HardwareBuilder; |
32 | import org.jclouds.compute.domain.Image; |
33 | import org.jclouds.compute.domain.OsFamily; |
34 | import org.jclouds.compute.domain.Processor; |
35 | import org.jclouds.compute.domain.Volume; |
36 | import org.jclouds.compute.domain.internal.VolumeImpl; |
37 | import org.jclouds.compute.predicates.ImagePredicates; |
38 | import org.jclouds.domain.Location; |
39 | import org.jclouds.ec2.domain.InstanceType; |
40 | import org.jclouds.ec2.domain.RootDeviceType; |
41 | import org.jclouds.ec2.domain.VirtualizationType; |
42 | |
43 | import com.google.common.base.Predicate; |
44 | import com.google.common.base.Predicates; |
45 | import com.google.common.collect.ImmutableList; |
46 | |
47 | /** |
48 | * |
49 | * @author Adrian Cole |
50 | * @see <a href= |
51 | * "http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instance-types.html" |
52 | * /> |
53 | */ |
54 | public class EC2HardwareBuilder extends HardwareBuilder { |
55 | private Predicate<Image> rootDeviceType = any(); |
56 | private Predicate<Image> virtualizationType = Predicates.or(new IsWindows(), new RequiresVirtualizationType( |
57 | VirtualizationType.PARAVIRTUAL)); |
58 | private Predicate<Image> imageIds = any(); |
59 | private Predicate<Image> is64Bit = any(); |
60 | |
61 | public EC2HardwareBuilder() { |
62 | this.supportsImage = null; |
63 | } |
64 | |
65 | /** |
66 | * evaluates true if the Image has the following rootDeviceType |
67 | * |
68 | * @param type |
69 | * rootDeviceType of the image |
70 | * @return predicate |
71 | */ |
72 | public static class RequiresRootDeviceType implements Predicate<Image> { |
73 | final RootDeviceType type; |
74 | |
75 | public RequiresRootDeviceType(final RootDeviceType type) { |
76 | this.type = checkNotNull(type, "type must be defined"); |
77 | } |
78 | |
79 | @Override |
80 | public boolean apply(Image image) { |
81 | return image.getUserMetadata().containsKey("rootDeviceType") |
82 | && type == RootDeviceType.fromValue(image.getUserMetadata().get("rootDeviceType")); |
83 | } |
84 | |
85 | @Override |
86 | public String toString() { |
87 | return "requiresRootDeviceType(" + type + ")"; |
88 | } |
89 | |
90 | } |
91 | |
92 | public static class IsWindows implements Predicate<Image> { |
93 | |
94 | @Override |
95 | public boolean apply(Image image) { |
96 | return image.getOperatingSystem() != null && OsFamily.WINDOWS == image.getOperatingSystem().getFamily(); |
97 | } |
98 | |
99 | @Override |
100 | public String toString() { |
101 | return "isWindows()"; |
102 | } |
103 | |
104 | } |
105 | |
106 | /** |
107 | * evaluates true if the Image requires the following virtualizationType |
108 | * |
109 | * @param type |
110 | * virtualizationType of the image |
111 | * @return predicate |
112 | */ |
113 | public static class RequiresVirtualizationType implements Predicate<Image> { |
114 | final VirtualizationType type; |
115 | |
116 | public RequiresVirtualizationType(final VirtualizationType type) { |
117 | this.type = checkNotNull(type, "type must be defined"); |
118 | } |
119 | |
120 | @Override |
121 | public boolean apply(Image image) { |
122 | return image.getOperatingSystem() != null && image.getOperatingSystem().getArch() != null |
123 | && type == VirtualizationType.fromValue(image.getOperatingSystem().getArch()); |
124 | } |
125 | |
126 | @Override |
127 | public String toString() { |
128 | return "requiresVirtualizationType(" + type + ")"; |
129 | } |
130 | |
131 | } |
132 | |
133 | public EC2HardwareBuilder(String instanceType) { |
134 | ids(instanceType); |
135 | } |
136 | |
137 | public EC2HardwareBuilder virtualizationType(VirtualizationType virtualizationType) { |
138 | this.virtualizationType = new RequiresVirtualizationType(virtualizationType); |
139 | return this; |
140 | } |
141 | |
142 | public EC2HardwareBuilder rootDeviceType(RootDeviceType rootDeviceType) { |
143 | this.rootDeviceType = new RequiresRootDeviceType(rootDeviceType); |
144 | return this; |
145 | } |
146 | |
147 | public EC2HardwareBuilder supportsImageIds(Iterable<String> ids) { |
148 | this.imageIds = idIn(ids); |
149 | return this; |
150 | } |
151 | |
152 | public EC2HardwareBuilder ids(String id) { |
153 | return EC2HardwareBuilder.class.cast(super.ids(id)); |
154 | } |
155 | |
156 | public EC2HardwareBuilder ram(int ram) { |
157 | return EC2HardwareBuilder.class.cast(super.ram(ram)); |
158 | } |
159 | |
160 | public EC2HardwareBuilder processors(List<Processor> processors) { |
161 | return EC2HardwareBuilder.class.cast(super.processors(processors)); |
162 | } |
163 | |
164 | public EC2HardwareBuilder volumes(List<Volume> volumes) { |
165 | return EC2HardwareBuilder.class.cast(super.volumes(volumes)); |
166 | } |
167 | |
168 | public EC2HardwareBuilder supportsImage(Predicate<Image> supportsImage) { |
169 | return EC2HardwareBuilder.class.cast(super.supportsImage(supportsImage)); |
170 | } |
171 | |
172 | public EC2HardwareBuilder is64Bit(boolean is64Bit) { |
173 | this.is64Bit = is64Bit ? ImagePredicates.is64Bit() : not(ImagePredicates.is64Bit()); |
174 | return this; |
175 | } |
176 | |
177 | public EC2HardwareBuilder id(String id) { |
178 | return EC2HardwareBuilder.class.cast(super.id(id)); |
179 | } |
180 | |
181 | @Override |
182 | public EC2HardwareBuilder providerId(String providerId) { |
183 | return EC2HardwareBuilder.class.cast(super.providerId(providerId)); |
184 | } |
185 | |
186 | @Override |
187 | public EC2HardwareBuilder name(String name) { |
188 | return EC2HardwareBuilder.class.cast(super.name(name)); |
189 | } |
190 | |
191 | @Override |
192 | public EC2HardwareBuilder location(Location location) { |
193 | return EC2HardwareBuilder.class.cast(super.location(location)); |
194 | } |
195 | |
196 | @Override |
197 | public EC2HardwareBuilder uri(URI uri) { |
198 | return EC2HardwareBuilder.class.cast(super.uri(uri)); |
199 | } |
200 | |
201 | @Override |
202 | public EC2HardwareBuilder userMetadata(Map<String, String> userMetadata) { |
203 | return EC2HardwareBuilder.class.cast(super.userMetadata(userMetadata)); |
204 | } |
205 | |
206 | /** |
207 | * @see InstanceType#M1_SMALL |
208 | */ |
209 | public static EC2HardwareBuilder m1_small() { |
210 | return new EC2HardwareBuilder(InstanceType.M1_SMALL) |
211 | .ram(1740) |
212 | .processors(ImmutableList.of(new Processor(1.0, 1.0))) |
213 | .volumes( |
214 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(150.0f, |
215 | "/dev/sda2", false, false))); |
216 | } |
217 | |
218 | /** |
219 | * @see InstanceType#M1_MEDIUM |
220 | */ |
221 | public static EC2HardwareBuilder m1_medium() { |
222 | return new EC2HardwareBuilder(InstanceType.M1_MEDIUM) |
223 | .ram(3750) |
224 | .processors(ImmutableList.of(new Processor(1.0, 2.0))) |
225 | .volumes( |
226 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(420.0f, |
227 | "/dev/sdb", false, false), new VolumeImpl(420.0f, "/dev/sdc", false, false))); |
228 | } |
229 | |
230 | |
231 | /** |
232 | * @see InstanceType#T1_MICRO |
233 | */ |
234 | public static EC2HardwareBuilder t1_micro() { |
235 | return new EC2HardwareBuilder(InstanceType.T1_MICRO).ram(630) |
236 | .processors(ImmutableList.of(new Processor(1.0, 1.0))).rootDeviceType(RootDeviceType.EBS); |
237 | } |
238 | |
239 | /** |
240 | * @see InstanceType#M1_LARGE |
241 | */ |
242 | public static EC2HardwareBuilder m1_large() { |
243 | return new EC2HardwareBuilder(InstanceType.M1_LARGE) |
244 | .ram(7680) |
245 | .processors(ImmutableList.of(new Processor(2.0, 2.0))) |
246 | .volumes( |
247 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(420.0f, |
248 | "/dev/sdb", false, false), new VolumeImpl(420.0f, "/dev/sdc", false, false))).is64Bit(true); |
249 | } |
250 | |
251 | /** |
252 | * @see InstanceType#M1_XLARGE |
253 | */ |
254 | public static EC2HardwareBuilder m1_xlarge() { |
255 | return new EC2HardwareBuilder(InstanceType.M1_XLARGE) |
256 | .ram(15360) |
257 | .processors(ImmutableList.of(new Processor(4.0, 2.0))) |
258 | .volumes( |
259 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(420.0f, |
260 | "/dev/sdb", false, false), new VolumeImpl(420.0f, "/dev/sdc", false, false), new VolumeImpl( |
261 | 420.0f, "/dev/sdd", false, false), new VolumeImpl(420.0f, "/dev/sde", false, false))) |
262 | .is64Bit(true); |
263 | } |
264 | |
265 | /** |
266 | * @see InstanceType#M2_XLARGE |
267 | */ |
268 | public static EC2HardwareBuilder m2_xlarge() { |
269 | return new EC2HardwareBuilder(InstanceType.M2_XLARGE).ram(17510) |
270 | .processors(ImmutableList.of(new Processor(2.0, 3.25))) |
271 | .volumes(ImmutableList.<Volume> of(new VolumeImpl(420.0f, "/dev/sda1", true, false))).is64Bit(true); |
272 | } |
273 | |
274 | /** |
275 | * @see InstanceType#M2_2XLARGE |
276 | */ |
277 | public static EC2HardwareBuilder m2_2xlarge() { |
278 | return new EC2HardwareBuilder(InstanceType.M2_2XLARGE) |
279 | .ram(35020) |
280 | .processors(ImmutableList.of(new Processor(4.0, 3.25))) |
281 | .volumes( |
282 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
283 | "/dev/sdb", false, false))).is64Bit(true); |
284 | } |
285 | |
286 | /** |
287 | * @see InstanceType#M2_4XLARGE |
288 | */ |
289 | public static EC2HardwareBuilder m2_4xlarge() { |
290 | return new EC2HardwareBuilder(InstanceType.M2_4XLARGE) |
291 | .ram(70041) |
292 | .processors(ImmutableList.of(new Processor(8.0, 3.25))) |
293 | .volumes( |
294 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
295 | "/dev/sdb", false, false), new VolumeImpl(840.0f, "/dev/sdc", false, false))).is64Bit(true); |
296 | } |
297 | |
298 | /** |
299 | * @see InstanceType#C1_MEDIUM |
300 | */ |
301 | public static EC2HardwareBuilder c1_medium() { |
302 | return new EC2HardwareBuilder(InstanceType.C1_MEDIUM) |
303 | .ram(1740) |
304 | .processors(ImmutableList.of(new Processor(2.0, 2.5))) |
305 | .volumes( |
306 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(340.0f, |
307 | "/dev/sda2", false, false))); |
308 | } |
309 | |
310 | /** |
311 | * @see InstanceType#C1_XLARGE |
312 | */ |
313 | public static EC2HardwareBuilder c1_xlarge() { |
314 | return new EC2HardwareBuilder(InstanceType.C1_XLARGE) |
315 | .ram(7168) |
316 | .processors(ImmutableList.of(new Processor(8.0, 2.5))) |
317 | .volumes( |
318 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(420.0f, |
319 | "/dev/sdb", false, false), new VolumeImpl(420.0f, "/dev/sdc", false, false), new VolumeImpl( |
320 | 420.0f, "/dev/sdd", false, false), new VolumeImpl(420.0f, "/dev/sde", false, false))) |
321 | .is64Bit(true); |
322 | } |
323 | |
324 | public static EC2HardwareBuilder cg1_4xlarge() { |
325 | return new EC2HardwareBuilder(InstanceType.CG1_4XLARGE) |
326 | .ram(22 * 1024) |
327 | .processors(ImmutableList.of(new Processor(4.0, 4.0), new Processor(4.0, 4.0))) |
328 | .volumes( |
329 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
330 | "/dev/sdb", false, false), new VolumeImpl(840.0f, "/dev/sdc", false, false))) |
331 | .virtualizationType(VirtualizationType.HVM); |
332 | } |
333 | |
334 | public static EC2HardwareBuilder cc1_4xlarge() { |
335 | return new EC2HardwareBuilder(InstanceType.CC1_4XLARGE) |
336 | .ram(23 * 1024) |
337 | .processors(ImmutableList.of(new Processor(4.0, 4.0), new Processor(4.0, 4.0))) |
338 | .volumes( |
339 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
340 | "/dev/sdb", false, false), new VolumeImpl(840.0f, "/dev/sdc", false, false))) |
341 | .virtualizationType(VirtualizationType.HVM); |
342 | } |
343 | |
344 | public static EC2HardwareBuilder cc2_8xlarge() { |
345 | return new EC2HardwareBuilder(InstanceType.CC2_8XLARGE) |
346 | .ram(60 * 1024 + 512) |
347 | .processors(ImmutableList.of(new Processor(8.0, 5.5), new Processor(8.0, 5.5))) |
348 | .volumes( |
349 | ImmutableList.<Volume> of(new VolumeImpl(10.0f, "/dev/sda1", true, false), new VolumeImpl(840.0f, |
350 | "/dev/sdb", false, false), new VolumeImpl(840.0f, "/dev/sdc", false, false), new VolumeImpl( |
351 | 840.0f, "/dev/sdb", false, false), new VolumeImpl(840.0f, "/dev/sdc", false, false))) |
352 | .virtualizationType(VirtualizationType.HVM); |
353 | } |
354 | |
355 | @SuppressWarnings("unchecked") |
356 | @Override |
357 | public Hardware build() { |
358 | boolean reset = false; |
359 | if (this.supportsImage == null) |
360 | reset = true; |
361 | try { |
362 | supportsImage = Predicates.<Image> and(rootDeviceType, virtualizationType, imageIds, is64Bit); |
363 | return super.build(); |
364 | } finally { |
365 | if (reset) |
366 | this.supportsImage = null; |
367 | } |
368 | |
369 | } |
370 | |
371 | } |