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.domain; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.Map; |
24 | import java.util.Set; |
25 | |
26 | import org.jclouds.javax.annotation.Nullable; |
27 | |
28 | import com.google.common.collect.Iterables; |
29 | import com.google.common.collect.Maps; |
30 | import com.google.common.collect.Sets; |
31 | |
32 | /** |
33 | * |
34 | * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-DescribeImagesResponseItemType.html" |
35 | * /> |
36 | * @author Adrian Cole |
37 | */ |
38 | public class Image implements Comparable<Image> { |
39 | |
40 | private final String region; |
41 | private final Architecture architecture; |
42 | @Nullable |
43 | private final String name; |
44 | @Nullable |
45 | private final String description; |
46 | private final String imageId; |
47 | private final String imageLocation; |
48 | private final String imageOwnerId; |
49 | private final ImageState imageState; |
50 | private final ImageType imageType; |
51 | private final boolean isPublic; |
52 | @Nullable |
53 | private final String kernelId; |
54 | @Nullable |
55 | private final String platform; |
56 | private final Set<String> productCodes = Sets.newHashSet(); |
57 | @Nullable |
58 | private final String ramdiskId; |
59 | private final RootDeviceType rootDeviceType; |
60 | @Nullable |
61 | private final String rootDeviceName; |
62 | private final Map<String, EbsBlockDevice> ebsBlockDevices = Maps.newHashMap(); |
63 | private final VirtualizationType virtualizationType; |
64 | |
65 | public VirtualizationType getVirtualizationType() { |
66 | return virtualizationType; |
67 | } |
68 | |
69 | private final Hypervisor hypervisor; |
70 | |
71 | public Hypervisor getHypervisor() { |
72 | return hypervisor; |
73 | } |
74 | |
75 | public Image(String region, Architecture architecture, @Nullable String name, @Nullable String description, |
76 | String imageId, String imageLocation, String imageOwnerId, ImageState imageState, ImageType imageType, |
77 | boolean isPublic, Iterable<String> productCodes, @Nullable String kernelId, @Nullable String platform, |
78 | @Nullable String ramdiskId, RootDeviceType rootDeviceType, @Nullable String rootDeviceName, |
79 | Map<String, EbsBlockDevice> ebsBlockDevices, VirtualizationType virtualizationType, Hypervisor hypervisor) { |
80 | this.region = checkNotNull(region, "region"); |
81 | this.architecture = checkNotNull(architecture, "architecture"); |
82 | this.imageId = checkNotNull(imageId, "imageId"); |
83 | this.name = name; |
84 | this.description = description; |
85 | this.rootDeviceName = rootDeviceName; |
86 | this.imageLocation = checkNotNull(imageLocation, "imageLocation"); |
87 | this.imageOwnerId = checkNotNull(imageOwnerId, "imageOwnerId"); |
88 | this.imageState = checkNotNull(imageState, "imageState"); |
89 | this.imageType = checkNotNull(imageType, "imageType"); |
90 | this.isPublic = isPublic; |
91 | this.kernelId = kernelId; |
92 | this.platform = platform; |
93 | Iterables.addAll(this.productCodes, checkNotNull(productCodes, "productCodes")); |
94 | this.ramdiskId = ramdiskId; |
95 | this.rootDeviceType = checkNotNull(rootDeviceType, "rootDeviceType"); |
96 | this.ebsBlockDevices.putAll(checkNotNull(ebsBlockDevices, "ebsBlockDevices")); |
97 | this.virtualizationType = checkNotNull(virtualizationType, "virtualizationType"); |
98 | this.hypervisor = checkNotNull(hypervisor, "hypervisor"); |
99 | } |
100 | |
101 | public static enum ImageState { |
102 | /** |
103 | * the image is successfully registered and available for launching |
104 | */ |
105 | AVAILABLE, |
106 | /** |
107 | * the image is deregistered and no longer available for launching |
108 | */ |
109 | DEREGISTERED, UNRECOGNIZED; |
110 | public String value() { |
111 | return name().toLowerCase(); |
112 | } |
113 | |
114 | public static ImageState fromValue(String v) { |
115 | try { |
116 | return valueOf(v.toUpperCase()); |
117 | } catch (IllegalArgumentException e) { |
118 | return UNRECOGNIZED; |
119 | } |
120 | } |
121 | } |
122 | |
123 | public static enum Architecture { |
124 | I386, X86_64, UNRECOGNIZED; |
125 | public String value() { |
126 | return name().toLowerCase(); |
127 | } |
128 | |
129 | public static Architecture fromValue(String v) { |
130 | try { |
131 | return valueOf(v.toUpperCase()); |
132 | } catch (IllegalArgumentException e) { |
133 | return UNRECOGNIZED; |
134 | } |
135 | } |
136 | } |
137 | |
138 | public static enum ImageType { |
139 | |
140 | MACHINE, KERNEL, RAMDISK, UNRECOGNIZED; |
141 | public String value() { |
142 | return name().toLowerCase(); |
143 | } |
144 | |
145 | public static ImageType fromValue(String v) { |
146 | try { |
147 | return valueOf(v.toUpperCase()); |
148 | } catch (IllegalArgumentException e) { |
149 | return UNRECOGNIZED; |
150 | } |
151 | } |
152 | |
153 | } |
154 | |
155 | public static class EbsBlockDevice { |
156 | @Nullable |
157 | private final String snapshotId; |
158 | private final long volumeSize; |
159 | private final boolean deleteOnTermination; |
160 | |
161 | public EbsBlockDevice(@Nullable String snapshotId, long volumeSize, boolean deleteOnTermination) { |
162 | this.snapshotId = snapshotId; |
163 | this.volumeSize = volumeSize; |
164 | this.deleteOnTermination = deleteOnTermination; |
165 | } |
166 | |
167 | public String getSnapshotId() { |
168 | return snapshotId; |
169 | } |
170 | |
171 | public long getVolumeSize() { |
172 | return volumeSize; |
173 | } |
174 | |
175 | public boolean isDeleteOnTermination() { |
176 | return deleteOnTermination; |
177 | } |
178 | |
179 | @Override |
180 | public int hashCode() { |
181 | final int prime = 31; |
182 | int result = 1; |
183 | result = prime * result + (deleteOnTermination ? 1231 : 1237); |
184 | result = prime * result + ((snapshotId == null) ? 0 : snapshotId.hashCode()); |
185 | result = prime * result + (int) (volumeSize ^ (volumeSize >>> 32)); |
186 | return result; |
187 | } |
188 | |
189 | @Override |
190 | public boolean equals(Object obj) { |
191 | if (this == obj) |
192 | return true; |
193 | if (obj == null) |
194 | return false; |
195 | if (getClass() != obj.getClass()) |
196 | return false; |
197 | EbsBlockDevice other = (EbsBlockDevice) obj; |
198 | if (deleteOnTermination != other.deleteOnTermination) |
199 | return false; |
200 | if (snapshotId == null) { |
201 | if (other.snapshotId != null) |
202 | return false; |
203 | } else if (!snapshotId.equals(other.snapshotId)) |
204 | return false; |
205 | if (volumeSize != other.volumeSize) |
206 | return false; |
207 | return true; |
208 | } |
209 | |
210 | @Override |
211 | public String toString() { |
212 | return "EbsBlockDevice [deleteOnTermination=" + deleteOnTermination + ", snapshotId=" + snapshotId |
213 | + ", volumeSize=" + volumeSize + "]"; |
214 | } |
215 | |
216 | } |
217 | |
218 | /** |
219 | * AMIs are tied to the Region where its files are located within Amazon S3. |
220 | * |
221 | */ |
222 | public String getRegion() { |
223 | return region; |
224 | } |
225 | |
226 | /** |
227 | * The architecture of the image (i386 or x86_64). |
228 | */ |
229 | public Architecture getArchitecture() { |
230 | return architecture; |
231 | } |
232 | |
233 | /** |
234 | * The ID of the AMI. |
235 | */ |
236 | public String getId() { |
237 | return imageId; |
238 | } |
239 | |
240 | /** |
241 | * The location of the AMI. |
242 | */ |
243 | public String getImageLocation() { |
244 | return imageLocation; |
245 | } |
246 | |
247 | /** |
248 | * AWS Access Key ID of the image owner. |
249 | */ |
250 | public String getImageOwnerId() { |
251 | return imageOwnerId; |
252 | } |
253 | |
254 | /** |
255 | * Current state of the AMI. If the operation returns available, the image is successfully |
256 | * registered and avail able for launching. If the operation returns deregistered, the image is |
257 | * deregistered and no longer available for launching. |
258 | */ |
259 | public ImageState getImageState() { |
260 | return imageState; |
261 | } |
262 | |
263 | /** |
264 | * The type of image (machine, kernel, or ramdisk). |
265 | */ |
266 | public ImageType getImageType() { |
267 | return imageType; |
268 | } |
269 | |
270 | /** |
271 | * Returns true if this image has public launch permissions. Returns false if it only has |
272 | * implicit and explicit launch permissions. |
273 | */ |
274 | public boolean isPublic() { |
275 | return isPublic; |
276 | } |
277 | |
278 | /** |
279 | * The kernel associated with the image, if any. Only applicable for machine images. |
280 | */ |
281 | public String getKernelId() { |
282 | return kernelId; |
283 | } |
284 | |
285 | /** |
286 | * The operating platform of the instance. |
287 | */ |
288 | public String getPlatform() { |
289 | return platform; |
290 | } |
291 | |
292 | /** |
293 | * Product codes of the AMI. |
294 | */ |
295 | public Set<String> getProductCodes() { |
296 | return productCodes; |
297 | } |
298 | |
299 | /** |
300 | * The RAM disk associated with the image, if any. Only applicable for machine images. |
301 | */ |
302 | public String getRamdiskId() { |
303 | return ramdiskId; |
304 | } |
305 | |
306 | /** |
307 | * {@inheritDoc} |
308 | */ |
309 | public int compareTo(Image o) { |
310 | return (this == o) ? 0 : getId().compareTo(o.getId()); |
311 | } |
312 | |
313 | /** |
314 | * |
315 | * @return The root device type used by the AMI. The AMI can use an Amazon EBS or instance store |
316 | * root device. |
317 | */ |
318 | public RootDeviceType getRootDeviceType() { |
319 | return rootDeviceType; |
320 | } |
321 | |
322 | public String getName() { |
323 | return name; |
324 | } |
325 | |
326 | public String getDescription() { |
327 | return description; |
328 | } |
329 | |
330 | public String getRootDeviceName() { |
331 | return rootDeviceName; |
332 | } |
333 | |
334 | public Map<String, EbsBlockDevice> getEbsBlockDevices() { |
335 | return ebsBlockDevices; |
336 | } |
337 | |
338 | @Override |
339 | public int hashCode() { |
340 | final int prime = 31; |
341 | int result = 1; |
342 | result = prime * result + ((architecture == null) ? 0 : architecture.hashCode()); |
343 | result = prime * result + ((description == null) ? 0 : description.hashCode()); |
344 | result = prime * result + ((ebsBlockDevices == null) ? 0 : ebsBlockDevices.hashCode()); |
345 | result = prime * result + ((imageId == null) ? 0 : imageId.hashCode()); |
346 | result = prime * result + ((imageLocation == null) ? 0 : imageLocation.hashCode()); |
347 | result = prime * result + ((imageOwnerId == null) ? 0 : imageOwnerId.hashCode()); |
348 | result = prime * result + ((imageState == null) ? 0 : imageState.hashCode()); |
349 | result = prime * result + ((imageType == null) ? 0 : imageType.hashCode()); |
350 | result = prime * result + (isPublic ? 1231 : 1237); |
351 | result = prime * result + ((kernelId == null) ? 0 : kernelId.hashCode()); |
352 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
353 | result = prime * result + ((platform == null) ? 0 : platform.hashCode()); |
354 | result = prime * result + ((productCodes == null) ? 0 : productCodes.hashCode()); |
355 | result = prime * result + ((ramdiskId == null) ? 0 : ramdiskId.hashCode()); |
356 | result = prime * result + ((region == null) ? 0 : region.hashCode()); |
357 | result = prime * result + ((rootDeviceName == null) ? 0 : rootDeviceName.hashCode()); |
358 | result = prime * result + ((rootDeviceType == null) ? 0 : rootDeviceType.hashCode()); |
359 | result = prime * result + ((virtualizationType == null) ? 0 : virtualizationType.hashCode()); |
360 | result = prime * result + ((hypervisor == null) ? 0 : hypervisor.hashCode()); |
361 | return result; |
362 | } |
363 | |
364 | @Override |
365 | public boolean equals(Object obj) { |
366 | if (this == obj) |
367 | return true; |
368 | if (obj == null) |
369 | return false; |
370 | if (getClass() != obj.getClass()) |
371 | return false; |
372 | Image other = (Image) obj; |
373 | if (architecture == null) { |
374 | if (other.architecture != null) |
375 | return false; |
376 | } else if (!architecture.equals(other.architecture)) |
377 | return false; |
378 | if (description == null) { |
379 | if (other.description != null) |
380 | return false; |
381 | } else if (!description.equals(other.description)) |
382 | return false; |
383 | if (ebsBlockDevices == null) { |
384 | if (other.ebsBlockDevices != null) |
385 | return false; |
386 | } else if (!ebsBlockDevices.equals(other.ebsBlockDevices)) |
387 | return false; |
388 | if (imageId == null) { |
389 | if (other.imageId != null) |
390 | return false; |
391 | } else if (!imageId.equals(other.imageId)) |
392 | return false; |
393 | if (imageLocation == null) { |
394 | if (other.imageLocation != null) |
395 | return false; |
396 | } else if (!imageLocation.equals(other.imageLocation)) |
397 | return false; |
398 | if (imageOwnerId == null) { |
399 | if (other.imageOwnerId != null) |
400 | return false; |
401 | } else if (!imageOwnerId.equals(other.imageOwnerId)) |
402 | return false; |
403 | if (imageState == null) { |
404 | if (other.imageState != null) |
405 | return false; |
406 | } else if (!imageState.equals(other.imageState)) |
407 | return false; |
408 | if (imageType == null) { |
409 | if (other.imageType != null) |
410 | return false; |
411 | } else if (!imageType.equals(other.imageType)) |
412 | return false; |
413 | if (isPublic != other.isPublic) |
414 | return false; |
415 | if (kernelId == null) { |
416 | if (other.kernelId != null) |
417 | return false; |
418 | } else if (!kernelId.equals(other.kernelId)) |
419 | return false; |
420 | if (name == null) { |
421 | if (other.name != null) |
422 | return false; |
423 | } else if (!name.equals(other.name)) |
424 | return false; |
425 | if (platform == null) { |
426 | if (other.platform != null) |
427 | return false; |
428 | } else if (!platform.equals(other.platform)) |
429 | return false; |
430 | if (productCodes == null) { |
431 | if (other.productCodes != null) |
432 | return false; |
433 | } else if (!productCodes.equals(other.productCodes)) |
434 | return false; |
435 | if (ramdiskId == null) { |
436 | if (other.ramdiskId != null) |
437 | return false; |
438 | } else if (!ramdiskId.equals(other.ramdiskId)) |
439 | return false; |
440 | if (region == null) { |
441 | if (other.region != null) |
442 | return false; |
443 | } else if (!region.equals(other.region)) |
444 | return false; |
445 | if (rootDeviceName == null) { |
446 | if (other.rootDeviceName != null) |
447 | return false; |
448 | } else if (!rootDeviceName.equals(other.rootDeviceName)) |
449 | return false; |
450 | if (rootDeviceType == null) { |
451 | if (other.rootDeviceType != null) |
452 | return false; |
453 | } else if (!rootDeviceType.equals(other.rootDeviceType)) |
454 | return false; |
455 | if (virtualizationType == null) { |
456 | if (other.virtualizationType != null) |
457 | return false; |
458 | } else if (!virtualizationType.equals(other.virtualizationType)) |
459 | return false; |
460 | if (hypervisor == null) { |
461 | if (other.hypervisor != null) |
462 | return false; |
463 | } else if (!hypervisor.equals(other.hypervisor)) |
464 | return false; |
465 | return true; |
466 | } |
467 | |
468 | @Override |
469 | public String toString() { |
470 | return "Image [architecture=" + architecture + ", description=" + description + ", ebsBlockDevices=" |
471 | + ebsBlockDevices + ", imageId=" + imageId + ", imageLocation=" + imageLocation + ", imageOwnerId=" |
472 | + imageOwnerId + ", imageState=" + imageState + ", imageType=" + imageType + ", isPublic=" + isPublic |
473 | + ", kernelId=" + kernelId + ", name=" + name + ", platform=" + platform + ", productCodes=" |
474 | + productCodes + ", ramdiskId=" + ramdiskId + ", region=" + region + ", rootDeviceName=" |
475 | + rootDeviceName + ", rootDeviceType=" + rootDeviceType + ", virtualizationType=" + virtualizationType |
476 | + ", hypervisor=" + hypervisor + "]"; |
477 | } |
478 | |
479 | } |