Skip to content

Commit

Permalink
Add enabled_cloudwatch_logs_exports to rds cluster resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Heymann committed Jun 18, 2018
1 parent 84f2a54 commit d8b359d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
41 changes: 41 additions & 0 deletions aws/resource_aws_rds_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ func resourceAwsRDSCluster() *schema.Resource {
ForceNew: true,
},

"enabled_cloudwatch_logs_exports": {
Type: schema.TypeList,
Computed: false,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"audit",
"error",
"general",
"slowquery",
}, false),
},
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -370,6 +385,10 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error
opts.Port = aws.Int64(int64(attr.(int)))
}

if attr, ok := d.GetOk("enabled_cloudwatch_logs_exports"); ok && len(attr.([]interface{})) > 0 {
opts.EnableCloudwatchLogsExports = expandStringList(attr.([]interface{}))
}

// Check if any of the parameters that require a cluster modification after creation are set
var clusterUpdate bool
if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 {
Expand Down Expand Up @@ -489,6 +508,10 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error
createOpts.SourceRegion = aws.String(attr.(string))
}

if attr, ok := d.GetOk("enabled_cloudwatch_logs_exports"); ok && len(attr.([]interface{})) > 0 {
createOpts.EnableCloudwatchLogsExports = expandStringList(attr.([]interface{}))
}

log.Printf("[DEBUG] Create RDS Cluster as read replica: %s", createOpts)
var resp *rds.CreateDBClusterOutput
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -584,6 +607,10 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error
createOpts.EnableIAMDatabaseAuthentication = aws.Bool(attr.(bool))
}

if attr, ok := d.GetOk("enabled_cloudwatch_logs_exports"); ok && len(attr.([]interface{})) > 0 {
createOpts.EnableCloudwatchLogsExports = expandStringList(attr.([]interface{}))
}

log.Printf("[DEBUG] RDS Cluster restore options: %s", createOpts)
// Retry for IAM/S3 eventual consistency
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -683,6 +710,10 @@ func resourceAwsRDSClusterCreate(d *schema.ResourceData, meta interface{}) error
createOpts.EnableIAMDatabaseAuthentication = aws.Bool(attr.(bool))
}

if attr, ok := d.GetOk("enabled_cloudwatch_logs_exports"); ok && len(attr.([]interface{})) > 0 {
createOpts.EnableCloudwatchLogsExports = expandStringList(attr.([]interface{}))
}

log.Printf("[DEBUG] RDS Cluster create options: %s", createOpts)
var resp *rds.CreateDBClusterOutput
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
Expand Down Expand Up @@ -807,6 +838,10 @@ func flattenAwsRdsClusterResource(d *schema.ResourceData, meta interface{}, dbc
d.Set("iam_database_authentication_enabled", dbc.IAMDatabaseAuthenticationEnabled)
d.Set("hosted_zone_id", dbc.HostedZoneId)

if dbc.EnabledCloudwatchLogsExports != nil {
d.Set("enabled_cloudwatch_logs_exports", dbc.EnabledCloudwatchLogsExports)
}

var vpcg []string
for _, g := range dbc.VpcSecurityGroups {
vpcg = append(vpcg, *g.VpcSecurityGroupId)
Expand Down Expand Up @@ -901,6 +936,12 @@ func resourceAwsRDSClusterUpdate(d *schema.ResourceData, meta interface{}) error
requestUpdate = true
}

if d.HasChange("enabled_cloudwatch_logs_exports") && !d.IsNewResource() {
d.SetPartial("enabled_cloudwatch_logs_exports")
req.CloudwatchLogsExportConfiguration = buildCloudwatchLogsExportConfiguration(d)
requestUpdate = true
}

if requestUpdate {
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.ModifyDBCluster(req)
Expand Down
60 changes: 59 additions & 1 deletion aws/resource_aws_rds_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestAccAWSRDSCluster_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "engine", "aurora"),
resource.TestCheckResourceAttrSet(resourceName, "engine_version"),
resource.TestCheckResourceAttrSet(resourceName, "hosted_zone_id"),
resource.TestCheckResourceAttr(resourceName,
"enabled_cloudwatch_logs_exports.0", "audit"),
resource.TestCheckResourceAttr(resourceName,
"enabled_cloudwatch_logs_exports.1", "error"),
),
},
},
Expand Down Expand Up @@ -210,6 +214,39 @@ func TestAccAWSRDSCluster_updateTags(t *testing.T) {
})
}

func TestAccAWSRDSCluster_updateCloudwatchLogsExports(t *testing.T) {
var v rds.DBCluster
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSClusterConfig(ri),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
resource.TestCheckResourceAttr("aws_rds_cluster.default",
"enabled_cloudwatch_logs_exports.0", "audit"),
resource.TestCheckResourceAttr("aws_rds_cluster.default",
"enabled_cloudwatch_logs_exports.1", "error"),
),
},
{
Config: testAccAWSClusterConfigUpdatedCloudwatchLogsExports(ri),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSClusterExists("aws_rds_cluster.default", &v),
resource.TestCheckResourceAttr("aws_rds_cluster.default",
"enabled_cloudwatch_logs_exports.0", "error"),
resource.TestCheckResourceAttr("aws_rds_cluster.default",
"enabled_cloudwatch_logs_exports.1", "slowquery"),
),
},
},
})
}

func TestAccAWSRDSCluster_updateIamRoles(t *testing.T) {
var v rds.DBCluster
ri := acctest.RandInt()
Expand Down Expand Up @@ -571,6 +608,10 @@ resource "aws_rds_cluster" "default" {
tags {
Environment = "production"
}
enabled_cloudwatch_logs_exports = [
"audit",
"error",
]
}`, n)
}

Expand Down Expand Up @@ -633,7 +674,7 @@ func testAccAWSClusterConfig_s3Restore(bucketName string, bucketPrefix string, u
return fmt.Sprintf(`
data "aws_region" "current" {}
resource "aws_s3_bucket" "xtrabackup" {
bucket = "%s"
region = "${data.aws_region.current.name}"
Expand Down Expand Up @@ -831,6 +872,23 @@ resource "aws_rds_cluster" "default" {
}`, n)
}

func testAccAWSClusterConfigUpdatedCloudwatchLogsExports(n int) string {
return fmt.Sprintf(`
resource "aws_rds_cluster" "default" {
cluster_identifier = "tf-aurora-cluster-%d"
availability_zones = ["us-west-2a","us-west-2b","us-west-2c"]
database_name = "mydb"
master_username = "foo"
master_password = "mustbeeightcharaters"
db_cluster_parameter_group_name = "default.aurora5.6"
skip_final_snapshot = true
enabled_cloudwatch_logs_exports = [
"error",
"slowquery"
]
}`, n)
}

func testAccAWSClusterConfig_kmsKey(n int) string {
return fmt.Sprintf(`
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/rds_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ Default: A 30-minute window selected at random from an 8-hour block of time per
* `engine` - (Optional) The name of the database engine to be used for this DB cluster. Defaults to `aurora`. Valid Values: aurora,aurora-mysql,aurora-postgresql
* `engine_version` - (Optional) The database engine version.
* `source_region` - (Optional) The source region for an encrypted replica DB cluster.
* `enabled_cloudwatch_logs_exports` - (Optional) List of log types to export to cloudwatch. If omitted, no logs will be exported.
The following log types are supported: `audit`, `error`, `general`, `slowquery`.
* `tags` - (Optional) A mapping of tags to assign to the DB cluster.

### S3 Import Options
Expand Down

0 comments on commit d8b359d

Please sign in to comment.