Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The DryRun field in EC2 APIs is no longer hidden by the SDK through customization and is now exposed #4353

Merged
merged 5 commits into from
Aug 30, 2023

Conversation

joviegas
Copy link
Contributor

@joviegas joviegas commented Aug 26, 2023

The DryRun field in EC2 APIs is no longer hidden by the SDK through customization and is now exposed

Motivation and Context

  • Address EC2 Dry-Runs #76
  • AWS SDK Java 2.x did not expose dryRun since 1.x had a special API to check the dryRun Status
  • Checked with other language SDKs and found that none of the SDK handle dryRun in a special way.
  • As mentioned in the Java Doc of the dryRun user will need to check the error response

Sample Code on how a USER can Write a generic API to check isDryRunComplete

Sync Client

import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
import software.amazon.awssdk.services.ec2.model.Ec2Response;
import software.amazon.awssdk.testutils.service.AwsIntegrationTestBase;


    private boolean isDryRunComplete(Supplier<Ec2Response> dryRunOp) {
        Ec2Response ec2Response = null;
        try {
            ec2Response = dryRunOp.get();
        } catch (Ec2Exception exception) {
            AwsErrorDetails awsErrorDetails = exception.awsErrorDetails();
            if ("AuthFailure".equals(awsErrorDetails.errorCode())) {
                return false;
            }
            if ("DryRunOperation".equals(awsErrorDetails.errorCode())) {
                return true;
            }
            throw exception;
        }
        throw new IllegalStateException("Unable to get dryRun status for ec2Response:  " + ec2Response);
    }

    @Test
    public void dryRunSuccessTest() {
        boolean isDryRunComplete = isDryRunComplete(
            () -> ec2Client.describeRegions(r -> r.regionNames(Region.US_EAST_1.id())
                                                  .dryRun(true)
            )
        );
        assertThat(isDryRunComplete).isTrue();

    }

Async Cient

        // Write a handle and check the Exception that is caught
        Boolean isDryRunComplete = asyncClient.describeRegions(r -> r.regionNames(Region.US_EAST_1.id())
                                                                     .dryRun(true))
                                              .handle((resp, e) -> isDryRunCompleted(e))
                                              .join();

    private static Boolean isDryRunCompleted(Throwable exceptionCaught) {
        if(exceptionCaught.getCause() instanceof Ec2Exception){
            Ec2Exception exception = (Ec2Exception) exceptionCaught.getCause();
            AwsErrorDetails awsErrorDetails = exception.awsErrorDetails();
            if ("AuthFailure".equals(awsErrorDetails.errorCode())) {
                return false;
            }
            if ("DryRunOperation".equals(awsErrorDetails.errorCode())) {
                return true;
            }
        }
        throw new IllegalStateException(exceptionCaught.getCause());
    }


Java Doc as generated after exposing the field

        /**
         * <p>
         * Checks whether you have the required permissions for the action, without actually making the request, and
         * provides an error response. If you have the required permissions, the error response is
         * <code>DryRunOperation</code>. Otherwise, it is <code>UnauthorizedOperation</code>.
         * </p>
         * 
         * @param dryRun
         *        Checks whether you have the required permissions for the action, without actually making the request,
         *        and provides an error response. If you have the required permissions, the error response is
         *        <code>DryRunOperation</code>. Otherwise, it is <code>UnauthorizedOperation</code>.
         * @return Returns a reference to this object so that method calls can be chained together.
         */
        Builder dryRun(Boolean dryRun);

Modifications

  • Removed customization

Testing

  • Tested on integ with both failure and success cases

Screenshots (if appropriate)

License

  • I confirm that this pull request can be released under the Apache 2 license

@joviegas joviegas requested a review from a team as a code owner August 26, 2023 00:29
@joviegas joviegas changed the title The DryRun field in EC2 APIs is no longer hidden by the SDK through c… The DryRun field in EC2 APIs is no longer hidden by the SDK through customization and is now exposed Aug 26, 2023
import software.amazon.awssdk.services.ec2.model.Ec2Response;
import software.amazon.awssdk.testutils.service.AwsIntegrationTestBase;

class Ec2DryRunSupportIntegrationTest extends AwsIntegrationTestBase {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check in this integ test since technically this is more like testing the service, rather than the SDK.

Copy link
Contributor Author

@joviegas joviegas Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done , removed the Test cases and added the code in description

@sonarcloud
Copy link

sonarcloud bot commented Aug 30, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@joviegas joviegas merged commit 39ae55c into master Aug 30, 2023
7 checks passed
@joviegas joviegas deleted the joviegas_dryrun branch May 15, 2024 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants