Skip to content

Commit

Permalink
Includes a lot of fixes for comments to the PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
beqqrry-aws committed Sep 20, 2024
1 parent f8f9d6c commit 4a27622
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 156 deletions.
38 changes: 11 additions & 27 deletions .doc_gen/metadata/redshift_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ redshift_Hello:
snippet_tags:
- python.example_code.redshift.Hello
services:
redshift: {describeClusters}
redshift: {DescribeClusters}
redshift_ListDatabases:
languages:
Java:
Expand All @@ -54,6 +54,8 @@ redshift_CreateCluster:
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.CreateCluster
Kotlin:
versions:
Expand Down Expand Up @@ -109,6 +111,8 @@ redshift_DeleteCluster:
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.DeleteCluster
Kotlin:
versions:
Expand Down Expand Up @@ -164,6 +168,8 @@ redshift_DescribeClusters:
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.DescribeClusters
Kotlin:
versions:
Expand Down Expand Up @@ -219,6 +225,8 @@ redshift_ModifyCluster:
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.ModifyCluster
Kotlin:
versions:
Expand Down Expand Up @@ -267,14 +275,6 @@ redshift_ModifyCluster:
redshift: {ModifyCluster}
redshift_DescribeStatement:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.DescribeStatement
Java:
versions:
- sdk_version: 2
Expand All @@ -301,14 +301,6 @@ redshift_DescribeStatement:
redshift: {DescribeStatement}
redshift_GetStatementResult:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.GetStatementResult
Java:
versions:
- sdk_version: 2
Expand All @@ -335,14 +327,6 @@ redshift_GetStatementResult:
redshift: {GetStatementResult}
redshift_ExecuteStatement:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.CreateTable
Java:
versions:
- sdk_version: 2
Expand All @@ -361,7 +345,7 @@ redshift_ExecuteStatement:
services:
redshift: {ExecuteStatement}
redshift_Scenario:
synopsis: learn core operations for &RS; using an &AWS;.
synopsis: learn core operations for &RS; using an &AWS; SDK.
category: Basics
languages:
Go:
Expand Down Expand Up @@ -407,4 +391,4 @@ redshift_Scenario:
- python.example_code.redshift_data.ListDatabases
- python.example_code.redshift.DeleteCluster
services:
redshift: {createCluster, describeClusters, executeStatement, describeStatement, modifyCluster, getStatementResult, listDatabasesPaginator}
redshift: {CreateCluster, DescribeClusters, ExecuteStatement, DescribeStatement, ModifyCluster, GetStatementResult, ListDatabasesPaginator}
13 changes: 5 additions & 8 deletions gov2/redshift/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ Code examples that show you how to perform the essential operations within a ser

Code excerpts that show you how to call individual service functions.

