Skip to content

Commit 4bc5d06

Browse files
committed
Initial review commit for redshift - go
1 parent be90fca commit 4bc5d06

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
@@ -26,6 +34,14 @@ redshift_Hello:
2634
redshift: {describeClusters}
2735
redshift_CreateCluster:
2836
languages:
37+
Go:
38+
versions:
39+
- sdk_version: 2
40+
github: gov2/redshift
41+
excerpts:
42+
- description:
43+
snippet_tags:
44+
- gov2.redshift.CreateCluster
2945
Kotlin:
3046
versions:
3147
- sdk_version: 1
@@ -73,6 +89,14 @@ redshift_CreateCluster:
7389
redshift: {CreateCluster}
7490
redshift_DeleteCluster:
7591
languages:
92+
Go:
93+
versions:
94+
- sdk_version: 2
95+
github: gov2/redshift
96+
excerpts:
97+
- description:
98+
snippet_tags:
99+
- gov2.redshift.DeleteCluster
76100
Kotlin:
77101
versions:
78102
- sdk_version: 1
@@ -120,6 +144,14 @@ redshift_DeleteCluster:
120144
redshift: {DeleteCluster}
121145
redshift_DescribeClusters:
122146
languages:
147+
Go:
148+
versions:
149+
- sdk_version: 2
150+
github: gov2/redshift
151+
excerpts:
152+
- description:
153+
snippet_tags:
154+
- gov2.redshift.DescribeClusters
123155
Kotlin:
124156
versions:
125157
- sdk_version: 1
@@ -167,6 +199,14 @@ redshift_DescribeClusters:
167199
redshift: {DescribeClusters}
168200
redshift_ModifyCluster:
169201
languages:
202+
Go:
203+
versions:
204+
- sdk_version: 2
205+
github: gov2/redshift
206+
excerpts:
207+
- description:
208+
snippet_tags:
209+
- gov2.redshift.ModifyCluster
170210
Kotlin:
171211
versions:
172212
- sdk_version: 1
@@ -240,6 +280,14 @@ redshift_Query:
240280
redshift: {Query}
241281
redshift_DescribeStatement:
242282
languages:
283+
Go:
284+
versions:
285+
- sdk_version: 2
286+
github: gov2/redshift
287+
excerpts:
288+
- description:
289+
snippet_tags:
290+
- gov2.redshift.DescribeStatement
243291
Java:
244292
versions:
245293
- sdk_version: 2
@@ -266,6 +314,14 @@ redshift_DescribeStatement:
266314
redshift: {DescribeStatement}
267315
redshift_GetStatementResult:
268316
languages:
317+
Go:
318+
versions:
319+
- sdk_version: 2
320+
github: gov2/redshift
321+
excerpts:
322+
- description:
323+
snippet_tags:
324+
- gov2.redshift.GetStatementResult
269325
Java:
270326
versions:
271327
- sdk_version: 2
@@ -292,6 +348,14 @@ redshift_GetStatementResult:
292348
redshift: {GetStatementResult}
293349
redshift_CreateTable:
294350
languages:
351+
Go:
352+
versions:
353+
- sdk_version: 2
354+
github: gov2/redshift
355+
excerpts:
356+
- description:
357+
snippet_tags:
358+
- gov2.redshift.CreateTable
295359
Java:
296360
versions:
297361
- sdk_version: 2
@@ -323,6 +387,14 @@ redshift_Scenario:
323387
synopsis: work with &RS; tables, items, and queries.
324388
category: Basics
325389
languages:
390+
Go:
391+
versions:
392+
- sdk_version: 2
393+
github: gov2/redshift
394+
excerpts:
395+
- description:
396+
snippet_tags:
397+
- gov2.redshift.BasicsScenario
326398
Java:
327399
versions:
328400
- 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)