Skip to content

Commit

Permalink
Merge pull request #26489 from bschaatsbergen/add-aurora-dual-stack-s…
Browse files Browse the repository at this point in the history
…upport

Add dual stack support for Amazon Aurora
  • Loading branch information
ewbankkit authored Sep 7, 2022
2 parents 1fbd67e + 083fbdc commit 45ac9ea
Show file tree
Hide file tree
Showing 18 changed files with 1,325 additions and 1,459 deletions.
11 changes: 11 additions & 0 deletions .changelog/26489.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:enhancement
resource/aws_rds_cluster: Add `network_type` argument
```

```release-note:enhancement
resource/aws_rds_cluster_instance: Add `network_type` attribute
```

```release-note:enhancement
data-source/aws_rds_cluster: Add `network_type` attribute
```
1,119 changes: 492 additions & 627 deletions internal/service/rds/cluster.go

Large diffs are not rendered by default.

152 changes: 41 additions & 111 deletions internal/service/rds/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package rds

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
Expand All @@ -19,146 +17,119 @@ func DataSourceCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"cluster_identifier": {
Type: schema.TypeString,
Required: true,
},

"availability_zones": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Set: schema.HashString,
},

"backtrack_window": {
Type: schema.TypeInt,
Computed: true,
},

"backup_retention_period": {
Type: schema.TypeInt,
Computed: true,
},

"cluster_identifier": {
Type: schema.TypeString,
Required: true,
},
"cluster_members": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Set: schema.HashString,
},

"cluster_resource_id": {
Type: schema.TypeString,
Computed: true,
},

"database_name": {
Type: schema.TypeString,
Computed: true,
},

"db_subnet_group_name": {
"db_cluster_parameter_group_name": {
Type: schema.TypeString,
Computed: true,
},

"db_cluster_parameter_group_name": {
"db_subnet_group_name": {
Type: schema.TypeString,
Computed: true,
},

"enabled_cloudwatch_logs_exports": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"endpoint": {
Type: schema.TypeString,
Computed: true,
},

"engine": {
Type: schema.TypeString,
Computed: true,
},

"engine_version": {
Type: schema.TypeString,
Computed: true,
},

"final_snapshot_identifier": {
Type: schema.TypeString,
Computed: true,
},

"hosted_zone_id": {
Type: schema.TypeString,
Computed: true,
},
"iam_database_authentication_enabled": {
Type: schema.TypeBool,
Computed: true,
},

"iam_roles": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},

"master_username": {
Type: schema.TypeString,
Computed: true,
},

"preferred_backup_window": {
Type: schema.TypeString,
Computed: true,
},

"preferred_maintenance_window": {
"network_type": {
Type: schema.TypeString,
Computed: true,
},

"port": {
Type: schema.TypeInt,
Computed: true,
},

"reader_endpoint": {
"preferred_backup_window": {
Type: schema.TypeString,
Computed: true,
},

"hosted_zone_id": {
"preferred_maintenance_window": {
Type: schema.TypeString,
Computed: true,
},
"reader_endpoint": {
Type: schema.TypeString,
Computed: true,
},

"replication_source_identifier": {
Type: schema.TypeString,
Computed: true,
},

"storage_encrypted": {
Type: schema.TypeBool,
Computed: true,
},

"tags": tftags.TagsSchemaComputed(),

"vpc_security_group_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
Expand All @@ -168,111 +139,70 @@ func dataSourceClusterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).RDSConn
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

dbClusterIdentifier := d.Get("cluster_identifier").(string)

params := &rds.DescribeDBClustersInput{
DBClusterIdentifier: aws.String(dbClusterIdentifier),
}
log.Printf("[DEBUG] Reading RDS Cluster: %s", params)
resp, err := conn.DescribeDBClusters(params)
dbClusterID := d.Get("cluster_identifier").(string)
dbc, err := FindDBClusterByID(conn, dbClusterID)

if err != nil {
return fmt.Errorf("Error retrieving RDS cluster: %w", err)
}

if resp == nil {
return fmt.Errorf("Error retrieving RDS cluster: empty response for: %s", params)
}

var dbc *rds.DBCluster
for _, c := range resp.DBClusters {
if aws.StringValue(c.DBClusterIdentifier) == dbClusterIdentifier {
dbc = c
break
}
}

if dbc == nil {
return fmt.Errorf("Error retrieving RDS cluster: cluster not found in response for: %s", params)
return fmt.Errorf("reading RDS Cluster (%s): %w", dbClusterID, err)
}

d.SetId(aws.StringValue(dbc.DBClusterIdentifier))

if err := d.Set("availability_zones", aws.StringValueSlice(dbc.AvailabilityZones)); err != nil {
return fmt.Errorf("error setting availability_zones: %w", err)
}

arn := dbc.DBClusterArn
d.Set("arn", arn)
clusterARN := aws.StringValue(dbc.DBClusterArn)
d.Set("arn", clusterARN)
d.Set("availability_zones", aws.StringValueSlice(dbc.AvailabilityZones))
d.Set("backtrack_window", dbc.BacktrackWindow)
d.Set("backup_retention_period", dbc.BackupRetentionPeriod)
d.Set("cluster_identifier", dbc.DBClusterIdentifier)

var cm []string
for _, m := range dbc.DBClusterMembers {
cm = append(cm, aws.StringValue(m.DBInstanceIdentifier))
var clusterMembers []string
for _, v := range dbc.DBClusterMembers {
clusterMembers = append(clusterMembers, aws.StringValue(v.DBInstanceIdentifier))
}
if err := d.Set("cluster_members", cm); err != nil {
return fmt.Errorf("error setting cluster_members: %w", err)
}

d.Set("cluster_members", clusterMembers)
d.Set("cluster_resource_id", dbc.DbClusterResourceId)

// Only set the DatabaseName if it is not nil. There is a known API bug where
// RDS accepts a DatabaseName but does not return it, causing a perpetual
// diff.
// See https://github.com/hashicorp/terraform/issues/4671 for backstory
if dbc.DatabaseName != nil {
d.Set("database_name", dbc.DatabaseName)
}

d.Set("db_cluster_parameter_group_name", dbc.DBClusterParameterGroup)
d.Set("db_subnet_group_name", dbc.DBSubnetGroup)

if err := d.Set("enabled_cloudwatch_logs_exports", aws.StringValueSlice(dbc.EnabledCloudwatchLogsExports)); err != nil {
return fmt.Errorf("error setting enabled_cloudwatch_logs_exports: %w", err)
}

d.Set("enabled_cloudwatch_logs_exports", aws.StringValueSlice(dbc.EnabledCloudwatchLogsExports))
d.Set("endpoint", dbc.Endpoint)
d.Set("engine_version", dbc.EngineVersion)
d.Set("engine", dbc.Engine)
d.Set("engine_version", dbc.EngineVersion)
d.Set("hosted_zone_id", dbc.HostedZoneId)
d.Set("iam_database_authentication_enabled", dbc.IAMDatabaseAuthenticationEnabled)

var roles []string
for _, r := range dbc.AssociatedRoles {
roles = append(roles, aws.StringValue(r.RoleArn))
var iamRoleARNs []string
for _, v := range dbc.AssociatedRoles {
iamRoleARNs = append(iamRoleARNs, aws.StringValue(v.RoleArn))
}
if err := d.Set("iam_roles", roles); err != nil {
return fmt.Errorf("error setting iam_roles: %w", err)
}

d.Set("iam_roles", iamRoleARNs)
d.Set("kms_key_id", dbc.KmsKeyId)
d.Set("master_username", dbc.MasterUsername)
d.Set("network_type", dbc.NetworkType)
d.Set("port", dbc.Port)
d.Set("preferred_backup_window", dbc.PreferredBackupWindow)
d.Set("preferred_maintenance_window", dbc.PreferredMaintenanceWindow)
d.Set("reader_endpoint", dbc.ReaderEndpoint)
d.Set("replication_source_identifier", dbc.ReplicationSourceIdentifier)

d.Set("storage_encrypted", dbc.StorageEncrypted)

var vpcg []string
for _, g := range dbc.VpcSecurityGroups {
vpcg = append(vpcg, aws.StringValue(g.VpcSecurityGroupId))
}
if err := d.Set("vpc_security_group_ids", vpcg); err != nil {
return fmt.Errorf("error setting vpc_security_group_ids: %w", err)
var securityGroupIDs []string
for _, v := range dbc.VpcSecurityGroups {
securityGroupIDs = append(securityGroupIDs, aws.StringValue(v.VpcSecurityGroupId))
}
d.Set("vpc_security_group_ids", securityGroupIDs)

tags, err := ListTags(conn, *arn)
tags, err := ListTags(conn, clusterARN)

if err != nil {
return fmt.Errorf("error listing tags for RDS Cluster (%s): %w", *arn, err)
return fmt.Errorf("listing tags for RDS Cluster (%s): %w", d.Id(), err)
}

if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
return fmt.Errorf("setting tags: %w", err)
}

return nil
Expand Down
49 changes: 11 additions & 38 deletions internal/service/rds/cluster_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,63 +31,36 @@ func TestAccRDSClusterDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceName, "db_subnet_group_name", resourceName, "db_subnet_group_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "hosted_zone_id", resourceName, "hosted_zone_id"),
resource.TestCheckResourceAttrPair(dataSourceName, "master_username", resourceName, "master_username"),
resource.TestCheckResourceAttrPair(dataSourceName, "network_type", resourceName, "network_type"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.Environment", resourceName, "tags.Environment"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.Name", resourceName, "tags.Name"),
),
},
},
})
}

func testAccClusterDataSourceConfig_basic(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
return acctest.ConfigCompose(acctest.ConfigVPCWithSubnets(rName, 2), fmt.Sprintf(`
resource "aws_db_subnet_group" "test" {
name = %[1]q
subnet_ids = aws_subnet.test[*].id
}
resource "aws_rds_cluster" "test" {
cluster_identifier = %[1]q
database_name = "mydb"
database_name = "test"
db_cluster_parameter_group_name = "default.aurora5.6"
db_subnet_group_name = aws_db_subnet_group.test.name
master_password = "mustbeeightcharacters"
master_username = "foo"
master_password = "avoid-plaintext-passwords"
master_username = "tfacctest"
skip_final_snapshot = true
tags = {
Environment = "test"
}
}
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
tags = {
Name = %[1]q
}
}
resource "aws_subnet" "a" {
vpc_id = aws_vpc.test.id
cidr_block = "10.0.0.0/24"
availability_zone = data.aws_availability_zones.available.names[0]
tags = {
Name = %[1]q
}
}
resource "aws_subnet" "b" {
vpc_id = aws_vpc.test.id
cidr_block = "10.0.1.0/24"
availability_zone = data.aws_availability_zones.available.names[1]
tags = {
Name = %[1]q
}
}
resource "aws_db_subnet_group" "test" {
name = %[1]q
subnet_ids = [aws_subnet.a.id, aws_subnet.b.id]
}
data "aws_rds_cluster" "test" {
cluster_identifier = aws_rds_cluster.test.cluster_identifier
}
Expand Down
Loading

0 comments on commit 45ac9ea

Please sign in to comment.