-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Go Redshift Basics Scenario #6846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f8f9d6c
Initial review commit for redshift - go
beqqrry-aws c294ad8
Includes a lot of fixes for comments to the PR.
beqqrry-aws b85ae3f
Added a query status check to account for race conditions creating th…
beqqrry-aws da951dc
Merge branch 'main' into go_redshift_2
beqqrry-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,120 @@ | ||
| # 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#L29) | ||
| - [DeleteCluster](actions/redshift_actions.go#L84) | ||
| - [DescribeClusters](actions/redshift_actions.go#L108) | ||
| - [ModifyCluster](actions/redshift_actions.go#L58) | ||
|
|
||
|
|
||
| <!--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 learn core operations for Amazon Redshift using an AWS SDK. | ||
|
|
||
|
|
||
| <!--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 hidden or 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,126 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package actions | ||
|
|
||
| // snippet-start:[gov2.redshift.Imports] | ||
|
|
||
| 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" | ||
| "time" | ||
| ) | ||
|
|
||
| // snippet-end:[gov2.redshift.Imports] | ||
|
|
||
| // snippet-start:[gov2.redshift.ActionsStruct] | ||
|
|
||
| // RedshiftActions wraps Redshift service actions. | ||
| type RedshiftActions struct { | ||
| RedshiftClient *redshift.Client | ||
| } | ||
|
|
||
| // snippet-end:[gov2.redshift.ActionsStruct] | ||
|
|
||
| // 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) | ||
beqqrry-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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), | ||
| } | ||
|
|
||
| var opErr *types.InvalidClusterStateFault | ||
| output, err := actor.RedshiftClient.ModifyCluster(ctx, input) | ||
beqqrry-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil && errors.As(err, &opErr) { | ||
| log.Println("Cluster is in an invalid state.") | ||
| panic(err) | ||
| } else if err != nil { | ||
| log.Printf("Failed to modify Redshift cluster: %v\n", err) | ||
| panic(err) | ||
| } | ||
|
|
||
| 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 | ||
|
|
||
| waiter := redshift.NewClusterDeletedWaiter(actor.RedshiftClient) | ||
| err := waiter.Wait(ctx, &redshift.DescribeClustersInput{ | ||
| ClusterIdentifier: aws.String(clusterId), | ||
| }, 5*time.Minute) | ||
| var opErr *types.ClusterNotFoundFault | ||
| if err != nil && errors.As(err, &opErr) { | ||
| log.Println("Cluster was not found. Where could it be?") | ||
| return false, err | ||
| } else 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] | ||
|
|
||
| // snippet-start:[gov2.redshift.DescribeClusters] | ||
|
|
||
| // DescribeClusters returns information about the given cluster. | ||
| func (actor RedshiftActions) DescribeClusters(ctx context.Context, clusterId string) (*redshift.DescribeClustersOutput, error) { | ||
| input, err := actor.RedshiftClient.DescribeClusters(ctx, &redshift.DescribeClustersInput{ | ||
| ClusterIdentifier: aws.String(clusterId), | ||
| }) | ||
| var opErr *types.AccessToClusterDeniedFault | ||
| if errors.As(err, &opErr) { | ||
| println("Access to cluster denied.") | ||
| panic(err) | ||
| } else if err != nil { | ||
| println("Failed to describe Redshift clusters.") | ||
| return nil, err | ||
| } | ||
| return input, nil | ||
| } | ||
|
|
||
| // snippet-end:[gov2.redshift.DescribeClusters] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.