Skip to content

Commit

Permalink
Initial review commit for redshift - go
Browse files Browse the repository at this point in the history
  • Loading branch information
beqqrry-aws committed Sep 19, 2024
1 parent f9e8022 commit f8f9d6c
Show file tree
Hide file tree
Showing 13 changed files with 1,417 additions and 0 deletions.
72 changes: 72 additions & 0 deletions .doc_gen/metadata/redshift_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ redshift_Hello:
synopsis: get started using &RS;.
category: Hello
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.Hello
Java:
versions:
- sdk_version: 2
Expand Down Expand Up @@ -39,6 +47,14 @@ redshift_ListDatabases:
redshift: {ListDatabases}
redshift_CreateCluster:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.CreateCluster
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -86,6 +102,14 @@ redshift_CreateCluster:
redshift: {CreateCluster}
redshift_DeleteCluster:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.DeleteCluster
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -133,6 +157,14 @@ redshift_DeleteCluster:
redshift: {DeleteCluster}
redshift_DescribeClusters:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.DescribeClusters
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -180,6 +212,14 @@ redshift_DescribeClusters:
redshift: {DescribeClusters}
redshift_ModifyCluster:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.ModifyCluster
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -227,6 +267,14 @@ redshift_ModifyCluster:
redshift: {ModifyCluster}
redshift_DescribeStatement:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.DescribeStatement
Java:
versions:
- sdk_version: 2
Expand All @@ -253,6 +301,14 @@ redshift_DescribeStatement:
redshift: {DescribeStatement}
redshift_GetStatementResult:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.GetStatementResult
Java:
versions:
- sdk_version: 2
Expand All @@ -279,6 +335,14 @@ redshift_GetStatementResult:
redshift: {GetStatementResult}
redshift_ExecuteStatement:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.CreateTable
Java:
versions:
- sdk_version: 2
Expand All @@ -300,6 +364,14 @@ redshift_Scenario:
synopsis: learn core operations for &RS; using an &AWS;.
category: Basics
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.BasicsScenario
Java:
versions:
- sdk_version: 2
Expand Down
123 changes: 123 additions & 0 deletions gov2/redshift/README.md
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
90 changes: 90 additions & 0 deletions gov2/redshift/actions/redshift_actions.go
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]
Loading

0 comments on commit f8f9d6c

Please sign in to comment.