Skip to content

Commit

Permalink
rolled in review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
scmacdon committed Jun 27, 2024
1 parent 95e0a73 commit a3bba4f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 39 deletions.
10 changes: 5 additions & 5 deletions getting_started_scenarios/ecr_scenario/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

This Amazon Elastic Container Registry Service (Amazon ECR) basic scenario demonstrates how to interact with the Amazon ECR service using an AWS SDK. The scenario covers various operations such as creating an Amazon ECR repository, pushing images to the repository, setting an ECR repository policy, and so on. Here are the 6 top service operations this scenario will cover.

1. **Create an ECR repository**: The program prompts the user to enter a name for the ECR repository, then creates the repository using the `createECRRepository` method.
1. **Create an ECR repository**: The program creates an Amazon ECR repository.

2. **Set an ECR repository policy**: The program sets an ECR repository policy using the `setRepoPolicy` method, which grants the specified IAM role the necessary permissions to access the repository.
2. **Set an ECR repository policy**: The program sets an ECR repository policy, which grants the specified IAM role the necessary permissions to access the repository.

3. **Retrieve an ECR authorization token**: The program retrieves an ECR authorization token using the `getAuthToken` method, which is required for subsequent operations.
3. **Retrieve an ECR authorization token**: The program retrieves an ECR authorization token which is required for subsequent operations.

4. **Get the ECR repository URI**: The program retrieves the URI of the ECR repository using the `getRepositoryURI` method.
4. **Get the ECR repository URI**: The program retrieves the URI of the ECR repository.

5. **Push a Docker image to the ECR repository**: The program pushes a local Docker image to the ECR repository using a Docker client.

6. **Verify the image in the ECR repository**: The program verifies that the Docker image was successfully pushed to the ECR repository using the `verifyImage` method.
6. **Verify the image in the ECR repository**: The program verifies that the Docker image was successfully pushed to the ECR repository.

Note: These steps are not the complete program, but summarizes the 5-6 high-level steps. See the Eng Spec for a complete listing of ECR operations.

Expand Down
8 changes: 6 additions & 2 deletions getting_started_scenarios/ecr_scenario/SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
## Overview
This SDK getting started scenario demonstrates how to interact with Amazon Elastic Container Registry Service (Amazon ECR) using the AWS SDK. It demonstrates various tasks such as creating a repository, setting an ECR repository policy, obtaining an authorization token, pushing a docker image to the repository, and so on. Finally this scenario demonstrates how to clean up resources. Its purpose is to demonstrate how to get up and running with Amazon ECR and the AWS SDK.

## Resources and User Input
The required resources for this SDK scenario are an IAM role and a local Docker image. The IAM role must have permission to interact with the Amazon ECR service. To create an IAM role, see [Creating IAM roles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html).
## Resources
The required resources for this SDK scenario are an IAM role and a local Docker image. The IAM role must have permission to interact with the Amazon ECR service (for example, ecr:PutImage).

To create an IAM role, see [Creating IAM roles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html).

