Example usage for com.amazonaws.services.ecr.model DescribeImagesRequest DescribeImagesRequest

List of usage examples for com.amazonaws.services.ecr.model DescribeImagesRequest DescribeImagesRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.ecr.model DescribeImagesRequest DescribeImagesRequest.

Prototype

DescribeImagesRequest

Source Link

Usage

From source file:com.netflix.spinnaker.clouddriver.ecs.provider.view.EcrImageProvider.java

License:Apache License

@Override
public List<EcsDockerImage> findImage(String url) {
    // HTTP(S) part is not needed.
    url = url.replace("http://", "").replace("https://", "");

    String accountId = extractAwsAccountId(url);
    String repository = extractEcrRepositoryName(url);
    String identifier = extractEcrIdentifier(repository, url);
    boolean isTag = !(identifier.startsWith("sha256:") && identifier.length() == ("sha256:".length() + 64));
    String region = extractAwsRegion(url);

    NetflixAmazonCredentials credentials = getCredentials(accountId);

    if (!isValidRegion(credentials, region)) {
        throw new IllegalArgumentException(
                "The repository URI provided does not belong to a region that the credentials have access to or the region is not valid.");
    }/*from ww w .  ja v  a2  s.  co m*/

    AmazonECR amazonECR = amazonClientProvider.getAmazonEcr(credentials.getName(),
            credentials.getCredentialsProvider(), region);

    ListImagesResult result = amazonECR
            .listImages(new ListImagesRequest().withRegistryId(accountId).withRepositoryName(repository));
    DescribeImagesResult imagesResult = amazonECR.describeImages(new DescribeImagesRequest()
            .withRegistryId(accountId).withRepositoryName(repository).withImageIds(result.getImageIds()));

    // TODO - what is the user interface we want to have here?  We should discuss with Lars and Ethan from the community as this whole thing will undergo a big refactoring
    List<ImageDetail> imagesWithThisIdentifier = imagesResult.getImageDetails().stream()
            .filter(imageDetail -> imageFilter(imageDetail, identifier, isTag)).collect(Collectors.toList());

    if (imagesWithThisIdentifier.size() > 1) {
        throw new IllegalArgumentException("More than 1 image has this " + (isTag ? "tag" : "digest")
                + "!  This is currently not supported.");
    } else if (imagesWithThisIdentifier.size() == 0) {
        throw new IllegalArgumentException(String
                .format("No image with the " + (isTag ? "tag" : "digest") + " %s was found.", identifier));
    }

    ImageDetail matchedImage = imagesWithThisIdentifier.get(0);

    EcsDockerImage ecsDockerImage = new EcsDockerImage();
    ecsDockerImage.setRegion(region);
    ecsDockerImage.addAmiForRegion(region, matchedImage.getImageDigest());
    ecsDockerImage.setAttribute("creationDate", matchedImage.getImagePushedAt());
    ecsDockerImage.setImageName(buildFullDockerImageUrl(matchedImage.getImageDigest(),
            matchedImage.getRegistryId(), matchedImage.getRepositoryName(), region));

    return Collections.singletonList(ecsDockerImage);
}