Skip to content

Commit f8f9d6c

Browse files
committed
Initial review commit for redshift - go
1 parent f9e8022 commit f8f9d6c

File tree

13 files changed

+1417
-0
lines changed

13 files changed

+1417
-0
lines changed

.doc_gen/metadata/redshift_metadata.yaml

Lines changed: 72 additions & 0 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
@@ -39,6 +47,14 @@ 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.CreateCluster
4258
Kotlin:
4359
versions:
4460
- sdk_version: 1
@@ -86,6 +102,14 @@ redshift_CreateCluster:
86102
redshift: {CreateCluster}
87103
redshift_DeleteCluster:
88104
languages:
105+
Go:
106+
versions:
107+
- sdk_version: 2
108+
github: gov2/redshift
109+
excerpts:
110+
- description:
111+
snippet_tags:
112+
- gov2.redshift.DeleteCluster
89113
Kotlin:
90114
versions:
91115
- sdk_version: 1
@@ -133,6 +157,14 @@ redshift_DeleteCluster:
133157
redshift: {DeleteCluster}
134158
redshift_DescribeClusters:
135159
languages:
160+
Go:
161+
versions:
162+
- sdk_version: 2
163+
github: gov2/redshift
164+
excerpts:
165+
- description:
166+
snippet_tags:
167+
- gov2.redshift.DescribeClusters
136168
Kotlin:
137169
versions:
138170
- sdk_version: 1
@@ -180,6 +212,14 @@ redshift_DescribeClusters:
180212
redshift: {DescribeClusters}
181213
redshift_ModifyCluster:
182214
languages:
215+
Go:
216+
versions:
217+
- sdk_version: 2
218+
github: gov2/redshift
219+
excerpts:
220+
- description:
221+
snippet_tags:
222+
- gov2.redshift.ModifyCluster
183223
Kotlin:
184224
versions:
185225
- sdk_version: 1
@@ -227,6 +267,14 @@ redshift_ModifyCluster:
227267
redshift: {ModifyCluster}
228268
redshift_DescribeStatement:
229269
languages:
270+
Go:
271+
versions:
272+
- sdk_version: 2
273+
github: gov2/redshift
274+
excerpts:
275+
- description:
276+
snippet_tags:
277+
- gov2.redshift.DescribeStatement
230278
Java:
231279
versions:
232280
- sdk_version: 2
@@ -253,6 +301,14 @@ redshift_DescribeStatement:
253301
redshift: {DescribeStatement}
254302
redshift_GetStatementResult:
255303
languages:
304+
Go:
305+
versions:
306+
- sdk_version: 2
307+
github: gov2/redshift
308+
excerpts:
309+
- description:
310+
snippet_tags:
311+
- gov2.redshift.GetStatementResult
256312
Java:
257313
versions:
258314
- sdk_version: 2
@@ -279,6 +335,14 @@ redshift_GetStatementResult:
279335
redshift: {GetStatementResult}
280336
redshift_ExecuteStatement:
281337
languages:
338+
Go:
339+
versions:
340+
- sdk_version: 2
341+
github: gov2/redshift
342+
excerpts:
343+
- description:
344+
snippet_tags:
345+
- gov2.redshift.CreateTable
282346
Java:
283347
versions:
284348
- sdk_version: 2
@@ -300,6 +364,14 @@ redshift_Scenario:
300364
synopsis: learn core operations for &RS; using an &AWS;.
301365
category: Basics
302366
languages:
367+
Go:
368+
versions:
369+
- sdk_version: 2
370+
github: gov2/redshift
371+
excerpts:
372+
- description:
373+
snippet_tags:
374+
- gov2.redshift.BasicsScenario
303375
Java:
304376
versions:
305377
- sdk_version: 2

