Skip to content

Commit f19d143

Browse files
committed
Includes a lot of fixes for comments to the PR.
1 parent f8f9d6c commit f19d143

File tree

16 files changed

+263
-161
lines changed

16 files changed

+263
-161
lines changed

.doc_gen/metadata/redshift_metadata.yaml

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ redshift_Hello:
3131
snippet_tags:
3232
- python.example_code.redshift.Hello
3333
services:
34-
redshift: {describeClusters}
34+
redshift: {DescribeClusters}
3535
redshift_ListDatabases:
3636
languages:
3737
Java:
@@ -54,6 +54,8 @@ redshift_CreateCluster:
5454
excerpts:
5555
- description:
5656
snippet_tags:
57+
- gov2.redshift.Imports
58+
- gov2.redshift.ActionsStruct
5759
- gov2.redshift.CreateCluster
5860
Kotlin:
5961
versions:
@@ -109,6 +111,8 @@ redshift_DeleteCluster:
109111
excerpts:
110112
- description:
111113
snippet_tags:
114+
- gov2.redshift.Imports
115+
- gov2.redshift.ActionsStruct
112116
- gov2.redshift.DeleteCluster
113117
Kotlin:
114118
versions:
@@ -164,6 +168,8 @@ redshift_DescribeClusters:
164168
excerpts:
165169
- description:
166170
snippet_tags:
171+
- gov2.redshift.Imports
172+
- gov2.redshift.ActionsStruct
167173
- gov2.redshift.DescribeClusters
168174
Kotlin:
169175
versions:
@@ -219,6 +225,8 @@ redshift_ModifyCluster:
219225
excerpts:
220226
- description:
221227
snippet_tags:
228+
- gov2.redshift.Imports
229+
- gov2.redshift.ActionsStruct
222230
- gov2.redshift.ModifyCluster
223231
Kotlin:
224232
versions:
@@ -267,14 +275,6 @@ redshift_ModifyCluster:
267275
redshift: {ModifyCluster}
268276
redshift_DescribeStatement:
269277
languages:
270-
Go:
271-
versions:
272-
- sdk_version: 2
273-
github: gov2/redshift
274-
excerpts:
275-
- description:
276-
snippet_tags:
277-
- gov2.redshift.DescribeStatement
278278
Java:
279279
versions:
280280
- sdk_version: 2
@@ -301,14 +301,6 @@ redshift_DescribeStatement:
301301
redshift: {DescribeStatement}
302302
redshift_GetStatementResult:
303303
languages:
304-
Go:
305-
versions:
306-
- sdk_version: 2
307-
github: gov2/redshift
308-
excerpts:
309-
- description:
310-
snippet_tags:
311-
- gov2.redshift.GetStatementResult
312304
Java:
313305
versions:
314306
- sdk_version: 2
@@ -335,14 +327,6 @@ redshift_GetStatementResult:
335327
redshift: {GetStatementResult}
336328
redshift_ExecuteStatement:
337329
languages:
338-
Go:
339-
versions:
340-
- sdk_version: 2
341-
github: gov2/redshift
342-
excerpts:
343-
- description:
344-
snippet_tags:
345-
- gov2.redshift.CreateTable
346330
Java:
347331
versions:
348332
- sdk_version: 2
@@ -361,7 +345,7 @@ redshift_ExecuteStatement:
361345
services:
362346
redshift: {ExecuteStatement}
363347
redshift_Scenario:
364-
synopsis: learn core operations for &RS; using an &AWS;.
348+
synopsis: learn core operations for &RS; using an &AWS; SDK.
365349
category: Basics
366350
languages:
367351
Go:
@@ -407,4 +391,4 @@ redshift_Scenario:
407391
- python.example_code.redshift_data.ListDatabases
408392
- python.example_code.redshift.DeleteCluster
409393
services:
410-
redshift: {createCluster, describeClusters, executeStatement, describeStatement, modifyCluster, getStatementResult, listDatabasesPaginator}
394+
redshift: {CreateCluster, DescribeClusters, ExecuteStatement, DescribeStatement, ModifyCluster, GetStatementResult, ListDatabasesPaginator}

