forked from aws-samples/aurora-dsql-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Bit-Quill/victor/ClusterManagement
Victor/cluster management
- Loading branch information
Showing
15 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,52 @@ | ||
name: pgx(go) Integration tests | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
paths: | ||
- 'go/cluster_management/**' | ||
- '.github/workflows/go-cm-integ-tests.yml' | ||
pull_request: | ||
branches: [ "main" ] | ||
paths: | ||
- 'go/cluster_management/**' | ||
- '.github/workflows/go-cm-integ-tests.yml' | ||
# Give us a button to allow running the workflow on demand for testing. | ||
workflow_dispatch: | ||
inputs: | ||
tags: | ||
description: 'Manual Workflow Run' | ||
required: false | ||
type: string | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
id-token: write | ||
contents: read | ||
# Explicitly set permissions, following the principle of least privilege | ||
actions: read | ||
checks: write | ||
pull-requests: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.23.2' | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
role-to-assume: ${{ secrets.GO_IAM_ROLE }} | ||
aws-region: us-east-1 | ||
|
||
- name: Build & Run | ||
working-directory: ./go/pgx | ||
run: | | ||
go env -w GOPROXY=direct | ||
go test |
This file contains 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 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 |
---|---|---|
|
@@ -23,3 +23,6 @@ go.work.sum | |
|
||
# env file | ||
.env | ||
|
||
# Mac Folder Metadata | ||
**/.DS_Store |
This file contains 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,33 @@ | ||
# Aurora DSQL Go SDK code examples | ||
|
||
## Overview | ||
|
||
The code examples in this topic show you how to use the AWS Go SDK with DSQL to create, read, update, and delete clusters. | ||
|
||
## Run the examples | ||
|
||
### Prerequisites | ||
|
||
* Go version >= 1.21 | ||
* AWS credentials file is configured | ||
|
||
|
||
### Setup test running environment | ||
|
||
Ensure you are authenticated with AWS credentials. No other setup is needed besides having Go installed. | ||
|
||
### Run the example tests | ||
|
||
In a terminal run the following commands: | ||
|
||
```sh | ||
# Use the account credentials dedicated for golang | ||
go env -w GOPROXY=direct | ||
go test | ||
``` | ||
|
||
--- | ||
|
||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: MIT-0 |
This file contains 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,45 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
dsql "github.com/aws/aws-sdk-go-v2/service/dsql" | ||
) | ||
|
||
type ClientUtil struct { | ||
clients map[string]*dsql.Client | ||
} | ||
|
||
func (clientUtil *ClientUtil) setRegion(region string) func(*dsql.Options) { | ||
return func(options *dsql.Options) { | ||
options.Region = region | ||
} | ||
} | ||
|
||
func (clientUtil *ClientUtil) GetInstance(region string) (client *dsql.Client, err error) { | ||
|
||
if clientUtil.clients == nil { | ||
clientUtil.clients = map[string]*dsql.Client{} | ||
} | ||
|
||
_, isExists := clientUtil.clients[region] | ||
|
||
if !isExists { | ||
cfg, err := config.LoadDefaultConfig(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
newClient := dsql.NewFromConfig(cfg, clientUtil.setRegion(region)) | ||
if newClient == nil { | ||
return nil, errors.New("failed to get a new client") | ||
} | ||
clientUtil.clients[region] = newClient | ||
} | ||
|
||
client = clientUtil.clients[region] | ||
|
||
return | ||
} |
This file contains 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,44 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
dsql "github.com/aws/aws-sdk-go-v2/service/dsql" | ||
"github.com/aws/aws-sdk-go-v2/service/dsql/types" | ||
) | ||
|
||
func CreateMultiRegionCluster(client *dsql.Client) (clustersStatus *dsql.CreateMultiRegionClustersOutput, err error) { | ||
|
||
deleteProtection := false | ||
witnessRegion := "us-west-2" | ||
|
||
usEast1Props := types.LinkedClusterProperties{ | ||
DeletionProtectionEnabled: &deleteProtection, | ||
Tags: map[string]string{ | ||
"Name": "us-east-1-go-example-cluster", | ||
"Usercase": "testing-mr-use1", | ||
}, | ||
} | ||
|
||
usEast2Props := types.LinkedClusterProperties{ | ||
DeletionProtectionEnabled: &deleteProtection, | ||
Tags: map[string]string{ | ||
"Name": "us-east-2-go-example-cluster", | ||
"Usercase": "testing-mr-use2", | ||
}, | ||
} | ||
|
||
clusterProperties := map[string]types.LinkedClusterProperties{ | ||
"us-east-1": usEast1Props, | ||
"us-east-2": usEast2Props, | ||
} | ||
|
||
input := dsql.CreateMultiRegionClustersInput{ | ||
LinkedRegionList: []string{"us-east-1", "us-east-2"}, | ||
WitnessRegion: &witnessRegion, | ||
ClusterProperties: clusterProperties, | ||
} | ||
|
||
clustersStatus, err = client.CreateMultiRegionClusters(context.Background(), &input) | ||
return | ||
} |
This file contains 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,18 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
dsql "github.com/aws/aws-sdk-go-v2/service/dsql" | ||
) | ||
|
||
func CreateCluster(client *dsql.Client, deleteProtection bool, tags map[string]string) (clusterStatus *dsql.CreateClusterOutput, err error) { | ||
|
||
input := dsql.CreateClusterInput{ | ||
DeletionProtectionEnabled: &deleteProtection, | ||
Tags: tags, | ||
} | ||
|
||
clusterStatus, err = client.CreateCluster(context.Background(), &input) | ||
return | ||
} |
This file contains 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,17 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
dsql "github.com/aws/aws-sdk-go-v2/service/dsql" | ||
) | ||
|
||
func DeleteMultiRegionCluster(arnList []string, client *dsql.Client) (clusterStatus *dsql.DeleteMultiRegionClustersOutput, err error) { | ||
|
||
input := dsql.DeleteMultiRegionClustersInput{ | ||
LinkedClusterArns: arnList, | ||
} | ||
|
||
clusterStatus, err = client.DeleteMultiRegionClusters(context.Background(), &input) | ||
return | ||
} |
This file contains 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,17 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
|
||
dsql "github.com/aws/aws-sdk-go-v2/service/dsql" | ||
) | ||
|
||
func DeleteCluster(id string, client *dsql.Client) (clusterStatus *dsql.DeleteClusterOutput, err error) { | ||
|
||
input := dsql.DeleteClusterInput{ | ||
Identifier: &id, | ||
} | ||
|
||
clusterStatus, err = client.DeleteCluster(context.Background(), &input) | ||
return | ||
} |
This file contains 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,83 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
dsql "github.com/aws/aws-sdk-go-v2/service/dsql" | ||
"github.com/aws/aws-sdk-go-v2/service/dsql/types" | ||
) | ||
|
||
func SingleRegionTest(client *dsql.Client) (err error) { | ||
|
||
deleteProtectionEnabled := true | ||
|
||
createdClusterStatus, err := CreateCluster(client, deleteProtectionEnabled, map[string]string{"Name": "ExampleClusterGo"}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clusterId := createdClusterStatus.Identifier | ||
|
||
if clusterId == nil || (clusterId != nil && len(*clusterId) == 0) { | ||
return errors.New("the cluster identifier is missing after creating a single cluster") | ||
} | ||
|
||
time.Sleep(60 * time.Second) // Just an approximate arbitrarily chosen time | ||
|
||
getClusterStatus, err := GetCluster(*clusterId, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if getClusterStatus.Status == types.ClusterStatusFailed { | ||
return errors.New("the cluster failed") | ||
} | ||
|
||
deleteProtectionEnabled = false | ||
updateClusteStatus, err := UpdateCluster(*clusterId, deleteProtectionEnabled, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if updateClusteStatus.Status != types.ClusterStatusUpdating { | ||
return errors.New("updateCluster failed") | ||
} | ||
|
||
time.Sleep(5 * time.Second) // Just an approximate arbitrarily chosen time | ||
|
||
fmt.Println("Deleting Cluster") | ||
deleteClusterStatus, err := DeleteCluster(*clusterId, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if deleteClusterStatus.Status != types.ClusterStatusDeleting { | ||
return errors.New("deleteCluster failed") | ||
} | ||
|
||
return err | ||
} | ||
|
||
func MultiRegionTest(client *dsql.Client) (err error) { | ||
|
||
multiRegionClusterStatus, err := CreateMultiRegionCluster(client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
time.Sleep(60 * time.Second) // Just an approximate arbitrarily chosen time | ||
|
||
_, err = DeleteMultiRegionCluster(multiRegionClusterStatus.LinkedClusterArns, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Deleted Cluster ARNs: ") | ||
for _, arn := range multiRegionClusterStatus.LinkedClusterArns { | ||
fmt.Println(arn) | ||
} | ||
|
||
return err | ||
} |
This file contains 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,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/dsql" | ||
) | ||
|
||
var client *dsql.Client | ||
var region = "us-east-1" | ||
|
||
func TestMain(m *testing.M) { | ||
|
||
awsDefaultRegion := os.Getenv("AWS_DEFAULT_REGION") // AWS CLI v1 environment region variable | ||
if awsDefaultRegion != "" { | ||
region = awsDefaultRegion | ||
} | ||
|
||
awsRegion := os.Getenv("AWS_REGION") // AWS CLI v2 environment region variable | ||
if awsRegion != "" { | ||
region = awsRegion | ||
} | ||
fmt.Println("Initializing the AWS client in region:" + region) | ||
clientUtil := ClientUtil{} | ||
|
||
awsClient, err := clientUtil.GetInstance(region) | ||
if err != nil { | ||
fmt.Println("failed to get the aws client:" + err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
client = awsClient | ||
|
||
if client == nil { | ||
fmt.Println("aws client is not initialized") | ||
os.Exit(1) | ||
} | ||
|
||
m.Run() | ||
|
||
fmt.Println("All tests completed") | ||
|
||
} | ||
|
||
func TestSingleRegion(t *testing.T) { | ||
fmt.Println("Single Region Cluster Test: Starting") | ||
|
||
err := SingleRegionTest(client) | ||
if err != nil { | ||
t.Error("single region test failed:" + err.Error()) | ||
} | ||
fmt.Println("Single Region Cluster Test: Completed") | ||
} | ||
|
||
func TestMultiRegion(t *testing.T) { | ||
fmt.Println("Multi Region Cluster Test: Starting") | ||
err := MultiRegionTest(client) | ||
if err != nil { | ||
t.Error("multi region test failed:" + err.Error()) | ||
} | ||
fmt.Println("Multi Region Cluster Test: Completed") | ||
} |
Oops, something went wrong.