-
Notifications
You must be signed in to change notification settings - Fork 20
java: refactor SR cluster management #128
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,54 @@ | ||
| package org.example; | ||
|
|
||
| import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
| import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||
| import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.retries.StandardRetryStrategy; | ||
| import software.amazon.awssdk.retries.api.BackoffStrategy; | ||
| import software.amazon.awssdk.services.dsql.DsqlClient; | ||
| import software.amazon.awssdk.services.dsql.model.ClusterStatus; | ||
| import software.amazon.awssdk.services.dsql.model.CreateClusterRequest; | ||
| import software.amazon.awssdk.services.dsql.model.CreateClusterResponse; | ||
| import software.amazon.awssdk.services.dsql.model.GetClusterResponse; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.time.Duration; | ||
| import java.util.Map; | ||
|
|
||
| public class CreateCluster { | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| Region region = Region.US_EAST_1; | ||
|
|
||
| ClientOverrideConfiguration clientOverrideConfiguration = ClientOverrideConfiguration.builder() | ||
| .retryStrategy(StandardRetryStrategy.builder().build()) | ||
| .build(); | ||
|
|
||
| DsqlClient client = DsqlClient.builder() | ||
| .httpClient(UrlConnectionHttpClient.create()) | ||
| .overrideConfiguration(clientOverrideConfiguration) | ||
| .region(region) | ||
| .credentialsProvider(DefaultCredentialsProvider.create()) | ||
| .build(); | ||
|
|
||
| boolean deletionProtectionEnabled = true; | ||
| Map<String, String> tags = new HashMap<>(); | ||
| tags.put("Name", "FooBar"); | ||
|
|
||
| String identifier = createCluster(client, deletionProtectionEnabled, tags); | ||
| System.out.println("Cluster Id: " + identifier); | ||
| public static void main(String[] args) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: style should we have main at bottom instead?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As is it reads imperatively: marshal input params -> set up client -> invoke. Other languages may have stricter requirements about the order of declared methods (i.e. no use before declare) but I'm biasing for readability. |
||
| Region region = Region.of(System.getenv().getOrDefault("REGION_1", "us-east-1")); | ||
|
|
||
| try ( | ||
| DsqlClient client = DsqlClient.builder() | ||
| .region(region) | ||
| .credentialsProvider(DefaultCredentialsProvider.create()) | ||
| .build() | ||
| ) { | ||
| GetClusterResponse cluster = example(client); | ||
| System.out.println("Created " + cluster); | ||
| } | ||
| } | ||
|
|
||
| public static String createCluster(DsqlClient client, boolean deletionProtectionEnabled, Map<String, String> tags) throws Exception { | ||
| CreateClusterRequest createClusterRequest = CreateClusterRequest | ||
| .builder() | ||
| .deletionProtectionEnabled(deletionProtectionEnabled) | ||
| .tags(tags) | ||
| public static GetClusterResponse example(DsqlClient client) { | ||
| CreateClusterRequest request = CreateClusterRequest.builder() | ||
| .deletionProtectionEnabled(true) | ||
| .tags(Map.of( | ||
| "Name", "java single region cluster", | ||
| "Repo", "aws-samples/aurora-dsql-samples" | ||
| )) | ||
| .build(); | ||
| CreateClusterResponse res = client.createCluster(createClusterRequest); | ||
| if (res.status() == ClusterStatus.CREATING) { | ||
| return res.identifier(); | ||
| } else { | ||
| throw new Exception("Failed to create cluster"); | ||
| } | ||
| CreateClusterResponse cluster = client.createCluster(request); | ||
| System.out.println("Created " + cluster.arn()); | ||
|
|
||
| // The DSQL SDK offers a built-in waiter to poll for a cluster's | ||
| // transition to ACTIVE. | ||
| System.out.println("Waiting for cluster to become ACTIVE"); | ||
| GetClusterResponse activeCluster = client.waiter().waitUntilClusterActive( | ||
| getCluster -> getCluster.identifier(cluster.identifier()), | ||
| config -> config.backoffStrategyV2( | ||
| BackoffStrategy.fixedDelayWithoutJitter(Duration.ofSeconds(10)) | ||
| ).waitTimeout(Duration.ofMinutes(5)) | ||
| ).matched().response().orElseThrow(); | ||
| System.out.println("Cluster is ACTIVE: " + activeCluster); | ||
|
|
||
| return activeCluster; | ||
| } | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.