- [CreateCluster](actions/redshift_actions.go#L20)
- [CreateTable](actions/redshiftdata_actions.go#L75)
- [DeleteCluster](actions/redshift_actions.go#L72)
- [DescribeClusters](scenarios/redshift_basics.go#L242)
- [DescribeStatement](actions/redshiftdata_actions.go#L173)
- [GetStatementResult](actions/redshiftdata_actions.go#L199)
- [ModifyCluster](actions/redshift_actions.go#L50)
- [CreateCluster](actions/redshift_actions.go#L29)
- [DeleteCluster](actions/redshift_actions.go#L84)
- [DescribeClusters](actions/redshift_actions.go#L108)
- [ModifyCluster](actions/redshift_actions.go#L58)


<!--custom.examples.start-->
Expand Down Expand Up @@ -83,7 +80,7 @@ go run ./cmd -h
```
#### Learn the basics

This example shows you how to work with Amazon Redshift tables, items, and queries.
This example shows you how to learn core operations for Amazon Redshift using an AWS SDK.


<!--custom.basic_prereqs.redshift_Scenario.start-->
Expand Down
54 changes: 45 additions & 9 deletions gov2/redshift/actions/redshift_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@

package actions

// snippet-start:[gov2.redshift.Imports]

import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/redshift"
"github.com/aws/aws-sdk-go-v2/service/redshift/types"
"log"
"time"
)

// snippet-end:[gov2.redshift.Imports]

// snippet-start:[gov2.redshift.ActionsStruct]

// RedshiftActions wraps Redshift service actions.
type RedshiftActions struct {
RedshiftClient *redshift.Client
}

// snippet-end:[gov2.redshift.ActionsStruct]

// snippet-start:[gov2.redshift.CreateCluster]

// CreateCluster sends a request to create a cluster with the given clusterId using the provided credentials.
Expand All @@ -30,7 +39,6 @@ func (actor RedshiftActions) CreateCluster(ctx context.Context, clusterId string
ClusterType: aws.String(clusterType),
PubliclyAccessible: aws.Bool(publiclyAccessible),
}

var opErr *types.ClusterAlreadyExistsFault
output, err := actor.RedshiftClient.CreateCluster(ctx, input)
if err != nil && errors.As(err, &opErr) {
Expand All @@ -57,10 +65,14 @@ func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string
PreferredMaintenanceWindow: aws.String(maintenanceWindow),
}

var opErr *types.InvalidClusterStateFault
output, err := actor.RedshiftClient.ModifyCluster(ctx, input)
if err != nil {
if err != nil && errors.As(err, &opErr) {
log.Println("Cluster is in an invalid state.")
panic(err)
} else if err != nil {
log.Printf("Failed to modify Redshift cluster: %v\n", err)
return nil
panic(err)
}

log.Printf("The cluster was successfully modified and now has %s as the maintenance window\n", *output.Cluster.PreferredMaintenanceWindow)
Expand All @@ -74,12 +86,16 @@ func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string
// DeleteCluster deletes the given cluster.
func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string) (bool, error) {
// Delete the specified Redshift cluster
input := &redshift.DeleteClusterInput{
ClusterIdentifier: aws.String(clusterId),
SkipFinalClusterSnapshot: aws.Bool(true),
}
_, err := actor.RedshiftClient.DeleteCluster(ctx, input)
if err != nil {

waiter := redshift.NewClusterDeletedWaiter(actor.RedshiftClient)
err := waiter.Wait(ctx, &redshift.DescribeClustersInput{
ClusterIdentifier: aws.String(clusterId),
}, 5*time.Minute)
var opErr *types.ClusterNotFoundFault
if err != nil && errors.As(err, &opErr) {
log.Println("Cluster was not found. Where could it be?")
return false, err
} else if err != nil {
log.Printf("Failed to delete Redshift cluster: %v\n", err)
return false, err
}
Expand All @@ -88,3 +104,23 @@ func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string
}

// snippet-end:[gov2.redshift.DeleteCluster]

// snippet-start:[gov2.redshift.DescribeClusters]

// DescribeClusters returns information about the given cluster.
func (actor RedshiftActions) DescribeClusters(ctx context.Context, clusterId string) (*redshift.DescribeClustersOutput, error) {
input, err := actor.RedshiftClient.DescribeClusters(ctx, &redshift.DescribeClustersInput{
ClusterIdentifier: aws.String(clusterId),
})
var opErr *types.AccessToClusterDeniedFault
if errors.As(err, &opErr) {
println("Access to cluster denied.")
panic(err)
} else if err != nil {
println("Failed to describe Redshift clusters.")
return nil, err
}
return input, nil
}

// snippet-end:[gov2.redshift.DescribeClusters]
73 changes: 56 additions & 17 deletions gov2/redshift/actions/redshiftdata_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import (
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/redshiftdata"
"github.com/aws/aws-sdk-go-v2/service/redshiftdata/types"
"github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools"
"log"
)

// snippet-start:[gov2.redshift.DataActionsStruct]

// RedshiftDataActions wraps RedshiftData actions.
type RedshiftDataActions struct {
RedshiftDataClient *redshiftdata.Client
}

// snippet-end:[gov2.redshift.DataActionsStruct]

// snippet-start:[gov2.redshift.RedshiftQuery.struct]

// RedshiftQuery makes it easier to deal with RedshiftQuery objects.
Expand All @@ -32,9 +37,7 @@ type RedshiftQuery struct {

// ExecuteStatement calls the ExecuteStatement operation from the RedshiftDataClient
func (actor RedshiftDataActions) ExecuteStatement(ctx context.Context, input redshiftdata.ExecuteStatementInput) (*redshiftdata.ExecuteStatementOutput, error) {

return actor.RedshiftDataClient.ExecuteStatement(ctx, &input)

}

// snippet-end:[gov2.redshift.ExecuteStatement
Expand All @@ -52,19 +55,28 @@ func (actor RedshiftDataActions) ExecuteBatchStatement(ctx context.Context, inpu

// ListDatabases lists all databases in the given cluster.
func (actor RedshiftDataActions) ListDatabases(ctx context.Context, clusterId string, databaseName string, userName string) error {
input := redshiftdata.ListDatabasesInput{
ClusterIdentifier: aws.String(clusterId),

var opErr *types.DatabaseConnectionException
var databaseNames []string
paginator := redshiftdata.NewListDatabasesPaginator(actor.RedshiftDataClient, &redshiftdata.ListDatabasesInput{
Database: aws.String(databaseName),
ClusterIdentifier: aws.String(clusterId),
DbUser: aws.String(userName),
})
for paginator.HasMorePages() {
output, err := paginator.NextPage(ctx)
if err != nil && errors.As(err, &opErr) {
log.Printf("Could not connect to the database.")
panic(err)
} else if err != nil {
log.Printf("Couldn't finish listing the tables. Here's why: %v\n", err)
return err
} else {
databaseNames = append(databaseNames, output.Databases...)
}
}

output, err := actor.RedshiftDataClient.ListDatabases(ctx, &input)
if err != nil {
log.Printf("Failed to list databases: %v\n", err)
return err
}

for _, database := range output.Databases {
for _, database := range databaseNames {
log.Printf("The database name is : %s\n", database)
}
return nil
Expand All @@ -90,8 +102,11 @@ func (actor RedshiftDataActions) CreateTable(ctx context.Context, clusterId stri
Sql: aws.String(sql),
}

var opErr *types.DatabaseConnectionException
output, err := actor.RedshiftDataClient.ExecuteStatement(ctx, createTableInput)
if err != nil {
if err != nil && errors.As(err, &opErr) {
log.Printf("Could not connect to the database.")
} else if err != nil {
log.Printf("Failed to create table: %v\n", err)
return err
}
Expand All @@ -114,8 +129,11 @@ func (actor RedshiftDataActions) DeleteTable(ctx context.Context, clusterId stri
Sql: aws.String(sql),
}

var opErr *types.DatabaseConnectionException
output, err := actor.RedshiftDataClient.ExecuteStatement(ctx, deleteTableInput)
if err != nil {
if err != nil && errors.As(err, &opErr) {
log.Printf("Could not connect to the database.")
} else if err != nil {
log.Printf("Failed to delete table "+tableName+" from "+databaseName+" database: %v\n", err)
return false, err
}
Expand Down Expand Up @@ -162,6 +180,24 @@ func (actor RedshiftDataActions) DeleteDataRows(ctx context.Context, clusterId s

// snippet-end:[gov2.redshift.DeleteRows]

// snippet-start:[gov2.redshift.DescribeStatement]

// DescribeStatement gets information about the given statement.
func (actor RedshiftDataActions) DescribeStatement(query RedshiftQuery) (*redshiftdata.DescribeStatementOutput, error) {
describeOutput, err := actor.RedshiftDataClient.DescribeStatement(query.Context, &query.Input)
var opErr *types.QueryTimeoutException
if errors.As(err, &opErr) {
println("The connection to the redshift data request timed out.")
panic(err)
} else if err != nil {
println("Failed to execute describe statement")
return nil, err
}
return describeOutput, nil
}

// snippet-end:[gov2.redshift.DescribeStatement]

// snippet-start:[gov2.redshift.WaitForQueryStatus]

// WaitForQueryStatus waits until the given RedshiftQuery object has succeeded or failed.
Expand All @@ -170,8 +206,7 @@ func (actor RedshiftDataActions) WaitForQueryStatus(query RedshiftQuery, pauser
attempts := 0
maxWaitCycles := 30
for done == false {
// snippet-start:[gov2.redshift.DescribeStatement]
describeOutput, err := actor.RedshiftDataClient.DescribeStatement(query.Context, &query.Input)
describeOutput, err := actor.DescribeStatement(query)
if err != nil {
return err
}
Expand All @@ -187,7 +222,6 @@ func (actor RedshiftDataActions) WaitForQueryStatus(query RedshiftQuery, pauser
if describeOutput.Status == "FINISHED" {
done = true
}
// snippet-end:[gov2.redshift.DescribeStatement]
attempts++
pauser.Pause(attempts)
}
Expand All @@ -200,10 +234,15 @@ func (actor RedshiftDataActions) WaitForQueryStatus(query RedshiftQuery, pauser

// GetStatementResult returns the result of the statement with the given id.
func (actor RedshiftDataActions) GetStatementResult(ctx context.Context, statementId string) (*redshiftdata.GetStatementResultOutput, error) {

var opErr *types.QueryTimeoutException
getStatementResultOutput, err := actor.RedshiftDataClient.GetStatementResult(ctx, &redshiftdata.GetStatementResultInput{
Id: aws.String(statementId),
})
if err != nil {
if err != nil && errors.As(err, &opErr) {
log.Printf("Query timed out: %v\n", err)
return nil, err
} else if err != nil {
return nil, err
}
return getStatementResultOutput, nil
Expand Down
4 changes: 1 addition & 3 deletions gov2/redshift/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (
//
// `-scenario` can be one of the following:
//
// - `getstarted` - Runs the interactive get started scenario that shows you how to use
// Amazon Simple Storage Service (Amazon S3) actions to work with
// S3 buckets and objects.
// - `basics` - Runs the interactive Basics scenario to show core Redshift actions.
func main() {
scenarioMap := map[string]func(sdkConfig aws.Config, helper scenarios.IScenarioHelper){
"basics": runRedshiftBasicsScenario,
Expand Down
Loading

0 comments on commit 4a27622

Please sign in to comment.