-
Notifications
You must be signed in to change notification settings - Fork 20
go: refactor multi-region for new dsql sdk #129
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5d47c0d
Initial version of new cluster management sdk
imforster ad65c67
Remove get_cluster old example
imforster 99bfd06
Update go.mod and go.sum
imforster c2afb0c
Add Makefile to build examples
imforster 20670dd
Update Make
imforster 006e0b8
Add find_cluster example to simplify test integration
imforster e59607c
Fix test.
imforster 7f0ed58
Add tests for multi-region cluster changes
imforster a99af3f
Update go cluster management workflow
imforster 71778a0
Update to provide separate targets for each test
imforster ba767a7
Add status check to ensure that cluster is in either Active or Pendin…
imforster b52c6a0
Add status check to ensure that cluster is in either Active or Pendin…
imforster 6bb002e
Fix typo
imforster d458416
Update to remove unnecessary comment and test ids
imforster fa9c739
* Fix environment variable display to test function
imforster 667cfac
Removed comment unnecessary
imforster 086081a
Updated README.md to align prerequisites and add Important warning no…
imforster e49d35b
Update go/cluster_management/README.md
imforster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Build all cluster management examples | ||
| # Go parameters | ||
| GOCMD=go | ||
| GOBUILD=$(GOCMD) build | ||
| GOCLEAN=$(GOCMD) clean | ||
| BINARY_DIR=bin | ||
|
|
||
| # Find all cmd directories | ||
| CMD_DIRS := $(wildcard cmd/*) | ||
| BINARIES := $(patsubst cmd/%,$(BINARY_DIR)/%,$(CMD_DIRS)) | ||
|
|
||
| # Default target | ||
| .PHONY: all | ||
| all: clean build | ||
|
|
||
| # Create bin directory | ||
| $(BINARY_DIR): | ||
| mkdir -p $(BINARY_DIR) | ||
|
|
||
| # Build all commands | ||
| .PHONY: build | ||
| build: $(BINARY_DIR) $(BINARIES) | ||
|
|
||
| # Rule to build each binary | ||
| $(BINARY_DIR)/%: cmd/% | ||
| $(GOBUILD) -o $@ ./cmd/$* | ||
|
|
||
| # Clean build artifacts | ||
| .PHONY: clean | ||
| clean: | ||
| $(GOCLEAN) | ||
| rm -rf $(BINARY_DIR) | ||
|
|
||
| # Build specific commands | ||
| .PHONY: $(CMD_DIRS) | ||
| $(CMD_DIRS): cmd/%: $(BINARY_DIR)/% |
This file was deleted.
Oops, something went wrong.
153 changes: 153 additions & 0 deletions
153
go/cluster_management/cmd/create_multi_region/create_multi_region.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "time" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/service/dsql" | ||
| dtypes "github.com/aws/aws-sdk-go-v2/service/dsql/types" | ||
| ) | ||
|
|
||
| func CreateMultiRegionClusters(ctx context.Context, witness, region1, region2 string) error { | ||
|
|
||
| cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region1)) | ||
| if err != nil { | ||
| log.Fatalf("Failed to load AWS configuration: %v", err) | ||
| } | ||
|
|
||
| // Create a DSQL region 1 client | ||
| client := dsql.NewFromConfig(cfg, func(o *dsql.Options) { | ||
| o.Region = region1 | ||
| }) | ||
|
|
||
| cfg2, err := config.LoadDefaultConfig(ctx, config.WithRegion(region2)) | ||
| if err != nil { | ||
| log.Fatalf("Failed to load AWS configuration: %v", err) | ||
| } | ||
|
|
||
| // Create a DSQL region 2 client | ||
| client2 := dsql.NewFromConfig(cfg2, func(o *dsql.Options) { | ||
| o.Region = region2 | ||
| }) | ||
|
|
||
| // Create cluster | ||
| deleteProtect := true | ||
|
|
||
| // We can only set the witness region for the first cluster | ||
| input := &dsql.CreateClusterInput{ | ||
| DeletionProtectionEnabled: &deleteProtect, | ||
| MultiRegionProperties: &dtypes.MultiRegionProperties{ | ||
| WitnessRegion: aws.String(witness), | ||
| }, | ||
| Tags: map[string]string{ | ||
| "Name": "go multi-region cluster", | ||
| }, | ||
| } | ||
|
|
||
| clusterProperties, err := client.CreateCluster(context.Background(), input) | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("failed to create first cluster: %v", err) | ||
| } | ||
|
|
||
| // create second cluster | ||
| cluster2Arns := []string{*clusterProperties.Arn} | ||
|
|
||
| // For the second cluster we can set witness region and designate the first cluster as a peer | ||
| input2 := &dsql.CreateClusterInput{ | ||
| DeletionProtectionEnabled: &deleteProtect, | ||
| MultiRegionProperties: &dtypes.MultiRegionProperties{ | ||
| WitnessRegion: aws.String("us-west-2"), | ||
| Clusters: cluster2Arns, | ||
| }, | ||
| Tags: map[string]string{ | ||
| "Name": "go multi-region cluster", | ||
| }, | ||
| } | ||
|
|
||
| clusterProperties2, err := client2.CreateCluster(context.Background(), input2) | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("failed to create second cluster: %v", err) | ||
| } | ||
|
|
||
| // link initial cluster to second cluster | ||
| cluster1Arns := []string{*clusterProperties2.Arn} | ||
|
|
||
| // Now that we know the second cluster arn we can set it as a peer of the first cluster | ||
| input3 := dsql.UpdateClusterInput{ | ||
| Identifier: clusterProperties.Identifier, | ||
| MultiRegionProperties: &dtypes.MultiRegionProperties{ | ||
| WitnessRegion: aws.String("us-west-2"), | ||
| Clusters: cluster1Arns, | ||
| }} | ||
|
|
||
| _, err = client.UpdateCluster(context.Background(), &input3) | ||
|
|
||
| if err != nil { | ||
| return fmt.Errorf("failed to update cluster to associate with first cluster. %v", err) | ||
| } | ||
|
|
||
| // Create the waiter with our custom options for first cluster | ||
| waiter := dsql.NewClusterActiveWaiter(client, func(o *dsql.ClusterActiveWaiterOptions) { | ||
| o.MaxDelay = 30 * time.Second // Creating a multi-region cluster can take a few minutes | ||
| o.MinDelay = 10 * time.Second | ||
| o.LogWaitAttempts = true | ||
| }) | ||
|
|
||
| // Now that multiRegionProperties is fully defined for both clusters | ||
| // they'll begin the transition to ACTIVE | ||
|
|
||
| // Create the input for the clusterProperties to monitor for first cluster | ||
| getInput := &dsql.GetClusterInput{ | ||
| Identifier: clusterProperties.Identifier, | ||
| } | ||
|
|
||
| // Wait for the first cluster to become active | ||
| fmt.Printf("Waiting for first cluster %s to become active...\n", *clusterProperties.Identifier) | ||
| err = waiter.Wait(ctx, getInput, 5*time.Minute) | ||
| if err != nil { | ||
| return fmt.Errorf("error waiting for first cluster to become active: %w", err) | ||
| } | ||
|
|
||
| // Create the waiter with our custom options | ||
| waiter2 := dsql.NewClusterActiveWaiter(client2, func(o *dsql.ClusterActiveWaiterOptions) { | ||
| o.MaxDelay = 30 * time.Second // Creating a multi-region cluster can take a few minutes | ||
| o.MinDelay = 10 * time.Second | ||
| o.LogWaitAttempts = true | ||
| }) | ||
|
|
||
| // Create the input for the clusterProperties to monitor for second | ||
| getInput2 := &dsql.GetClusterInput{ | ||
| Identifier: clusterProperties2.Identifier, | ||
| } | ||
|
|
||
| // Wait for the second cluster to become active | ||
| fmt.Printf("Waiting for second cluster %s to become active...\n", *clusterProperties2.Identifier) | ||
| err = waiter2.Wait(ctx, getInput2, 5*time.Minute) | ||
| if err != nil { | ||
| return fmt.Errorf("error waiting for second cluster to become active: %w", err) | ||
| } | ||
|
|
||
| fmt.Printf("Cluster %s is now active\n", *clusterProperties.Identifier) | ||
| fmt.Printf("Cluster %s is now active\n", *clusterProperties2.Identifier) | ||
| return nil | ||
| } | ||
|
|
||
| // Example usage in main function | ||
| func main() { | ||
| // Set up context with timeout | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) | ||
| defer cancel() | ||
|
|
||
| err := CreateMultiRegionClusters(ctx, "us-west-2", "us-east-1", "us-east-2") | ||
| if err != nil { | ||
| fmt.Printf("failed to create multi-region clusters: %v", err) | ||
| panic(err) | ||
| } | ||
|
|
||
| } |
69 changes: 69 additions & 0 deletions
69
go/cluster_management/cmd/create_multi_region/create_multi_region_integ_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // Global variables for shared resources | ||
| var ( | ||
| testCtx context.Context | ||
| cancel context.CancelFunc | ||
| ) | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| // Setup before running tests | ||
| setup() | ||
|
|
||
| // Run all tests | ||
| code := m.Run() | ||
|
|
||
| // Cleanup after tests complete | ||
| teardown() | ||
|
|
||
| // Exit with the test status code | ||
| os.Exit(code) | ||
| } | ||
|
|
||
| func setup() { | ||
| // Initialize context with timeout for all tests | ||
| testCtx, cancel = context.WithTimeout(context.Background(), 10*time.Minute) | ||
| } | ||
|
|
||
| func teardown() { | ||
| // Cancel the context | ||
|
imforster marked this conversation as resolved.
Outdated
|
||
| cancel() | ||
|
|
||
| // Clean up any resources, close connections, etc. | ||
| } | ||
|
|
||
| // Test for CreateMultiRegionCluster function | ||
| func TestCreateMultiRegionCluster(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| region1 string | ||
| region2 string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "Create multi-region clusters", | ||
| region1: "us-east-1", | ||
| region2: "us-east-2", | ||
| wantErr: false, | ||
| }, | ||
| // Add more test cases as needed | ||
|
imforster marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := CreateMultiRegionClusters(testCtx, "us-west-2", tt.region1, tt.region2) | ||
| if err != nil { | ||
| fmt.Printf("failed to create multi-region clusters: %v", err) | ||
| panic(err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.