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

Redshift cluster restore from snapshot: Correctly set number_of_nodes #13203

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions .changelog/13203.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_redshift_cluster: Correctly use `number_of_nodes` argument value when restoring from snapshot
```
598 changes: 270 additions & 328 deletions internal/service/redshift/cluster.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion internal/service/redshift/cluster_snapshot_test.go

This file was deleted.

83 changes: 81 additions & 2 deletions internal/service/redshift/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func TestAccRedshiftCluster_changeAvailabilityZone_availabilityZoneRelocationNot
},
{
Config: testAccClusterConfig_updateAvailabilityZone_availabilityZoneRelocationNotSet(rName, 1),
ExpectError: regexp.MustCompile(`cannot change availability_zone if availability_zone_relocation_enabled is not true`),
ExpectError: regexp.MustCompile("cannot change `availability_zone` if `availability_zone_relocation_enabled` is not true"),
},
},
})
Expand Down Expand Up @@ -663,7 +663,55 @@ func TestAccRedshiftCluster_availabilityZoneRelocation_publiclyAccessible(t *tes
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_availabilityZoneRelocation_publiclyAccessible(rName),
ExpectError: regexp.MustCompile(`availability_zone_relocation_enabled can not be true when publicly_accessible is true`),
ExpectError: regexp.MustCompile("`availability_zone_relocation_enabled` cannot be true when `publicly_accessible` is true"),
},
},
})
}

func TestAccRedshiftCluster_restoreFromSnapshot(t *testing.T) {
var v redshift.Cluster
resourceName := "aws_redshift_cluster.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, redshift.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckDestroyClusterSnapshot(rName),
Steps: []resource.TestStep{
{
Config: testAccClusterCreateSnapshotConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(resourceName, &v),
resource.TestCheckResourceAttrPair(resourceName, "availability_zone", "data.aws_availability_zones.available", "names.0"),
resource.TestCheckResourceAttr(resourceName, "node_type", "dc2.8xlarge"),
resource.TestCheckResourceAttr(resourceName, "number_of_nodes", "2"),
),
},
// Apply a configuration without the source cluster to ensure final snapshot creation.
{
Config: acctest.ConfigAvailableAZsNoOptInExclude("usw2-az2"),
},
{
Config: testAccClusterRestoreFromSnapshotConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckClusterExists(resourceName, &v),
resource.TestCheckResourceAttrPair(resourceName, "availability_zone", "data.aws_availability_zones.available", "names.1"),
resource.TestCheckResourceAttr(resourceName, "node_type", "dc2.large"),
resource.TestCheckResourceAttr(resourceName, "number_of_nodes", "8"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"final_snapshot_identifier",
"master_password",
"skip_final_snapshot",
"snapshot_identifier",
},
},
},
})
Expand Down Expand Up @@ -1488,3 +1536,34 @@ resource "aws_redshift_cluster" "test" {
}
`, rName))
}

func testAccClusterCreateSnapshotConfig(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptInExclude("usw2-az2"), fmt.Sprintf(`
resource "aws_redshift_cluster" "test" {
cluster_identifier = %[1]q
availability_zone = data.aws_availability_zones.available.names[0]
database_name = "mydb"
master_username = "foo_test"
master_password = "Mustbe8characters"
node_type = "dc2.8xlarge"
number_of_nodes = 2
final_snapshot_identifier = %[1]q
}
`, rName))
}

func testAccClusterRestoreFromSnapshotConfig(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptInExclude("usw2-az2"), fmt.Sprintf(`
resource "aws_redshift_cluster" "test" {
cluster_identifier = %[1]q
snapshot_identifier = %[1]q
availability_zone = data.aws_availability_zones.available.names[1]
database_name = "mydb"
master_username = "foo_test"
master_password = "Mustbe8characters"
node_type = "dc2.large"
number_of_nodes = 8
skip_final_snapshot = true
}
`, rName))
}
12 changes: 12 additions & 0 deletions internal/service/redshift/enum.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package redshift

