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

r/redshift_cluster: Add support for snapshot_copy #2238

Merged
merged 2 commits into from
Nov 10, 2017
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
70 changes: 69 additions & 1 deletion aws/resource_aws_redshift_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,29 @@ func resourceAwsRedshiftCluster() *schema.Resource {
Set: schema.HashString,
},

"snapshot_copy": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"destination_region": {
Type: schema.TypeString,
Required: true,
},
"retention_period": {
Type: schema.TypeInt,
Optional: true,
Default: 7,
},
"grant_name": {
Type: schema.TypeString,
Optional: true,
},
},
},
},

"logging": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -471,6 +494,13 @@ func resourceAwsRedshiftClusterCreate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("[WARN] Error waiting for Redshift Cluster state to be \"available\": %s", err)
}

if v, ok := d.GetOk("snapshot_copy"); ok {
err := enableRedshiftSnapshotCopy(d.Id(), v.([]interface{}), conn)
if err != nil {
return err
}
}

logging, ok := d.GetOk("logging.0.enable")
_, deprecatedOk := d.GetOk("enable_logging")
if (ok && logging.(bool)) || deprecatedOk {
Expand All @@ -479,7 +509,6 @@ func resourceAwsRedshiftClusterCreate(d *schema.ResourceData, meta interface{})
log.Printf("[ERROR] Error Enabling Logging on Redshift Cluster: %s", err)
return loggingErr
}

}

return resourceAwsRedshiftClusterRead(d, meta)
Expand Down Expand Up @@ -586,6 +615,8 @@ func resourceAwsRedshiftClusterRead(d *schema.ResourceData, meta interface{}) er
d.Set("cluster_revision_number", rsc.ClusterRevisionNumber)
d.Set("tags", tagsToMapRedshift(rsc.Tags))

d.Set("snapshot_copy", flattenRedshiftSnapshotCopy(rsc.ClusterSnapshotCopyStatus))

// TODO: Deprecated fields - remove in next major version
d.Set("bucket_name", loggingStatus.BucketName)
d.Set("enable_logging", loggingStatus.LoggingEnabled)
Expand Down Expand Up @@ -747,6 +778,22 @@ func resourceAwsRedshiftClusterUpdate(d *schema.ResourceData, meta interface{})
}
}

if d.HasChange("snapshot_copy") {
if v, ok := d.GetOk("snapshot_copy"); ok {
err := enableRedshiftSnapshotCopy(d.Id(), v.([]interface{}), conn)
if err != nil {
return err
}
} else {
_, err := conn.DisableSnapshotCopy(&redshift.DisableSnapshotCopyInput{
ClusterIdentifier: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("Failed to disable snapshot copy: %s", err)
}
}
}

deprecatedHasChange := (d.HasChange("enable_logging") || d.HasChange("bucket_name") || d.HasChange("s3_key_prefix"))
if d.HasChange("logging") || deprecatedHasChange {
var loggingErr error
Expand Down Expand Up @@ -814,6 +861,27 @@ func enableRedshiftClusterLogging(d *schema.ResourceData, conn *redshift.Redshif
return nil
}

func enableRedshiftSnapshotCopy(id string, scList []interface{}, conn *redshift.Redshift) error {
sc := scList[0].(map[string]interface{})

input := redshift.EnableSnapshotCopyInput{
ClusterIdentifier: aws.String(id),
DestinationRegion: aws.String(sc["destination_region"].(string)),
}
if rp, ok := sc["retention_period"]; ok {
input.RetentionPeriod = aws.Int64(int64(rp.(int)))
}
if gn, ok := sc["grant_name"]; ok {
input.SnapshotCopyGrantName = aws.String(gn.(string))
}

_, err := conn.EnableSnapshotCopy(&input)
if err != nil {
return fmt.Errorf("Failed to enable snapshot copy: %s", err)
}
return nil
}

func resourceAwsRedshiftClusterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).redshiftconn
log.Printf("[DEBUG] Destroying Redshift Cluster (%s)", d.Id())
Expand Down
65 changes: 65 additions & 0 deletions aws/resource_aws_redshift_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,37 @@ func TestAccAWSRedshiftCluster_loggingEnabled(t *testing.T) {
})
}

