Skip to content

Commit

Permalink
Merge pull request #28751 from bennylu2/f-aws_redshiftdata_statement-…
Browse files Browse the repository at this point in the history
…26346

r/aws_redshiftdata_statement - add `workgroup_name`
  • Loading branch information
ewbankkit authored Jan 10, 2023
2 parents 505bcca + 86e6ef8 commit b191964
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .changelog/28751.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_redshiftdata_statement: Add `workgroup_name` argument
```
23 changes: 18 additions & 5 deletions internal/service/redshiftdata/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func ResourceStatement() *schema.Resource {
Schema: map[string]*schema.Schema{
"cluster_identifier": {
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
},
"database": {
Expand Down Expand Up @@ -84,6 +84,11 @@ func ResourceStatement() *schema.Resource {
Optional: true,
ForceNew: true,
},
"workgroup_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -92,10 +97,13 @@ func resourceStatementCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).RedshiftDataConn()

input := &redshiftdataapiservice.ExecuteStatementInput{
ClusterIdentifier: aws.String(d.Get("cluster_identifier").(string)),
Database: aws.String(d.Get("database").(string)),
Sql: aws.String(d.Get("sql").(string)),
WithEvent: aws.Bool(d.Get("with_event").(bool)),
Database: aws.String(d.Get("database").(string)),
Sql: aws.String(d.Get("sql").(string)),
WithEvent: aws.Bool(d.Get("with_event").(bool)),
}

if v, ok := d.GetOk("cluster_identifier"); ok {
input.ClusterIdentifier = aws.String(v.(string))
}

if v, ok := d.GetOk("db_user"); ok {
Expand All @@ -114,6 +122,10 @@ func resourceStatementCreate(d *schema.ResourceData, meta interface{}) error {
input.StatementName = aws.String(v.(string))
}

if v, ok := d.GetOk("workgroup_name"); ok {
input.WorkgroupName = aws.String(v.(string))
}

output, err := conn.ExecuteStatement(input)

if err != nil {
Expand Down Expand Up @@ -149,6 +161,7 @@ func resourceStatementRead(d *schema.ResourceData, meta interface{}) error {
d.Set("database", d.Get("database").(string))
d.Set("db_user", d.Get("db_user").(string))
d.Set("sql", sub.QueryString)
d.Set("workgroup_name", sub.WorkgroupName)

if err := d.Set("parameters", flattenParameters(sub.QueryParameters)); err != nil {
return fmt.Errorf("setting parameters: %w", err)
Expand Down
53 changes: 52 additions & 1 deletion internal/service/redshiftdata/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,47 @@ func TestAccRedshiftDataStatement_basic(t *testing.T) {
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, redshiftdataapiservice.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: nil,
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccStatementConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStatementExists(resourceName, &v),
resource.TestCheckResourceAttrPair(resourceName, "cluster_identifier", "aws_redshift_cluster.test", "cluster_identifier"),
resource.TestCheckResourceAttr(resourceName, "parameters.#", "0"),
resource.TestCheckResourceAttr(resourceName, "sql", "CREATE GROUP group_name;"),
resource.TestCheckResourceAttr(resourceName, "workgroup_name", ""),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"database", "db_user"},
},
},
})
}

func TestAccRedshiftDataStatement_workgroup(t *testing.T) {
var v redshiftdataapiservice.DescribeStatementOutput
resourceName := "aws_redshiftdata_statement.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, redshiftdataapiservice.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: acctest.CheckDestroyNoop,
Steps: []resource.TestStep{
{
Config: testAccStatementConfig_workgroup(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckStatementExists(resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "cluster_identifier", ""),
resource.TestCheckResourceAttr(resourceName, "parameters.#", "0"),
resource.TestCheckResourceAttr(resourceName, "sql", "CREATE GROUP group_name;"),
resource.TestCheckResourceAttrPair(resourceName, "workgroup_name", "aws_redshiftserverless_workgroup.test", "workgroup_name"),
),
},
{
Expand Down Expand Up @@ -90,3 +122,22 @@ resource "aws_redshiftdata_statement" "test" {
}
`, rName))
}