//nolint:deadcode,varcheck // These constants are missing from the AWS SDK
const (
clusterAvailabilityStatusAvailable = "Available"
clusterAvailabilityStatusFailed = "Failed"
clusterAvailabilityStatusMaintenance = "Maintenance"
clusterAvailabilityStatusModifying = "Modifying"
clusterAvailabilityStatusUnavailable = "Unavailable"
)

// https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-clusters.html#rs-mgmt-cluster-status.
//nolint:deadcode,varcheck // These constants are missing from the AWS SDK
const (
clusterStatusAvailable = "available"
clusterStatusAvailablePrepForResize = "available, prep-for-resize"
clusterStatusAvailableResizeCleanup = "available, resize-cleanup"
clusterStatusBackingUp = "backing-up"
clusterStatusCancellingResize = "cancelling-resize"
clusterStatusCreating = "creating"
clusterStatusDeleting = "deleting"
Expand All @@ -18,8 +28,10 @@ const (
clusterStatusModifying = "modifying"
clusterStatusPaused = "paused"
clusterStatusRebooting = "rebooting"
clusterStatusRecovering = "recovering"
clusterStatusRenaming = "renaming"
clusterStatusResizing = "resizing"
clusterStatusRestoring = "restoring"
clusterStatusRotatingKeys = "rotating-keys"
clusterStatusStorageFull = "storage-full"
clusterStatusUpdatingHSM = "updating-hsm"
Expand Down
4 changes: 2 additions & 2 deletions internal/service/redshift/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func statusCluster(conn *redshift.Redshift, id string) resource.StateRefreshFunc {
func statusClusterAvailability(conn *redshift.Redshift, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindClusterByID(conn, id)

Expand All @@ -19,7 +19,7 @@ func statusCluster(conn *redshift.Redshift, id string) resource.StateRefreshFunc
return nil, "", err
}

return output, aws.StringValue(output.ClusterStatus), nil
return output, aws.StringValue(output.ClusterAvailabilityStatus), nil
}
}

Expand Down
56 changes: 46 additions & 10 deletions internal/service/redshift/wait.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package redshift

import (
"errors"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/redshift"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

const (
Expand All @@ -13,25 +16,58 @@ const (
clusterRelocationStatusResolvedTimeout = 1 * time.Minute
)

func waitClusterCreated(conn *redshift.Redshift, id string, timeout time.Duration) (*redshift.Cluster, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{clusterAvailabilityStatusModifying, clusterAvailabilityStatusUnavailable},
Target: []string{clusterAvailabilityStatusAvailable},
Refresh: statusClusterAvailability(conn, id),
Timeout: timeout,
MinTimeout: 10 * time.Second,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*redshift.Cluster); ok {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.ClusterStatus)))

return output, err
}

return nil, err
}

func waitClusterDeleted(conn *redshift.Redshift, id string, timeout time.Duration) (*redshift.Cluster, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{
clusterStatusAvailable,
clusterStatusCreating,
clusterStatusDeleting,
clusterStatusFinalSnapshot,
clusterStatusRebooting,
clusterStatusRenaming,
clusterStatusResizing,
},
Pending: []string{clusterAvailabilityStatusModifying},
Target: []string{},
Refresh: statusCluster(conn, id),
Refresh: statusClusterAvailability(conn, id),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*redshift.Cluster); ok {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.ClusterStatus)))

return output, err
}

return nil, err
}

func waitClusterUpdated(conn *redshift.Redshift, id string, timeout time.Duration) (*redshift.Cluster, error) { //nolint:unparam
stateConf := &resource.StateChangeConf{
Pending: []string{clusterAvailabilityStatusMaintenance, clusterAvailabilityStatusModifying, clusterAvailabilityStatusUnavailable},
Target: []string{clusterAvailabilityStatusAvailable},
Refresh: statusClusterAvailability(conn, id),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*redshift.Cluster); ok {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.ClusterStatus)))

return output, err
}

Expand Down