-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22672 from roberth-k/f-aws_ecs_cluster_capacity_p…
…roviders New Resource: aws_ecs_cluster_capacity_providers
- Loading branch information
Showing
15 changed files
with
1,001 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:new-resource | ||
aws_ecs_cluster_capacity_providers | ||
``` | ||
|
||
```release-note:bug | ||
resource/aws_ecs_cluster: Provide new resource `aws_ecs_cluster_capacity_providers` to avoid bugs using `capacity_providers` and `default_capacity_provider_strategy`, which arguments will be deprecated in a future version | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package ecs | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
"github.com/hashicorp/terraform-provider-aws/internal/flex" | ||
"github.com/hashicorp/terraform-provider-aws/internal/tfresource" | ||
) | ||
|
||
func ResourceClusterCapacityProviders() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceClusterCapacityProvidersPut, | ||
ReadContext: resourceClusterCapacityProvidersRead, | ||
UpdateContext: resourceClusterCapacityProvidersPut, | ||
DeleteContext: resourceClusterCapacityProvidersDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"capacity_providers": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"cluster_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
// The API accepts both an ARN and a name in a generic "cluster" | ||
// parameter, but allowing that would force the resource to guess | ||
// which one to return on read. | ||
ValidateFunc: validateClusterName, | ||
}, | ||
"default_capacity_provider_strategy": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"base": { | ||
Type: schema.TypeInt, | ||
Default: 0, | ||
Optional: true, | ||
ValidateFunc: validation.IntBetween(0, 100000), | ||
}, | ||
"capacity_provider": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"weight": { | ||
Type: schema.TypeInt, | ||
Default: 0, | ||
Optional: true, | ||
ValidateFunc: validation.IntBetween(0, 1000), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceClusterCapacityProvidersPut(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.AWSClient).ECSConn | ||
|
||
clusterName := d.Get("cluster_name").(string) | ||
|
||
input := &ecs.PutClusterCapacityProvidersInput{ | ||
Cluster: aws.String(clusterName), | ||
CapacityProviders: flex.ExpandStringSet(d.Get("capacity_providers").(*schema.Set)), | ||
DefaultCapacityProviderStrategy: expandCapacityProviderStrategy(d.Get("default_capacity_provider_strategy").(*schema.Set)), | ||
} | ||
|
||
log.Printf("[DEBUG] Updating ECS cluster capacity providers: %s", input) | ||
|
||
err := retryClusterCapacityProvidersPut(ctx, conn, input) | ||
|
||
if err != nil { | ||
return diag.Errorf("error updating ECS Cluster (%s) capacity providers: %s", clusterName, err) | ||
} | ||
|
||
if _, err := waitClusterAvailable(ctx, conn, clusterName); err != nil { | ||
return diag.Errorf("error waiting for ECS Cluster (%s) to become available: %s", clusterName, err) | ||
} | ||
|
||
d.SetId(clusterName) | ||
|
||
return resourceClusterCapacityProvidersRead(ctx, d, meta) | ||
} | ||
|
||
func resourceClusterCapacityProvidersRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.AWSClient).ECSConn | ||
|
||
cluster, err := FindClusterByNameOrARN(ctx, conn, d.Id()) | ||
|
||
if tfresource.NotFound(err) { | ||
diag.Errorf("[WARN] ECS Cluster (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return diag.Errorf("error reading ECS Cluster (%s): %s", d.Id(), err) | ||
} | ||
|
||
// Status==INACTIVE means deleted cluster | ||
if aws.StringValue(cluster.Status) == "INACTIVE" { | ||
diag.Errorf("[WARN] ECS Cluster (%s) deleted, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err := d.Set("capacity_providers", aws.StringValueSlice(cluster.CapacityProviders)); err != nil { | ||
return diag.Errorf("error setting capacity_providers: %s", err) | ||
} | ||
|
||
d.Set("cluster_name", cluster.ClusterName) | ||
|
||
if err := d.Set("default_capacity_provider_strategy", flattenCapacityProviderStrategy(cluster.DefaultCapacityProviderStrategy)); err != nil { | ||
return diag.Errorf("error setting default_capacity_provider_strategy: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceClusterCapacityProvidersDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.AWSClient).ECSConn | ||
|
||
input := &ecs.PutClusterCapacityProvidersInput{ | ||
Cluster: aws.String(d.Id()), | ||
CapacityProviders: []*string{}, | ||
DefaultCapacityProviderStrategy: []*ecs.CapacityProviderStrategyItem{}, | ||
} | ||
|
||
log.Printf("[DEBUG] Removing ECS cluster (%s) capacity providers", d.Id()) | ||
|
||
err := retryClusterCapacityProvidersPut(ctx, conn, input) | ||
|
||
if tfawserr.ErrCodeEquals(err, ecs.ErrCodeClusterNotFoundException) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return diag.Errorf("error deleting ECS Cluster (%s) capacity providers: %s", d.Id(), err) | ||
} | ||
|
||
if _, err := waitClusterAvailable(ctx, conn, d.Id()); err != nil { | ||
return diag.Errorf("error waiting for ECS Cluster (%s) to become available: %s", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func retryClusterCapacityProvidersPut(ctx context.Context, conn *ecs.ECS, input *ecs.PutClusterCapacityProvidersInput) error { | ||
err := resource.RetryContext(ctx, ecsClusterTimeoutUpdate, func() *resource.RetryError { | ||
_, err := conn.PutClusterCapacityProvidersWithContext(ctx, input) | ||
if err != nil { | ||
if tfawserr.ErrMessageContains(err, ecs.ErrCodeClientException, "Cluster was not ACTIVE") { | ||
return resource.RetryableError(err) | ||
} | ||
if tfawserr.ErrMessageContains(err, ecs.ErrCodeResourceInUseException, "") { | ||
return resource.RetryableError(err) | ||
} | ||
if tfawserr.ErrMessageContains(err, ecs.ErrCodeUpdateInProgressException, "") { | ||
return resource.RetryableError(err) | ||
} | ||
return resource.NonRetryableError(err) | ||
} | ||
return nil | ||
}) | ||
|
||
if tfresource.TimedOut(err) { | ||
_, err = conn.PutClusterCapacityProvidersWithContext(ctx, input) | ||
} | ||
|
||
return err | ||
} |
Oops, something went wrong.