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

Allow ECS Service Capacity Provider Updates #20707

Merged
merged 8 commits into from
Nov 25, 2021
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
3 changes: 3 additions & 0 deletions .changelog/20707.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_ecs_service: Allow `capacity_provider_strategy` changes to be updated in place, when possible
```
46 changes: 38 additions & 8 deletions internal/service/ecs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ecs

import (
"bytes"
"context"
"fmt"
"log"
"math"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"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"
Expand All @@ -34,8 +36,6 @@ func ResourceService() *schema.Resource {
State: resourceServiceImport,
},

CustomizeDiff: verify.SetTagsDiff,

Timeouts: &schema.ResourceTimeout{
Delete: schema.DefaultTimeout(20 * time.Minute),
},
Expand All @@ -44,27 +44,21 @@ func ResourceService() *schema.Resource {
"capacity_provider_strategy": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"base": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 100000),
ForceNew: true,
},

"capacity_provider": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"weight": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 1000),
ForceNew: true,
},
},
},
Expand Down Expand Up @@ -383,7 +377,43 @@ func ResourceService() *schema.Resource {
Default: false,
},
},

CustomizeDiff: customdiff.Sequence(
verify.SetTagsDiff,
capacityProviderStrategyCustomizeDiff,
),
}
}

func capacityProviderStrategyCustomizeDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error {
// to be backward compatible, should ForceNew almost always (previous behavior), unless:
// force_new_deployment is true and
// neither the old set nor new set is 0 length
if v := d.Get("force_new_deployment").(bool); !v {
return capacityProviderStrategyForceNew(d)
}

old, new := d.GetChange("capacity_provider_strategy")

ol := old.(*schema.Set).Len()
nl := new.(*schema.Set).Len()

if (ol == 0 && nl > 0) || (ol > 0 && nl == 0) {
return capacityProviderStrategyForceNew(d)
}

return nil
}

func capacityProviderStrategyForceNew(d *schema.ResourceDiff) error {
for _, key := range d.GetChangedKeysPrefix("capacity_provider_strategy") {
if d.HasChange(key) {
if err := d.ForceNew(key); err != nil {
return fmt.Errorf("while attempting to force a new ECS service for capacity_provider_strategy: %w", err)
}
}
}
return nil
}

func resourceServiceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
Expand Down
Loading