Skip to content
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

d/aws_redshift_data_shares: new data source #35937

Merged
merged 1 commit into from
Feb 27, 2024
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
3 changes: 3 additions & 0 deletions .changelog/35937.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_redshift_data_shares
```
107 changes: 107 additions & 0 deletions internal/service/redshift/data_shares_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package redshift

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/redshift"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource(name="Data Shares")
func newDataSourceDataShares(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceDataShares{}, nil
}

const (
DSNameDataShares = "Data Shares Data Source"
)

type dataSourceDataShares struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceDataShares) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
resp.TypeName = "aws_redshift_data_shares"
}

func (d *dataSourceDataShares) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": framework.IDAttribute(),
},
Blocks: map[string]schema.Block{
"data_shares": schema.ListNestedBlock{
CustomType: fwtypes.NewListNestedObjectTypeOf[dataSharesData](ctx),
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"data_share_arn": schema.StringAttribute{
Computed: true,
},
"managed_by": schema.StringAttribute{
Computed: true,
},
"producer_arn": schema.StringAttribute{
Computed: true,
},
},
},
},
},
}
}
func (d *dataSourceDataShares) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().RedshiftClient(ctx)

var data dataSourceDataSharesData
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
data.ID = types.StringValue(d.Meta().Region)

paginator := redshift.NewDescribeDataSharesPaginator(conn, &redshift.DescribeDataSharesInput{})

var out redshift.DescribeDataSharesOutput
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.Redshift, create.ErrActionReading, DSNameDataShares, data.ID.String(), err),
err.Error(),
)
return
}

if page != nil && len(page.DataShares) > 0 {
out.DataShares = append(out.DataShares, page.DataShares...)
}
}

resp.Diagnostics.Append(flex.Flatten(ctx, out, &data)...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

type dataSourceDataSharesData struct {
DataShares fwtypes.ListNestedObjectValueOf[dataSharesData] `tfsdk:"data_shares"`
ID types.String `tfsdk:"id"`
}

type dataSharesData struct {
DataShareARN types.String `tfsdk:"data_share_arn"`
ManagedBy types.String `tfsdk:"managed_by"`
ProducerARN types.String `tfsdk:"producer_arn"`
}
88 changes: 88 additions & 0 deletions internal/service/redshift/data_shares_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package redshift_test

import (
"fmt"
"regexp"
"testing"

"github.com/YakDriver/regexache"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccRedshiftDataSharesDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceName := "data.aws_redshift_data_shares.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.RedshiftEndpointID)
},
ErrorCheck: acctest.ErrorCheck(t, names.RedshiftServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccDataSharesDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "data_shares.#"),
resource.TestMatchTypeSetElemNestedAttrs(dataSourceName, "data_shares.*", map[string]*regexp.Regexp{
"data_share_arn": regexache.MustCompile(`datashare:+.`),
"producer_arn": regexache.MustCompile(`namespace/+.`),
}),
),
},
},
})
}

func testAccDataSharesDataSourceConfig_basic(rName string) string {
return acctest.ConfigCompose(
fmt.Sprintf(`
data "aws_caller_identity" "current" {}
resource "aws_redshiftserverless_namespace" "test" {
namespace_name = %[1]q
db_name = "test"
}
resource "aws_redshiftserverless_workgroup" "test" {
namespace_name = aws_redshiftserverless_namespace.test.namespace_name
workgroup_name = %[1]q
}
resource "aws_redshiftdata_statement" "test_create" {
workgroup_name = aws_redshiftserverless_workgroup.test.workgroup_name
database = aws_redshiftserverless_namespace.test.db_name
sql = "CREATE DATASHARE tfacctest;"
}
`, rName),
// Split this resource into a string literal so the terraform `format` function
// interpolates properly.
//
// Grant statement must be run before the data share will be returned from the
// DescribeDataShares API.
`
resource "aws_redshiftdata_statement" "test_grant_usage" {
depends_on = [aws_redshiftdata_statement.test_create]
workgroup_name = aws_redshiftserverless_workgroup.test.workgroup_name
database = aws_redshiftserverless_namespace.test.db_name
sql = format("GRANT USAGE ON DATASHARE tfacctest TO ACCOUNT '%s';", data.aws_caller_identity.current.account_id)
}
data "aws_redshift_data_shares" "test" {
depends_on = [aws_redshiftdata_statement.test_grant_usage]
}
`)
}
7 changes: 6 additions & 1 deletion internal/service/redshift/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions names/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (
PipesEndpointID = "pipes"
PollyEndpointID = "polly"
QLDBEndpointID = "qldb"
RedshiftEndpointID = "redshift"
RekognitionEndpointID = "rekognition"
ResourceExplorer2EndpointID = "resource-explorer-2"
RolesAnywhereEndpointID = "rolesanywhere"
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/redshift_data_shares.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
subcategory: "Redshift"
layout: "aws"
page_title: "AWS: aws_redshift_data_shares"
description: |-
Terraform data source for managing AWS Redshift Data Shares.
---

# Data Source: aws_redshift_data_shares

Terraform data source for managing AWS Redshift Data Shares.

## Example Usage

### Basic Usage

```terraform
data "aws_redshift_data_shares" "example" {}
```

## Argument Reference

There are no arguments available for this data source.

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `id` - AWS region.
* `data_shares` - An array of all data shares in the current region. See [`data_shares`](#data_shares-attribute-reference) below.

### `data_shares` Attribute Reference

* `data_share_arn` - ARN (Amazon Resource Name) of the data share.
* `managed_by` - Identifier of a datashare to show its managing entity.
* `producer_arn` - ARN (Amazon Resource Name) of the producer.
Loading