-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial review commit for redshift - go
- Loading branch information
1 parent
f9e8022
commit f8f9d6c
Showing
13 changed files
with
1,417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# Amazon Redshift code examples for the SDK for Go V2 | ||
|
||
## Overview | ||
|
||
Shows how to use the AWS SDK for Go V2 to work with Amazon Redshift. | ||
|
||
<!--custom.overview.start--> | ||
<!--custom.overview.end--> | ||
|
||
_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._ | ||
|
||
## ⚠ Important | ||
|
||
* 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/). | ||
* Running the tests might result in charges to your AWS account. | ||
* 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). | ||
* 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). | ||
|
||
<!--custom.important.start--> | ||
<!--custom.important.end--> | ||
|
||
## Code examples | ||
|
||
### Prerequisites | ||
|
||
For prerequisites, see the [README](../README.md#Prerequisites) in the `gov2` folder. | ||
|
||
|
||
<!--custom.prerequisites.start--> | ||
<!--custom.prerequisites.end--> | ||
|
||
### Get started | ||
|
||
- [Hello Amazon Redshift](hello/hello.go#L4) (`describeClusters`) | ||
|
||
|
||
### Basics | ||
|
||
Code examples that show you how to perform the essential operations within a service. | ||
|
||
- [Learn the basics](scenarios/redshift_basics.go) | ||
|
||
|
||
### Single actions | ||
|
||
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) | ||
|
||
|
||
<!--custom.examples.start--> | ||
<!--custom.examples.end--> | ||
|
||
## Run the examples | ||
|
||
### Instructions | ||
|
||
|
||
<!--custom.instructions.start--> | ||
<!--custom.instructions.end--> | ||
|
||
#### Hello Amazon Redshift | ||
|
||
This example shows you how to get started using Amazon Redshift. | ||
|
||
``` | ||
go run ./hello | ||
``` | ||
|
||
#### Run a scenario | ||
|
||
All scenarios can be run with the `cmd` runner. To get a list of scenarios | ||
and to get help for running a scenario, use the following command: | ||
|
||
``` | ||
go run ./cmd -h | ||
``` | ||
#### Learn the basics | ||
|
||
This example shows you how to work with Amazon Redshift tables, items, and queries. | ||
|
||
|
||
<!--custom.basic_prereqs.redshift_Scenario.start--> | ||
<!--custom.basic_prereqs.redshift_Scenario.end--> | ||
|
||
|
||
<!--custom.basics.redshift_Scenario.start--> | ||
<!--custom.basics.redshift_Scenario.end--> | ||
|
||
|
||
### Tests | ||
|
||
⚠ Running tests might result in charges to your AWS account. | ||
|
||
|
||
To find instructions for running these tests, see the [README](../README.md#Tests) | ||
in the `gov2` folder. | ||
|
||
|
||
|
||
<!--custom.tests.start--> | ||
<!--custom.tests.end--> | ||
|
||
## Additional resources | ||
|
||
- [Amazon Redshift Management Guide](https://docs.aws.amazon.com/redshift/latest/mgmt/welcome.html) | ||
- [Amazon Redshift API Reference](https://docs.aws.amazon.com/redshift/latest/APIReference/Welcome.html) | ||
- [SDK for Go V2 Amazon Redshift reference](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/redshift) | ||
|
||
<!--custom.resources.start--> | ||
<!--custom.resources.end--> | ||
|
||
--- | ||
|
||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
SPDX-License-Identifier: Apache-2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package actions | ||
|
||
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" | ||
) | ||
|
||
// RedshiftActions wraps Redshift service actions. | ||
type RedshiftActions struct { | ||
RedshiftClient *redshift.Client | ||
} | ||
|
||
// snippet-start:[gov2.redshift.CreateCluster] | ||
|
||
// CreateCluster sends a request to create a cluster with the given clusterId using the provided credentials. | ||
func (actor RedshiftActions) CreateCluster(ctx context.Context, clusterId string, userName string, userPassword string, nodeType string, clusterType string, publiclyAccessible bool) (*redshift.CreateClusterOutput, error) { | ||
// Create a new Redshift cluster | ||
input := &redshift.CreateClusterInput{ | ||
ClusterIdentifier: aws.String(clusterId), | ||
MasterUserPassword: aws.String(userPassword), | ||
MasterUsername: aws.String(userName), | ||
NodeType: aws.String(nodeType), | ||
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) { | ||
log.Println("Cluster already exists") | ||
return nil, nil | ||
} else if err != nil { | ||
log.Printf("Failed to create Redshift cluster: %v\n", err) | ||
return nil, err | ||
} | ||
|
||
log.Printf("Created cluster %s\n", *output.Cluster.ClusterIdentifier) | ||
return output, nil | ||
} | ||
|
||
// snippet-end:[gov2.redshift.CreateCluster] | ||
|
||
// snippet-start:[gov2.redshift.ModifyCluster] | ||
|
||
// ModifyCluster sets the preferred maintenance window for the given cluster. | ||
func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string, maintenanceWindow string) *redshift.ModifyClusterOutput { | ||
// Modify the cluster's maintenance window | ||
input := &redshift.ModifyClusterInput{ | ||
ClusterIdentifier: aws.String(clusterId), | ||
PreferredMaintenanceWindow: aws.String(maintenanceWindow), | ||
} | ||
|
||
output, err := actor.RedshiftClient.ModifyCluster(ctx, input) | ||
if err != nil { | ||
log.Printf("Failed to modify Redshift cluster: %v\n", err) | ||
return nil | ||
} | ||
|
||
log.Printf("The cluster was successfully modified and now has %s as the maintenance window\n", *output.Cluster.PreferredMaintenanceWindow) | ||
return output | ||
} | ||
|
||
// snippet-end:[gov2.redshift.ModifyCluster] | ||
|
||
// snippet-start:[gov2.redshift.DeleteCluster] | ||
|
||
// 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 { | ||
log.Printf("Failed to delete Redshift cluster: %v\n", err) | ||
return false, err | ||
} | ||
log.Printf("The %s was deleted\n", clusterId) | ||
return true, nil | ||
} | ||
|
||
// snippet-end:[gov2.redshift.DeleteCluster] |
Oops, something went wrong.