For more information about using permissions with ECR (for example, how to create Amazon ECR Identity-based policies), see [How Amazon Elastic Container Registry works with IAM](https://docs.aws.amazon.com/AmazonECR/latest/userguide/security_iam_service-with-iam.html).

To see the instructions to create a local docker image, see [README.md](README.md).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ private static EcrAsyncClient getAsyncClient() {
}

// snippet-start:[ecr.java2.delete.repo.main]

/**
* Deletes an ECR (Elastic Container Registry) repository.
*
Expand All @@ -116,10 +115,7 @@ public void deleteECRRepository(String repoName) {
.repositoryName(repoName)
.build();

// Execute the request asynchronously.
CompletableFuture<DeleteRepositoryResponse> response = getAsyncClient().deleteRepository(repositoryRequest);

// Use whenComplete to handle the response or any exceptions.
response.whenComplete((deleteRepositoryResponse, ex) -> {
if (deleteRepositoryResponse != null) {
System.out.println("You have successfully deleted the " + repoName + " repository");
Expand Down Expand Up @@ -163,10 +159,7 @@ public void verifyImage(String repositoryName, String imageTag) {
.imageIds(ImageIdentifier.builder().imageTag(imageTag).build())
.build();

// Retrieve the image details asynchronously.
CompletableFuture<DescribeImagesResponse> response = getAsyncClient().describeImages(request);

// Use whenComplete to handle the response or any exceptions.
response.whenComplete((describeImagesResponse, ex) -> {
if (ex != null) {
if (ex instanceof CompletionException) {
Expand Down Expand Up @@ -224,10 +217,7 @@ public void setLifeCyclePolicy(String repoName) {
.repositoryName(repoName)
.build();

// Execute the request asynchronously.
CompletableFuture<StartLifecyclePolicyPreviewResponse> response = getAsyncClient().startLifecyclePolicyPreview(lifecyclePolicyPreviewRequest);

// Use whenComplete to handle the response or any exceptions.
response.whenComplete((lifecyclePolicyPreviewResponse, ex) -> {
if (lifecyclePolicyPreviewResponse != null) {
System.out.println("Lifecycle policy preview started successfully.");
Expand Down Expand Up @@ -266,7 +256,6 @@ public String getRepositoryURI(String repoName) {
.repositoryNames(repoName)
.build();

// Retrieve the repository URI asynchronously.
CompletableFuture<DescribeRepositoriesResponse> response = getAsyncClient().describeRepositories(request);

try {
Expand All @@ -293,7 +282,6 @@ public String getRepositoryURI(String repoName) {
// snippet-end:[ecr.java2.describe.policy.main]

// snippet-start:[ecr.java2.get.token.main]

/**
* Retrieves the authorization token for Amazon Elastic Container Registry (ECR).
* This method makes an asynchronous call to the ECR client to retrieve the authorization token.
Expand All @@ -304,10 +292,7 @@ public String getRepositoryURI(String repoName) {
* @throws RuntimeException if there is an unexpected error during the operation.
*/
public void getAuthToken() {
// Retrieve the authorization token for ECR asynchronously.
CompletableFuture<GetAuthorizationTokenResponse> response = getAsyncClient().getAuthorizationToken();

// Use whenComplete to handle the response or any exceptions
response.whenComplete((authorizationTokenResponse, ex) -> {
if (authorizationTokenResponse != null) {
AuthorizationData authorizationData = authorizationTokenResponse.authorizationData().get(0);
Expand All @@ -333,7 +318,6 @@ public void getAuthToken() {
// snippet-end:[ecr.java2.get.token.main]

// snippet-start:[ecr.java2.get.repo.policy.main]

/**
* Gets the repository policy for the specified repository.
*
Expand All @@ -345,15 +329,11 @@ public String getRepoPolicy(String repoName) {
throw new IllegalArgumentException("Repository name cannot be null or empty");
}

// Create the request
GetRepositoryPolicyRequest getRepositoryPolicyRequest = GetRepositoryPolicyRequest.builder()
.repositoryName(repoName)
.build();

// Execute the request asynchronously and store the CompletableFuture.
CompletableFuture<GetRepositoryPolicyResponse> response = getAsyncClient().getRepositoryPolicy(getRepositoryPolicyRequest);

// Use whenComplete to handle the response or any exceptions.
response.whenComplete((resp, ex) -> {
if (resp != null) {
System.out.println("Repository policy retrieved successfully.");
Expand All @@ -371,14 +351,12 @@ public String getRepoPolicy(String repoName) {
}
});

// Wait for the CompletableFuture to complete and return the result.
GetRepositoryPolicyResponse result = response.join();
return result != null ? result.policyText() : null;
}
// snippet-end:[ecr.java2.get.repo.policy.main]

// snippet-start:[ecr.java2.set.repo.policy.main]

/**
* Sets the repository policy for the specified ECR repository.
*
Expand Down Expand Up @@ -435,7 +413,6 @@ public void setRepoPolicy(String repoName, String iamRole) {
// snippet-end:[ecr.java2.set.repo.policy.main]

// snippet-start:[ecr.java2.create.repo.main]

/**
* Creates an Amazon Elastic Container Registry (Amazon ECR) repository.
*
Expand All @@ -453,11 +430,8 @@ public String createECRRepository(String repoName) {
.repositoryName(repoName)
.build();

// Execute the request asynchronously.
CompletableFuture<CreateRepositoryResponse> response = getAsyncClient().createRepository(request);

try {
// Wait for the CompletableFuture to complete and return the result.
CreateRepositoryResponse result = response.join();
if (result != null) {
System.out.println("The " + repoName + " repository was created successfully.");
Expand All @@ -471,7 +445,6 @@ public String createECRRepository(String repoName) {
EcrException ex = (EcrException) e.getCause();
if (ex.awsErrorDetails().errorCode().equals("RepositoryAlreadyExistsException")) {
System.out.println("ECR repository already exists, moving on...");
// Fetch the existing repository's ARN
DescribeRepositoriesRequest describeRequest = DescribeRepositoriesRequest.builder()
.repositoryNames(repoName)
.build();
Expand All @@ -490,7 +463,6 @@ public String createECRRepository(String repoName) {
// snippet-end:[ecr.java2.create.repo.main]

// snippet-start:[ecr.java2.push.image.main]

/**
* Pushes a Docker image to an Amazon Elastic Container Registry (ECR) repository.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ or Amazon Elastic Container Service (ECS), you need to specify the full image UR
repository name and image tag, and then pushes the image to the ECR repository using the Docker client.
If the push operation is successful, the method prints a message indicating that the image was pushed to ECR.
""");

waitForInputToContinue(scanner);
ecrActions.pushDockerImage(repoName, localImageName);
waitForInputToContinue(scanner);
Expand Down Expand Up @@ -210,7 +209,6 @@ or Amazon Elastic Container Service (ECS), you need to specify the full image UR
instructions = String.format(instructions, accountId, repoName, localImageName, accountId, repoName, localImageName);
System.out.println(instructions);
}

waitForInputToContinue(scanner);

System.out.println(DASHES);
Expand Down
3 changes: 1 addition & 2 deletions javav2/example_code/ecr/src/test/java/ECRTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public void testHello() {
System.out.println("Test 2 passed");
}


private static String getSecretValues() {
SecretsManagerClient secretClient = SecretsManagerClient.builder()
.region(Region.US_EAST_1)
Expand Down Expand Up @@ -108,7 +107,7 @@ public String getExistingRepo() {
}

public String getRepoName() {
return repoName ;
return repoName;
}

public String getIamRole() {
Expand Down

0 comments on commit a3bba4f

Please sign in to comment.