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

resource/aws_elasticache_replication_group: Support number_cache_nodes updates #4504

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
90 changes: 59 additions & 31 deletions aws/resource_aws_elasticache_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,31 +406,16 @@ func resourceAwsElasticacheClusterCreate(d *schema.ResourceData, meta interface{
req.PreferredAvailabilityZones = azs
}

resp, err := conn.CreateCacheCluster(req)
id, err := createElasticacheCacheCluster(conn, req)
if err != nil {
return fmt.Errorf("Error creating Elasticache: %s", err)
return fmt.Errorf("error creating Elasticache Cache Cluster: %s", err)
}

// Assign the cluster id as the resource ID
// Elasticache always retains the id in lower case, so we have to
// mimic that or else we won't be able to refresh a resource whose
// name contained uppercase characters.
d.SetId(strings.ToLower(*resp.CacheCluster.CacheClusterId))

pending := []string{"creating", "modifying", "restoring", "snapshotting"}
stateConf := &resource.StateChangeConf{
Pending: pending,
Target: []string{"available"},
Refresh: cacheClusterStateRefreshFunc(conn, d.Id(), "available", pending),
Timeout: 40 * time.Minute,
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second,
}
d.SetId(id)

log.Printf("[DEBUG] Waiting for state to become available: %v", d.Id())
_, sterr := stateConf.WaitForState()
if sterr != nil {
return fmt.Errorf("Error waiting for elasticache (%s) to be created: %s", d.Id(), sterr)
err = createElasticacheCacheClusterWaiter(conn, d.Id(), 40*time.Minute)
if err != nil {
return fmt.Errorf("error waiting for Elasticache Cache Cluster (%s) to be created: %s", d.Id(), err)
}

return resourceAwsElasticacheClusterRead(d, meta)
Expand Down Expand Up @@ -682,9 +667,16 @@ func (b byCacheNodeId) Less(i, j int) bool {
func resourceAwsElasticacheClusterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticacheconn

err := deleteElasticacheCluster(d.Id(), 40*time.Minute, conn)
err := deleteElasticacheCacheCluster(conn, d.Id())
if err != nil {
if isAWSErr(err, elasticache.ErrCodeCacheClusterNotFoundFault, "") {
return nil
}
return fmt.Errorf("error deleting Elasticache Cache Cluster (%s): %s", d.Id(), err)
}
err = deleteElasticacheCacheClusterWaiter(conn, d.Id(), 40*time.Minute)
if err != nil {
return fmt.Errorf("error deleting Elasticache Cluster (%s): %s", d.Id(), err)
return fmt.Errorf("error waiting for Elasticache Cache Cluster (%s) to be deleted: %s", d.Id(), err)
}

return nil
Expand Down Expand Up @@ -761,13 +753,49 @@ func cacheClusterStateRefreshFunc(conn *elasticache.ElastiCache, clusterID, give
}
}

func deleteElasticacheCluster(clusterID string, timeout time.Duration, conn *elasticache.ElastiCache) error {
func createElasticacheCacheCluster(conn *elasticache.ElastiCache, input *elasticache.CreateCacheClusterInput) (string, error) {
log.Printf("[DEBUG] Creating Elasticache Cache Cluster: %s", input)
output, err := conn.CreateCacheCluster(input)
if err != nil {
return "", err
}
if output == nil || output.CacheCluster == nil {
return "", errors.New("missing cluster ID after creation")
}
// Elasticache always retains the id in lower case, so we have to
// mimic that or else we won't be able to refresh a resource whose
// name contained uppercase characters.
return strings.ToLower(aws.StringValue(output.CacheCluster.CacheClusterId)), nil
}

func createElasticacheCacheClusterWaiter(conn *elasticache.ElastiCache, cacheClusterID string, timeout time.Duration) error {
pending := []string{"creating", "modifying", "restoring", "snapshotting"}
stateConf := &resource.StateChangeConf{
Pending: pending,
Target: []string{"available"},
Refresh: cacheClusterStateRefreshFunc(conn, cacheClusterID, "available", pending),
Timeout: timeout,
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second,
}

log.Printf("[DEBUG] Waiting for Elasticache Cache Cluster (%s) to be created", cacheClusterID)
_, err := stateConf.WaitForState()
return err
}

func deleteElasticacheCacheCluster(conn *elasticache.ElastiCache, cacheClusterID string) error {
input := &elasticache.DeleteCacheClusterInput{
CacheClusterId: aws.String(clusterID),
CacheClusterId: aws.String(cacheClusterID),
}
log.Printf("[DEBUG] Deleting Elasticache Cache Cluster: %s", input)
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.DeleteCacheCluster(input)
if err != nil {
// This will not be fixed by retrying
if isAWSErr(err, elasticache.ErrCodeInvalidCacheClusterStateFault, "serving as primary") {
return resource.NonRetryableError(err)
}
// The cluster may be just snapshotting, so we retry until it's ready for deletion
if isAWSErr(err, elasticache.ErrCodeInvalidCacheClusterStateFault, "") {
return resource.RetryableError(err)
Expand All @@ -776,20 +804,20 @@ func deleteElasticacheCluster(clusterID string, timeout time.Duration, conn *ela
}
return nil
})
if err != nil {
return err
}
return err
}

log.Printf("[DEBUG] Waiting for deletion: %v", clusterID)
func deleteElasticacheCacheClusterWaiter(conn *elasticache.ElastiCache, cacheClusterID string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"creating", "available", "deleting", "incompatible-parameters", "incompatible-network", "restore-failed", "snapshotting"},
Target: []string{},
Refresh: cacheClusterStateRefreshFunc(conn, clusterID, "", []string{}),
Refresh: cacheClusterStateRefreshFunc(conn, cacheClusterID, "", []string{}),
Timeout: timeout,
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second,
}
log.Printf("[DEBUG] Waiting for Elasticache Cache Cluster deletion: %v", cacheClusterID)

_, err = stateConf.WaitForState()
_, err := stateConf.WaitForState()
return err
}
8 changes: 6 additions & 2 deletions aws/resource_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ func testSweepElasticacheClusters(region string) error {
continue
}
log.Printf("[INFO] Deleting Elasticache Cluster: %s", id)
err := deleteElasticacheCluster(id, 40*time.Minute, conn)
err := deleteElasticacheCacheCluster(conn, id)
if err != nil {
log.Printf("[ERROR] Failed to delete Elasticache Cluster (%s): %s", id, err)
log.Printf("[ERROR] Failed to delete Elasticache Cache Cluster (%s): %s", id, err)
}
err = deleteElasticacheCacheClusterWaiter(conn, id, 40*time.Minute)
if err != nil {
log.Printf("[ERROR] Failed waiting for Elasticache Cache Cluster (%s) to be deleted: %s", id, err)
}
}
return !isLast
Expand Down
Loading