func TestAccAWSRedshiftCluster_snapshotCopy(t *testing.T) {
var v redshift.Cluster
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRedshiftClusterConfig_snapshotCopyEnabled(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
resource.TestCheckResourceAttr(
"aws_redshift_cluster.default", "snapshot_copy.0.destination_region", "us-east-1"),
resource.TestCheckResourceAttr(
"aws_redshift_cluster.default", "snapshot_copy.0.retention_period", "1"),
),
},

{
Config: testAccAWSRedshiftClusterConfig_snapshotCopyDisabled(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSRedshiftClusterExists("aws_redshift_cluster.default", &v),
resource.TestCheckResourceAttr("aws_redshift_cluster.default", "snapshot_copy.#", "0"),
),
},
},
})
}

func TestAccAWSRedshiftCluster_iamRoles(t *testing.T) {
var v redshift.Cluster

Expand Down Expand Up @@ -907,6 +938,40 @@ EOF
}`, rInt, rInt, rInt, rInt)
}

func testAccAWSRedshiftClusterConfig_snapshotCopyDisabled(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
database_name = "mydb"
master_username = "foo_test"
master_password = "Mustbe8characters"
node_type = "dc1.large"
automated_snapshot_retention_period = 0
allow_version_upgrade = false
skip_final_snapshot = true
}`, rInt)
}

func testAccAWSRedshiftClusterConfig_snapshotCopyEnabled(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
cluster_identifier = "tf-redshift-cluster-%d"
availability_zone = "us-west-2a"
database_name = "mydb"
master_username = "foo_test"
master_password = "Mustbe8characters"
node_type = "dc1.large"
automated_snapshot_retention_period = 0
allow_version_upgrade = false
snapshot_copy {
destination_region = "us-east-1"
retention_period = 1
}
skip_final_snapshot = true
}`, rInt)
}

func testAccAWSRedshiftClusterConfig_tags(rInt int) string {
return fmt.Sprintf(`
resource "aws_redshift_cluster" "default" {
Expand Down
19 changes: 19 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2356,3 +2356,22 @@ func flattenRedshiftLogging(ls *redshift.LoggingStatus) []interface{} {
}
return []interface{}{cfg}
}

func flattenRedshiftSnapshotCopy(scs *redshift.ClusterSnapshotCopyStatus) []interface{} {
if scs == nil {
return []interface{}{}
}

cfg := make(map[string]interface{}, 0)
if scs.DestinationRegion != nil {
cfg["destination_region"] = *scs.DestinationRegion
}
if scs.RetentionPeriod != nil {
cfg["retention_period"] = *scs.RetentionPeriod
}
if scs.SnapshotCopyGrantName != nil {
cfg["grant_name"] = *scs.SnapshotCopyGrantName
}

return []interface{}{cfg}
}
7 changes: 7 additions & 0 deletions website/docs/r/redshift_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ string.
* `owner_account` - (Optional) The AWS customer account used to create or copy the snapshot. Required if you are restoring a snapshot you do not own, optional if you own the snapshot.
* `iam_roles` - (Optional) A list of IAM Role ARNs to associate with the cluster. A Maximum of 10 can be associated to the cluster at any time.
* `logging` - (Optional) Logging, documented below.
* `snapshot_copy` - (Optional) Configuration of automatic copy of snapshots from one region to another. Documented below.
* `tags` - (Optional) A mapping of tags to assign to the resource.

### Nested Blocks
Expand All @@ -79,6 +80,12 @@ string.
For more information on the permissions required for the bucket, please read the AWS [documentation](http://docs.aws.amazon.com/redshift/latest/mgmt/db-auditing.html#db-auditing-enable-logging)
* `s3_key_prefix` - (Optional) The prefix applied to the log file names.

#### `snapshot_copy`

* `destination_region` - (Required) The destination region that you want to copy snapshots to.
* `retention_period` - (Optional) The number of days to retain automated snapshots in the destination region after they are copied from the source region. Defaults to `7`.
* `grant_name` - (Optional) The name of the snapshot copy grant to use when snapshots of an AWS KMS-encrypted cluster are copied to the destination region.

## Attributes Reference

The following attributes are exported:
Expand Down