Skip to content

Commit 6929bcd

Browse files
authored
Go Redshift Basics Scenario (#6846)
1 parent 1dd228f commit 6929bcd

File tree

16 files changed

+1561
-7
lines changed

16 files changed

+1561
-7
lines changed

.doc_gen/metadata/redshift_metadata.yaml

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ redshift_Hello:
55
synopsis: get started using &RS;.
66
category: Hello
77
languages:
8+
Go:
9+
versions:
10+
- sdk_version: 2
11+
github: gov2/redshift
12+
excerpts:
13+
- description:
14+
snippet_tags:
15+
- gov2.redshift.Hello
816
Java:
917
versions:
1018
- sdk_version: 2
@@ -23,7 +31,7 @@ redshift_Hello:
2331
snippet_tags:
2432
- python.example_code.redshift.Hello
2533
services:
26-
redshift: {describeClusters}
34+
redshift: {DescribeClusters}
2735
redshift_ListDatabases:
2836
languages:
2937
Java:
@@ -39,6 +47,16 @@ redshift_ListDatabases:
3947
redshift: {ListDatabases}
4048
redshift_CreateCluster:
4149
languages:
50+
Go:
51+
versions:
52+
- sdk_version: 2
53+
github: gov2/redshift
54+
excerpts:
55+
- description:
56+
snippet_tags:
57+
- gov2.redshift.Imports
58+
- gov2.redshift.ActionsStruct
59+
- gov2.redshift.CreateCluster
4260
Kotlin:
4361
versions:
4462
- sdk_version: 1
@@ -86,6 +104,16 @@ redshift_CreateCluster:
86104
redshift: {CreateCluster}
87105
redshift_DeleteCluster:
88106
languages:
107+
Go:
108+
versions:
109+
- sdk_version: 2
110+
github: gov2/redshift
111+
excerpts:
112+
- description:
113+
snippet_tags:
114+
- gov2.redshift.Imports
115+
- gov2.redshift.ActionsStruct
116+
- gov2.redshift.DeleteCluster
89117
Kotlin:
90118
versions:
91119
- sdk_version: 1
@@ -133,6 +161,16 @@ redshift_DeleteCluster:
133161
redshift: {DeleteCluster}
134162
redshift_DescribeClusters:
135163
languages:
164+
Go:
165+
versions:
166+
- sdk_version: 2
167+
github: gov2/redshift
168+
excerpts:
169+
- description:
170+
snippet_tags:
171+
- gov2.redshift.Imports
172+
- gov2.redshift.ActionsStruct
173+
- gov2.redshift.DescribeClusters
136174
Kotlin:
137175
versions:
138176
- sdk_version: 1
@@ -180,6 +218,16 @@ redshift_DescribeClusters:
180218
redshift: {DescribeClusters}
181219
redshift_ModifyCluster:
182220
languages:
221+
Go:
222+
versions:
223+
- sdk_version: 2
224+
github: gov2/redshift
225+
excerpts:
226+
- description:
227+
snippet_tags:
228+
- gov2.redshift.Imports
229+
- gov2.redshift.ActionsStruct
230+
- gov2.redshift.ModifyCluster
183231
Kotlin:
184232
versions:
185233
- sdk_version: 1
@@ -297,9 +345,17 @@ redshift_ExecuteStatement:
297345
services:
298346
redshift: {ExecuteStatement}
299347
redshift_Scenario:
300-
synopsis: learn core operations for &RS; using an &AWS;.
348+
synopsis: learn core operations for &RS; using an &AWS; SDK.
301349
category: Basics
302350
languages:
351+
Go:
352+
versions:
353+
- sdk_version: 2
354+
github: gov2/redshift
355+
excerpts:
356+
- description:
357+
snippet_tags:
358+
- gov2.redshift.BasicsScenario
303359
Java:
304360
versions:
305361
- sdk_version: 2
@@ -335,4 +391,4 @@ redshift_Scenario:
335391
- python.example_code.redshift_data.ListDatabases
336392
- python.example_code.redshift.DeleteCluster
337393
services:
338-
redshift: {createCluster, describeClusters, executeStatement, describeStatement, modifyCluster, getStatementResult, listDatabasesPaginator}
394+
redshift: {CreateCluster, DescribeClusters, ExecuteStatement, DescribeStatement, ModifyCluster, GetStatementResult, ListDatabasesPaginator}

gov2/redshift/README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Amazon Redshift code examples for the SDK for Go V2
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Go V2 to work with Amazon Redshift.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_Amazon Redshift is a fast, fully managed, petabyte-scale data warehouse service that makes it simple and cost-effective to efficiently analyze all your data using your existing business intelligence tools._
11+
12+
## ⚠ Important
13+
14+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
15+
* Running the tests might result in charges to your AWS account.
16+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
17+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../README.md#Prerequisites) in the `gov2` folder.
27+
28+
29+
<!--custom.prerequisites.start-->
30+
<!--custom.prerequisites.end-->
31+
32+
### Get started
33+
34+
- [Hello Amazon Redshift](hello/hello.go#L4) (`DescribeClusters`)
35+
36+
37+
### Basics
38+
39+
Code examples that show you how to perform the essential operations within a service.
40+
41+
- [Learn the basics](scenarios/redshift_basics.go)
42+
43+
44+
### Single actions
45+
46+
Code excerpts that show you how to call individual service functions.
47+
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)
52+
53+
54+
<!--custom.examples.start-->
55+
<!--custom.examples.end-->
56+
57+
## Run the examples
58+
59+
### Instructions
60+
61+
62+
<!--custom.instructions.start-->
63+
<!--custom.instructions.end-->
64+
65+
#### Hello Amazon Redshift
66+
67+
This example shows you how to get started using Amazon Redshift.
68+
69+
```
70+
go run ./hello
71+
```
72+
73+
#### Run a scenario
74+
75+
All scenarios can be run with the `cmd` runner. To get a list of scenarios
76+
and to get help for running a scenario, use the following command:
77+
78+
```
79+
go run ./cmd -h
80+
```
81+
#### Learn the basics
82+
83+
This example shows you how to learn core operations for Amazon Redshift using an AWS SDK.
84+
85+
86+
<!--custom.basic_prereqs.redshift_Scenario.start-->
87+
<!--custom.basic_prereqs.redshift_Scenario.end-->
88+
89+
90+
<!--custom.basics.redshift_Scenario.start-->
91+
<!--custom.basics.redshift_Scenario.end-->
92+
93+
94+
### Tests
95+
96+
⚠ Running tests might result in charges to your AWS account.
97+
98+
99+
To find instructions for running these tests, see the [README](../README.md#Tests)
100+
in the `gov2` folder.
101+
102+
103+
104+
<!--custom.tests.start-->
105+
<!--custom.tests.end-->
106+
107+
## Additional resources
108+
109+
- [Amazon Redshift Management Guide](https://docs.aws.amazon.com/redshift/latest/mgmt/welcome.html)
110+
- [Amazon Redshift API Reference](https://docs.aws.amazon.com/redshift/latest/APIReference/Welcome.html)
111+
- [SDK for Go V2 Amazon Redshift reference](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/redshift)
112+
113+
<!--custom.resources.start-->
114+
<!--custom.resources.end-->
115+
116+
---
117+
118+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
119+
120+
SPDX-License-Identifier: Apache-2.0
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package actions
5+
6+
// snippet-start:[gov2.redshift.Imports]
7+
8+
import (
9+
"context"
10+
"errors"
11+
"github.com/aws/aws-sdk-go-v2/aws"
12+
"github.com/aws/aws-sdk-go-v2/service/redshift"
13+
"github.com/aws/aws-sdk-go-v2/service/redshift/types"
14+
"log"
15+
"time"
16+
)
17+
18+
// snippet-end:[gov2.redshift.Imports]
19+
20+
// snippet-start:[gov2.redshift.ActionsStruct]
21+
22+
// RedshiftActions wraps Redshift service actions.
23+
type RedshiftActions struct {
24+
RedshiftClient *redshift.Client
25+
}
26+
27+
// snippet-end:[gov2.redshift.ActionsStruct]
28+
29+
// snippet-start:[gov2.redshift.CreateCluster]
30+
31+
// CreateCluster sends a request to create a cluster with the given clusterId using the provided credentials.
32+
func (actor RedshiftActions) CreateCluster(ctx context.Context, clusterId string, userName string, userPassword string, nodeType string, clusterType string, publiclyAccessible bool) (*redshift.CreateClusterOutput, error) {
33+
// Create a new Redshift cluster
34+
input := &redshift.CreateClusterInput{
35+
ClusterIdentifier: aws.String(clusterId),
36+
MasterUserPassword: aws.String(userPassword),
37+
MasterUsername: aws.String(userName),
38+
NodeType: aws.String(nodeType),
39+
ClusterType: aws.String(clusterType),
40+
PubliclyAccessible: aws.Bool(publiclyAccessible),
41+
}
42+
var opErr *types.ClusterAlreadyExistsFault
43+
output, err := actor.RedshiftClient.CreateCluster(ctx, input)
44+
if err != nil && errors.As(err, &opErr) {
45+
log.Println("Cluster already exists")
46+
return nil, nil
47+
} else if err != nil {
48+
log.Printf("Failed to create Redshift cluster: %v\n", err)
49+
return nil, err
50+
}
51+
52+
log.Printf("Created cluster %s\n", *output.Cluster.ClusterIdentifier)
53+
return output, nil
54+
}
55+
56+
// snippet-end:[gov2.redshift.CreateCluster]
57+
58+
// snippet-start:[gov2.redshift.ModifyCluster]
59+
60+
// ModifyCluster sets the preferred maintenance window for the given cluster.
61+
func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string, maintenanceWindow string) *redshift.ModifyClusterOutput {
62+
// Modify the cluster's maintenance window
63+
input := &redshift.ModifyClusterInput{
64+
ClusterIdentifier: aws.String(clusterId),
65+
PreferredMaintenanceWindow: aws.String(maintenanceWindow),
66+
}
67+
68+
var opErr *types.InvalidClusterStateFault
69+
output, err := actor.RedshiftClient.ModifyCluster(ctx, input)
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 {
74+
log.Printf("Failed to modify Redshift cluster: %v\n", err)
75+
panic(err)
76+
}
77+
78+
log.Printf("The cluster was successfully modified and now has %s as the maintenance window\n", *output.Cluster.PreferredMaintenanceWindow)
79+
return output
80+
}
81+
82+
// snippet-end:[gov2.redshift.ModifyCluster]
83+
84+
// snippet-start:[gov2.redshift.DeleteCluster]
85+
86+
// DeleteCluster deletes the given cluster.
87+
func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string) (bool, error) {
88+
// Delete the specified Redshift cluster
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 {
99+
log.Printf("Failed to delete Redshift cluster: %v\n", err)
100+
return false, err
101+
}
102+
log.Printf("The %s was deleted\n", clusterId)
103+
return true, nil
104+
}
105+
106+
// 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]

0 commit comments

Comments
 (0)