gov2/redshift/README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ For prerequisites, see the [README](../README.md#Prerequisites) in the `gov2` fo
3131

3232
### Get started
3333

34-
- [Hello Amazon Redshift](hello/hello.go#L4) (`describeClusters`)
34+
- [Hello Amazon Redshift](hello/hello.go#L4) (`DescribeClusters`)
3535

3636

3737
### Basics
@@ -45,13 +45,10 @@ Code examples that show you how to perform the essential operations within a ser
4545

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

48-
- [CreateCluster](actions/redshift_actions.go#L20)
49-
- [CreateTable](actions/redshiftdata_actions.go#L75)
50-
- [DeleteCluster](actions/redshift_actions.go#L72)
51-
- [DescribeClusters](scenarios/redshift_basics.go#L242)
52-
- [DescribeStatement](actions/redshiftdata_actions.go#L173)
53-
- [GetStatementResult](actions/redshiftdata_actions.go#L199)
54-
- [ModifyCluster](actions/redshift_actions.go#L50)
48+
- [CreateCluster](actions/redshift_actions.go#L29)
49+
- [DeleteCluster](actions/redshift_actions.go#L84)
50+
- [DescribeClusters](actions/redshift_actions.go#L108)
51+
- [ModifyCluster](actions/redshift_actions.go#L58)
5552

5653

5754
<!--custom.examples.start-->
@@ -83,7 +80,7 @@ go run ./cmd -h
8380
```
8481
#### Learn the basics
8582

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

8885

8986
<!--custom.basic_prereqs.redshift_Scenario.start-->

gov2/redshift/actions/redshift_actions.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33

44
package actions
55

6+
// snippet-start:[gov2.redshift.Imports]
7+
68
import (
79
"context"
810
"errors"
911
"github.com/aws/aws-sdk-go-v2/aws"
1012
"github.com/aws/aws-sdk-go-v2/service/redshift"
1113
"github.com/aws/aws-sdk-go-v2/service/redshift/types"
1214
"log"
15+
"time"
1316
)
1417

18+
// snippet-end:[gov2.redshift.Imports]
19+
20+
// snippet-start:[gov2.redshift.ActionsStruct]
21+
1522
// RedshiftActions wraps Redshift service actions.
1623
type RedshiftActions struct {
1724
RedshiftClient *redshift.Client
1825
}
1926

27+
// snippet-end:[gov2.redshift.ActionsStruct]
28+
2029
// snippet-start:[gov2.redshift.CreateCluster]
2130

2231
// CreateCluster sends a request to create a cluster with the given clusterId using the provided credentials.
@@ -30,7 +39,6 @@ func (actor RedshiftActions) CreateCluster(ctx context.Context, clusterId string
3039
ClusterType: aws.String(clusterType),
3140
PubliclyAccessible: aws.Bool(publiclyAccessible),
3241
}
33-
3442
var opErr *types.ClusterAlreadyExistsFault
3543
output, err := actor.RedshiftClient.CreateCluster(ctx, input)
3644
if err != nil && errors.As(err, &opErr) {
@@ -57,10 +65,14 @@ func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string
5765
PreferredMaintenanceWindow: aws.String(maintenanceWindow),
5866
}
5967

68+
var opErr *types.InvalidClusterStateFault
6069
output, err := actor.RedshiftClient.ModifyCluster(ctx, input)
61-
if err != nil {
70+
if err != nil && errors.As(err, &opErr) {
71+
log.Println("Cluster is in an invalid state.")
72+
panic(err)
73+
} else if err != nil {
6274
log.Printf("Failed to modify Redshift cluster: %v\n", err)
63-
return nil
75+
panic(err)
6476
}
6577

6678
log.Printf("The cluster was successfully modified and now has %s as the maintenance window\n", *output.Cluster.PreferredMaintenanceWindow)
@@ -74,12 +86,16 @@ func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string
7486
// DeleteCluster deletes the given cluster.
7587
func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string) (bool, error) {
7688
// Delete the specified Redshift cluster
77-
input := &redshift.DeleteClusterInput{
78-
ClusterIdentifier: aws.String(clusterId),
79-
SkipFinalClusterSnapshot: aws.Bool(true),
80-
}
81-
_, err := actor.RedshiftClient.DeleteCluster(ctx, input)
82-
if err != nil {
89+
90+
waiter := redshift.NewClusterDeletedWaiter(actor.RedshiftClient)
91+
err := waiter.Wait(ctx, &redshift.DescribeClustersInput{
92+
ClusterIdentifier: aws.String(clusterId),
93+
}, 5*time.Minute)
94+
var opErr *types.ClusterNotFoundFault
95+
if err != nil && errors.As(err, &opErr) {
96+
log.Println("Cluster was not found. Where could it be?")
97+
return false, err
98+
} else if err != nil {
8399
log.Printf("Failed to delete Redshift cluster: %v\n", err)
84100
return false, err
85101
}
@@ -88,3 +104,23 @@ func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string
88104
}
89105

90106
// snippet-end:[gov2.redshift.DeleteCluster]
107+
108+
// snippet-start:[gov2.redshift.DescribeClusters]
109+
110+
// DescribeClusters returns information about the given cluster.
111+
func (actor RedshiftActions) DescribeClusters(ctx context.Context, clusterId string) (*redshift.DescribeClustersOutput, error) {
112+
input, err := actor.RedshiftClient.DescribeClusters(ctx, &redshift.DescribeClustersInput{
113+
ClusterIdentifier: aws.String(clusterId),
114+
})
115+
var opErr *types.AccessToClusterDeniedFault
116+
if errors.As(err, &opErr) {
117+
println("Access to cluster denied.")
118+
panic(err)
119+
} else if err != nil {
120+
println("Failed to describe Redshift clusters.")
121+
return nil, err
122+
}
123+
return input, nil
124+
}
125+
126+
// snippet-end:[gov2.redshift.DescribeClusters]

gov2/redshift/actions/redshiftdata_actions.go

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ import (
88
"errors"
99
"github.com/aws/aws-sdk-go-v2/aws"
1010
"github.com/aws/aws-sdk-go-v2/service/redshiftdata"
11+
"github.com/aws/aws-sdk-go-v2/service/redshiftdata/types"
1112
"github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools"
1213
"log"
1314
)
1415

16+
// snippet-start:[gov2.redshift.DataActionsStruct]
17+
1518
// RedshiftDataActions wraps RedshiftData actions.
1619
type RedshiftDataActions struct {
1720
RedshiftDataClient *redshiftdata.Client
1821
}
1922

23+
// snippet-end:[gov2.redshift.DataActionsStruct]
24+
2025
// snippet-start:[gov2.redshift.RedshiftQuery.struct]
2126

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

3338
// ExecuteStatement calls the ExecuteStatement operation from the RedshiftDataClient
3439
func (actor RedshiftDataActions) ExecuteStatement(ctx context.Context, input redshiftdata.ExecuteStatementInput) (*redshiftdata.ExecuteStatementOutput, error) {
35-
3640
return actor.RedshiftDataClient.ExecuteStatement(ctx, &input)
37-
3841
}
3942

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

5356
// ListDatabases lists all databases in the given cluster.
5457
func (actor RedshiftDataActions) ListDatabases(ctx context.Context, clusterId string, databaseName string, userName string) error {
55-
input := redshiftdata.ListDatabasesInput{
56-
ClusterIdentifier: aws.String(clusterId),
58+
59+
var opErr *types.DatabaseConnectionException
60+
var databaseNames []string
61+
paginator := redshiftdata.NewListDatabasesPaginator(actor.RedshiftDataClient, &redshiftdata.ListDatabasesInput{
5762
Database: aws.String(databaseName),
63+
ClusterIdentifier: aws.String(clusterId),
5864
DbUser: aws.String(userName),
65+
})
66+
for paginator.HasMorePages() {
67+
output, err := paginator.NextPage(ctx)
68+
if err != nil && errors.As(err, &opErr) {
69+
log.Printf("Could not connect to the database.")
70+
panic(err)
71+
} else if err != nil {
72+
log.Printf("Couldn't finish listing the tables. Here's why: %v\n", err)
73+
return err
74+
} else {
75+
databaseNames = append(databaseNames, output.Databases...)
76+
}
5977
}
6078

61-
output, err := actor.RedshiftDataClient.ListDatabases(ctx, &input)
62-
if err != nil {
63-
log.Printf("Failed to list databases: %v\n", err)
64-
return err
65-
}
66-
67-
for _, database := range output.Databases {
79+
for _, database := range databaseNames {
6880
log.Printf("The database name is : %s\n", database)
6981
}
7082
return nil
@@ -90,8 +102,11 @@ func (actor RedshiftDataActions) CreateTable(ctx context.Context, clusterId stri
90102
Sql: aws.String(sql),
91103
}
92104

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

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

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

183+
// snippet-start:[gov2.redshift.DescribeStatement]
184+
185+
// DescribeStatement gets information about the given statement.
186+
func (actor RedshiftDataActions) DescribeStatement(query RedshiftQuery) (*redshiftdata.DescribeStatementOutput, error) {
187+
describeOutput, err := actor.RedshiftDataClient.DescribeStatement(query.Context, &query.Input)
188+
var opErr *types.QueryTimeoutException
189+
if errors.As(err, &opErr) {
190+
println("The connection to the redshift data request timed out.")
191+
panic(err)
192+
} else if err != nil {
193+
println("Failed to execute describe statement")
194+
return nil, err
195+
}
196+
return describeOutput, nil
197+
}
198+
199+
// snippet-end:[gov2.redshift.DescribeStatement]
200+
165201
// snippet-start:[gov2.redshift.WaitForQueryStatus]
166202

167203
// WaitForQueryStatus waits until the given RedshiftQuery object has succeeded or failed.
@@ -170,8 +206,7 @@ func (actor RedshiftDataActions) WaitForQueryStatus(query RedshiftQuery, pauser
170206
attempts := 0
171207
maxWaitCycles := 30
172208
for done == false {
173-
// snippet-start:[gov2.redshift.DescribeStatement]
174-
describeOutput, err := actor.RedshiftDataClient.DescribeStatement(query.Context, &query.Input)
209+
describeOutput, err := actor.DescribeStatement(query)
175210
if err != nil {
176211
return err
177212
}
@@ -187,7 +222,6 @@ func (actor RedshiftDataActions) WaitForQueryStatus(query RedshiftQuery, pauser
187222
if describeOutput.Status == "FINISHED" {
188223
done = true
189224
}
190-
// snippet-end:[gov2.redshift.DescribeStatement]
191225
attempts++
192226
pauser.Pause(attempts)
193227
}
@@ -200,10 +234,15 @@ func (actor RedshiftDataActions) WaitForQueryStatus(query RedshiftQuery, pauser
200234

201235
// GetStatementResult returns the result of the statement with the given id.
202236
func (actor RedshiftDataActions) GetStatementResult(ctx context.Context, statementId string) (*redshiftdata.GetStatementResultOutput, error) {
237+
238+
var opErr *types.QueryTimeoutException
203239
getStatementResultOutput, err := actor.RedshiftDataClient.GetStatementResult(ctx, &redshiftdata.GetStatementResultInput{
204240
Id: aws.String(statementId),
205241
})
206-
if err != nil {
242+
if err != nil && errors.As(err, &opErr) {
243+
log.Printf("Query timed out: %v\n", err)
244+
return nil, err
245+
} else if err != nil {
207246
return nil, err
208247
}
209248
return getStatementResultOutput, nil

0 commit comments

Comments
 (0)