protected String getImageId(String imageName) {
    DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest();

    List<Filter> filters = new ArrayList<Filter>();

    Filter filter = new Filter();

    filter.setName("name");

    List<String> values = new ArrayList<String>();

    values.add(imageName);

    filter.setValues(values);

    filters.add(filter);

    describeImagesRequest.setFilters(filters);

    DescribeImagesResult describeImagesResult =
        amazonEC2Client.describeImages(describeImagesRequest);

    List<Image> images = describeImagesResult.getImages();

    if (images.isEmpty()) {
      throw new RuntimeException("Image " + imageName + " does not exist");
    }

    Image image = images.get(0);

    return image.getImageId();
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj) return true;
    if (obj == null) return false;

    if (obj instanceof DescribeImagesResult == false) return false;
    DescribeImagesResult other = (DescribeImagesResult) obj;
    if (other.getImages() == null ^ this.getImages() == null) return false;
    if (other.getImages() != null && other.getImages().equals(this.getImages()) == false)
      return false;
    return true;
  }
  /**
   * Describe a set of specific images.
   *
   * @param imageIds the image ids
   * @return the images
   */
  public List<Image> describeImages(String... imageIds) {
    if (imageIds == null || imageIds.length == 0) {
      LOGGER.info("Getting all AMIs.");
    } else {
      LOGGER.info(String.format("Getting AMIs for %d ids.", imageIds.length));
    }

    AmazonEC2 ec2Client = ec2Client();
    DescribeImagesRequest request = new DescribeImagesRequest();
    if (imageIds != null) {
      request.setImageIds(Arrays.asList(imageIds));
    }
    DescribeImagesResult result = ec2Client.describeImages(request);
    List<Image> images = result.getImages();

    LOGGER.info(String.format("Got %d AMIs.", images.size()));
    return images;
  }
Exemple #4
0
  private static void putCache() throws CacheException {
    if (amazon == null) amazon = JCS.getInstance("Instances");
    synchronized (AmazonService.class) {
      amazon.clear();
      DescribeInstancesResult result = amazonClient.describeInstances();
      for (Reservation r : result.getReservations()) {
        for (Instance i : r.getInstances()) {
          amazon.putInGroup(i.getInstanceId(), "Instances", i);
        }
      }

      DescribeImagesRequest ir = new DescribeImagesRequest().withOwners("self");
      DescribeImagesResult images = amazonClient.describeImages(ir);
      for (Image r : images.getImages()) {
        amazon.putInGroup(r.getImageId(), "AMIs", r);
      }
      log.debug("put cache finished");
    }
  }