Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 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 All @@ -23,7 +31,7 @@ redshift_Hello:
snippet_tags:
- python.example_code.redshift.Hello
services:
redshift: {describeClusters}
redshift: {DescribeClusters}
redshift_ListDatabases:
languages:
Java:
Expand All @@ -39,6 +47,16 @@ redshift_ListDatabases:
redshift: {ListDatabases}
redshift_CreateCluster:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.CreateCluster
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -86,6 +104,16 @@ redshift_CreateCluster:
redshift: {CreateCluster}
redshift_DeleteCluster:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.DeleteCluster
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -133,6 +161,16 @@ redshift_DeleteCluster:
redshift: {DeleteCluster}
redshift_DescribeClusters:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.DescribeClusters
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -180,6 +218,16 @@ redshift_DescribeClusters:
redshift: {DescribeClusters}
redshift_ModifyCluster:
languages:
Go:
versions:
- sdk_version: 2
github: gov2/redshift
excerpts:
- description:
snippet_tags:
- gov2.redshift.Imports
- gov2.redshift.ActionsStruct
- gov2.redshift.ModifyCluster
Kotlin:
versions:
- sdk_version: 1
Expand Down Expand Up @@ -297,9 +345,17 @@ redshift_ExecuteStatement:
services:
redshift: {ExecuteStatement}
redshift_Scenario:
synopsis: learn core operations for &RS; using an &AWS;.
synopsis: learn core operations for &RS; using an &AWS; SDK.
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 Expand Up @@ -335,4 +391,4 @@ redshift_Scenario:
- python.example_code.redshift_data.ListDatabases
- python.example_code.redshift.DeleteCluster
services:
redshift: {createCluster, describeClusters, executeStatement, describeStatement, modifyCluster, getStatementResult, listDatabasesPaginator}
redshift: {CreateCluster, DescribeClusters, ExecuteStatement, DescribeStatement, ModifyCluster, GetStatementResult, ListDatabasesPaginator}
120 changes: 120 additions & 0 deletions gov2/redshift/README.md
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
126 changes: 126 additions & 0 deletions gov2/redshift/actions/redshift_actions.go
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)
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)
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]
Loading
Loading