Skip to content

Commit

Permalink
r/redshift_cluster: Add support for snapshot_copy
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 9, 2017
1 parent e398f97 commit f7331dd
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
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}
}

0 comments on commit f7331dd

Please sign in to comment.