func testAccStatementConfig_workgroup(rName string) string {
return fmt.Sprintf(`
resource "aws_redshiftserverless_namespace" "test" {
namespace_name = %[1]q
}
resource "aws_redshiftserverless_workgroup" "test" {
namespace_name = aws_redshiftserverless_namespace.test.namespace_name
workgroup_name = %[1]q
}
resource "aws_redshiftdata_statement" "test" {
workgroup_name = aws_redshiftserverless_workgroup.test.workgroup_name
database = "dev"
sql = "CREATE GROUP group_name;"
}
`, rName)
}
14 changes: 7 additions & 7 deletions internal/service/redshiftserverless/workgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ func ResourceWorkgroup() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"publicly_accessible": {
Type: schema.TypeBool,
Optional: true,
},
"namespace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"publicly_accessible": {
Type: schema.TypeBool,
Optional: true,
},
"security_group_ids": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -227,14 +227,14 @@ func resourceWorkgroupRead(d *schema.ResourceData, meta interface{}) error {

arn := aws.StringValue(out.WorkgroupArn)
d.Set("arn", arn)
d.Set("namespace_name", out.NamespaceName)
d.Set("workgroup_name", out.WorkgroupName)
d.Set("workgroup_id", out.WorkgroupId)
d.Set("base_capacity", out.BaseCapacity)
d.Set("enhanced_vpc_routing", out.EnhancedVpcRouting)
d.Set("namespace_name", out.NamespaceName)
d.Set("publicly_accessible", out.PubliclyAccessible)
d.Set("security_group_ids", flex.FlattenStringSet(out.SecurityGroupIds))
d.Set("subnet_ids", flex.FlattenStringSet(out.SubnetIds))
d.Set("workgroup_id", out.WorkgroupId)
d.Set("workgroup_name", out.WorkgroupName)
if err := d.Set("config_parameter", flattenConfigParameters(out.ConfigParameters)); err != nil {
return fmt.Errorf("setting config_parameter: %w", err)
}
Expand Down
22 changes: 19 additions & 3 deletions website/docs/r/redshiftdata_statement.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Executes a Redshift Data Statement.

## Example Usage

### cluster_identifier

```terraform
resource "aws_redshiftdata_statement" "example" {
cluster_identifier = aws_redshift_cluster.example.cluster_identifier
Expand All @@ -21,17 +23,31 @@ resource "aws_redshiftdata_statement" "example" {
}
```

### workgroup_name

```terraform
resource "aws_redshiftdata_statement" "example" {
workgroup_name = aws_redshiftserverless_workgroup.example.workgroup_name
database = "dev"
sql = "CREATE GROUP group_name;"
}
```

## Argument Reference

The following arguments are supported:
The following arguments are required:

* `cluster_identifier` - (Required) The cluster identifier.
* `database` - (Required) The name of the database.
* `sql` - (Required) The SQL statement text to run.

The following arguments are optional:

* `cluster_identifier` - (Optional) The cluster identifier. This parameter is required when connecting to a cluster and authenticating using either Secrets Manager or temporary credentials.
* `db_user` - (Optional) The database user name.
* `secret_arn` - (Optional) The name or ARN of the secret that enables access to the database.
* `sql` - (Required) The SQL statement text to run.
* `statement_name` - (Optional) The name of the SQL statement. You can name the SQL statement when you create it to identify the query.
* `with_event` - (Optional) A value that indicates whether to send an event to the Amazon EventBridge event bus after the SQL statement runs.
* `workgroup_name` - (Optional) The serverless workgroup name. This parameter is required when connecting to a serverless workgroup and authenticating using either Secrets Manager or temporary credentials.

## Attributes Reference

Expand Down
8 changes: 6 additions & 2 deletions website/docs/r/redshiftserverless_workgroup.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ resource "aws_redshiftserverless_workgroup" "example" {

## Argument Reference

The following arguments are supported:
The following arguments are required:

* `namespace_name` - (Required) The name of the namespace.
* `workgroup_name` - (Required) The name of the workgroup.

The following arguments are optional:

* `base_capacity` - (Optional) The base data warehouse capacity of the workgroup in Redshift Processing Units (RPUs).
* `config_parameter` - (Optional) An array of parameters to set for more control over a serverless database. See `Config Parameter` below.
* `enhanced_vpc_routing` - (Optional) The value that specifies whether to turn on enhanced virtual private cloud (VPC) routing, which forces Amazon Redshift Serverless to route traffic through your VPC instead of over the internet.
* `publicly_accessible` - (Optional) A value that specifies whether the workgroup can be accessed from a public network.
* `security_group_ids` - (Optional) An array of security group IDs to associate with the workgroup.
* `subnet_ids` - (Optional) An array of VPC subnet IDs to associate with the workgroup.
* `workgroup_name` - (Required) The name of the workgroup.
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

### Config Parameter
Expand Down

0 comments on commit b191964

Please sign in to comment.