gov2/redshift/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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#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)
55+
56+
57+
<!--custom.examples.start-->
58+
<!--custom.examples.end-->
59+
60+
## Run the examples
61+
62+
### Instructions
63+
64+
65+
<!--custom.instructions.start-->
66+
<!--custom.instructions.end-->
67+
68+
#### Hello Amazon Redshift
69+
70+
This example shows you how to get started using Amazon Redshift.
71+
72+
```
73+
go run ./hello
74+
```
75+
76+
#### Run a scenario
77+
78+
All scenarios can be run with the `cmd` runner. To get a list of scenarios
79+
and to get help for running a scenario, use the following command:
80+
81+
```
82+
go run ./cmd -h
83+
```
84+
#### Learn the basics
85+
86+
This example shows you how to work with Amazon Redshift tables, items, and queries.
87+
88+
89+
<!--custom.basic_prereqs.redshift_Scenario.start-->
90+
<!--custom.basic_prereqs.redshift_Scenario.end-->
91+
92+
93+
<!--custom.basics.redshift_Scenario.start-->
94+
<!--custom.basics.redshift_Scenario.end-->
95+
96+
97+
### Tests
98+
99+
⚠ Running tests might result in charges to your AWS account.
100+
101+
102+
To find instructions for running these tests, see the [README](../README.md#Tests)
103+
in the `gov2` folder.
104+
105+
106+
107+
<!--custom.tests.start-->
108+
<!--custom.tests.end-->
109+
110+
## Additional resources
111+
112+
- [Amazon Redshift Management Guide](https://docs.aws.amazon.com/redshift/latest/mgmt/welcome.html)
113+
- [Amazon Redshift API Reference](https://docs.aws.amazon.com/redshift/latest/APIReference/Welcome.html)
114+
- [SDK for Go V2 Amazon Redshift reference](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/redshift)
115+
116+
<!--custom.resources.start-->
117+
<!--custom.resources.end-->
118+
119+
---
120+
121+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
122+
123+
SPDX-License-Identifier: Apache-2.0
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package actions
5+
6+
import (
7+
"context"
8+
"errors"
9+
"github.com/aws/aws-sdk-go-v2/aws"
10+
"github.com/aws/aws-sdk-go-v2/service/redshift"
11+
"github.com/aws/aws-sdk-go-v2/service/redshift/types"
12+
"log"
13+
)
14+
15+
// RedshiftActions wraps Redshift service actions.
16+
type RedshiftActions struct {
17+
RedshiftClient *redshift.Client
18+
}
19+
20+
// snippet-start:[gov2.redshift.CreateCluster]
21+
22+
// CreateCluster sends a request to create a cluster with the given clusterId using the provided credentials.
23+
func (actor RedshiftActions) CreateCluster(ctx context.Context, clusterId string, userName string, userPassword string, nodeType string, clusterType string, publiclyAccessible bool) (*redshift.CreateClusterOutput, error) {
24+
// Create a new Redshift cluster
25+
input := &redshift.CreateClusterInput{
26+
ClusterIdentifier: aws.String(clusterId),
27+
MasterUserPassword: aws.String(userPassword),
28+
MasterUsername: aws.String(userName),
29+
NodeType: aws.String(nodeType),
30+
ClusterType: aws.String(clusterType),
31+
PubliclyAccessible: aws.Bool(publiclyAccessible),
32+
}
33+
34+
var opErr *types.ClusterAlreadyExistsFault
35+
output, err := actor.RedshiftClient.CreateCluster(ctx, input)
36+
if err != nil && errors.As(err, &opErr) {
37+
log.Println("Cluster already exists")
38+
return nil, nil
39+
} else if err != nil {
40+
log.Printf("Failed to create Redshift cluster: %v\n", err)
41+
return nil, err
42+
}
43+
44+
log.Printf("Created cluster %s\n", *output.Cluster.ClusterIdentifier)
45+
return output, nil
46+
}
47+
48+
// snippet-end:[gov2.redshift.CreateCluster]
49+
50+
// snippet-start:[gov2.redshift.ModifyCluster]
51+
52+
// ModifyCluster sets the preferred maintenance window for the given cluster.
53+
func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string, maintenanceWindow string) *redshift.ModifyClusterOutput {
54+
// Modify the cluster's maintenance window
55+
input := &redshift.ModifyClusterInput{
56+
ClusterIdentifier: aws.String(clusterId),
57+
PreferredMaintenanceWindow: aws.String(maintenanceWindow),
58+
}
59+
60+
output, err := actor.RedshiftClient.ModifyCluster(ctx, input)
61+
if err != nil {
62+
log.Printf("Failed to modify Redshift cluster: %v\n", err)
63+
return nil
64+
}
65+
66+
log.Printf("The cluster was successfully modified and now has %s as the maintenance window\n", *output.Cluster.PreferredMaintenanceWindow)
67+
return output
68+
}
69+
70+
// snippet-end:[gov2.redshift.ModifyCluster]
71+
72+
// snippet-start:[gov2.redshift.DeleteCluster]
73+
74+
// DeleteCluster deletes the given cluster.
75+
func (actor RedshiftActions) DeleteCluster(ctx context.Context, clusterId string) (bool, error) {
76+
// 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 {
83+
log.Printf("Failed to delete Redshift cluster: %v\n", err)
84+
return false, err
85+
}
86+
log.Printf("The %s was deleted\n", clusterId)
87+
return true, nil
88+
}
89+
90+
// snippet-end:[gov2.redshift.DeleteCluster]

0 commit comments

Comments
 (0)