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