Skip to content

Commit

Permalink
Merge pull request #2 from Bit-Quill/victor/ClusterManagement
Browse files Browse the repository at this point in the history
Victor/cluster management
  • Loading branch information
imforster authored Jan 7, 2025
2 parents c16f518 + 8aeef33 commit 04b8dae
Show file tree
Hide file tree
Showing 15 changed files with 480 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/go-cm-integ-tests.yml
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The subdirectories contain code examples for connecting and using Aurora DSQL in
| C++ | [cluster_management](cpp/cluster_management) |
| C# (dotnet) | [cluster_management](dotnet/cluster_management) |
| Java | [cluster_management](java/cluster_management) |
| Go | [cluster_management](go/cluster_management) |
| Javascript | [cluster_management](javascript/cluster_management) |
| Python | [cluster_management](python/cluster_management) |
| Ruby | [cluster_management](ruby/cluster_management) |
Expand Down
3 changes: 3 additions & 0 deletions go/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ go.work.sum

# env file
.env

# Mac Folder Metadata
**/.DS_Store
33 changes: 33 additions & 0 deletions go/cluster_management/README.md
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
45 changes: 45 additions & 0 deletions go/cluster_management/client_util.go
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
}
44 changes: 44 additions & 0 deletions go/cluster_management/create_multi_region.go
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
}
18 changes: 18 additions & 0 deletions go/cluster_management/create_single_region.go
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
}
17 changes: 17 additions & 0 deletions go/cluster_management/delete_multi_region.go
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
}
17 changes: 17 additions & 0 deletions go/cluster_management/delete_single_region.go
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
}
83 changes: 83 additions & 0 deletions go/cluster_management/example.go
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
}
64 changes: 64 additions & 0 deletions go/cluster_management/example_test.go
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")
}
Loading

0 comments on commit 04b8dae

Please sign in to comment.